| 1 |
wakaba |
1.1 |
package WebHACC::Language::Base; |
| 2 |
|
|
use strict; |
| 3 |
|
|
|
| 4 |
|
|
sub new ($) { |
| 5 |
|
|
die "$0: No constructor is defined for " . ref $_[0]; |
| 6 |
|
|
} # new |
| 7 |
|
|
|
| 8 |
wakaba |
1.2 |
## NOTE: |
| 9 |
|
|
## Language ->input, ->output, ->result |
| 10 |
|
|
## Input |
| 11 |
|
|
## Output ->input |
| 12 |
|
|
## Result ->output |
| 13 |
|
|
|
| 14 |
wakaba |
1.1 |
sub input ($;$) { |
| 15 |
|
|
if (@_ > 1) { |
| 16 |
|
|
if (defined $_[1]) { |
| 17 |
|
|
$_[0]->{input} = $_[1]; |
| 18 |
|
|
} else { |
| 19 |
|
|
delete $_[0]->{input}; |
| 20 |
|
|
} |
| 21 |
|
|
} |
| 22 |
|
|
|
| 23 |
|
|
return $_[0]->{input}; |
| 24 |
|
|
} # input |
| 25 |
|
|
|
| 26 |
|
|
sub output ($;$) { |
| 27 |
|
|
if (@_ > 1) { |
| 28 |
|
|
if (defined $_[1]) { |
| 29 |
|
|
$_[0]->{output} = $_[1]; |
| 30 |
|
|
} else { |
| 31 |
|
|
delete $_[0]->{output}; |
| 32 |
|
|
} |
| 33 |
|
|
} |
| 34 |
|
|
|
| 35 |
|
|
return $_[0]->{output}; |
| 36 |
|
|
} # output |
| 37 |
|
|
|
| 38 |
|
|
sub result ($;$) { |
| 39 |
|
|
if (@_ > 1) { |
| 40 |
|
|
if (defined $_[1]) { |
| 41 |
|
|
$_[0]->{result} = $_[1]; |
| 42 |
|
|
} else { |
| 43 |
|
|
delete $_[0]->{result}; |
| 44 |
|
|
} |
| 45 |
|
|
} |
| 46 |
|
|
|
| 47 |
|
|
return $_[0]->{result}; |
| 48 |
|
|
} # result |
| 49 |
|
|
|
| 50 |
|
|
sub onsubdoc ($;$) { |
| 51 |
|
|
if (@_ > 1) { |
| 52 |
|
|
if (defined $_[1]) { |
| 53 |
|
|
$_[0]->{onsubdoc} = $_[1]; |
| 54 |
|
|
} else { |
| 55 |
|
|
delete $_[0]->{onsubdoc}; |
| 56 |
|
|
} |
| 57 |
|
|
} |
| 58 |
|
|
|
| 59 |
|
|
return $_[0]->{onsubdoc} || sub { }; |
| 60 |
|
|
} # onsubdoc |
| 61 |
|
|
|
| 62 |
|
|
sub generate_syntax_error_section ($) { |
| 63 |
wakaba |
1.2 |
die "$0: Syntactical checking for " . (ref $_[0]) . " is not supported"; |
| 64 |
wakaba |
1.1 |
} # generate_syntax_error_section |
| 65 |
|
|
|
| 66 |
|
|
sub generate_structure_dump_section ($) { |
| 67 |
|
|
# |
| 68 |
|
|
} # generate_structure_dump_section |
| 69 |
|
|
|
| 70 |
|
|
sub generate_structure_error_section ($) { |
| 71 |
|
|
my $self = shift; |
| 72 |
|
|
|
| 73 |
|
|
my $out = $self->output; |
| 74 |
|
|
|
| 75 |
wakaba |
1.5 |
$out->start_section (role => 'structure-errors'); |
| 76 |
|
|
$out->start_error_list (role => 'structure-errors'); |
| 77 |
|
|
|
| 78 |
|
|
$self->result->add_error (input => $self->input, |
| 79 |
|
|
level => 'u', |
| 80 |
|
|
layer => 'structure', |
| 81 |
|
|
type => 'media type not supported:structure', |
| 82 |
|
|
text => $self->input->{media_type}); |
| 83 |
|
|
|
| 84 |
|
|
$out->end_error_list (role => 'structure-errors'); |
| 85 |
wakaba |
1.1 |
$out->end_section; |
| 86 |
|
|
} # generate_structure_error_section |
| 87 |
|
|
|
| 88 |
|
|
sub source_charset ($) { |
| 89 |
|
|
return 'utf-8'; |
| 90 |
|
|
} # source_charset |
| 91 |
|
|
|
| 92 |
|
|
sub generate_source_string_section ($) { |
| 93 |
|
|
my $self = shift; |
| 94 |
|
|
my $input = $self->input; |
| 95 |
|
|
|
| 96 |
|
|
my $s; |
| 97 |
|
|
unless ($input->{is_char_string}) { |
| 98 |
|
|
open my $byte_stream, '<', \($input->{s}); |
| 99 |
|
|
require Message::Charset::Info; |
| 100 |
|
|
my $charset = Message::Charset::Info->get_by_iana_name |
| 101 |
|
|
($self->source_charset); |
| 102 |
|
|
my ($char_stream, $e_status) = $charset->get_decode_handle |
| 103 |
|
|
($byte_stream, allow_error_reporting => 1, allow_fallback => 1); |
| 104 |
|
|
return unless $char_stream; |
| 105 |
|
|
|
| 106 |
|
|
$char_stream->onerror (sub { |
| 107 |
|
|
my (undef, $type, %opt) = @_; |
| 108 |
|
|
if ($opt{octets}) { |
| 109 |
|
|
${$opt{octets}} = "\x{FFFD}"; |
| 110 |
|
|
} |
| 111 |
|
|
}); |
| 112 |
|
|
|
| 113 |
|
|
my $t = ''; |
| 114 |
|
|
while (1) { |
| 115 |
|
|
my $c = $char_stream->getc; |
| 116 |
|
|
last unless defined $c; |
| 117 |
|
|
$t .= $c; |
| 118 |
|
|
} |
| 119 |
|
|
$s = \$t; |
| 120 |
|
|
## TODO: Output for each line, don't concat all of lines. |
| 121 |
|
|
} else { |
| 122 |
|
|
$s = \($input->{s}); |
| 123 |
|
|
} |
| 124 |
|
|
|
| 125 |
|
|
my $out = $self->output; |
| 126 |
|
|
my $i = 1; |
| 127 |
wakaba |
1.6 |
$out->start_section (role => 'source'); |
| 128 |
wakaba |
1.1 |
$out->start_tag ('ol', lang => ''); |
| 129 |
|
|
|
| 130 |
|
|
if (length $$s) { |
| 131 |
|
|
while ($$s =~ /\G([^\x0D\x0A]*?)(?>\x0D\x0A?|\x0A)/gc) { |
| 132 |
|
|
$out->start_tag ('li', id => 'line-' . $i); |
| 133 |
|
|
$out->text ($1); |
| 134 |
|
|
$i++; |
| 135 |
|
|
} |
| 136 |
|
|
if ($$s =~ /\G([^\x0D\x0A]+)/gc) { |
| 137 |
|
|
$out->start_tag ('li', id => 'line-' . $i); |
| 138 |
|
|
$out->text ($1); |
| 139 |
|
|
} |
| 140 |
|
|
} else { |
| 141 |
|
|
$out->start_tag ('li', id => 'line-1'); |
| 142 |
|
|
} |
| 143 |
|
|
$out->end_tag ('ol'); |
| 144 |
wakaba |
1.4 |
$out->add_source_to_parse_error_list ('parse-errors-list'); |
| 145 |
wakaba |
1.6 |
$out->end_section; |
| 146 |
wakaba |
1.1 |
} # generate_source_string_section |
| 147 |
|
|
|
| 148 |
|
|
sub generate_additional_sections ($) { |
| 149 |
|
|
my $self = shift; |
| 150 |
|
|
$self->generate_url_section; |
| 151 |
|
|
} # generate_additional_sections |
| 152 |
|
|
|
| 153 |
|
|
sub generate_url_section ($) { |
| 154 |
|
|
my $self = shift; |
| 155 |
|
|
my $urls = $self->{add_info}->{uri} || {}; |
| 156 |
|
|
return unless keys %$urls; |
| 157 |
|
|
|
| 158 |
|
|
## NOTE: URIs contained in the DOM (i.e. in HTML or XML documents), |
| 159 |
|
|
## except for those in RDF triples. |
| 160 |
|
|
## TODO: URIs in CSS |
| 161 |
|
|
|
| 162 |
|
|
my $out = $self->output; |
| 163 |
|
|
$out->start_section (id => 'urls', title => 'URLs'); |
| 164 |
|
|
$out->start_tag ('dl'); |
| 165 |
|
|
|
| 166 |
|
|
my $input = $self->input; |
| 167 |
|
|
my $result = $self->result; |
| 168 |
|
|
|
| 169 |
|
|
for my $url (sort {$a cmp $b} keys %$urls) { |
| 170 |
|
|
$out->start_tag ('dt'); |
| 171 |
|
|
$out->url ($url); |
| 172 |
|
|
$out->start_tag ('dd'); |
| 173 |
wakaba |
1.2 |
$out->link_to_webhacc ('Check conformance of this document', url => $url); |
| 174 |
wakaba |
1.1 |
$out->html ('<dd>Found in: <ul>'); |
| 175 |
|
|
for my $entry (@{$urls->{$url}}) { |
| 176 |
wakaba |
1.3 |
$out->start_tag ('li'); |
| 177 |
|
|
$out->node_link ($entry->{node}); |
| 178 |
wakaba |
1.1 |
if (keys %{$entry->{type} or {}}) { |
| 179 |
|
|
$out->text (' ('); |
| 180 |
|
|
$out->text (join ', ', map { |
| 181 |
|
|
{ |
| 182 |
|
|
hyperlink => 'Hyperlink', |
| 183 |
|
|
resource => 'Link to an external resource', |
| 184 |
|
|
namespace => 'Namespace URI', |
| 185 |
|
|
cite => 'Citation or link to a long description', |
| 186 |
|
|
embedded => 'Link to an embedded content', |
| 187 |
|
|
base => 'Base URI', |
| 188 |
|
|
action => 'Submission URI', |
| 189 |
|
|
}->{$_} |
| 190 |
|
|
or |
| 191 |
|
|
$_ |
| 192 |
|
|
} keys %{$entry->{type}}); |
| 193 |
|
|
$out->text (')'); |
| 194 |
|
|
} |
| 195 |
|
|
} |
| 196 |
|
|
$out->end_tag ('ul'); |
| 197 |
|
|
} |
| 198 |
|
|
$out->end_tag ('dl'); |
| 199 |
|
|
$out->end_section; |
| 200 |
|
|
} # generate_url_section |
| 201 |
|
|
|
| 202 |
|
|
1; |