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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (show annotations) (download)
Sun Oct 10 06:09:47 2004 UTC (20 years, 1 month ago) by wakaba
Branch: MAIN
Changes since 1.2: +234 -39 lines
File MIME type: text/plain
domtest2perl.pl: New

1 #!/usr/bin/perl -w
2 use lib q<../lib>;
3 use strict;
4 BEGIN { require 'manakai/genlib.pl' }
5
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 use Getopt::Long;
13
14 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 our $Method;
28 our $Attr;
29 our $MethodParam;
30 my $Assert = {
31 qw/assertDOMException 1
32 assertFalse 1
33 assertNotNull 1
34 assertNull 1
35 assertTrue 1/
36 };
37 my $Misc = {
38 qw/if 1
39 implementationAttribute 1
40 var 1/
41 };
42 my $Condition = {
43 qw/condition 1
44 contains 1
45 contentType 1
46 hasSize 1
47 implementationAttribute 1
48 not 1
49 notNull 1
50 or 1/
51 };
52
53 my $Status = {Number => 0, our => {Info => 1}};
54
55 ## Defined in genlib.pl but redefined.
56 sub output_result ($) {
57 print $output_file shift;
58 }
59
60 sub to_perl_value ($;%) {
61 my ($s, %opt) = @_;
62 if (defined $s) {
63 if ($s =~ /^(?!\d)\w+$/) {
64 return perl_var (type => '$', local_name => $s);
65 } else {
66 return $s;
67 }
68 } elsif (defined $opt{default}) {
69 return $opt{default};
70 } else {
71 return '';
72 }
73 }
74
75 sub body2code ($) {
76 my $parent = shift;
77 my $result = '';
78 my $children = $parent->childNodes;
79 for (my $i = 0; $i < $children->length; $i++) {
80 my $child = $children->item ($i);
81 if ($child->nodeType == $child->ELEMENT_NODE) {
82 my $ln = $child->localName;
83 if ($Method->{$ln} or $Attr->{$ln} or
84 $Assert->{$ln} or $Misc->{$ln}) {
85 $result .= node2code ($child);
86 } else {
87 valid_err q<Unknown element type: >.$child->localName,
88 node => $child;
89 }
90 } elsif ($child->nodeType == $child->COMMENT_NODE) {
91 $result .= perl_comment $child->data;
92 } elsif ($child->nodeType == $child->TEXT_NODE) {
93 if ($child->data =~ /\S/) {
94 valid_err q<Unknown character data: >.$child->data,
95 node => $child;
96 }
97 } else {
98 valid_err q<Unknown type of node: >.$child->nodeType,
99 node => $child;
100 }
101 }
102 $result;
103 }
104
105 sub condition2code ($;%) {
106 my ($parent, %opt) = @_;
107 my $result = '';
108 my @result;
109 my $children = $parent->childNodes;
110 for (my $i = 0; $i < $children->length; $i++) {
111 my $child = $children->item ($i);
112 if ($child->nodeType == $child->ELEMENT_NODE) {
113 my $ln = $child->localName;
114 if ($Condition->{$ln}) {
115 push @result, node2code ($child);
116 } else {
117 valid_err q<Unknown element type: >.$child->localName,
118 node => $child;
119 }
120 } elsif ($child->nodeType == $child->COMMENT_NODE) {
121 $result .= perl_comment $child->data;
122 } elsif ($child->nodeType == $child->TEXT_NODE) {
123 if ($child->data =~ /\S/) {
124 valid_err q<Unknown character data: >.$child->data,
125 node => $child;
126 }
127 } else {
128 valid_err q<Unknown type of node: >.$child->nodeType,
129 node => $child;
130 }
131 }
132 $result .= join (($opt{join}||='or' eq 'or' ? ' || ' :
133 $opt{join} eq 'and' ? ' && ' :
134 valid_err q<Multiple condition not supported>,
135 node => $parent),
136 map {"($_)"} @result);
137 $result;
138 } #condition2code
139
140 sub node2code ($);
141 sub node2code ($) {
142 my $node = shift;
143 my $result = '';
144 if ($node->nodeType != $node->ELEMENT_NODE) {
145 if ($node->nodeType == $node->COMMENT_NODE) {
146 $result .= perl_comment $node->data;
147 } elsif ($node->nodeType == $node->TEXT_NODE) {
148 if ($node->data =~ /\S/) {
149 valid_err q<Unknown character data: >.$node->data,
150 node => $node;
151 }
152 } else {
153 valid_err q<Unknown type of node: >.$node->nodeType,
154 node => $node;
155 }
156 return $result;
157 }
158 my $ln = $node->localName;
159
160 if ($ln eq 'var') {
161 my $name = $node->getAttributeNS (undef, 'name');
162 $result .= perl_statement
163 perl_var
164 local_name => $name,
165 scope => 'my',
166 type => '$';
167 if ($node->getAttributeNS (undef, 'value')) {
168 valid_err q<Attribute "value" not supported>, node => $node;
169 }
170 $Status->{var}->{$name}->{type} = $node->getAttributeNS (undef, 'type');
171 } elsif ($ln eq 'load') {
172 $result .= perl_statement
173 perl_assign
174 perl_var
175 (type => '$',
176 local_name => $node->getAttributeNS (undef, 'var'))
177 => 'load (' .
178 perl_literal ($node->getAttributeNS (undef, 'href')).
179 ')';
180 } elsif ($Method->{$ln}) {
181 $result .= perl_var (type => '$',
182 local_name => $node->getAttributeNS (undef, 'var')).
183 ' = '
184 if $node->hasAttributeNS (undef, 'var');
185 $result .= perl_var (type => '$',
186 local_name => $node->getAttributeNS (undef, 'obj')).
187 '->'.$ln.' ('.
188 join (', ',
189 map {
190 to_perl_value ($node->getAttributeNS (undef, $_),
191 default => 'undef')
192 } @{$Method->{$ln}}).
193 ");\n";
194 } elsif ($Attr->{$ln}) {
195 if ($node->hasAttributeNS (undef, 'var')) {
196 $result .= perl_var (type => '$',
197 local_name => $node->getAttributeNS (undef, 'var')).
198 ' = ';
199 } elsif ($node->hasAttributeNS (undef, 'value')) {
200 #
201 } else {
202 valid_err q<Unknown operation to an attribute>, node => $node;
203 }
204 $result .= perl_var (type => '$',
205 local_name => $node->getAttributeNS (undef, 'obj')).
206 '->'.$ln;
207 if ($node->hasAttributeNS (undef, 'var')) {
208 $result .= ";\n";
209 } elsif ($node->hasAttributeNS (undef, 'value')) {
210 $result .= " (".to_perl_value ($node->getAttributeNS (undef, 'value')).
211 ");\n";
212 }
213 } elsif ($ln eq 'assertEquals') {
214 my $expected = $node->getAttributeNS (undef, 'expected');
215 my $expectedType = $Status->{var}->{$expected}->{type} || '';
216 $result .= 'assertEquals'.
217 ({Collection => 'Collection',
218 List => 'List'}->{$expectedType}||'');
219 my $ignoreCase = $node->getAttributeNS (undef, 'ignoreCase') || 'false';
220 if ($ignoreCase eq 'auto') {
221 $result .= 'AutoCase ('.
222 perl_literal ($node->getAttributeNS (undef, 'context') ||
223 'element').
224 ', ';
225 } else {
226 $result .= ' (';
227 }
228 $result .= perl_literal ($node->getAttributeNS (undef, 'id')).', ';
229 $result .= join ", ", map {
230 $ignoreCase eq 'true'
231 ? ($expectedType eq 'Collection' or
232 $expectedType eq 'List')
233 ? "toLowerArray ($_)" : "lc ($_)"
234 : $_
235 } map {
236 to_perl_value ($_)
237 } (
238 $expected,
239 $node->getAttributeNS (undef, 'actual'),
240 );
241 $result .= ");\n";
242 $Status->{Number}++;
243 } elsif ($ln eq 'assertTrue' or $ln eq 'assertFalse') {
244 my $condition;
245 if ($node->hasAttributeNS (undef, 'actual')) {
246 $condition = perl_var (type => '$',
247 local_name => $node->getAttributeNS
248 (undef, 'actual'));
249 if ($node->hasChildNodes) {
250 valid_err q<Child of $ln found but not supported>,
251 node => $node;
252 }
253 } elsif ($node->hasChildNodes) {
254 $condition = condition2code ($node);
255 } else {
256 valid_err $ln.q< w/o @actual not supported>, node => $node;
257 }
258 $result .= perl_statement $ln . ' ('.
259 perl_literal ($node->getAttributeNS (undef, 'id')).', '.
260 $condition. ')';
261 $Status->{Number}++;
262 } elsif ($ln eq 'assertNotNull' or $ln eq 'assertNull') {
263 $result .= perl_statement $ln . ' (' .
264 perl_literal ($node->getAttributeNS (undef, 'id')).', '.
265 perl_var (type => '$',
266 local_name => $node->getAttributeNS (undef, 'actual')).
267 ')';
268 if ($node->hasChildNodes) {
269 valid_err q<Child of $ln found but not supported>,
270 node => $node;
271 }
272 $Status->{Number}++;
273 } elsif ($ln eq 'assertDOMException') {
274 $Status->{use}->{'Message::Util::Error'} = 1;
275 $result .= q[
276 {
277 my $success = 0;
278 try {
279 ];
280 my $children = $node->childNodes;
281 my $errname;
282 for (my $i = 0; $i < $children->length; $i++) {
283 my $child = $children->item ($i);
284 $errname = $child->localName if $child->nodeType == $child->ELEMENT_NODE;
285 $result .= body2code ($child);
286 }
287 $result .= q[
288 } catch Message::DOM::DOMException with {
289 my $err = shift;
290 $success = 1 if $err->{-type} eq ].perl_literal ($errname).q[;
291 }
292 assertTrue (].perl_literal ($node->getAttributeNS (undef, 'id')).
293 q[, $success);
294 }
295 ];
296 $Status->{Number}++;
297 } elsif ($ln eq 'contentType') {
298 $result .= '$builder->{contentType} eq '.
299 perl_literal ($node->getAttributeNS (undef, 'type'));
300 $Status->{our}->{builder} = 1;
301 } elsif ($ln eq 'if') {
302 my $children = $node->childNodes;
303 my $condition;
304 my $true = '';
305 for (my $i = 0; $i < $children->length; $i++) {
306 my $child = $children->item ($i);
307 if ($child->nodeType == $child->ELEMENT_NODE) {
308 if (not $condition) {
309 $condition = node2code ($child);
310 } elsif ($child->localName eq 'else') {
311 valid_err q<Multiple 'else's found>, node => $child
312 if $true;
313 $true = $result;
314 $result = '';
315 } else {
316 $result .= node2code ($child);
317 }
318 } else {
319 $result .= node2code ($child);
320 }
321 }
322 $result = perl_if
323 $condition,
324 $true || $result,
325 $true ? $result : undef;
326 } elsif ($ln eq 'or') {
327 $result .= condition2code ($node, join => 'or');
328 } elsif ($ln eq 'not') {
329 $result .= 'not '.condition2code ($node, join => 'nosupport');
330 } elsif ($ln eq 'notNull') {
331 $result .= 'defined '.
332 perl_var (type => '$',
333 local_name => $node->getAttributeNS (undef, 'obj'));
334 } else {
335 valid_err q<Unknown element type: >.$ln;
336 }
337 $result;
338 }
339
340 our $result = '';
341
342 my $input;
343 {
344 local $/ = undef;
345 $input = <>;
346 }
347
348 {
349 my $dom = Message::DOM::DOMImplementationRegistry
350 ->getDOMImplementation
351 ({Core => undef,
352 XML => undef,
353 ExpandedURI q<ManakaiDOMLS2003:LS> => '1.0'});
354
355 my $parser = $dom->createLSParser (MODE_SYNCHRONOUS);
356 my $in = $dom->createLSInput;
357 $in->stringData ($input);
358
359 my $src = $parser->parse ($in)->documentElement;
360
361 {
362 my $children = $src->ownerDocument->childNodes;
363 for (my $i = 0; $i < $children->length; $i++) {
364 my $node = $children->item ($i);
365 if ($node->nodeType == $node->COMMENT_NODE) {
366 if ($node->data =~ /Copyright/) {
367 $result .= perl_comment
368 qq<This script was generated by "$0"\n>.
369 qq<and is a derived work from the source document.\n>.
370 qq<The source document contained the following notice:\n>.
371 $node->data;
372 } else {
373 $result .= perl_comment $node->data;
374 }
375 }
376 }
377 }
378
379 my $child = $src->childNodes;
380
381 for (my $i = 0; $i < $child->length; $i++) {
382 my $node = $child->item ($i);
383 if ($node->nodeType == $node->ELEMENT_NODE) {
384 my $ln = $node->localName;
385 if ($ln eq 'metadata') {
386 my $md = $node->childNodes;
387 for (my $j = 0; $j < $md->length; $j++) {
388 my $node = $md->item ($j);
389 if ($node->nodeType == $node->ELEMENT_NODE) {
390 my $ln = $node->localName;
391 if ($ln eq 'title') {
392 $result .= perl_statement
393 perl_assign
394 '$Info->{Name}'
395 => perl_literal $node->textContent;
396 } elsif ($ln eq 'description') {
397 $result .= perl_statement
398 perl_assign
399 '$Info->{Description}'
400 => perl_literal $node->textContent;
401 } else {
402 # valid_err q<Unknown element type: >.$ln,
403 # node => $node;
404 }
405 } elsif ($node->nodeType == $node->TEXT_NODE) {
406 if ($node->data =~ /\S/) {
407 valid_err q<Unknown character data: >.$node->data,
408 node => $node;
409 }
410 } elsif ($node->nodeType == $node->COMMENT_NODE) {
411 $result .= perl_comment $node->data;
412 } else {
413 valid_err q<Unknown node type: >.$node->nodeType,
414 node => $node;
415 }
416 }
417 } elsif ($ln eq 'implementationAttribute') {
418 $result .= perl_comment
419 sprintf 'Implementation attribute: @name=%s, @value=%s',
420 $node->getAttributeNS (undef, 'name'),
421 $node->getAttributeNS (undef, 'value');
422 } else {
423 $result .= node2code ($node);
424 }
425 } elsif ($node->nodeType == $node->COMMENT_NODE) {
426 $result .= perl_comment $node->data;
427 } elsif ($node->nodeType == $node->TEXT_NODE) {
428 if ($node->data =~ /\S/) {
429 valid_err q<Unknown character data: >.$node->data,
430 node => $node;
431 }
432 } else {
433 valid_err q<Unknown type of node: >.$node->nodeType,
434 node => $node;
435 }
436 }
437 }
438
439 my $pre = "#!/usr/bin/perl -w\nuse strict;\n";
440 $pre .= perl_statement ('require '.perl_literal 'manakai/domtest.pl');
441 $pre .= perl_statement
442 ('use Message::Util::Error')
443 if $Status->{use}->{'Message::Util::Error'};
444 for (keys %{$Status->{our}}) {
445 $pre .= perl_statement perl_var type => '$', local_name => $_,
446 scope => 'our';
447 }
448 $pre .= perl_statement q<plan (>.(0+$Status->{Number}).q<)>;
449
450 output_result $pre.$result;
451

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24