/[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.18 - (hide annotations) (download)
Wed Sep 10 10:23:00 2008 UTC (16 years, 2 months ago) by wakaba
Branch: MAIN
Changes since 1.17: +2 -1 lines
++ ChangeLog	10 Sep 2008 10:21:38 -0000
2008-09-10  Wakaba  <wakaba@suika.fam.cx>

	* error-description-source.xml: Encoding layer errors added.

++ html/WebHACC/ChangeLog	10 Sep 2008 10:22:56 -0000
2008-09-10  Wakaba  <wakaba@suika.fam.cx>

	* Output.pm (nl_text): Support for |<var>{octets}</var>|,
	|<var>{char}</var>|, and |<var>{char:hexref}</var>|
	macros.
	(generate_input_section): More charsets are added
	to the list of charsets.

	* Result.pm: Pass |char| and |octets| arguments
	to |nl_text|.

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24