/[suikacvs]/messaging/manakai/bin/domtest2perl.pl
Suika

Contents of /messaging/manakai/bin/domtest2perl.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.8 - (hide annotations) (download)
Thu Jan 6 10:41:31 2005 UTC (19 years, 10 months ago) by wakaba
Branch: MAIN
Changes since 1.7: +30 -14 lines
File MIME type: text/plain
DOM test improved; Node.normalize() implemented; DOMMain:ManakaiDOMExceptionIF interface added; DOMCore:ManakaiDOMErrorHandler added

1 wakaba 1.1 #!/usr/bin/perl -w
2     use lib q<../lib>;
3     use strict;
4 wakaba 1.2 BEGIN { require 'manakai/genlib.pl' }
5 wakaba 1.1
6 wakaba 1.6 use Message::Util::QName::Filter {
7     ManakaiDOMLS2003 => q<http://suika.fam.cx/~wakaba/archive/2004/9/27/mdom-old-ls#>,
8 wakaba 1.1 };
9     use Message::DOM::ManakaiDOMLS2003;
10     use Message::DOM::DOMLS qw/MODE_SYNCHRONOUS/;
11 wakaba 1.3 use Getopt::Long;
12 wakaba 1.1
13 wakaba 1.3 require 'dommemlist.pl.tmp'; ## Generated by mkdommemlist.pl
14    
15     my $output_filename;
16     GetOptions (
17     'output-file=s' => \$output_filename,
18     );
19 wakaba 1.8
20 wakaba 1.3 if (defined $output_filename) {
21 wakaba 1.8 open my $f, '>', $output_filename or die "$0: $output_filename: $!";
22     our $ResultOutput = $f;
23 wakaba 1.3 } else {
24 wakaba 1.8 our $ResultOutput = \*STDOUT;
25 wakaba 1.3 }
26    
27 wakaba 1.4 our $Method;
28     our $IFMethod;
29 wakaba 1.3 our $Attr;
30 wakaba 1.1 my $Assert = {
31     qw/assertDOMException 1
32 wakaba 1.5 assertEquals 1
33 wakaba 1.3 assertFalse 1
34 wakaba 1.5 assertInstanceOf 1
35 wakaba 1.1 assertNotNull 1
36 wakaba 1.3 assertNull 1
37 wakaba 1.5 assertSame 1
38 wakaba 1.4 assertSize 1
39 wakaba 1.5 assertTrue 1
40     assertURIEquals 1/
41 wakaba 1.1 };
42     my $Misc = {
43 wakaba 1.5 qw/append 1
44     assign 1
45     decrement 1
46     fail 1
47     if 1
48 wakaba 1.3 implementationAttribute 1
49 wakaba 1.5 increment 1
50     for 1
51     plus 1
52     var 1
53     while 1/
54 wakaba 1.1 };
55 wakaba 1.3 my $Condition = {
56     qw/condition 1
57     contains 1
58     contentType 1
59 wakaba 1.5 equals 1
60     greater 1
61     greaterOrEquals 1
62 wakaba 1.3 hasSize 1
63     implementationAttribute 1
64 wakaba 1.5 instanceOf 1
65     isNull 1
66     less 1
67     lessOrEquals 1
68 wakaba 1.3 not 1
69 wakaba 1.5 notEquals 1
70 wakaba 1.3 notNull 1
71     or 1/
72     };
73    
74     my $Status = {Number => 0, our => {Info => 1}};
75    
76     sub to_perl_value ($;%) {
77     my ($s, %opt) = @_;
78     if (defined $s) {
79     if ($s =~ /^(?!\d)\w+$/) {
80 wakaba 1.5 if ({true => 1, false => 1}->{$s}) {
81     return {true => '1', false => '0'}->{$s};
82     } else {
83     return perl_var (type => '$', local_name => $s);
84     }
85 wakaba 1.3 } else {
86     return $s;
87     }
88     } elsif (defined $opt{default}) {
89     return $opt{default};
90     } else {
91     return '';
92     }
93     }
94 wakaba 1.1
95     sub body2code ($) {
96     my $parent = shift;
97     my $result = '';
98     my $children = $parent->childNodes;
99     for (my $i = 0; $i < $children->length; $i++) {
100     my $child = $children->item ($i);
101     if ($child->nodeType == $child->ELEMENT_NODE) {
102     my $ln = $child->localName;
103     if ($Method->{$ln} or $Attr->{$ln} or
104     $Assert->{$ln} or $Misc->{$ln}) {
105     $result .= node2code ($child);
106     } else {
107     valid_err q<Unknown element type: >.$child->localName,
108     node => $child;
109     }
110     } elsif ($child->nodeType == $child->COMMENT_NODE) {
111     $result .= perl_comment $child->data;
112     } elsif ($child->nodeType == $child->TEXT_NODE) {
113     if ($child->data =~ /\S/) {
114     valid_err q<Unknown character data: >.$child->data,
115     node => $child;
116     }
117     } else {
118     valid_err q<Unknown type of node: >.$child->nodeType,
119     node => $child;
120     }
121     }
122     $result;
123     }
124    
125 wakaba 1.3 sub condition2code ($;%) {
126     my ($parent, %opt) = @_;
127     my $result = '';
128     my @result;
129     my $children = $parent->childNodes;
130     for (my $i = 0; $i < $children->length; $i++) {
131     my $child = $children->item ($i);
132     if ($child->nodeType == $child->ELEMENT_NODE) {
133     my $ln = $child->localName;
134     if ($Condition->{$ln}) {
135     push @result, node2code ($child);
136     } else {
137     valid_err q<Unknown element type: >.$child->localName,
138     node => $child;
139     }
140     } elsif ($child->nodeType == $child->COMMENT_NODE) {
141     $result .= perl_comment $child->data;
142     } elsif ($child->nodeType == $child->TEXT_NODE) {
143     if ($child->data =~ /\S/) {
144     valid_err q<Unknown character data: >.$child->data,
145     node => $child;
146     }
147     } else {
148     valid_err q<Unknown type of node: >.$child->nodeType,
149     node => $child;
150     }
151     }
152     $result .= join (($opt{join}||='or' eq 'or' ? ' || ' :
153     $opt{join} eq 'and' ? ' && ' :
154     valid_err q<Multiple condition not supported>,
155     node => $parent),
156     map {"($_)"} @result);
157     $result;
158     } #condition2code
159    
160     sub node2code ($);
161 wakaba 1.1 sub node2code ($) {
162     my $node = shift;
163     my $result = '';
164 wakaba 1.3 if ($node->nodeType != $node->ELEMENT_NODE) {
165     if ($node->nodeType == $node->COMMENT_NODE) {
166     $result .= perl_comment $node->data;
167     } elsif ($node->nodeType == $node->TEXT_NODE) {
168     if ($node->data =~ /\S/) {
169     valid_err q<Unknown character data: >.$node->data,
170     node => $node;
171     }
172     } else {
173     valid_err q<Unknown type of node: >.$node->nodeType,
174     node => $node;
175     }
176     return $result;
177     }
178 wakaba 1.1 my $ln = $node->localName;
179    
180     if ($ln eq 'var') {
181 wakaba 1.3 my $name = $node->getAttributeNS (undef, 'name');
182 wakaba 1.5 my $var = perl_var
183 wakaba 1.3 local_name => $name,
184 wakaba 1.1 scope => 'my',
185     type => '$';
186 wakaba 1.5 my $type = $node->getAttributeNS (undef, 'type');
187     $result .= perl_comment $type;
188     if ($node->hasAttributeNS (undef, 'isNull') and
189     $node->getAttributeNS (undef, 'isNull') eq 'true') {
190     $result .= perl_statement perl_assign $var => 'undef';
191     } elsif ($node->hasAttributeNS (undef, 'value')) {
192     $result .= perl_statement
193     perl_assign
194     $var
195     => to_perl_value ($node->getAttributeNS (undef, 'value'));
196     } else {
197     if ($type eq 'List' or $type eq 'Collection') {
198     my @member;
199     my $children = $node->childNodes;
200     for (my $i = 0; $i < $children->length; $i++) {
201     my $child = $children->item ($i);
202     if ($child->nodeType == $child->ELEMENT_NODE) {
203     if ($child->localName eq 'member') {
204     push @member, perl_code_literal
205     (to_perl_value ($child->textContent));
206     } else {
207     valid_err q<Unsupported element type>, node => $child;
208     }
209     } elsif ($child->nodeType == $child->COMMENT_NODE) {
210     $result .= perl_comment $child->data;
211     }
212     }
213     $result .= perl_statement
214     perl_assign
215     $var
216     => perl_list \@member;
217     } elsif ($type =~ /Monitor/) {
218     valid_err qq<Type $type not supported>, node => $node;
219     } elsif ($node->hasChildNodes) {
220     valid_err q<Children not supported>, node => $node;
221     } else {
222     $result .= perl_statement $var;
223     }
224 wakaba 1.3 }
225     $Status->{var}->{$name}->{type} = $node->getAttributeNS (undef, 'type');
226     } elsif ($ln eq 'load') {
227 wakaba 1.1 $result .= perl_statement
228     perl_assign
229     perl_var
230     (type => '$',
231     local_name => $node->getAttributeNS (undef, 'var'))
232     => 'load (' .
233     perl_literal ($node->getAttributeNS (undef, 'href')).
234     ')';
235 wakaba 1.7 } elsif ($ln eq 'hasFeature' and
236     not $node->hasAttributeNS (undef, 'var')) {
237     ## If there is a "hasFeature" element in "body" and
238     ## it does not have "var" attribute, then it is part of the
239     ## implementation condition.
240     $result .= perl_statement 'hasFeature ('.
241     to_perl_value ($node->getAttributeNS (undef, 'feature'),
242     default => 'undef') . ', '.
243     to_perl_value ($node->getAttributeNS (undef, 'version'),
244     default => 'undef') . ')';
245     } elsif ($Method->{$ln}) {
246 wakaba 1.1 $result .= perl_var (type => '$',
247     local_name => $node->getAttributeNS (undef, 'var')).
248     ' = '
249     if $node->hasAttributeNS (undef, 'var');
250 wakaba 1.4 my $param;
251     if ($node->hasAttributeNS (undef, 'interface')) {
252 wakaba 1.5 my $if = $node->getAttributeNS (undef, 'interface');
253     $param = $IFMethod->{$if}->{$ln};
254     unless ($param) {
255     valid_err "Method $if.$ln not supported", node => $node;
256     }
257     if ($if eq 'Element' and $ln eq 'getElementsByTagName' and
258     not $node->hasAttributeNS (undef, 'name') and
259     $node->hasAttributeNS (undef, 'tagname')) {
260     $node->setAttributeNS (undef, 'name'
261     => $node->getAttributeNS (undef, 'tagname'));
262     }
263 wakaba 1.4 } else {
264     $param = $Method->{$ln};
265     }
266 wakaba 1.1 $result .= perl_var (type => '$',
267     local_name => $node->getAttributeNS (undef, 'obj')).
268     '->'.$ln.' ('.
269 wakaba 1.3 join (', ',
270     map {
271     to_perl_value ($node->getAttributeNS (undef, $_),
272     default => 'undef')
273 wakaba 1.4 } @$param).
274 wakaba 1.1 ");\n";
275     } elsif ($Attr->{$ln}) {
276     if ($node->hasAttributeNS (undef, 'var')) {
277     $result .= perl_var (type => '$',
278     local_name => $node->getAttributeNS (undef, 'var')).
279     ' = ';
280 wakaba 1.3 } elsif ($node->hasAttributeNS (undef, 'value')) {
281     #
282 wakaba 1.1 } else {
283 wakaba 1.3 valid_err q<Unknown operation to an attribute>, node => $node;
284 wakaba 1.1 }
285 wakaba 1.5 my $obj = perl_var (type => '$',
286     local_name => $node->getAttributeNS (undef, 'obj'));
287     my $if = $node->getAttributeNS (undef, 'interface');
288     if (defined $if and $if eq 'DOMString') {
289     if ($ln eq 'length') {
290     $result .= 'length '.$obj;
291     } else {
292     valid_err q<$if.$ln not supported>, node => $node;
293     }
294     } else {
295     $result .= $obj.'->'.$ln;
296     }
297 wakaba 1.1 if ($node->hasAttributeNS (undef, 'var')) {
298     $result .= ";\n";
299 wakaba 1.3 } elsif ($node->hasAttributeNS (undef, 'value')) {
300     $result .= " (".to_perl_value ($node->getAttributeNS (undef, 'value')).
301     ");\n";
302     }
303     } elsif ($ln eq 'assertEquals') {
304     my $expected = $node->getAttributeNS (undef, 'expected');
305     my $expectedType = $Status->{var}->{$expected}->{type} || '';
306     $result .= 'assertEquals'.
307     ({Collection => 'Collection',
308     List => 'List'}->{$expectedType}||'');
309     my $ignoreCase = $node->getAttributeNS (undef, 'ignoreCase') || 'false';
310     if ($ignoreCase eq 'auto') {
311     $result .= 'AutoCase ('.
312     perl_literal ($node->getAttributeNS (undef, 'context') ||
313     'element').
314     ', ';
315     } else {
316     $result .= ' (';
317 wakaba 1.1 }
318 wakaba 1.3 $result .= perl_literal ($node->getAttributeNS (undef, 'id')).', ';
319     $result .= join ", ", map {
320     $ignoreCase eq 'true'
321     ? ($expectedType eq 'Collection' or
322     $expectedType eq 'List')
323     ? "toLowerArray ($_)" : "lc ($_)"
324     : $_
325     } map {
326     to_perl_value ($_)
327     } (
328     $expected,
329     $node->getAttributeNS (undef, 'actual'),
330     );
331     $result .= ");\n";
332     $Status->{Number}++;
333 wakaba 1.5 } elsif ($ln eq 'assertInstanceOf') {
334     my $obj = perl_code_literal
335     (to_perl_value ($node->getAttributeNS (undef, 'obj')));
336     $result .= perl_statement 'assertInstanceOf ('.
337     perl_list
338     ($node->getAttributeNS (undef, 'id'),
339     $node->getAttributeNS (undef, 'type'),
340     $obj).
341     ')';
342     if ($node->hasChildNodes) {
343     $result .= perl_if
344     'isInstanceOf ('.
345     perl_list
346     ($node->getAttributeNS (undef, 'type'),
347     $obj) . ')',
348     body2code ($node);
349     }
350     $Status->{Number}++;
351     } elsif ($ln eq 'assertSame') {
352     my $expected = to_perl_value ($node->getAttributeNS (undef, 'expected'));
353     my $actual = to_perl_value ($node->getAttributeNS (undef, 'actual'));
354     $result .= perl_statement 'assertSame ('.
355     perl_list
356     ($node->getAttributeNS (undef, 'id'),
357     $expected, $actual).
358     ')';
359     if ($node->hasChildNodes) {
360     $result .= perl_if
361     'same ('.(perl_list $expected, $actual).')',
362     body2code ($node);
363     }
364     $Status->{Number}++;
365 wakaba 1.4 } elsif ($ln eq 'assertSize') {
366     my $size = to_perl_value ($node->getAttributeNS (undef, 'size'));
367     my $coll = to_perl_value ($node->getAttributeNS (undef, 'collection'));
368     $result .= perl_statement 'assertSize ('.
369     perl_list
370     ($node->getAttributeNS (undef, 'id'),
371     perl_code_literal $size, perl_code_literal $coll).
372     ')';
373     if ($node->hasChildNodes) {
374     $result .= perl_if
375     qq<$size == size ($coll)>,
376 wakaba 1.5 body2code ($node);
377 wakaba 1.4 }
378 wakaba 1.5 $Status->{Number}++;
379 wakaba 1.3 } elsif ($ln eq 'assertTrue' or $ln eq 'assertFalse') {
380     my $condition;
381 wakaba 1.1 if ($node->hasAttributeNS (undef, 'actual')) {
382 wakaba 1.3 $condition = perl_var (type => '$',
383 wakaba 1.1 local_name => $node->getAttributeNS
384 wakaba 1.3 (undef, 'actual'));
385 wakaba 1.1 if ($node->hasChildNodes) {
386     valid_err q<Child of $ln found but not supported>,
387     node => $node;
388     }
389 wakaba 1.3 } elsif ($node->hasChildNodes) {
390     $condition = condition2code ($node);
391 wakaba 1.1 } else {
392 wakaba 1.3 valid_err $ln.q< w/o @actual not supported>, node => $node;
393 wakaba 1.1 }
394 wakaba 1.3 $result .= perl_statement $ln . ' ('.
395     perl_literal ($node->getAttributeNS (undef, 'id')).', '.
396     $condition. ')';
397     $Status->{Number}++;
398 wakaba 1.5 } elsif ($ln eq 'assertNotNull' or $ln eq 'assertNull') {
399     $result .= perl_statement $ln . ' (' .
400 wakaba 1.1 perl_literal ($node->getAttributeNS (undef, 'id')).', '.
401     perl_var (type => '$',
402     local_name => $node->getAttributeNS (undef, 'actual')).
403     ')';
404 wakaba 1.5 if ($node->hasChildNodes) {
405     valid_err q<Child of $ln found but not supported>,
406 wakaba 1.1 node => $node;
407 wakaba 1.5 }
408     $Status->{Number}++;
409     } elsif ($ln eq 'assertURIEquals') {
410     $result .= perl_statement 'assertURIEquals ('.
411     perl_list
412     ($node->getAttributeNS (undef, 'id'),
413     perl_code_literal
414     (to_perl_value ($node->getAttributeNS (undef, 'scheme'),
415     default => 'undef')),
416     perl_code_literal
417     (to_perl_value ($node->getAttributeNS (undef, 'path'),
418     default => 'undef')),
419     perl_code_literal
420     (to_perl_value ($node->getAttributeNS (undef, 'host'),
421     default => 'undef')),
422     perl_code_literal
423     (to_perl_value ($node->getAttributeNS (undef, 'file'),
424     default => 'undef')),
425     perl_code_literal
426     (to_perl_value ($node->getAttributeNS (undef, 'name'),
427     default => 'undef')),
428     perl_code_literal
429     (to_perl_value ($node->getAttributeNS (undef, 'query'),
430     default => 'undef')),
431     perl_code_literal
432     (to_perl_value ($node->getAttributeNS (undef, 'fragment'),
433     default => 'undef')),
434     perl_code_literal
435     (to_perl_value ($node->getAttributeNS (undef, 'isAbsolute'),
436     default => 'undef')),
437     perl_code_literal
438     (to_perl_value ($node->getAttributeNS (undef, 'actual')))).
439     ')';
440 wakaba 1.3 $Status->{Number}++;
441 wakaba 1.1 } elsif ($ln eq 'assertDOMException') {
442     $Status->{use}->{'Message::Util::Error'} = 1;
443     $result .= q[
444     {
445     my $success = 0;
446     try {
447     ];
448     my $children = $node->childNodes;
449     my $errname;
450     for (my $i = 0; $i < $children->length; $i++) {
451     my $child = $children->item ($i);
452     $errname = $child->localName if $child->nodeType == $child->ELEMENT_NODE;
453     $result .= body2code ($child);
454     }
455     $result .= q[
456 wakaba 1.7 } catch Message::DOM::IF::DOMException with {
457 wakaba 1.1 my $err = shift;
458     $success = 1 if $err->{-type} eq ].perl_literal ($errname).q[;
459 wakaba 1.5 };
460 wakaba 1.1 assertTrue (].perl_literal ($node->getAttributeNS (undef, 'id')).
461     q[, $success);
462     }
463     ];
464 wakaba 1.3 $Status->{Number}++;
465     } elsif ($ln eq 'contentType') {
466     $result .= '$builder->{contentType} eq '.
467     perl_literal ($node->getAttributeNS (undef, 'type'));
468     $Status->{our}->{builder} = 1;
469 wakaba 1.5 } elsif ($ln eq 'for-each') {
470     my $collection = $node->getAttributeNS (undef, 'collection');
471     my $collType = $Status->{var}->{$collection}->{type};
472     my $coll = to_perl_value ($collection);
473 wakaba 1.8 my $assert;
474     my $code;
475     {
476     local $Status->{Number} = 0;
477     $code = body2code ($node);
478     $assert = $Status->{Number};
479     }
480     $Status->{Number_local} = 1;
481 wakaba 1.5 $result .= 'for (my $i = 0; $i < '.
482     ({'Collection'=>1,'List'=>1}->{$collType}
483     ? '@{'.$coll.'}' : $coll.'->length').
484     '; $i++) {'.
485 wakaba 1.8 perl_statement (qq<plan_local ($assert)>).
486 wakaba 1.5 perl_statement
487     (perl_assign
488     to_perl_value ($node->getAttributeNS (undef, 'member'))
489     => $coll . ({'Collection'=>1,'List'=>1}->{$collType}
490     ? '->[$i]' : '->item ($i)')).
491 wakaba 1.8 $code.
492 wakaba 1.5 '}';
493     } elsif ($ln eq 'try') {
494     my $children = $node->childNodes;
495     my $true = '';
496     my $false = '';
497     for (my $i = 0; $i < $children->length; $i++) {
498     my $child = $children->item ($i);
499     if ($child->nodeType == $child->ELEMENT_NODE) {
500     if ($child->localName eq 'catch') {
501     valid_err q<Multiple 'catch'es found>, node => $child
502     if $false;
503     my @case;
504     my $children2 = $child->childNodes;
505     for (my $j = 0; $j < $children2->length; $j++) {
506     my $child2 = $children2->item ($j);
507     if ($child2->nodeType == $child2->ELEMENT_NODE) {
508     if ($child2->localName eq 'ImplementationException') {
509     valid_err q<Element type not supported>, node => $child2;
510     } else {
511     push @case, '$err->{-type} eq '.
512     perl_literal ($child2->getAttributeNS (undef, 'code'))
513     => body2code ($child2);
514     }
515     } else {
516     $false .= node2code ($child2);
517     }
518     }
519     $false .= perl_cases @case, else => perl_statement '$err->throw';
520     } else {
521     $true .= node2code ($child);
522     }
523     } else {
524     $true .= node2code ($child);
525     }
526     }
527     $result = "try {
528     $true
529 wakaba 1.7 } catch Message::DOM::DOMMain::ManakaiDOMException with {
530 wakaba 1.5 my \$err = shift;
531     $false
532     };";
533     $Status->{use}->{'Message::Util::Error'} = 1;
534 wakaba 1.3 } elsif ($ln eq 'if') {
535     my $children = $node->childNodes;
536     my $condition;
537     my $true = '';
538 wakaba 1.5 my $false = '';
539     my $assert_true = 0;
540     my $assert_false = 0;
541 wakaba 1.3 for (my $i = 0; $i < $children->length; $i++) {
542     my $child = $children->item ($i);
543     if ($child->nodeType == $child->ELEMENT_NODE) {
544     if (not $condition) {
545     $condition = node2code ($child);
546     } elsif ($child->localName eq 'else') {
547     valid_err q<Multiple 'else's found>, node => $child
548 wakaba 1.5 if $false;
549     local $Status->{Number} = 0;
550     $false = body2code ($child);
551     $assert_false = $Status->{Number};
552 wakaba 1.3 } else {
553 wakaba 1.5 local $Status->{Number} = 0;
554     $true .= node2code ($child);
555     $assert_true += $Status->{Number};
556 wakaba 1.3 }
557     } else {
558 wakaba 1.5 $true .= node2code ($child);
559 wakaba 1.3 }
560     }
561 wakaba 1.5 if ($assert_true == $assert_false) {
562     $Status->{Number} += $assert_true;
563     } elsif ($assert_true > $assert_false) {
564 wakaba 1.8 $false .= perl_statement 'skip_n ('.
565     perl_list ($assert_true - $assert_false,
566     msg => q<Conditional>).')';
567 wakaba 1.5 $Status->{Number} += $assert_true;
568     } else {
569 wakaba 1.8 $true .= perl_statement 'skip_n ('.
570     perl_list ($assert_false - $assert_true,
571     msg => q<Conditional>).')';
572 wakaba 1.5 $Status->{Number} += $assert_false;
573     }
574 wakaba 1.3 $result = perl_if
575     $condition,
576 wakaba 1.5 $true,
577     $false ? $false : undef;
578     } elsif ($ln eq 'while') {
579     my $children = $node->childNodes;
580     my $condition;
581     my $true = '';
582     my $assert = 0;
583     {
584     local $Status->{Number} = 0;
585     for (my $i = 0; $i < $children->length; $i++) {
586     my $child = $children->item ($i);
587     if ($child->nodeType == $child->ELEMENT_NODE) {
588     if (not $condition) {
589     $condition = node2code ($child);
590     } else {
591     $true .= node2code ($child);
592     }
593     } else {
594     $true .= node2code ($child);
595     }
596     }
597     $assert = $Status->{Number};
598     }
599 wakaba 1.8 $Status->{Number_local} = 1;
600 wakaba 1.5 $result .= "while ($condition) {
601 wakaba 1.8 plan_local ($assert);
602 wakaba 1.5 $true
603     }";
604 wakaba 1.3 } elsif ($ln eq 'or') {
605     $result .= condition2code ($node, join => 'or');
606     } elsif ($ln eq 'not') {
607     $result .= 'not '.condition2code ($node, join => 'nosupport');
608 wakaba 1.5 } elsif ($ln eq 'notNull' or $ln eq 'isNull') {
609 wakaba 1.3 $result .= 'defined '.
610     perl_var (type => '$',
611     local_name => $node->getAttributeNS (undef, 'obj'));
612 wakaba 1.5 $result = 'not ' . $result if $ln eq 'isNull';
613     } elsif ({less => 1, lessOrEquals => 1,
614     greater => 1, greaterOrEquals => 1}->{$ln}) {
615     $result .= to_perl_value ($node->getAttributeNS (undef, 'actual')).
616     {less => '<', lessOrEquals => '<=',
617     greater => '>', greaterOrEquals => '>='}->{$ln}.
618     to_perl_value ($node->getAttributeNS (undef, 'expected'));
619     } elsif ($ln eq 'equals' or $ln eq 'notEquals') {
620     my $case = $node->getAttributeNS (undef, 'ignoreCase');
621     if ($case and $case eq 'auto') {
622     $result .= 'equalsAutoCase (' .
623     perl_list
624     ($node->getAttributeNS (undef, 'context') || 'element',
625     to_perl_value
626     ($node->getAttributeNS (undef, 'expected')),
627     to_perl_value
628     ($node->getAttributeNS (undef, 'actual'))) . ')';
629     } else {
630     my $expected = to_perl_value
631     ($node->getAttributeNS (undef, 'expected'));
632     my $actual = to_perl_value
633     ($node->getAttributeNS (undef, 'actual'));
634     if ($case eq 'true') {
635     $result = "(uc ($expected) eq uc ($actual))";
636     } elsif ($node->hasAttributeNS (undef, 'bitmask')) {
637     my $bm = ' & ' . to_perl_value
638     ($node->getAttributeNS (undef, 'bitmask'));
639     $result = "($expected$bm == $actual$bm)";
640     } else {
641     $result = "($expected eq $actual)";
642     }
643     }
644     $result = "(not $result)" if $ln eq 'notEquals';
645     } elsif ($ln eq 'increment' or $ln eq 'decrement') {
646     $result .= perl_statement
647     to_perl_value ($node->getAttributeNS (undef, 'var')).
648     {increment => ' += ', decrement => ' -= '}->{$ln}.
649     to_perl_value ($node->getAttributeNS (undef, 'value'));
650     } elsif ({qw/plus 1 subtract 1 mult 1 divide 1/}->{$ln}) {
651     $result .= perl_statement
652     (perl_assign
653     to_perl_value ($node->getAttributeNS (undef, 'var'))
654     => to_perl_value ($node->getAttributeNS (undef, 'op1')).
655     {qw<plus + subtract - mult * divide />}->{$ln}.
656     to_perl_value ($node->getAttributeNS (undef, 'op2')));
657     } elsif ($ln eq 'append') {
658     $result .= perl_statement
659     'push @{'.
660     to_perl_value ($node->getAttributeNS (undef, 'collection')).
661     '}, '.
662     to_perl_value ($node->getAttributeNS (undef, 'item'));
663     } elsif ($ln eq 'instanceOf') {
664     $result .= 'isInstanceOf ('.
665     perl_list ($node->getAttributeNS (undef, 'type'),
666     perl_code_literal to_perl_value
667     ($node->getAttributeNS (undef, 'obj'))).
668     ')';
669     } elsif ($ln eq 'assign') {
670     $result .= perl_statement
671     perl_assign
672     to_perl_value ($node->getAttributeNS (undef, 'var'))
673     => to_perl_value ($node->getAttributeNS (undef, 'value'));
674     } elsif ($ln eq 'fail') {
675     $result .= perl_statement 'fail ('.
676     perl_literal ($node->getAttributeNS (undef, 'id')). ')';
677 wakaba 1.1 } else {
678     valid_err q<Unknown element type: >.$ln;
679     }
680     $result;
681     }
682    
683 wakaba 1.3 our $result = '';
684    
685 wakaba 1.8 my $input_filename;
686 wakaba 1.1 my $input;
687     {
688     local $/ = undef;
689     $input = <>;
690 wakaba 1.8 $input_filename = $ARGV;
691 wakaba 1.1 }
692    
693 wakaba 1.3 {
694 wakaba 1.6 my $dom = $Message::DOM::DOMImplementationRegistry
695 wakaba 1.1 ->getDOMImplementation
696     ({Core => undef,
697     XML => undef,
698 wakaba 1.6 ExpandedURI q<ManakaiDOMLS2003:LS> => ''});
699 wakaba 1.1
700     my $parser = $dom->createLSParser (MODE_SYNCHRONOUS);
701     my $in = $dom->createLSInput;
702     $in->stringData ($input);
703    
704     my $src = $parser->parse ($in)->documentElement;
705    
706     {
707     my $children = $src->ownerDocument->childNodes;
708     for (my $i = 0; $i < $children->length; $i++) {
709     my $node = $children->item ($i);
710     if ($node->nodeType == $node->COMMENT_NODE) {
711     if ($node->data =~ /Copyright/) {
712     $result .= perl_comment
713     qq<This script was generated by "$0"\n>.
714 wakaba 1.8 qq<and is a derived work from the source document\n>.
715     qq<"$input_filename".\n>.
716 wakaba 1.1 qq<The source document contained the following notice:\n>.
717     $node->data;
718     } else {
719     $result .= perl_comment $node->data;
720     }
721     }
722     }
723     }
724    
725     my $child = $src->childNodes;
726    
727     for (my $i = 0; $i < $child->length; $i++) {
728     my $node = $child->item ($i);
729     if ($node->nodeType == $node->ELEMENT_NODE) {
730     my $ln = $node->localName;
731     if ($ln eq 'metadata') {
732     my $md = $node->childNodes;
733     for (my $j = 0; $j < $md->length; $j++) {
734     my $node = $md->item ($j);
735     if ($node->nodeType == $node->ELEMENT_NODE) {
736     my $ln = $node->localName;
737 wakaba 1.3 if ($ln eq 'title') {
738     $result .= perl_statement
739     perl_assign
740     '$Info->{Name}'
741     => perl_literal $node->textContent;
742     } elsif ($ln eq 'description') {
743     $result .= perl_statement
744     perl_assign
745     '$Info->{Description}'
746     => perl_literal $node->textContent;
747 wakaba 1.1 } else {
748     # valid_err q<Unknown element type: >.$ln,
749     # node => $node;
750     }
751     } elsif ($node->nodeType == $node->TEXT_NODE) {
752     if ($node->data =~ /\S/) {
753     valid_err q<Unknown character data: >.$node->data,
754     node => $node;
755     }
756     } elsif ($node->nodeType == $node->COMMENT_NODE) {
757     $result .= perl_comment $node->data;
758     } else {
759     valid_err q<Unknown node type: >.$node->nodeType,
760     node => $node;
761     }
762     }
763 wakaba 1.3 } elsif ($ln eq 'implementationAttribute') {
764 wakaba 1.7 $result .= perl_statement 'impl_attr ('.
765     perl_list
766     ($node->getAttributeNS (undef, 'name'),
767     $node->getAttributeNS (undef, 'value')).')';
768 wakaba 1.1 } else {
769     $result .= node2code ($node);
770     }
771     } elsif ($node->nodeType == $node->COMMENT_NODE) {
772     $result .= perl_comment $node->data;
773     } elsif ($node->nodeType == $node->TEXT_NODE) {
774     if ($node->data =~ /\S/) {
775     valid_err q<Unknown character data: >.$node->data,
776     node => $node;
777     }
778     } else {
779     valid_err q<Unknown type of node: >.$node->nodeType,
780     node => $node;
781     }
782     }
783 wakaba 1.3 }
784    
785     my $pre = "#!/usr/bin/perl -w\nuse strict;\n";
786     $pre .= perl_statement ('require '.perl_literal 'manakai/domtest.pl');
787     $pre .= perl_statement
788     ('use Message::Util::Error')
789     if $Status->{use}->{'Message::Util::Error'};
790     for (keys %{$Status->{our}}) {
791     $pre .= perl_statement perl_var type => '$', local_name => $_,
792     scope => 'our';
793     }
794 wakaba 1.8 my $plan = $Status->{Number_local} ? 'plan_local' : 'plan';
795     $pre .= perl_statement qq<$plan (>.(0+$Status->{Number}).q<)>;
796    
797     $result .= perl_statement q<end_of_test ()>;
798 wakaba 1.1
799 wakaba 1.3 output_result $pre.$result;
800 wakaba 1.7
801     1;
802    
803     __END__
804    
805     =head1 NAME
806    
807     domtest2perl - DOM Test Suite XML Test File to Perl Test Code Converter
808    
809     =head1 SYNOPSIS
810    
811     perl path/to/domtest2perl.pl input.xml > output.pl
812     perl path/to/domtest2perl.pl input.xml --output-file=output.pl
813    
814     =over 4
815    
816     =item I<input.xml>
817    
818     The name of file to input. It should be an XML document
819     in the DOM Test Suite.
820    
821     =item I<output.pl>
822    
823     The name of file to output. It is overwritten if already exists.
824    
825     =back
826    
827     =head1 SEE ALSO
828    
829     I<Document Object Model (DOM) Conformance Test Suites>,
830     <http://www.w3.org/DOM/Test/>.
831    
832     F<domts2perl.pl>
833    
834     F<mkdommemlist.pl>
835    
836     =head1 LICENSE
837    
838     Copyright 2004-2005 Wakaba <w@suika.fam.cx>. All rights reserved.
839    
840     This program is free software; you can redistribute it and/or
841     modify it under the same terms as Perl itself.
842    
843     =cut
844 wakaba 1.1

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24