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