2 |
use strict; |
use strict; |
3 |
|
|
4 |
sub new ($) { |
sub new ($) { |
5 |
return bless {}, shift; |
return bless { |
6 |
|
global_status => 'conforming', |
7 |
|
# or, 'should-error', 'non-conforming', 'uncertain' |
8 |
|
subdoc_results => [], |
9 |
|
}, shift; |
10 |
} # new |
} # new |
11 |
|
|
12 |
|
sub output ($;$) { |
13 |
|
if (@_ > 1) { |
14 |
|
if (defined $_[1]) { |
15 |
|
$_[0]->{output} = $_[1]; |
16 |
|
} else { |
17 |
|
delete $_[0]->{output}; |
18 |
|
} |
19 |
|
} |
20 |
|
|
21 |
|
return $_[0]->{output}; |
22 |
|
} # output |
23 |
|
|
24 |
|
sub parent_result ($;$) { |
25 |
|
if (@_ > 1) { |
26 |
|
if (defined $_[1]) { |
27 |
|
$_[0]->{parent_result} = $_[1]; |
28 |
|
} else { |
29 |
|
delete $_[0]->{parent_result}; |
30 |
|
} |
31 |
|
} |
32 |
|
|
33 |
sub get_error_label ($$) { |
return $_[0]->{parent_result}; |
34 |
|
} # parent_result |
35 |
|
|
36 |
|
sub layer_applicable ($$) { |
37 |
|
my $self = shift; |
38 |
|
my $layer = shift; |
39 |
|
$self->{layers}->{$layer}->{applicable} = 1; |
40 |
|
} # layer_applicable |
41 |
|
|
42 |
|
sub layer_uncertain ($$) { |
43 |
my $self = shift; |
my $self = shift; |
44 |
my ($input, $err) = @_; |
my $layer = shift; |
45 |
|
$self->{layers}->{$layer}->{uncertain} ||= 1; |
46 |
|
$self->{layers}->{$layer}->{applicable} = 1; |
47 |
|
$self->{global_status} = 'uncertain' |
48 |
|
unless $self->{global_status} eq 'non-conforming'; |
49 |
|
} # layer_uncertain |
50 |
|
|
51 |
|
sub add_error ($%) { |
52 |
|
my ($self, %opt) = @_; |
53 |
|
|
54 |
|
my $out = $self->output; |
55 |
|
$out->has_error (1); |
56 |
|
|
57 |
|
my $error_level = $opt{level}; |
58 |
|
if (not defined $error_level) { |
59 |
|
$error_level = 'm'; ## NOTE: Unknown - an error of the implementation |
60 |
|
} elsif ({ |
61 |
|
m => 1, s => 1, w => 1, i => 1, u => 1, |
62 |
|
}->{$error_level}) { |
63 |
|
# |
64 |
|
} else { |
65 |
|
$error_level = 'm'; ## NOTE: Unknown - an error of the implementation |
66 |
|
} |
67 |
|
|
68 |
|
my $error_layer = $opt{layer}; |
69 |
|
if (not defined $error_layer) { |
70 |
|
$error_layer = 'syntax'; ## NOTE: Unknown - an error of the implementation |
71 |
|
} elsif ({ |
72 |
|
transfer => 1, |
73 |
|
encode => 1, |
74 |
|
charset => 1, |
75 |
|
syntax => 1, |
76 |
|
structure => 1, |
77 |
|
semantics => 1, |
78 |
|
}->{$error_layer}) { |
79 |
|
# |
80 |
|
} else { |
81 |
|
$error_layer = 'syntax'; ## NOTE: Unknown - an error of the implementation |
82 |
|
} |
83 |
|
|
84 |
my $r = ''; |
my $class = qq[level-$error_level layer-$error_layer]; |
85 |
|
|
86 |
|
## Line & column numbers (prepare values) |
87 |
|
|
88 |
my $line; |
my $line; |
89 |
my $column; |
my $column; |
90 |
|
|
91 |
if (defined $err->{node}) { |
if (defined $opt{node}) { |
92 |
$line = $err->{node}->get_user_data ('manakai_source_line'); |
$line = $opt{node}->get_user_data ('manakai_source_line'); |
93 |
if (defined $line) { |
if (defined $line) { |
94 |
$column = $err->{node}->get_user_data ('manakai_source_column'); |
$column = $opt{node}->get_user_data ('manakai_source_column'); |
95 |
} else { |
} elsif ($opt{node}->isa ('Message::IF::Node')) { |
96 |
if ($err->{node}->node_type == $err->{node}->ATTRIBUTE_NODE) { |
if ($opt{node}->node_type == $opt{node}->ATTRIBUTE_NODE) { |
97 |
my $owner = $err->{node}->owner_element; |
my $owner = $opt{node}->owner_element; |
98 |
$line = $owner->get_user_data ('manakai_source_line'); |
if ($owner) { |
99 |
$column = $owner->get_user_data ('manakai_source_column'); |
$line = $owner->get_user_data ('manakai_source_line'); |
100 |
|
$column = $owner->get_user_data ('manakai_source_column'); |
101 |
|
} |
102 |
} else { |
} else { |
103 |
my $parent = $err->{node}->parent_node; |
my $parent = $opt{node}->parent_node; |
104 |
if ($parent) { |
if ($parent) { |
105 |
$line = $parent->get_user_data ('manakai_source_line'); |
$line = $parent->get_user_data ('manakai_source_line'); |
106 |
$column = $parent->get_user_data ('manakai_source_column'); |
$column = $parent->get_user_data ('manakai_source_column'); |
109 |
} |
} |
110 |
} |
} |
111 |
unless (defined $line) { |
unless (defined $line) { |
112 |
if (defined $err->{token} and defined $err->{token}->{line}) { |
if (defined $opt{token} and defined $opt{token}->{line}) { |
113 |
$line = $err->{token}->{line}; |
$line = $opt{token}->{line}; |
114 |
$column = $err->{token}->{column}; |
$column = $opt{token}->{column}; |
115 |
} elsif (defined $err->{line}) { |
} elsif (defined $opt{line}) { |
116 |
$line = $err->{line}; |
$line = $opt{line}; |
117 |
$column = $err->{column}; |
$column = $opt{column}; |
118 |
} |
} |
119 |
} |
} |
120 |
|
$line = $line - 1 || 1 |
121 |
|
if defined $line and not (defined $column and $column > 0); |
122 |
|
|
123 |
|
$out->start_tag ('dt', class => $class, |
124 |
|
'data-type' => $opt{type}, |
125 |
|
'data-level' => $error_level, |
126 |
|
'data-layer' => $error_layer, |
127 |
|
($line ? ('data-line' => $line) : ()), |
128 |
|
($column ? ('data-column' => $column) : ())); |
129 |
|
my $has_location; |
130 |
|
|
131 |
|
## URL |
132 |
|
|
133 |
|
if (defined $opt{url}) { |
134 |
|
$out->url ($opt{url}); |
135 |
|
$has_location = 1; |
136 |
|
} |
137 |
|
|
138 |
|
## Line & column numbers (real output) |
139 |
|
|
140 |
if (defined $line) { |
if (defined $line) { |
141 |
if (defined $column and $column > 0) { |
if (defined $column and $column > 0) { |
142 |
$r = qq[<a href="#$input->{id_prefix}line-$line">Line $line</a> column $column]; |
$out->xref ('Line #', text => $line, target => 'line-' . $line); |
143 |
|
$out->text (' '); |
144 |
|
$out->nl_text ('column #', text => $column); |
145 |
} else { |
} else { |
146 |
$line = $line - 1 || 1; |
$out->xref ('Line #', text => $line, target => 'line-' . $line); |
|
$r = qq[<a href="#$input->{id_prefix}line-$line">Line $line</a>]; |
|
147 |
} |
} |
148 |
|
$has_location = 1; |
149 |
} |
} |
150 |
|
|
151 |
if (defined $err->{node}) { |
## Node path |
152 |
$r .= ' ' if length $r; |
|
153 |
$r .= $self->get_node_link ($input, $err->{node}); |
if (defined $opt{node}) { |
154 |
|
$out->html (' '); |
155 |
|
$out->node_link ($opt{node}); |
156 |
|
$has_location = 1; |
157 |
} |
} |
158 |
|
|
159 |
if (defined $err->{index}) { |
if (defined $opt{index}) { |
160 |
if (length $r) { |
if ($opt{index_has_link}) { |
161 |
$r .= ', Index ' . (0+$err->{index}); |
$out->html (' '); |
162 |
|
$out->xref ('Index #', text => (0+$opt{index}), |
163 |
|
target => 'index-' . (0+$opt{index})); |
164 |
} else { |
} else { |
165 |
$r .= "<a href='#$input->{id_prefix}index-@{[0+$err->{index}]}'>Index " |
$out->html (' '); |
166 |
. (0+$err->{index}) . '</a>'; |
$out->nl_text ('Index #', text => (0+$opt{index})); |
167 |
} |
} |
168 |
|
$has_location = 1; |
169 |
} |
} |
170 |
|
|
171 |
if (defined $err->{value}) { |
if (defined $opt{value}) { |
172 |
$r .= ' ' if length $r; ## BUG: v must be escaped |
$out->html (' '); |
173 |
$r .= '<q><code>' . ($err->{value}) . '</code></q>'; |
$out->code ($opt{value}); |
174 |
|
$has_location = 1; |
175 |
} |
} |
176 |
|
|
177 |
return $r; |
unless ($has_location) { |
178 |
} # get_error_label |
if (defined $opt{input}) { |
179 |
|
if (defined $opt{input}->{container_node}) { |
180 |
|
my $original_input = $out->input; |
181 |
|
$out->input ($opt{input}->{parent_input}); |
182 |
|
$out->node_link ($opt{input}->{container_node}); |
183 |
|
$out->input ($original_input); |
184 |
|
$has_location = 1; |
185 |
|
} elsif (defined $opt{input}->{request_uri}) { |
186 |
|
$out->url ($opt{input}->{request_uri}); |
187 |
|
$has_location = 1; |
188 |
|
} elsif (defined $opt{input}->url) { |
189 |
|
$out->url ($opt{input}->url); |
190 |
|
$has_location = 1; |
191 |
|
} |
192 |
|
} |
193 |
|
|
194 |
|
unless ($has_location) { |
195 |
|
$out->nl_text ('Unknown location'); |
196 |
|
} |
197 |
|
} |
198 |
|
|
199 |
|
$out->start_tag ('dd', class => $class); |
200 |
|
|
201 |
sub get_error_level_label ($) { |
## Error level |
202 |
|
$out->nl_text ('Error level ' . $error_level); |
203 |
|
$out->text (': '); |
204 |
|
|
205 |
|
## Error message |
206 |
|
my $error_type_text = $opt{type}; |
207 |
|
$out->nl_text ($error_type_text, node => $opt{node}, text => $opt{text}, |
208 |
|
value => $opt{value}); |
209 |
|
|
210 |
|
## Link to a long description |
211 |
|
|
212 |
|
my $fragment = $opt{type}; |
213 |
|
$fragment =~ tr/ /-/; |
214 |
|
$fragment = $out->encode_url_component ($fragment); |
215 |
|
$out->text (' ['); |
216 |
|
$out->link ('Description', url => '../error-description#' . $fragment, |
217 |
|
rel => 'help'); |
218 |
|
$out->text (']'); |
219 |
|
|
220 |
|
if ($error_level eq 'm') { |
221 |
|
$self->{layers}->{$error_layer}->{must}++; |
222 |
|
$self->{global_status} = 'non-conforming'; |
223 |
|
} elsif ($error_level eq 's') { |
224 |
|
$self->{layers}->{$error_layer}->{should}++; |
225 |
|
$self->{global_status} = 'should-error' |
226 |
|
unless {'non-conforming' => 1, |
227 |
|
uncertain => 1}->{$self->{global_status}}; |
228 |
|
} elsif ($error_level eq 'w') { |
229 |
|
$self->{layers}->{$error_layer}->{warning}++; |
230 |
|
} elsif ($error_level eq 'u') { |
231 |
|
$self->{layers}->{$error_layer}->{uncertain}++; |
232 |
|
$self->{global_status} = 'uncertain' |
233 |
|
unless $self->{global_status} eq 'non-conforming'; |
234 |
|
} elsif ($error_level eq 'i') { |
235 |
|
$self->{layers}->{$error_layer}->{info}++; |
236 |
|
} |
237 |
|
} # add_error |
238 |
|
|
239 |
|
sub generate_result_section ($) { |
240 |
my $self = shift; |
my $self = shift; |
|
my $err = shift; |
|
241 |
|
|
242 |
my $r = ''; |
my $result = $self; |
243 |
|
|
244 |
|
my $out = $result->output; |
245 |
|
|
246 |
|
$out->start_section (role => 'result'); |
247 |
|
|
248 |
|
my $para_class = { |
249 |
|
'conforming' => 'result-para no-error', |
250 |
|
'should-error' => 'result-para should-errors', |
251 |
|
'non-conforming' => 'result-para must-errors', |
252 |
|
'uncertain' => 'result-para uncertain', |
253 |
|
}->{$self->{global_status}}; |
254 |
|
$out->start_tag ('p', class => $para_class); |
255 |
|
$out->nl_text ('Conformance is ' . $self->{global_status}); |
256 |
|
$out->end_tag ('p'); |
257 |
|
|
258 |
|
$out->html (qq[<table> |
259 |
|
<colgroup><col><col><colgroup><col><col><col><col><colgroup><col> |
260 |
|
<thead> |
261 |
|
<tr><th scope=col colspan=2>]); |
262 |
|
for ('Error level m', 'Error level s', 'Error level w', |
263 |
|
'Error level i', 'Score') { |
264 |
|
$out->start_tag ('th'); |
265 |
|
$out->nl_text ($_); |
266 |
|
} |
267 |
|
|
268 |
if (not defined $err->{level} or $err->{level} eq 'm') { |
my $maindoc_status = {must => 0, should => 0, warning => 0, info => 0, |
269 |
$r = qq[<strong><a href="../error-description#level-m"><em class=rfc2119>MUST</em>-level |
uncertain => 0, applicable => 1}; |
270 |
error</a></strong>: ]; |
my $subdocs_status = {must => 0, should => 0, warning => 0, info => 0, |
271 |
} elsif ($err->{level} eq 's') { |
uncertain => 0, applicable => 1}; |
272 |
$r = qq[<strong><a href="../error-description#level-s"><em class=rfc2119>SHOULD</em>-level |
my $global_status = {must => 0, should => 0, warning => 0, info => 0, |
273 |
error</a></strong>: ]; |
uncertain => 0, applicable => 1}; |
274 |
} elsif ($err->{level} eq 'w') { |
|
275 |
$r = qq[<strong><a href="../error-description#level-w">Warning</a></strong>: |
my $score_unit = 2; |
276 |
]; |
|
277 |
} elsif ($err->{level} eq 'u' or $err->{level} eq 'unsupported') { |
my @row = ( |
278 |
$r = qq[<strong><a href="../error-description#level-u">Not |
sub { |
279 |
supported</a></strong>: ]; |
$out->start_tag ('tbody'); |
280 |
} elsif ($err->{level} eq 'i') { |
$out->start_tag ('tr'); |
281 |
$r = qq[<strong><a href="../error-description#level-i">Information</a></strong>: ]; |
$out->start_tag ('th', colspan => 7, scope => 'col'); |
282 |
|
$out->nl_text ('Main document'); |
283 |
|
}, |
284 |
|
{label => 'Transfer L.', status => $self->{layers}->{transfer}, |
285 |
|
target => 'transfer-errors', score_base => 20, |
286 |
|
parent_status => $maindoc_status}, |
287 |
|
{label => 'Encode L.', status => $self->{layers}->{encode}, |
288 |
|
target => 'parse-errors', score_base => 10, |
289 |
|
parent_status => $maindoc_status}, |
290 |
|
{label => 'Char L.', status => $self->{layers}->{charset}, |
291 |
|
score_base => 10, |
292 |
|
parent_status => $maindoc_status}, |
293 |
|
{label => 'Syntax L.', status => $self->{layers}->{syntax}, |
294 |
|
target => 'parse-errors', score_base => 20, |
295 |
|
parent_status => $maindoc_status}, |
296 |
|
{label => 'Structure L.', status => $self->{layers}->{structure}, |
297 |
|
target => 'document-errors', score_base => 20, |
298 |
|
parent_status => $maindoc_status}, |
299 |
|
{label => 'Semantics L.', status => $self->{layers}->{semantics}, |
300 |
|
score_base => 20, |
301 |
|
parent_status => $maindoc_status}, |
302 |
|
); |
303 |
|
|
304 |
|
if (@{$self->{subdoc_results}}) { |
305 |
|
push @row, {label => 'Subtotal', status => $maindoc_status, |
306 |
|
score_base => 100, |
307 |
|
parent_status => $global_status, is_total => 1}; |
308 |
|
push @row, sub { |
309 |
|
$out->start_tag ('tbody'); |
310 |
|
$out->start_tag ('tr'); |
311 |
|
$out->start_tag ('th', colspan => 7, scope => 'col'); |
312 |
|
$out->nl_text ('Subdocuments'); |
313 |
|
}; |
314 |
|
for (@{$self->{subdoc_results}}) { |
315 |
|
push @row, {label => '#' . $_->{input}->full_subdocument_index, |
316 |
|
status => $_, |
317 |
|
target => $_->{input}->id_prefix . 'result-summary', |
318 |
|
score_base => 100, parent_status => $subdocs_status}; |
319 |
|
} |
320 |
|
push @row, {label => 'Subtotal', status => $subdocs_status, |
321 |
|
score_base => 100 * @{$self->{subdoc_results}}, |
322 |
|
parent_status => $global_status, is_total => 1}; |
323 |
} else { |
} else { |
324 |
my $elevel = htescape ($err->{level}); |
$global_status = $maindoc_status; |
|
$r = qq[<strong><a href="../error-description#level-$elevel">$elevel</a></strong>: |
|
|
]; |
|
325 |
} |
} |
326 |
|
|
327 |
return $r; |
push @row, sub { |
328 |
} # get_error_level_label |
$out->start_tag ('tfoot'); |
329 |
|
}; |
330 |
|
push @row, {label => 'Total', status => $global_status, |
331 |
|
score_base => 100 * (@{$self->{subdoc_results}} + 1), |
332 |
|
parent_status => {}, is_total => 1}; |
333 |
|
|
334 |
|
for my $x (@row) { |
335 |
|
if (ref $x eq 'CODE') { |
336 |
|
$x->(); |
337 |
|
next; |
338 |
|
} |
339 |
|
|
340 |
sub get_node_path ($) { |
$x->{parent_status}->{$_} += $x->{status}->{$_} |
341 |
my $self = shift; |
for qw/must should warning info uncertain/; |
342 |
my $node = shift; |
|
343 |
my @r; |
my $row_class = $x->{status}->{uncertain} ? 'uncertain' : ''; |
344 |
while (defined $node) { |
$row_class .= ' total' if $x->{is_total}; |
345 |
my $rs; |
$out->start_tag ('tr', class => $row_class); |
346 |
if ($node->node_type == 1) { |
my $uncertain = $x->{status}->{uncertain} ? '?' : ''; |
347 |
$rs = $node->node_name; |
|
348 |
$node = $node->parent_node; |
$out->start_tag ('td', class => 'subrow') unless $x->{is_total}; |
349 |
} elsif ($node->node_type == 2) { |
|
350 |
$rs = '@' . $node->node_name; |
## Layer name |
351 |
$node = $node->owner_element; |
$out->start_tag ('th', colspan => $x->{is_total} ? 2 : 1, |
352 |
} elsif ($node->node_type == 3) { |
scope => 'row'); |
353 |
$rs = '"' . $node->data . '"'; |
if (defined $x->{target} and |
354 |
$node = $node->parent_node; |
($x->{status}->{must} or $x->{status}->{should} or |
355 |
} elsif ($node->node_type == 9) { |
$x->{status}->{warning} or $x->{status}->{info} or |
356 |
@r = ('') unless @r; |
$x->{status}->{uncertain})) { |
357 |
$rs = ''; |
$out->xref ($x->{label}, target => $x->{target}); |
358 |
$node = $node->parent_node; |
} else { |
359 |
} else { |
$out->nl_text ($x->{label}); |
|
$rs = '#' . $node->node_type; |
|
|
$node = $node->parent_node; |
|
360 |
} |
} |
361 |
unshift @r, $rs; |
|
362 |
|
## MUST-level errors |
363 |
|
$out->start_tag ('td', class => $x->{status}->{must} ? 'must-errors' : ''); |
364 |
|
if ($x->{status}->{applicable}) { |
365 |
|
$out->text (($x->{status}->{must} or 0) . $uncertain); |
366 |
|
} else { |
367 |
|
$out->nl_text ('N/A'); |
368 |
|
} |
369 |
|
|
370 |
|
## SHOULD-level errors |
371 |
|
$out->start_tag ('td', |
372 |
|
class => $x->{status}->{should} ? 'should-errors' : ''); |
373 |
|
if ($x->{status}->{applicable}) { |
374 |
|
$out->text (($x->{status}->{should} or 0) . $uncertain); |
375 |
|
} else { |
376 |
|
$out->nl_text ('N/A'); |
377 |
|
} |
378 |
|
|
379 |
|
## Warnings |
380 |
|
$out->start_tag ('td', class => $x->{status}->{warning} ? 'warnings' : ''); |
381 |
|
if ($x->{status}->{applicable}) { |
382 |
|
$out->text (($x->{status}->{warning} or 0) . $uncertain); |
383 |
|
} else { |
384 |
|
$out->nl_text ('N/A'); |
385 |
|
} |
386 |
|
|
387 |
|
## Informations |
388 |
|
$out->start_tag ('td', class => $x->{status}->{info} ? 'infos' : ''); |
389 |
|
if ($x->{status}->{applicable}) { |
390 |
|
$out->text (($x->{status}->{info} or 0) . $uncertain); |
391 |
|
} else { |
392 |
|
$out->nl_text ('N/A'); |
393 |
|
} |
394 |
|
|
395 |
|
## Score |
396 |
|
$out->start_tag ('td', |
397 |
|
class => $x->{status}->{must} ? 'score must-errors' : |
398 |
|
$x->{status}->{should} ? 'score should-errors' : |
399 |
|
'score'); |
400 |
|
|
401 |
|
my $max_score = $x->{score_base}; |
402 |
|
$max_score -= $x->{status}->{must} * $score_unit; |
403 |
|
my $min_score = $max_score; |
404 |
|
$min_score -= $x->{status}->{should} * $score_unit; |
405 |
|
|
406 |
|
$out->start_tag ('strong'); |
407 |
|
if ($x->{status}->{uncertain}) { |
408 |
|
$out->html ('−∞ '); # negative inifinity |
409 |
|
$out->nl_text ('...'); |
410 |
|
$out->html ($max_score < 0 ? |
411 |
|
' −' . substr ($max_score, 1) : ' ' . $max_score); |
412 |
|
} elsif ($min_score != $max_score) { |
413 |
|
$out->html ($min_score < 0 ? |
414 |
|
'−' . substr ($min_score, 1) . ' ': $min_score . ' '); |
415 |
|
$out->nl_text ('...'); |
416 |
|
$out->html ($max_score < 0 ? |
417 |
|
' −' . substr ($max_score, 1) : ' ' . $max_score); |
418 |
|
} else { |
419 |
|
$out->html ($max_score < 0 ? |
420 |
|
'−' . substr ($max_score, 1) : $max_score); |
421 |
|
} |
422 |
|
$out->end_tag ('strong'); |
423 |
|
|
424 |
|
$out->text (' / ' . $x->{score_base}); |
425 |
} |
} |
426 |
return join '/', @r; |
|
427 |
} # get_node_path |
$out->end_tag ('table'); |
428 |
|
|
429 |
use Scalar::Util qw/refaddr/; |
my $parent = $self->parent_result; |
430 |
|
if ($parent) { |
431 |
|
$global_status->{input} = $out->input; |
432 |
|
push @{$parent->{subdoc_results}}, $global_status; |
433 |
|
} |
434 |
|
|
435 |
sub get_node_link ($$) { |
$out->nl_text ('This checker is work in progress.'); |
436 |
my $self = shift; |
$out->end_section; |
437 |
return qq[<a href="#$_[0]->{id_prefix}node-@{[refaddr $_[1]]}">] . |
} # generate_result_section |
|
($self->get_node_path ($_[1])) . qq[</a>]; |
|
|
## BUG: ^ must be escaped |
|
|
} # get_node_link |
|
438 |
|
|
439 |
1; |
1; |