/[suikacvs]/test/html-webhacc/WebHACC/Result.pm
Suika

Contents of /test/html-webhacc/WebHACC/Result.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.17 - (show annotations) (download)
Sat Aug 30 10:27:47 2008 UTC (16 years, 2 months ago) by wakaba
Branch: MAIN
Changes since 1.16: +2 -1 lines
++ html/WebHACC/ChangeLog	30 Aug 2008 10:27:43 -0000
	* Result.pm (add_error): Don't raise an error even if the
	end position is after the end of the string.

2008-08-30  Wakaba  <wakaba@suika.fam.cx>

1 package WebHACC::Result;
2 use strict;
3
4 sub new ($) {
5 return bless {
6 global_status => 'conforming',
7 # or, 'should-error', 'non-conforming', 'uncertain'
8 subdoc_results => [],
9 }, shift;
10 } # 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 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;
44 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 $class = qq[level-$error_level layer-$error_layer];
85
86 ## Line & column numbers (prepare values)
87
88 my $line;
89 my $column;
90
91 if (defined $opt{node}) {
92 $line = $opt{node}->get_user_data ('manakai_source_line');
93 if (defined $line) {
94 $column = $opt{node}->get_user_data ('manakai_source_column');
95 } elsif ($opt{node}->isa ('Message::IF::Node')) {
96 if ($opt{node}->node_type == $opt{node}->ATTRIBUTE_NODE) {
97 my $owner = $opt{node}->owner_element;
98 if ($owner) {
99 $line = $owner->get_user_data ('manakai_source_line');
100 $column = $owner->get_user_data ('manakai_source_column');
101 }
102 } else {
103 my $parent = $opt{node}->parent_node;
104 if ($parent) {
105 $line = $parent->get_user_data ('manakai_source_line');
106 $column = $parent->get_user_data ('manakai_source_column');
107 }
108 }
109 }
110 }
111 unless (defined $line) {
112 if (defined $opt{token} and defined $opt{token}->{line}) {
113 $line = $opt{token}->{line};
114 $column = $opt{token}->{column};
115 } elsif (defined $opt{line}) {
116 $line = $opt{line};
117 $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) {
141 if (defined $column and $column > 0) {
142 $out->xref ('Line #', text => $line, target => 'line-' . $line);
143 $out->text (' ');
144 $out->nl_text ('column #', text => $column);
145 } else {
146 $out->xref ('Line #', text => $line, target => 'line-' . $line);
147 }
148 $has_location = 1;
149 }
150
151 ## Node path
152
153 if (defined $opt{node}) {
154 $out->html (' ');
155 $out->node_link ($opt{node});
156 $has_location = 1;
157 }
158
159 if (defined $opt{index}) {
160 if ($opt{index_has_link}) {
161 $out->html (' ');
162 $out->xref ('Index #', text => (0+$opt{index}),
163 target => 'index-' . (0+$opt{index}));
164 } else {
165 $out->html (' ');
166 $out->nl_text ('Index #', text => (0+$opt{index}));
167 }
168 $has_location = 1;
169 }
170
171 if (defined $opt{value}) {
172 $out->html (' ');
173 if (defined $opt{pos_start}) {
174 $out->start_tag ('code');
175 $out->text (substr $opt{value}, 0, $opt{pos_start});
176 $out->start_tag ('mark');
177 $out->text (substr $opt{value}, $opt{pos_start},
178 $opt{pos_end} - $opt{pos_start} + 1);
179 $out->end_tag ('mark');
180 $out->text (substr $opt{value}, $opt{pos_end} + 1)
181 if $opt{pos_end} < length $opt{value};
182 $out->end_tag ('code');
183 } elsif ($opt{value_mark_end}) {
184 $out->start_tag ('code');
185 $out->text ($opt{value});
186 $out->start_tag ('mark');
187 $out->end_tag ('mark');
188 $out->end_tag ('code');
189 } elsif (defined $opt{value_mark}) {
190 $out->start_tag ('code');
191 for (split /($opt{value_mark})/, $opt{value}) {
192 if (/$opt{value_mark}/) {
193 $out->start_tag ('mark');
194 $out->text ($_);
195 $out->end_tag ('mark');
196 } else {
197 $out->text ($_);
198 }
199 }
200 $out->end_tag ('code');
201 } else {
202 $out->code ($opt{value});
203 }
204 $has_location = 1;
205 }
206
207 unless ($has_location) {
208 if (defined $opt{input}) {
209 if (defined $opt{input}->{container_node}) {
210 my $original_input = $out->input;
211 $out->input ($opt{input}->{parent_input});
212 $out->node_link ($opt{input}->{container_node});
213 $out->input ($original_input);
214 $has_location = 1;
215 } elsif (defined $opt{input}->{request_uri}) {
216 $out->url ($opt{input}->{request_uri});
217 $has_location = 1;
218 } elsif (defined $opt{input}->url) {
219 $out->url ($opt{input}->url);
220 $has_location = 1;
221 }
222 }
223
224 unless ($has_location) {
225 $out->nl_text ('Unknown location');
226 }
227 }
228
229 $out->start_tag ('dd', class => $class);
230
231 ## Error level
232 $out->nl_text ('Error level ' . $error_level);
233 $out->text (': ');
234
235 ## Error message
236 my $error_type_text = $opt{type};
237 $out->nl_text ($error_type_text, node => $opt{node}, text => $opt{text},
238 value => $opt{value});
239
240 ## Link to a long description
241
242 my $fragment = $opt{type};
243 $fragment =~ tr/ /-/;
244 $fragment = $out->encode_url_component ($fragment);
245 $out->text (' [');
246 $out->start_tag ('a', href => '../error-description#' . $fragment,
247 rel => 'help');
248 $out->nl_text ('Description');
249 $out->end_tag ('a');
250 $out->text (']');
251
252 if ($error_level eq 'm') {
253 $self->{layers}->{$error_layer}->{must}++;
254 $self->{global_status} = 'non-conforming';
255 } elsif ($error_level eq 's') {
256 $self->{layers}->{$error_layer}->{should}++;
257 $self->{global_status} = 'should-error'
258 unless {'non-conforming' => 1,
259 uncertain => 1}->{$self->{global_status}};
260 } elsif ($error_level eq 'w') {
261 $self->{layers}->{$error_layer}->{warning}++;
262 } elsif ($error_level eq 'u') {
263 $self->{layers}->{$error_layer}->{uncertain}++;
264 $self->{global_status} = 'uncertain'
265 unless $self->{global_status} eq 'non-conforming';
266 } elsif ($error_level eq 'i') {
267 $self->{layers}->{$error_layer}->{info}++;
268 }
269 } # add_error
270
271 sub generate_result_section ($) {
272 my $self = shift;
273
274 my $result = $self;
275
276 my $out = $result->output;
277
278 $out->start_section (role => 'result');
279
280 my $para_class = {
281 'conforming' => 'result-para no-error',
282 'should-error' => 'result-para should-errors',
283 'non-conforming' => 'result-para must-errors',
284 'uncertain' => 'result-para uncertain',
285 }->{$self->{global_status}};
286 $out->start_tag ('p', class => $para_class);
287 $out->nl_text ('Conformance is ' . $self->{global_status});
288 $out->end_tag ('p');
289
290 $out->html (qq[<table>
291 <colgroup><col><col><colgroup><col><col><col><col><colgroup><col>
292 <thead>
293 <tr><th scope=col colspan=2>]);
294 for ('Error level m', 'Error level s', 'Error level w',
295 'Error level i', 'Score') {
296 $out->start_tag ('th');
297 $out->nl_text ($_);
298 }
299
300 my $maindoc_status = {must => 0, should => 0, warning => 0, info => 0,
301 uncertain => 0, applicable => 1};
302 my $subdocs_status = {must => 0, should => 0, warning => 0, info => 0,
303 uncertain => 0, applicable => 1};
304 my $global_status = {must => 0, should => 0, warning => 0, info => 0,
305 uncertain => 0, applicable => 1};
306
307 my $score_unit = 2;
308
309 my @row = (
310 sub {
311 $out->start_tag ('tbody');
312 $out->start_tag ('tr');
313 $out->start_tag ('th', colspan => 7, scope => 'col');
314 $out->nl_text ('Main document');
315 },
316 {label => 'Transfer L.', status => $self->{layers}->{transfer},
317 target => 'transfer-errors', score_base => 20,
318 parent_status => $maindoc_status},
319 {label => 'Encode L.', status => $self->{layers}->{encode},
320 target => 'parse-errors', score_base => 10,
321 parent_status => $maindoc_status},
322 {label => 'Char L.', status => $self->{layers}->{charset},
323 score_base => 10,
324 parent_status => $maindoc_status},
325 {label => 'Syntax L.', status => $self->{layers}->{syntax},
326 target => 'parse-errors', score_base => 20,
327 parent_status => $maindoc_status},
328 {label => 'Structure L.', status => $self->{layers}->{structure},
329 target => 'document-errors', score_base => 20,
330 parent_status => $maindoc_status},
331 {label => 'Semantics L.', status => $self->{layers}->{semantics},
332 score_base => 20,
333 parent_status => $maindoc_status},
334 );
335
336 if (@{$self->{subdoc_results}}) {
337 push @row, {label => 'Subtotal', status => $maindoc_status,
338 score_base => 100,
339 parent_status => $global_status, is_total => 1};
340 push @row, sub {
341 $out->start_tag ('tbody');
342 $out->start_tag ('tr');
343 $out->start_tag ('th', colspan => 7, scope => 'col');
344 $out->nl_text ('Subdocuments');
345 };
346 for (@{$self->{subdoc_results}}) {
347 push @row, {label => '#' . $_->{input}->full_subdocument_index,
348 status => $_,
349 target => $_->{input}->id_prefix . 'result-summary',
350 score_base => 100, parent_status => $subdocs_status};
351 }
352 push @row, {label => 'Subtotal', status => $subdocs_status,
353 score_base => 100 * @{$self->{subdoc_results}},
354 parent_status => $global_status, is_total => 1};
355 } else {
356 $global_status = $maindoc_status;
357 }
358
359 push @row, sub {
360 $out->start_tag ('tfoot');
361 };
362 push @row, {label => 'Total', status => $global_status,
363 score_base => 100 * (@{$self->{subdoc_results}} + 1),
364 parent_status => {}, is_total => 1};
365
366 for my $x (@row) {
367 if (ref $x eq 'CODE') {
368 $x->();
369 next;
370 }
371
372 $x->{parent_status}->{$_} += $x->{status}->{$_}
373 for qw/must should warning info uncertain/;
374
375 my $row_class = $x->{status}->{uncertain} ? 'uncertain' : '';
376 $row_class .= ' total' if $x->{is_total};
377 $out->start_tag ('tr', class => $row_class);
378 my $uncertain = $x->{status}->{uncertain} ? '?' : '';
379
380 $out->start_tag ('td', class => 'subrow') unless $x->{is_total};
381
382 ## Layer name
383 $out->start_tag ('th', colspan => $x->{is_total} ? 2 : 1,
384 scope => 'row');
385 if (defined $x->{target} and
386 ($x->{status}->{must} or $x->{status}->{should} or
387 $x->{status}->{warning} or $x->{status}->{info} or
388 $x->{status}->{uncertain})) {
389 $out->xref ($x->{label}, target => $x->{target});
390 } else {
391 $out->nl_text ($x->{label});
392 }
393
394 ## MUST-level errors
395 $out->start_tag ('td', class => $x->{status}->{must} ? 'must-errors' : '');
396 if ($x->{status}->{applicable}) {
397 $out->text (($x->{status}->{must} or 0) . $uncertain);
398 } else {
399 $out->nl_text ('N/A');
400 }
401
402 ## SHOULD-level errors
403 $out->start_tag ('td',
404 class => $x->{status}->{should} ? 'should-errors' : '');
405 if ($x->{status}->{applicable}) {
406 $out->text (($x->{status}->{should} or 0) . $uncertain);
407 } else {
408 $out->nl_text ('N/A');
409 }
410
411 ## Warnings
412 $out->start_tag ('td', class => $x->{status}->{warning} ? 'warnings' : '');
413 if ($x->{status}->{applicable}) {
414 $out->text (($x->{status}->{warning} or 0) . $uncertain);
415 } else {
416 $out->nl_text ('N/A');
417 }
418
419 ## Informations
420 $out->start_tag ('td', class => $x->{status}->{info} ? 'infos' : '');
421 if ($x->{status}->{applicable}) {
422 $out->text (($x->{status}->{info} or 0) . $uncertain);
423 } else {
424 $out->nl_text ('N/A');
425 }
426
427 ## Score
428 $out->start_tag ('td',
429 class => $x->{status}->{must} ? 'score must-errors' :
430 $x->{status}->{should} ? 'score should-errors' :
431 'score');
432
433 my $max_score = $x->{score_base};
434 $max_score -= $x->{status}->{must} * $score_unit;
435 my $min_score = $max_score;
436 $min_score -= $x->{status}->{should} * $score_unit;
437
438 $out->start_tag ('strong');
439 if ($x->{status}->{uncertain}) {
440 $out->html ('&#x2212;&#x221E; '); # negative inifinity
441 $out->nl_text ('...');
442 $out->html ($max_score < 0 ?
443 ' &#x2212;' . substr ($max_score, 1) : ' ' . $max_score);
444 } elsif ($min_score != $max_score) {
445 $out->html ($min_score < 0 ?
446 '&#x2212;' . substr ($min_score, 1) . ' ': $min_score . ' ');
447 $out->nl_text ('...');
448 $out->html ($max_score < 0 ?
449 ' &#x2212;' . substr ($max_score, 1) : ' ' . $max_score);
450 } else {
451 $out->html ($max_score < 0 ?
452 '&#x2212;' . substr ($max_score, 1) : $max_score);
453 }
454 $out->end_tag ('strong');
455
456 $out->text (' / ' . $x->{score_base});
457 }
458
459 $out->end_tag ('table');
460
461 my $parent = $self->parent_result;
462 if ($parent) {
463 $global_status->{input} = $out->input;
464 push @{$parent->{subdoc_results}}, $global_status;
465 }
466
467 $out->nl_text ('This checker is work in progress.');
468 $out->end_section;
469 } # generate_result_section
470
471 1;

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24