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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.5 - (hide annotations) (download)
Sun Oct 31 12:29:00 2004 UTC (20 years ago) by wakaba
Branch: MAIN
CVS Tags: before-dis2-200411
Changes since 1.4: +331 -28 lines
File MIME type: text/plain
More DOMTS elements support

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24