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 |
|
|
$out->start_section (id => 'document-errors', title => 'Document Errors'); |
76 |
|
|
$out->html ('<p>Not supported for this kind of contents.'); |
77 |
|
|
## TODO: class="???" |
78 |
|
|
$out->end_section; |
79 |
|
|
} # generate_structure_error_section |
80 |
|
|
|
81 |
|
|
sub source_charset ($) { |
82 |
|
|
return 'utf-8'; |
83 |
|
|
} # source_charset |
84 |
|
|
|
85 |
|
|
sub generate_source_string_section ($) { |
86 |
|
|
my $self = shift; |
87 |
|
|
my $input = $self->input; |
88 |
|
|
|
89 |
|
|
my $s; |
90 |
|
|
unless ($input->{is_char_string}) { |
91 |
|
|
open my $byte_stream, '<', \($input->{s}); |
92 |
|
|
require Message::Charset::Info; |
93 |
|
|
my $charset = Message::Charset::Info->get_by_iana_name |
94 |
|
|
($self->source_charset); |
95 |
|
|
my ($char_stream, $e_status) = $charset->get_decode_handle |
96 |
|
|
($byte_stream, allow_error_reporting => 1, allow_fallback => 1); |
97 |
|
|
return unless $char_stream; |
98 |
|
|
|
99 |
|
|
$char_stream->onerror (sub { |
100 |
|
|
my (undef, $type, %opt) = @_; |
101 |
|
|
if ($opt{octets}) { |
102 |
|
|
${$opt{octets}} = "\x{FFFD}"; |
103 |
|
|
} |
104 |
|
|
}); |
105 |
|
|
|
106 |
|
|
my $t = ''; |
107 |
|
|
while (1) { |
108 |
|
|
my $c = $char_stream->getc; |
109 |
|
|
last unless defined $c; |
110 |
|
|
$t .= $c; |
111 |
|
|
} |
112 |
|
|
$s = \$t; |
113 |
|
|
## TODO: Output for each line, don't concat all of lines. |
114 |
|
|
} else { |
115 |
|
|
$s = \($input->{s}); |
116 |
|
|
} |
117 |
|
|
|
118 |
|
|
my $out = $self->output; |
119 |
|
|
my $i = 1; |
120 |
|
|
$out->start_section (id => 'source-string', title => 'Document Source', |
121 |
|
|
short_title => 'Source'); |
122 |
|
|
$out->start_tag ('ol', lang => ''); |
123 |
|
|
|
124 |
|
|
if (length $$s) { |
125 |
|
|
while ($$s =~ /\G([^\x0D\x0A]*?)(?>\x0D\x0A?|\x0A)/gc) { |
126 |
|
|
$out->start_tag ('li', id => 'line-' . $i); |
127 |
|
|
$out->text ($1); |
128 |
|
|
$i++; |
129 |
|
|
} |
130 |
|
|
if ($$s =~ /\G([^\x0D\x0A]+)/gc) { |
131 |
|
|
$out->start_tag ('li', id => 'line-' . $i); |
132 |
|
|
$out->text ($1); |
133 |
|
|
} |
134 |
|
|
} else { |
135 |
|
|
$out->start_tag ('li', id => 'line-1'); |
136 |
|
|
} |
137 |
|
|
$out->end_tag ('ol'); |
138 |
|
|
$out->html (qq[<script> |
139 |
|
|
addSourceToParseErrorList ('@{[$input->id_prefix]}', 'parse-errors-list'); |
140 |
|
|
</script>]); |
141 |
|
|
$out->end_section |
142 |
|
|
} # generate_source_string_section |
143 |
|
|
|
144 |
|
|
sub generate_additional_sections ($) { |
145 |
|
|
my $self = shift; |
146 |
|
|
$self->generate_url_section; |
147 |
|
|
} # generate_additional_sections |
148 |
|
|
|
149 |
|
|
sub generate_url_section ($) { |
150 |
|
|
my $self = shift; |
151 |
|
|
my $urls = $self->{add_info}->{uri} || {}; |
152 |
|
|
return unless keys %$urls; |
153 |
|
|
|
154 |
|
|
## NOTE: URIs contained in the DOM (i.e. in HTML or XML documents), |
155 |
|
|
## except for those in RDF triples. |
156 |
|
|
## TODO: URIs in CSS |
157 |
|
|
|
158 |
|
|
my $out = $self->output; |
159 |
|
|
$out->start_section (id => 'urls', title => 'URLs'); |
160 |
|
|
$out->start_tag ('dl'); |
161 |
|
|
|
162 |
|
|
my $input = $self->input; |
163 |
|
|
my $result = $self->result; |
164 |
|
|
|
165 |
|
|
for my $url (sort {$a cmp $b} keys %$urls) { |
166 |
|
|
$out->start_tag ('dt'); |
167 |
|
|
$out->url ($url); |
168 |
|
|
$out->start_tag ('dd'); |
169 |
wakaba |
1.2 |
$out->link_to_webhacc ('Check conformance of this document', url => $url); |
170 |
wakaba |
1.1 |
$out->html ('<dd>Found in: <ul>'); |
171 |
|
|
for my $entry (@{$urls->{$url}}) { |
172 |
|
|
$out->html (qq[<li>] . $result->get_node_link ($input, $entry->{node})); |
173 |
|
|
if (keys %{$entry->{type} or {}}) { |
174 |
|
|
$out->text (' ('); |
175 |
|
|
$out->text (join ', ', map { |
176 |
|
|
{ |
177 |
|
|
hyperlink => 'Hyperlink', |
178 |
|
|
resource => 'Link to an external resource', |
179 |
|
|
namespace => 'Namespace URI', |
180 |
|
|
cite => 'Citation or link to a long description', |
181 |
|
|
embedded => 'Link to an embedded content', |
182 |
|
|
base => 'Base URI', |
183 |
|
|
action => 'Submission URI', |
184 |
|
|
}->{$_} |
185 |
|
|
or |
186 |
|
|
$_ |
187 |
|
|
} keys %{$entry->{type}}); |
188 |
|
|
$out->text (')'); |
189 |
|
|
} |
190 |
|
|
} |
191 |
|
|
$out->end_tag ('ul'); |
192 |
|
|
} |
193 |
|
|
$out->end_tag ('dl'); |
194 |
|
|
$out->end_section; |
195 |
|
|
} # generate_url_section |
196 |
|
|
|
197 |
|
|
1; |