line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# Copyrights 2006-2019 by [Mark Overmeer ]. |
2
|
|
|
|
|
|
|
# For other contributors see ChangeLog. |
3
|
|
|
|
|
|
|
# See the manual pages for details on the licensing terms. |
4
|
|
|
|
|
|
|
# Pod stripped from pm file by OODoc 2.02. |
5
|
|
|
|
|
|
|
# This code is part of distribution XML-Compile. Meta-POD processed with |
6
|
|
|
|
|
|
|
# OODoc into POD and HTML manual-pages. See README.md |
7
|
|
|
|
|
|
|
# Copyright Mark Overmeer. Licensed under the same terms as Perl itself. |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
package XML::Compile::Translate::Reader; |
10
|
45
|
|
|
45
|
|
3453
|
use vars '$VERSION'; |
|
45
|
|
|
|
|
73
|
|
|
45
|
|
|
|
|
2549
|
|
11
|
|
|
|
|
|
|
$VERSION = '1.63'; |
12
|
|
|
|
|
|
|
|
13
|
45
|
|
|
45
|
|
220
|
use base 'XML::Compile::Translate'; |
|
45
|
|
|
|
|
71
|
|
|
45
|
|
|
|
|
4425
|
|
14
|
|
|
|
|
|
|
|
15
|
45
|
|
|
45
|
|
247
|
use strict; |
|
45
|
|
|
|
|
76
|
|
|
45
|
|
|
|
|
975
|
|
16
|
45
|
|
|
45
|
|
204
|
use warnings; |
|
45
|
|
|
|
|
401
|
|
|
45
|
|
|
|
|
1410
|
|
17
|
45
|
|
|
45
|
|
214
|
no warnings 'once', 'recursion'; |
|
45
|
|
|
|
|
75
|
|
|
45
|
|
|
|
|
1967
|
|
18
|
|
|
|
|
|
|
|
19
|
45
|
|
|
45
|
|
256
|
use Log::Report 'xml-compile'; |
|
45
|
|
|
|
|
78
|
|
|
45
|
|
|
|
|
346
|
|
20
|
|
|
|
|
|
|
|
21
|
45
|
|
|
45
|
|
13502
|
use List::Util qw/first/; |
|
45
|
|
|
|
|
80
|
|
|
45
|
|
|
|
|
2973
|
|
22
|
45
|
|
|
45
|
|
247
|
use Scalar::Util qw/weaken blessed/; |
|
45
|
|
|
|
|
70
|
|
|
45
|
|
|
|
|
2447
|
|
23
|
|
|
|
|
|
|
|
24
|
45
|
|
|
45
|
|
288
|
use XML::Compile::Util qw/pack_type odd_elements type_of_node SCHEMA2001i/; |
|
45
|
|
|
|
|
92
|
|
|
45
|
|
|
|
|
2413
|
|
25
|
45
|
|
|
45
|
|
250
|
use XML::Compile::Iterator (); |
|
45
|
|
|
|
|
83
|
|
|
45
|
|
|
|
|
6320
|
|
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
# Each action implementation returns a code reference, which will be |
29
|
|
|
|
|
|
|
# used to do the run-time work. The mechanism of `closures' is used to |
30
|
|
|
|
|
|
|
# keep the important information. Be sure that you understand closures |
31
|
|
|
|
|
|
|
# before you attempt to change anything. |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
# The returned reader subroutines will always be called |
34
|
|
|
|
|
|
|
# my @pairs = $reader->($tree); |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
# Some error messages are labeled with 'misfit' which is used to indicate |
37
|
|
|
|
|
|
|
# that the structure of found data is not conforming the needs. For optional |
38
|
|
|
|
|
|
|
# blocks, these errors are caught and un-done. |
39
|
|
|
|
|
|
|
|
40
|
1331
|
|
|
1331
|
0
|
5288
|
sub actsAs($) {$_[1] eq 'READER'} |
41
|
935
|
|
|
935
|
0
|
1737
|
sub makeTagUnqualified(@) {$_[3]} # ($self, $path, $node, $local, $ns) |
42
|
118
|
|
|
118
|
0
|
236
|
sub makeTagQualified(@) {$_[3]} # same params |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
sub typemapToHooks($$) |
45
|
396
|
|
|
396
|
0
|
808
|
{ my ($self, $hooks, $typemap) = @_; |
46
|
396
|
|
|
|
|
1484
|
while(my($type, $action) = each %$typemap) |
47
|
3
|
50
|
|
|
|
7
|
{ defined $action or next; |
48
|
3
|
|
|
|
|
4
|
my $hook; |
49
|
3
|
100
|
|
|
|
9
|
if(!ref $action) |
|
|
100
|
|
|
|
|
|
50
|
1
|
|
|
|
|
2
|
{ my $class = $action; |
51
|
45
|
|
|
45
|
|
281
|
no strict 'refs'; |
|
45
|
|
|
|
|
79
|
|
|
45
|
|
|
|
|
353886
|
|
52
|
1
|
50
|
|
|
|
1
|
keys %{$class.'::'} |
|
1
|
|
|
|
|
6
|
|
53
|
|
|
|
|
|
|
or error __x"class {pkg} for typemap {type} is not loaded" |
54
|
|
|
|
|
|
|
, pkg => $class, type => $type; |
55
|
|
|
|
|
|
|
|
56
|
1
|
50
|
|
|
|
10
|
$class->can('fromXML') |
57
|
|
|
|
|
|
|
or error __x"class {pkg} does not implement fromXML(), required for typemap {type}" |
58
|
|
|
|
|
|
|
, pkg => $class, type => $type; |
59
|
|
|
|
|
|
|
|
60
|
1
|
|
|
|
|
6
|
trace "created reader hook for type $type to class $class"; |
61
|
1
|
|
|
1
|
|
21
|
$hook = sub { $class->fromXML($_[1], $type) }; |
|
1
|
|
|
|
|
4
|
|
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
elsif(ref $action eq 'CODE') |
64
|
1
|
|
|
1
|
|
5
|
{ $hook = sub { $action->(READER => $_[1], $type) }; |
|
1
|
|
|
|
|
3
|
|
65
|
1
|
|
|
|
|
5
|
trace "created reader hook for type $type to CODE"; |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
else |
68
|
1
|
|
|
|
|
2
|
{ my $object = $action; |
69
|
1
|
50
|
|
|
|
5
|
$object->can('fromXML') |
70
|
|
|
|
|
|
|
or error __x"object of class {pkg} does not implement fromXML(), required for typemap {type}" |
71
|
|
|
|
|
|
|
, pkg => ref($object), type => $type; |
72
|
|
|
|
|
|
|
|
73
|
1
|
|
|
|
|
4
|
trace "created reader hook for type $type to object"; |
74
|
1
|
|
|
1
|
|
39
|
$hook = sub {$object->fromXML($_[1], $type)}; |
|
1
|
|
|
|
|
4
|
|
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
3
|
|
|
|
|
34
|
push @$hooks, +{action => 'READER', type => $type, after => $hook}; |
78
|
|
|
|
|
|
|
} |
79
|
396
|
|
|
|
|
648
|
$hooks; |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
sub makeElementWrapper |
83
|
390
|
|
|
390
|
0
|
749
|
{ my ($self, $path, $processor) = @_; |
84
|
|
|
|
|
|
|
# no copy of $_[0], because it may be a large string |
85
|
397
|
|
|
397
|
|
186173
|
sub { my $tree; |
86
|
397
|
50
|
33
|
|
|
1715
|
if(blessed $_[0] && $_[0]->isa('XML::LibXML::Iterator')) |
87
|
0
|
|
|
|
|
0
|
{ $tree = $_[0]; |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
else |
90
|
397
|
100
|
|
|
|
1889
|
{ my $xml = XML::Compile->dataToXML($_[0]) |
91
|
|
|
|
|
|
|
or return (); |
92
|
396
|
50
|
|
|
|
3150
|
$xml = $xml->documentElement |
93
|
|
|
|
|
|
|
if $xml->isa('XML::LibXML::Document'); |
94
|
|
|
|
|
|
|
$tree = XML::Compile::Iterator->new($xml, 'top', |
95
|
396
|
|
|
|
|
2273
|
sub { $_[0]->isa('XML::LibXML::Element') } ); |
|
856
|
|
|
|
|
2336
|
|
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
396
|
|
|
|
|
1138
|
my $data = ($processor->($tree))[-1]; |
99
|
328
|
50
|
|
|
|
716
|
unless(defined $data) |
100
|
0
|
|
|
|
|
0
|
{ my $node = $tree->node; |
101
|
0
|
|
|
|
|
0
|
error __x"data not recognized, found a `{type}' at {where}" |
102
|
|
|
|
|
|
|
, type => type_of_node $node, where => $node->nodePath; |
103
|
|
|
|
|
|
|
} |
104
|
328
|
|
|
|
|
1332
|
$data; |
105
|
390
|
|
|
|
|
1659
|
}; |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
sub makeAttributeWrapper |
109
|
4
|
|
|
4
|
0
|
8
|
{ my ($self, $path, $processor) = @_; |
110
|
|
|
|
|
|
|
|
111
|
4
|
|
|
4
|
|
1840
|
sub { my $attr = shift; |
112
|
4
|
50
|
0
|
|
|
37
|
ref $attr && $attr->isa('XML::LibXML::Attr') |
|
|
|
33
|
|
|
|
|
113
|
|
|
|
|
|
|
or error __x"expects an attribute node, but got `{something}' at {path}" |
114
|
|
|
|
|
|
|
, something => (ref $attr || $attr), path => $path; |
115
|
|
|
|
|
|
|
|
116
|
4
|
|
|
|
|
25
|
my $node = XML::LibXML::Element->new('dummy'); |
117
|
4
|
|
|
|
|
45
|
$node->addChild($attr); |
118
|
|
|
|
|
|
|
|
119
|
4
|
|
|
|
|
18
|
$processor->($node); |
120
|
4
|
|
|
|
|
14
|
}; |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
sub makeWrapperNs # no namespaces in the HASH |
124
|
62
|
|
|
62
|
0
|
162
|
{ my ($self, $path, $processor, $index, $filter) = @_; |
125
|
62
|
|
|
|
|
785
|
$processor; |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
# |
129
|
|
|
|
|
|
|
## Element |
130
|
|
|
|
|
|
|
# |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
sub makeSequence($@) |
133
|
220
|
|
|
220
|
0
|
624
|
{ my ($self, $path, @pairs) = @_; |
134
|
220
|
100
|
|
|
|
515
|
if(@pairs==2) |
135
|
86
|
|
|
|
|
163
|
{ my ($take, $action) = @pairs; |
136
|
|
|
|
|
|
|
my $code |
137
|
|
|
|
|
|
|
= (ref $action eq 'BLOCK' || ref $action eq 'ANY') |
138
|
6
|
|
|
6
|
|
17
|
? sub { $action->($_[0]) } |
139
|
86
|
100
|
66
|
68
|
|
547
|
: sub { $action->($_[0] && $_[0]->currentType eq $take ? $_[0]:undef)}; |
|
68
|
100
|
66
|
|
|
278
|
|
140
|
86
|
|
|
|
|
434
|
return bless $code, 'BLOCK'; |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
bless |
144
|
147
|
|
|
147
|
|
190
|
sub { my $tree = shift; |
145
|
147
|
|
|
|
|
182
|
my @res; |
146
|
147
|
|
|
|
|
375
|
my @do = @pairs; |
147
|
|
|
|
|
|
|
|
148
|
147
|
|
|
|
|
343
|
while(@do) |
149
|
332
|
|
|
|
|
603
|
{ my ($take, $do) = (shift @do, shift @do); |
150
|
332
|
100
|
100
|
|
|
1827
|
push @res, ref $do eq 'BLOCK' |
151
|
|
|
|
|
|
|
|| ref $do eq 'ANY' |
152
|
|
|
|
|
|
|
|| (defined $tree && $tree->currentType eq $take) |
153
|
|
|
|
|
|
|
? $do->($tree) : $do->(undef); |
154
|
|
|
|
|
|
|
} |
155
|
|
|
|
|
|
|
|
156
|
132
|
|
|
|
|
582
|
@res; |
157
|
134
|
|
|
|
|
1027
|
}, 'BLOCK'; |
158
|
|
|
|
|
|
|
} |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
sub makeChoice($@) |
161
|
40
|
|
|
40
|
0
|
150
|
{ my ($self, $path, %do) = @_; |
162
|
40
|
|
|
|
|
57
|
my @specials; |
163
|
40
|
|
|
|
|
135
|
foreach my $el (keys %do) |
164
|
|
|
|
|
|
|
{ push @specials, delete $do{$el} |
165
|
91
|
100
|
66
|
|
|
371
|
if ref $do{$el} eq 'BLOCK' || ref $do{$el} eq 'ANY'; |
166
|
|
|
|
|
|
|
} |
167
|
|
|
|
|
|
|
|
168
|
40
|
100
|
100
|
|
|
131
|
if(keys %do==1 && !@specials) |
169
|
4
|
|
|
|
|
9
|
{ my ($option, $action) = %do; |
170
|
|
|
|
|
|
|
return bless |
171
|
4
|
|
|
4
|
|
4
|
sub { my $tree = shift; |
172
|
4
|
50
|
|
|
|
13
|
my $type = defined $tree ? $tree->currentType : ''; |
173
|
4
|
100
|
|
|
|
11
|
return $action->($tree) |
174
|
|
|
|
|
|
|
if $type eq $option; |
175
|
|
|
|
|
|
|
|
176
|
1
|
|
|
|
|
9
|
try { $action->(undef) }; # minOccurs=0 |
|
1
|
|
|
|
|
210
|
|
177
|
1
|
50
|
|
|
|
196
|
$@ or return (); |
178
|
|
|
|
|
|
|
|
179
|
1
|
50
|
|
|
|
10
|
$type |
180
|
|
|
|
|
|
|
or error __x"element `{tag}' expected for choice at {path}" |
181
|
|
|
|
|
|
|
, tag => $option, path => $path, _class => 'misfit'; |
182
|
|
|
|
|
|
|
|
183
|
0
|
|
|
|
|
0
|
error __x"single choice option `{option}' at `{type}' at {path}" |
184
|
|
|
|
|
|
|
, option => $option, type => $type, path => $path |
185
|
|
|
|
|
|
|
, _class => 'misfit'; |
186
|
4
|
|
|
|
|
30
|
}, 'BLOCK'; |
187
|
|
|
|
|
|
|
} |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
@specials or return bless |
190
|
37
|
|
|
37
|
|
56
|
sub { my $tree = shift; |
191
|
37
|
50
|
|
|
|
118
|
my $type = defined $tree ? $tree->currentType : undef; |
192
|
37
|
50
|
|
|
|
103
|
my $elem = defined $type ? $do{$type} : undef; |
193
|
37
|
100
|
|
|
|
93
|
return $elem->($tree) if $elem; |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
# very silly situation: some people use a minOccurs within |
196
|
|
|
|
|
|
|
# a choice, instead on choice itself. That always succeeds. |
197
|
4
|
|
|
|
|
10
|
foreach my $some (values %do) |
198
|
9
|
|
|
|
|
74
|
{ try { $some->(undef) }; |
|
9
|
|
|
|
|
2028
|
|
199
|
9
|
100
|
|
|
|
1849
|
$@ or return (); |
200
|
|
|
|
|
|
|
} |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
$type |
203
|
3
|
100
|
|
|
|
27
|
or error __x"no element left to pick choice at {path}" |
204
|
|
|
|
|
|
|
, path => $path, _class => 'misfit'; |
205
|
|
|
|
|
|
|
|
206
|
2
|
|
|
|
|
3
|
trace "choose element from @{[sort keys %do]}"; |
|
2
|
|
|
|
|
18
|
|
207
|
2
|
|
|
|
|
86
|
error __x"no applicable choice for `{tag}' at {path}" |
208
|
|
|
|
|
|
|
, tag => $type, path => $path, _class => 'misfit'; |
209
|
36
|
100
|
|
|
|
232
|
}, 'BLOCK'; |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
return bless |
212
|
10
|
|
|
10
|
|
15
|
sub { my $tree = shift; |
213
|
10
|
50
|
|
|
|
34
|
my $type = defined $tree ? $tree->currentType : undef; |
214
|
10
|
50
|
|
|
|
35
|
my $elem = defined $type ? $do{$type} : undef; |
215
|
10
|
100
|
|
|
|
28
|
return $elem->($tree) if $elem; |
216
|
|
|
|
|
|
|
|
217
|
6
|
|
|
|
|
10
|
my @special_errors; |
218
|
6
|
|
|
|
|
12
|
foreach (@specials) |
219
|
7
|
|
|
|
|
66
|
{ my @d = try { $_->($tree) }; |
|
7
|
|
|
|
|
1659
|
|
220
|
7
|
100
|
66
|
|
|
426
|
return @d if !$@ && @d; |
221
|
1
|
50
|
|
|
|
6
|
push @special_errors, $@->wasFatal->message if $@; |
222
|
|
|
|
|
|
|
} |
223
|
|
|
|
|
|
|
|
224
|
0
|
|
|
|
|
0
|
foreach my $some (values %do, @specials) |
225
|
0
|
|
|
|
|
0
|
{ try { $some->(undef) }; |
|
0
|
|
|
|
|
0
|
|
226
|
0
|
0
|
|
|
|
0
|
$@ or return (); |
227
|
|
|
|
|
|
|
} |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
$type |
230
|
0
|
0
|
|
|
|
0
|
or error __x"choice needs more elements at {path}" |
231
|
|
|
|
|
|
|
, path => $path, _class => 'misfit'; |
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
|
234
|
0
|
|
|
|
|
0
|
my @elems = sort keys %do; |
235
|
0
|
0
|
|
|
|
0
|
trace "choose element from @elems or fix special at $path" if @elems; |
236
|
0
|
|
|
|
|
0
|
trace "failed specials in choice: $_" for @special_errors; |
237
|
|
|
|
|
|
|
|
238
|
0
|
|
|
|
|
0
|
error __x"no applicable choice for `{tag}' at {path}" |
239
|
|
|
|
|
|
|
, tag => $type, path => $path, _class => 'misfit'; |
240
|
10
|
|
|
|
|
76
|
}, 'BLOCK'; |
241
|
|
|
|
|
|
|
} |
242
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
sub makeAll($@) |
244
|
30
|
|
|
30
|
0
|
103
|
{ my ($self, $path, %pairs) = @_; |
245
|
30
|
|
|
|
|
39
|
my %specials; |
246
|
30
|
|
|
|
|
102
|
foreach my $el (keys %pairs) |
247
|
|
|
|
|
|
|
{ $specials{$el} = delete $pairs{$el} |
248
|
80
|
100
|
66
|
|
|
281
|
if ref $pairs{$el} eq 'BLOCK' || ref $pairs{$el} eq 'ANY'; |
249
|
|
|
|
|
|
|
} |
250
|
|
|
|
|
|
|
|
251
|
30
|
100
|
100
|
|
|
111
|
if(!%specials && keys %pairs==1) |
252
|
5
|
|
|
|
|
10
|
{ my ($take, $do) = %pairs; |
253
|
|
|
|
|
|
|
return bless |
254
|
4
|
|
|
4
|
|
5
|
sub { my $tree = shift; |
255
|
4
|
100
|
66
|
|
|
16
|
$do->($tree && $tree->currentType eq $take ? $tree : undef); |
256
|
5
|
|
|
|
|
33
|
}, 'BLOCK'; |
257
|
|
|
|
|
|
|
} |
258
|
|
|
|
|
|
|
|
259
|
|
|
|
|
|
|
keys %specials or return bless |
260
|
22
|
|
|
22
|
|
29
|
sub { my $tree = shift; |
261
|
22
|
|
|
|
|
92
|
my %do = %pairs; |
262
|
22
|
|
|
|
|
35
|
my @res; |
263
|
22
|
|
|
|
|
25
|
while(1) |
264
|
67
|
100
|
66
|
|
|
167
|
{ my $type = $tree && $tree->currentType or last; |
265
|
46
|
100
|
|
|
|
114
|
my $do = delete $do{$type} or last; # already seen? |
266
|
45
|
|
|
|
|
75
|
push @res, $do->($tree); |
267
|
|
|
|
|
|
|
} |
268
|
|
|
|
|
|
|
|
269
|
|
|
|
|
|
|
# saw all of all? |
270
|
|
|
|
|
|
|
push @res, $_->(undef) |
271
|
22
|
|
|
|
|
76
|
for values %do; |
272
|
|
|
|
|
|
|
|
273
|
9
|
|
|
|
|
40
|
@res; |
274
|
25
|
100
|
|
|
|
169
|
}, 'BLOCK'; |
275
|
|
|
|
|
|
|
|
276
|
|
|
|
|
|
|
# an 'all' block with nested structures or any is quite nasty. Don't |
277
|
|
|
|
|
|
|
# forget that 'all' can have maxOccurs > 1 ! |
278
|
|
|
|
|
|
|
bless |
279
|
3
|
|
|
3
|
|
6
|
sub { my $tree = shift; |
280
|
3
|
|
|
|
|
14
|
my %do = %pairs; |
281
|
3
|
|
|
|
|
6
|
my %spseen; |
282
|
|
|
|
|
|
|
my @res; |
283
|
|
|
|
|
|
|
PARTICLE: |
284
|
3
|
|
|
|
|
7
|
while(1) |
285
|
11
|
100
|
|
|
|
22
|
{ my $type = $tree->currentType or last; |
286
|
9
|
100
|
|
|
|
27
|
if(my $do = delete $do{$type}) |
287
|
6
|
|
|
|
|
13
|
{ push @res, $do->($tree); |
288
|
6
|
|
|
|
|
14
|
next PARTICLE; |
289
|
|
|
|
|
|
|
} |
290
|
|
|
|
|
|
|
|
291
|
3
|
|
|
|
|
13
|
foreach (keys %specials) |
292
|
3
|
50
|
|
|
|
9
|
{ next if $spseen{$_}; |
293
|
3
|
|
|
|
|
22
|
my @d = try { $specials{$_}->($tree) }; |
|
3
|
|
|
|
|
811
|
|
294
|
3
|
100
|
|
|
|
342
|
next if $@; |
295
|
|
|
|
|
|
|
|
296
|
2
|
|
|
|
|
17
|
$spseen{$_}++; |
297
|
2
|
|
|
|
|
5
|
push @res, @d; |
298
|
2
|
|
|
|
|
7
|
next PARTICLE; |
299
|
|
|
|
|
|
|
} |
300
|
|
|
|
|
|
|
|
301
|
1
|
|
|
|
|
9
|
last; |
302
|
|
|
|
|
|
|
} |
303
|
3
|
50
|
|
|
|
7
|
@res or return (); |
304
|
|
|
|
|
|
|
|
305
|
|
|
|
|
|
|
# saw all of all? |
306
|
|
|
|
|
|
|
push @res, $_->(undef) |
307
|
3
|
|
|
|
|
12
|
for values %do; |
308
|
|
|
|
|
|
|
|
309
|
|
|
|
|
|
|
push @res, $_->(undef) |
310
|
3
|
100
|
|
|
|
9
|
for map {$spseen{$_} ? () : $specials{$_}} keys %specials; |
|
3
|
|
|
|
|
15
|
|
311
|
|
|
|
|
|
|
|
312
|
2
|
|
|
|
|
12
|
@res; |
313
|
3
|
|
|
|
|
23
|
}, 'BLOCK'; |
314
|
|
|
|
|
|
|
} |
315
|
|
|
|
|
|
|
|
316
|
|
|
|
|
|
|
sub makeBlockHandler |
317
|
299
|
|
|
299
|
0
|
778
|
{ my ($self, $path, $label, $min, $max, $process, $kind, $multi) = @_; |
318
|
|
|
|
|
|
|
|
319
|
|
|
|
|
|
|
# flatten the HASH: when a block appears only once, there will |
320
|
|
|
|
|
|
|
# not be an additional nesting in the output tree. |
321
|
299
|
100
|
100
|
|
|
1121
|
if($max ne 'unbounded' && $max==1) |
322
|
|
|
|
|
|
|
{ |
323
|
269
|
100
|
|
|
|
1171
|
return ($label => $process) if $min==1; |
324
|
|
|
|
|
|
|
|
325
|
|
|
|
|
|
|
my $code = |
326
|
5
|
50
|
|
5
|
|
12
|
sub { my $tree = shift or return (); |
327
|
5
|
100
|
|
|
|
12
|
my $starter = $tree->currentChild or return (); |
328
|
4
|
|
|
|
|
39
|
my @pairs = try { $process->($tree) }; |
|
4
|
|
|
|
|
960
|
|
329
|
4
|
100
|
|
|
|
480
|
if($@->wasFatal(class => 'misfit')) |
|
|
50
|
|
|
|
|
|
330
|
2
|
|
|
|
|
114
|
{ my $ending = $tree->currentChild; |
331
|
2
|
100
|
66
|
|
|
11
|
$@->reportAll if !$ending || $ending!=$starter; |
332
|
1
|
|
|
|
|
34
|
return (); |
333
|
|
|
|
|
|
|
} |
334
|
0
|
|
|
|
|
0
|
elsif($@) {$@->reportAll} |
335
|
2
|
|
|
|
|
48
|
@pairs; |
336
|
5
|
|
|
|
|
21
|
}; |
337
|
5
|
|
|
|
|
30
|
return ($label => bless($code, 'BLOCK')); |
338
|
|
|
|
|
|
|
} |
339
|
|
|
|
|
|
|
|
340
|
30
|
50
|
66
|
|
|
122
|
if($max ne 'unbounded' && $min>=$max) |
341
|
|
|
|
|
|
|
{ my $code = |
342
|
0
|
|
|
0
|
|
0
|
sub { my $tree = shift; |
343
|
0
|
|
|
|
|
0
|
my @res; |
344
|
0
|
|
|
|
|
0
|
while(@res < $min) |
345
|
0
|
|
|
|
|
0
|
{ my @pairs = $process->($tree); |
346
|
0
|
|
|
|
|
0
|
push @res, {@pairs}; |
347
|
|
|
|
|
|
|
} |
348
|
0
|
|
|
|
|
0
|
($multi => \@res); |
349
|
0
|
|
|
|
|
0
|
}; |
350
|
0
|
|
|
|
|
0
|
return ($label => bless($code, 'BLOCK')); |
351
|
|
|
|
|
|
|
} |
352
|
|
|
|
|
|
|
|
353
|
30
|
100
|
|
|
|
77
|
if($min==0) |
354
|
|
|
|
|
|
|
{ my $code = |
355
|
18
|
50
|
|
18
|
|
47
|
sub { my $tree = shift or return (); |
356
|
18
|
|
|
|
|
29
|
my @res; |
357
|
18
|
|
100
|
|
|
72
|
while($max eq 'unbounded' || @res < $max) |
358
|
46
|
100
|
|
|
|
93
|
{ my $starter = $tree->currentChild or last; |
359
|
35
|
|
|
|
|
265
|
my @pairs = try { $process->($tree) }; |
|
35
|
|
|
|
|
8895
|
|
360
|
35
|
100
|
|
|
|
1286
|
if($@->wasFatal(class => 'misfit')) |
|
|
50
|
|
|
|
|
|
361
|
|
|
|
|
|
|
{ # misfit error is ok, if nothing consumed |
362
|
3
|
|
|
|
|
154
|
trace "misfit $label ($min..$max) ".$@->wasFatal->message; |
363
|
3
|
|
|
|
|
200
|
my $ending = $tree->currentChild; |
364
|
3
|
50
|
33
|
|
|
8
|
$@->reportAll if !$ending || $ending!=$starter; |
365
|
3
|
|
|
|
|
71
|
last; |
366
|
|
|
|
|
|
|
} |
367
|
0
|
|
|
|
|
0
|
elsif($@) {$@->reportAll} |
368
|
|
|
|
|
|
|
|
369
|
32
|
100
|
|
|
|
444
|
@pairs or last; |
370
|
29
|
|
|
|
|
130
|
push @res, {@pairs}; |
371
|
|
|
|
|
|
|
} |
372
|
|
|
|
|
|
|
|
373
|
18
|
100
|
|
|
|
81
|
@res ? ($multi => \@res) : (); |
374
|
18
|
|
|
|
|
82
|
}; |
375
|
18
|
|
|
|
|
91
|
return ($label => bless($code, 'BLOCK')); |
376
|
|
|
|
|
|
|
} |
377
|
|
|
|
|
|
|
|
378
|
|
|
|
|
|
|
my $code = |
379
|
10
|
50
|
|
10
|
|
30
|
sub { my $tree = shift or error __xn |
380
|
|
|
|
|
|
|
"block with `{name}' is required at least once at {path}" |
381
|
|
|
|
|
|
|
, "block with `{name}' is required at least {_count} times at {path}" |
382
|
|
|
|
|
|
|
, $min, name => $label, path => $path; |
383
|
|
|
|
|
|
|
|
384
|
10
|
|
|
|
|
15
|
my @res; |
385
|
10
|
|
|
|
|
54
|
while(@res < $min) |
386
|
10
|
|
|
|
|
28
|
{ my @pairs = $process->($tree); |
387
|
8
|
|
|
|
|
41
|
push @res, {@pairs}; |
388
|
|
|
|
|
|
|
} |
389
|
8
|
|
100
|
|
|
39
|
while($max eq 'unbounded' || @res < $max) |
390
|
18
|
100
|
|
|
|
35
|
{ my $starter = $tree->currentChild or last; |
391
|
12
|
|
|
|
|
86
|
my @pairs = try { $process->($tree) }; |
|
12
|
|
|
|
|
2710
|
|
392
|
12
|
50
|
|
|
|
226
|
if($@->wasFatal(class => 'misfit')) |
|
|
50
|
|
|
|
|
|
393
|
|
|
|
|
|
|
{ # misfit error is ok, if nothing consumed |
394
|
0
|
|
|
|
|
0
|
trace "misfit $label ($min..) ".$@->wasFatal->message; |
395
|
0
|
|
|
|
|
0
|
my $ending = $tree->currentChild; |
396
|
0
|
0
|
0
|
|
|
0
|
$@->reportAll if !$ending || $ending!=$starter; |
397
|
0
|
|
|
|
|
0
|
last; |
398
|
|
|
|
|
|
|
} |
399
|
0
|
|
|
|
|
0
|
elsif($@) {$@->reportAll}; |
400
|
|
|
|
|
|
|
|
401
|
12
|
50
|
|
|
|
165
|
@pairs or last; |
402
|
12
|
|
|
|
|
55
|
push @res, {@pairs}; |
403
|
|
|
|
|
|
|
} |
404
|
8
|
|
|
|
|
28
|
($multi => \@res); |
405
|
12
|
|
|
|
|
53
|
}; |
406
|
|
|
|
|
|
|
|
407
|
12
|
|
|
|
|
63
|
($label => bless($code, 'BLOCK')); |
408
|
|
|
|
|
|
|
} |
409
|
|
|
|
|
|
|
|
410
|
|
|
|
|
|
|
sub makeElementHandler |
411
|
522
|
|
|
522
|
0
|
1550
|
{ my ($self, $path, $label, $min, $max, $required, $optional) = @_; |
412
|
522
|
100
|
|
1
|
|
1031
|
$max eq "0" and return sub {}; # max can be "unbounded", hence strcmp |
413
|
|
|
|
|
|
|
|
414
|
521
|
100
|
100
|
|
|
1708
|
if($max ne 'unbounded' && $max==1) |
415
|
|
|
|
|
|
|
{ return $min==1 |
416
|
376
|
|
|
376
|
|
483
|
? sub { my $tree = shift; |
417
|
376
|
100
|
|
|
|
2419
|
my @pairs = $required->(defined $tree ? $tree->descend :undef); |
418
|
339
|
100
|
|
|
|
1338
|
$tree->nextChild if defined $tree; |
419
|
339
|
|
|
|
|
1065
|
($label => $pairs[1]); |
420
|
|
|
|
|
|
|
} |
421
|
83
|
100
|
|
83
|
|
257
|
: sub { my $tree = shift or return (); |
422
|
53
|
50
|
|
|
|
114
|
$tree->currentChild or return (); |
423
|
53
|
|
|
|
|
260
|
my @pairs = $optional->($tree->descend); |
424
|
52
|
|
|
|
|
180
|
$tree->nextChild; |
425
|
52
|
50
|
|
|
|
114
|
@pairs or return (); |
426
|
52
|
|
|
|
|
191
|
($label => $pairs[1]); |
427
|
495
|
100
|
|
|
|
3788
|
}; |
428
|
|
|
|
|
|
|
} |
429
|
|
|
|
|
|
|
|
430
|
26
|
100
|
100
|
|
|
84
|
if($max ne 'unbounded' && $min>=$max) |
431
|
|
|
|
|
|
|
{ return |
432
|
2
|
|
|
2
|
|
4
|
sub { my $tree = shift; |
433
|
2
|
|
|
|
|
3
|
my @res; |
434
|
2
|
|
|
|
|
5
|
while(@res < $min) |
435
|
4
|
50
|
|
|
|
12
|
{ my @pairs = $required->(defined $tree ? $tree->descend:undef); |
436
|
3
|
|
|
|
|
6
|
push @res, $pairs[1]; |
437
|
3
|
50
|
|
|
|
10
|
$tree->nextChild if defined $tree; |
438
|
|
|
|
|
|
|
} |
439
|
1
|
50
|
|
|
|
5
|
@res ? ($label => \@res) : (); |
440
|
2
|
|
|
|
|
15
|
}; |
441
|
|
|
|
|
|
|
} |
442
|
|
|
|
|
|
|
|
443
|
24
|
100
|
|
|
|
46
|
if(!defined $required) |
444
|
|
|
|
|
|
|
{ return |
445
|
22
|
100
|
|
22
|
|
71
|
sub { my $tree = shift or return (); |
446
|
16
|
|
|
|
|
21
|
my @res; |
447
|
16
|
|
66
|
|
|
60
|
while($max eq 'unbounded' || @res < $max) |
448
|
50
|
100
|
|
|
|
80
|
{ $tree->currentChild or last; |
449
|
40
|
|
|
|
|
181
|
my @pairs = $optional->($tree->descend); |
450
|
40
|
100
|
|
|
|
113
|
@pairs or last; |
451
|
34
|
|
|
|
|
161
|
push @res, $pairs[1]; |
452
|
34
|
|
|
|
|
63
|
$tree->nextChild; |
453
|
|
|
|
|
|
|
} |
454
|
16
|
50
|
|
|
|
73
|
@res ? ($label => \@res) : (); |
455
|
22
|
|
|
|
|
169
|
}; |
456
|
|
|
|
|
|
|
} |
457
|
|
|
|
|
|
|
|
458
|
2
|
|
|
2
|
|
3
|
sub { my $tree = shift; |
459
|
2
|
|
|
|
|
4
|
my @res; |
460
|
2
|
|
|
|
|
4
|
while(@res < $min) |
461
|
2
|
50
|
|
|
|
8
|
{ my @pairs = $required->(defined $tree ? $tree->descend : undef); |
462
|
2
|
|
|
|
|
6
|
push @res, $pairs[1]; |
463
|
2
|
50
|
|
|
|
8
|
$tree->nextChild if defined $tree; |
464
|
|
|
|
|
|
|
} |
465
|
2
|
|
66
|
|
|
13
|
while(defined $tree && ($max eq 'unbounded' || @res < $max)) |
|
|
|
66
|
|
|
|
|
466
|
2
|
50
|
|
|
|
4
|
{ $tree->currentChild or last; |
467
|
2
|
|
|
|
|
12
|
my @pairs = $optional->($tree->descend); |
468
|
2
|
100
|
|
|
|
7
|
@pairs or last; |
469
|
1
|
|
|
|
|
3
|
push @res, $pairs[1]; |
470
|
1
|
|
|
|
|
3
|
$tree->nextChild; |
471
|
|
|
|
|
|
|
} |
472
|
2
|
|
|
|
|
8
|
($label => \@res); |
473
|
2
|
|
|
|
|
15
|
}; |
474
|
|
|
|
|
|
|
} |
475
|
|
|
|
|
|
|
|
476
|
|
|
|
|
|
|
sub makeRequired |
477
|
429
|
|
|
429
|
0
|
801
|
{ my ($self, $path, $label, $do) = @_; |
478
|
|
|
|
|
|
|
|
479
|
|
|
|
|
|
|
my $req = |
480
|
382
|
|
|
382
|
|
471
|
sub { my $tree = shift; # can be undef |
481
|
382
|
|
|
|
|
615
|
my @pairs = $do->($tree); |
482
|
|
|
|
|
|
|
@pairs |
483
|
377
|
100
|
|
|
|
853
|
or error __x"data for element or block starting with `{tag}' missing at {path}" |
484
|
|
|
|
|
|
|
, tag => $label, path => $path, _class => 'misfit'; |
485
|
344
|
|
|
|
|
672
|
@pairs; |
486
|
429
|
|
|
|
|
1429
|
}; |
487
|
429
|
50
|
|
|
|
1214
|
ref $do eq 'BLOCK' ? bless($req, 'BLOCK') : $req; |
488
|
|
|
|
|
|
|
} |
489
|
|
|
|
|
|
|
|
490
|
|
|
|
|
|
|
sub makeElementHref |
491
|
0
|
|
|
0
|
0
|
0
|
{ my ($self, $path, $ns, $childname, $do) = @_; |
492
|
|
|
|
|
|
|
|
493
|
0
|
|
|
0
|
|
0
|
sub { my $tree = shift; |
494
|
|
|
|
|
|
|
|
495
|
0
|
0
|
0
|
|
|
0
|
return ($childname => $tree->node) |
|
|
|
0
|
|
|
|
|
496
|
|
|
|
|
|
|
if defined $tree |
497
|
|
|
|
|
|
|
&& $tree->nodeType eq $childname |
498
|
|
|
|
|
|
|
&& $tree->node->hasAttribute('href'); |
499
|
|
|
|
|
|
|
|
500
|
0
|
|
|
|
|
0
|
$do->($tree); |
501
|
0
|
|
|
|
|
0
|
}; |
502
|
|
|
|
|
|
|
} |
503
|
|
|
|
|
|
|
|
504
|
|
|
|
|
|
|
sub makeElement |
505
|
900
|
|
|
900
|
0
|
1951
|
{ my ($self, $path, $ns, $childname, $do) = @_; |
506
|
856
|
|
|
856
|
|
1108
|
sub { my $tree = shift; |
507
|
856
|
100
|
100
|
|
|
2604
|
my $value = defined $tree && $tree->nodeType eq $childname |
508
|
|
|
|
|
|
|
? $do->($tree) : $do->(undef); |
509
|
783
|
100
|
|
|
|
2081
|
defined $value ? ($childname => $value) : (); |
510
|
900
|
|
|
|
|
2841
|
}; |
511
|
|
|
|
|
|
|
} |
512
|
|
|
|
|
|
|
|
513
|
|
|
|
|
|
|
sub makeElementDefault |
514
|
19
|
|
|
19
|
0
|
46
|
{ my ($self, $path, $ns, $childname, $do, $default) = @_; |
515
|
|
|
|
|
|
|
|
516
|
19
|
|
|
|
|
27
|
my $mode = $self->{default_values}; |
517
|
|
|
|
|
|
|
$mode eq 'IGNORE' |
518
|
|
|
|
|
|
|
and return sub |
519
|
2
|
50
|
|
2
|
|
5
|
{ my $tree = shift or return (); |
520
|
2
|
50
|
33
|
|
|
5
|
return () if $tree->nodeType ne $childname |
521
|
|
|
|
|
|
|
|| $tree->node->textContent eq ''; |
522
|
2
|
|
|
|
|
5
|
$do->($tree); |
523
|
19
|
100
|
|
|
|
45
|
}; |
524
|
|
|
|
|
|
|
|
525
|
17
|
|
|
|
|
25
|
my $def = $do->($default); |
526
|
|
|
|
|
|
|
|
527
|
|
|
|
|
|
|
$mode eq 'EXTEND' |
528
|
|
|
|
|
|
|
and return sub |
529
|
15
|
|
|
15
|
|
19
|
{ my $tree = shift; |
530
|
15
|
100
|
66
|
|
|
47
|
return ($childname => $def) |
|
|
|
100
|
|
|
|
|
531
|
|
|
|
|
|
|
if !defined $tree |
532
|
|
|
|
|
|
|
|| $tree->nodeType ne $childname |
533
|
|
|
|
|
|
|
|| $tree->node->textContent eq ''; |
534
|
|
|
|
|
|
|
|
535
|
7
|
|
|
|
|
16
|
$do->($tree); |
536
|
17
|
100
|
|
|
|
89
|
}; |
537
|
|
|
|
|
|
|
|
538
|
|
|
|
|
|
|
$mode eq 'MINIMAL' |
539
|
|
|
|
|
|
|
and return sub |
540
|
3
|
50
|
|
3
|
|
8
|
{ my $tree = shift or return (); |
541
|
3
|
50
|
33
|
|
|
7
|
return () if $tree->nodeType ne $childname |
542
|
|
|
|
|
|
|
|| $tree->node->textContent eq ''; |
543
|
3
|
|
|
|
|
10
|
my $v = $do->($tree); |
544
|
3
|
100
|
66
|
|
|
17
|
undef $v if defined $v && $v eq $def; |
545
|
3
|
|
|
|
|
7
|
($childname => $v); |
546
|
3
|
50
|
|
|
|
19
|
}; |
547
|
|
|
|
|
|
|
|
548
|
0
|
|
|
|
|
0
|
error __x"illegal default_values mode `{mode}'", mode => $mode; |
549
|
|
|
|
|
|
|
} |
550
|
|
|
|
|
|
|
|
551
|
|
|
|
|
|
|
sub makeElementFixed |
552
|
3
|
|
|
3
|
0
|
10
|
{ my ($self, $path, $ns, $childname, $do, $fixed) = @_; |
553
|
3
|
|
|
|
|
8
|
my ($tag, $fix) = $do->($fixed); |
554
|
|
|
|
|
|
|
|
555
|
3
|
|
|
3
|
|
5
|
sub { my $tree = shift; |
556
|
3
|
100
|
66
|
|
|
19
|
my ($label, $value) |
557
|
|
|
|
|
|
|
= $tree && $tree->nodeType eq $childname ? $do->($tree) : (); |
558
|
|
|
|
|
|
|
|
559
|
3
|
100
|
|
|
|
12
|
defined $value |
560
|
|
|
|
|
|
|
or return ($tag => $fix); |
561
|
|
|
|
|
|
|
|
562
|
2
|
50
|
|
|
|
7
|
$value eq $fix |
563
|
|
|
|
|
|
|
or error __x"element `{name}' must have fixed value `{fixed}', got `{value}' at {path}" |
564
|
|
|
|
|
|
|
, name => $childname, fixed => $fix, value => $value |
565
|
|
|
|
|
|
|
, path => $path; |
566
|
|
|
|
|
|
|
|
567
|
2
|
|
|
|
|
55
|
($label => $value); |
568
|
3
|
|
|
|
|
14
|
}; |
569
|
|
|
|
|
|
|
} |
570
|
|
|
|
|
|
|
|
571
|
|
|
|
|
|
|
sub makeElementAbstract |
572
|
9
|
|
|
9
|
0
|
32
|
{ my ($self, $path, $ns, $childname, $do, $tag) = @_; |
573
|
3
|
100
|
|
3
|
|
10
|
sub { my $tree = shift or return (); |
574
|
2
|
50
|
|
|
|
14
|
$tree->nodeType eq $childname or return (); |
575
|
|
|
|
|
|
|
|
576
|
2
|
|
|
|
|
10
|
error __x"abstract element `{name}' used at {path}" |
577
|
|
|
|
|
|
|
, name => $childname, path => $path; |
578
|
9
|
|
|
|
|
50
|
}; |
579
|
|
|
|
|
|
|
} |
580
|
|
|
|
|
|
|
|
581
|
|
|
|
|
|
|
# |
582
|
|
|
|
|
|
|
# complexType and complexType/ComplexContent |
583
|
|
|
|
|
|
|
# |
584
|
|
|
|
|
|
|
|
585
|
|
|
|
|
|
|
# Be warned that the location reported in 'path' may not be the actual |
586
|
|
|
|
|
|
|
# location, caused by the cashing of compiled schema components. The |
587
|
|
|
|
|
|
|
# path you see is the first path where that element was encountered. |
588
|
|
|
|
|
|
|
sub _not_processed($$) |
589
|
4
|
|
|
4
|
|
10
|
{ my ($child, $path) = @_; |
590
|
4
|
|
|
|
|
13
|
error __x"element `{name}' not processed for {path} at {where}" |
591
|
|
|
|
|
|
|
, name => type_of_node($child), path => $path |
592
|
|
|
|
|
|
|
, _class => 'misfit', where => $child->nodePath; |
593
|
|
|
|
|
|
|
} |
594
|
|
|
|
|
|
|
|
595
|
|
|
|
|
|
|
sub makeComplexElement |
596
|
262
|
|
|
262
|
0
|
790
|
{ my ($self, $path, $tag, $elems, $attrs, $attrs_any,undef,$is_nillable) = @_; |
597
|
|
|
|
|
|
|
#my @e = @$elems; my @a = @$attrs; |
598
|
|
|
|
|
|
|
|
599
|
262
|
|
|
|
|
808
|
my @elems = odd_elements @$elems; |
600
|
262
|
|
|
|
|
555
|
my @attrs = (odd_elements(@$attrs), @$attrs_any); |
601
|
|
|
|
|
|
|
|
602
|
|
|
|
|
|
|
$is_nillable and return |
603
|
10
|
50
|
|
10
|
|
24
|
sub { my $tree = shift or return (); |
604
|
10
|
|
|
|
|
19
|
my $node = $tree->node; |
605
|
10
|
100
|
|
|
|
22
|
my %complex = |
606
|
|
|
|
|
|
|
( ($tree->nodeNil ? (_ => 'NIL') : (map $_->($tree), @elems)) |
607
|
|
|
|
|
|
|
, (map $_->($node), @attrs) |
608
|
|
|
|
|
|
|
); |
609
|
|
|
|
|
|
|
|
610
|
10
|
50
|
|
|
|
25
|
_not_processed $tree->currentChild, $path |
611
|
|
|
|
|
|
|
if $tree->currentChild; |
612
|
|
|
|
|
|
|
|
613
|
10
|
|
|
|
|
21
|
($tag => \%complex); |
614
|
262
|
100
|
|
|
|
596
|
}; |
615
|
|
|
|
|
|
|
|
616
|
|
|
|
|
|
|
@elems > 1 || @attrs and return |
617
|
61
|
100
|
|
61
|
|
168
|
sub { my $tree = shift or return (); |
618
|
60
|
|
|
|
|
119
|
my $node = $tree->node; |
619
|
60
|
|
|
|
|
173
|
my %complex = ((map $_->($tree), @elems), (map $_->($node), @attrs)); |
620
|
|
|
|
|
|
|
|
621
|
56
|
50
|
|
|
|
164
|
_not_processed $tree->currentChild, $path |
622
|
|
|
|
|
|
|
if $tree->currentChild; |
623
|
|
|
|
|
|
|
|
624
|
56
|
|
|
|
|
126
|
($tag => \%complex); |
625
|
256
|
100
|
100
|
|
|
2809
|
}; |
626
|
|
|
|
|
|
|
|
627
|
|
|
|
|
|
|
@elems || return |
628
|
8
|
50
|
|
8
|
|
26
|
sub { my $tree = shift or return (); |
629
|
8
|
50
|
|
|
|
19
|
_not_processed $tree->currentChild, $path |
630
|
|
|
|
|
|
|
if $tree->currentChild; |
631
|
|
|
|
|
|
|
|
632
|
8
|
|
|
|
|
18
|
($tag => {}); |
633
|
199
|
100
|
|
|
|
471
|
}; |
634
|
|
|
|
|
|
|
|
635
|
189
|
|
|
|
|
280
|
my $el = shift @elems; |
636
|
185
|
50
|
|
185
|
|
454
|
sub { my $tree = shift or return (); |
637
|
185
|
|
|
|
|
380
|
my %complex = $el->($tree); |
638
|
|
|
|
|
|
|
|
639
|
156
|
100
|
|
|
|
398
|
_not_processed $tree->currentChild, $path |
640
|
|
|
|
|
|
|
if $tree->currentChild; |
641
|
|
|
|
|
|
|
|
642
|
152
|
|
|
|
|
277
|
($tag => \%complex); |
643
|
189
|
|
|
|
|
945
|
}; |
644
|
|
|
|
|
|
|
} |
645
|
|
|
|
|
|
|
|
646
|
|
|
|
|
|
|
# |
647
|
|
|
|
|
|
|
# complexType/simpleContent |
648
|
|
|
|
|
|
|
# |
649
|
|
|
|
|
|
|
|
650
|
|
|
|
|
|
|
sub makeTaggedElement |
651
|
10
|
|
|
10
|
0
|
31
|
{ my ($self, $path, $tag, $st, $attrs, $attrs_any,undef,$is_nillable) = @_; |
652
|
10
|
|
|
|
|
49
|
my @attrs = (odd_elements(@$attrs), @$attrs_any); |
653
|
|
|
|
|
|
|
|
654
|
11
|
|
50
|
11
|
|
30
|
sub { my $tree = shift // return (); |
655
|
11
|
100
|
66
|
|
|
55
|
my $simple = $is_nillable && ref $tree && $tree->nodeNil ? 'NIL' : $st->($tree); |
656
|
11
|
100
|
|
|
|
28
|
ref $tree or return ($tag => {_ => $simple}); |
657
|
10
|
|
|
|
|
21
|
my $node = $tree->node; |
658
|
10
|
|
|
|
|
26
|
my @pairs = map $_->($node), @attrs; |
659
|
10
|
50
|
33
|
|
|
57
|
defined $simple || @pairs ? ($tag => {_ => $simple, @pairs}) : (); |
660
|
10
|
|
|
|
|
49
|
}; |
661
|
|
|
|
|
|
|
} |
662
|
|
|
|
|
|
|
|
663
|
|
|
|
|
|
|
# |
664
|
|
|
|
|
|
|
# complexType mixed or complexContent mixed |
665
|
|
|
|
|
|
|
# |
666
|
|
|
|
|
|
|
|
667
|
|
|
|
|
|
|
sub makeMixedElement |
668
|
6
|
|
|
6
|
0
|
16
|
{ my ($self, $path, $tag, $elems, $attrs, $attrs_any,undef,$is_nillable) = @_; |
669
|
6
|
|
|
|
|
15
|
my @attrs = (odd_elements(@$attrs), @$attrs_any); |
670
|
|
|
|
|
|
|
my $mixed = $self->{mixed_elements} |
671
|
6
|
50
|
|
|
|
13
|
or panic "how to handle mixed?"; |
672
|
6
|
50
|
|
|
|
10
|
$is_nillable and panic "nillable mixed not yet supported"; |
673
|
|
|
|
|
|
|
|
674
|
|
|
|
|
|
|
ref $mixed eq 'CODE' |
675
|
1
|
50
|
|
1
|
|
3
|
? sub { my $tree = shift or return; |
676
|
1
|
50
|
|
|
|
3
|
my $node = $tree->node or return; |
677
|
1
|
|
|
|
|
7
|
my @v = $mixed->($path, $node); |
678
|
1
|
50
|
|
|
|
7
|
@v ? ($tag => $v[0]) : (); |
679
|
|
|
|
|
|
|
} |
680
|
|
|
|
|
|
|
|
681
|
|
|
|
|
|
|
: $mixed eq 'XML_NODE' |
682
|
1
|
50
|
|
1
|
|
5
|
? sub {$_[0] ? ($tag => $_[0]->node) : () } |
683
|
|
|
|
|
|
|
|
684
|
|
|
|
|
|
|
: $mixed eq 'ATTRIBUTES' |
685
|
2
|
50
|
|
2
|
|
5
|
? sub { my $tree = shift or return; |
686
|
2
|
|
|
|
|
4
|
my $node = $tree->node; |
687
|
2
|
|
|
|
|
7
|
my @pairs = map $_->($node), @attrs; |
688
|
2
|
|
|
|
|
9
|
($tag => { _ => $node, @pairs |
689
|
|
|
|
|
|
|
, _MIXED_ELEMENT_MODE => 'ATTRIBUTES'}); |
690
|
|
|
|
|
|
|
} |
691
|
|
|
|
|
|
|
: $mixed eq 'TEXTUAL' |
692
|
1
|
50
|
|
1
|
|
4
|
? sub { my $tree = shift or return; |
693
|
1
|
|
|
|
|
3
|
my $node = $tree->node; |
694
|
1
|
|
|
|
|
4
|
my @pairs = map $_->($node), @attrs; |
695
|
1
|
|
|
|
|
15
|
($tag => { _ => $node->textContent, @pairs |
696
|
|
|
|
|
|
|
, _MIXED_ELEMENT_MODE => 'TEXTUAL'}); |
697
|
|
|
|
|
|
|
} |
698
|
|
|
|
|
|
|
: $mixed eq 'XML_STRING' |
699
|
1
|
50
|
|
1
|
|
5
|
? sub { my $tree = shift or return; |
700
|
1
|
50
|
|
|
|
3
|
my $node = $tree->node or return; |
701
|
1
|
|
|
|
|
21
|
($tag => $node->toString); |
702
|
|
|
|
|
|
|
} |
703
|
6
|
0
|
|
|
|
42
|
: $mixed eq 'STRUCTURAL' |
|
|
50
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
704
|
|
|
|
|
|
|
|
705
|
|
|
|
|
|
|
# this cannot be reached, because handled somewhere else |
706
|
|
|
|
|
|
|
? panic "mixed structural handled as normal element" |
707
|
|
|
|
|
|
|
|
708
|
|
|
|
|
|
|
: error __x"unknown mixed_elements value `{value}'", value => $mixed; |
709
|
|
|
|
|
|
|
} |
710
|
|
|
|
|
|
|
|
711
|
|
|
|
|
|
|
# |
712
|
|
|
|
|
|
|
# simpleType |
713
|
|
|
|
|
|
|
# |
714
|
|
|
|
|
|
|
|
715
|
|
|
|
|
|
|
sub makeSimpleElement |
716
|
653
|
|
|
653
|
0
|
1691
|
{ my ( $self, $path, $tag, $st, undef, undef, $comptype, $is_nillable) = @_; |
717
|
|
|
|
|
|
|
|
718
|
|
|
|
|
|
|
$is_nillable |
719
|
15
|
|
100
|
15
|
|
38
|
? sub { my $tree = shift // return $st->(undef); |
720
|
14
|
100
|
66
|
|
|
49
|
my $value = (ref $tree && $tree->nodeNil) ? 'NIL' : $st->($tree); |
721
|
14
|
50
|
|
|
|
31
|
defined $value ? ($tag => $value) : (); |
722
|
|
|
|
|
|
|
} |
723
|
594
|
|
|
594
|
|
1135
|
: sub { my $value = $st->(@_); |
724
|
558
|
100
|
|
|
|
1592
|
defined $value ? ($tag => $value) : (); |
725
|
653
|
100
|
|
|
|
2700
|
}; |
726
|
|
|
|
|
|
|
|
727
|
|
|
|
|
|
|
} |
728
|
|
|
|
|
|
|
|
729
|
|
|
|
|
|
|
sub default_anytype_handler($$) |
730
|
1
|
|
|
1
|
0
|
6
|
{ my ($path, $node) = @_; |
731
|
1
|
50
|
|
|
|
3
|
ref $node or return $node; |
732
|
1
|
50
|
|
1
|
|
4
|
(first{ UNIVERSAL::isa($_, 'XML::LibXML::Element') } $node->childNodes) |
|
1
|
|
|
|
|
23
|
|
733
|
|
|
|
|
|
|
? $node : $node->textContent; |
734
|
|
|
|
|
|
|
} |
735
|
|
|
|
|
|
|
|
736
|
|
|
|
|
|
|
sub makeBuiltin |
737
|
796
|
|
|
796
|
0
|
1571
|
{ my ($self, $path, $node, $type, $def, $check_values) = @_; |
738
|
|
|
|
|
|
|
|
739
|
796
|
100
|
|
|
|
1802
|
if($type =~ m/}anyType$/) |
740
|
|
|
|
|
|
|
{ |
741
|
8
|
100
|
|
|
|
22
|
if(my $a = $self->{any_type}) |
742
|
|
|
|
|
|
|
{ return sub { |
743
|
1
|
50
|
33
|
1
|
|
9
|
my $node |
744
|
|
|
|
|
|
|
= ref $_[0] && UNIVERSAL::isa($_[0], 'XML::Compile::Iterator') |
745
|
|
|
|
|
|
|
? $_[0]->node : $_[0]; |
746
|
1
|
|
|
|
|
8
|
$a->( $path, $node, \&default_anytype_handler)}; |
|
1
|
|
|
|
|
5
|
|
747
|
|
|
|
|
|
|
} |
748
|
|
|
|
|
|
|
else |
749
|
|
|
|
|
|
|
{ return sub |
750
|
7
|
50
|
|
7
|
|
20
|
{ ref $_[0] or return $_[0]; |
751
|
7
|
100
|
|
|
|
29
|
my $node = UNIVERSAL::isa($_[0], 'XML::Compile::Iterator') |
752
|
|
|
|
|
|
|
? $_[0]->node : $_[0]; |
753
|
5
|
|
|
|
|
79
|
(first{ UNIVERSAL::isa($_, 'XML::LibXML::Element') } |
754
|
7
|
100
|
|
|
|
37
|
$node->childNodes) ? $node : $node->textContent; |
755
|
7
|
|
|
|
|
40
|
}; |
756
|
|
|
|
|
|
|
} |
757
|
|
|
|
|
|
|
} |
758
|
|
|
|
|
|
|
|
759
|
788
|
50
|
|
|
|
1635
|
my $check = $check_values ? $def->{check} : undef; |
760
|
788
|
|
|
|
|
1063
|
my $parse = $def->{parse}; |
761
|
788
|
100
|
|
|
|
2515
|
my $err = $path eq $type |
762
|
|
|
|
|
|
|
? N__"illegal value `{value}' for type {type}" |
763
|
|
|
|
|
|
|
: N__"illegal value `{value}' for type {type} at {path}"; |
764
|
|
|
|
|
|
|
|
765
|
|
|
|
|
|
|
$check |
766
|
|
|
|
|
|
|
? ( defined $parse |
767
|
667
|
100
|
|
667
|
|
2087
|
? sub { my $value = ref $_[0] ? $_[0]->textContent : $_[0]; |
768
|
667
|
100
|
|
|
|
1341
|
defined $value or return undef; |
769
|
629
|
100
|
100
|
|
|
1366
|
return $parse->($value, $_[1]||$_[0]) |
770
|
|
|
|
|
|
|
if $check->($value); |
771
|
12
|
|
|
|
|
42
|
error __x$err, value => $value, type => $type, path => $path; |
772
|
|
|
|
|
|
|
} |
773
|
0
|
0
|
|
0
|
|
0
|
: sub { my $value = ref $_[0] ? $_[0]->textContent : $_[0]; |
774
|
0
|
0
|
|
|
|
0
|
defined $value or return undef; |
775
|
0
|
0
|
|
|
|
0
|
return $value if $check->($value); |
776
|
0
|
|
|
|
|
0
|
error __x$err, value => $value, type => $type, path => $path; |
777
|
|
|
|
|
|
|
} |
778
|
|
|
|
|
|
|
) |
779
|
|
|
|
|
|
|
|
780
|
|
|
|
|
|
|
: ( defined $parse |
781
|
13
|
100
|
|
13
|
|
43
|
? sub { my $value = ref $_[0] ? $_[0]->textContent : $_[0]; |
782
|
13
|
50
|
|
|
|
41
|
defined $value or return undef; |
783
|
13
|
|
66
|
|
|
52
|
$parse->($value, $_[1]||$_[0]); |
784
|
|
|
|
|
|
|
} |
785
|
55
|
100
|
|
55
|
|
203
|
: sub { ref $_[0] ? shift->textContent : $_[0] } |
786
|
788
|
50
|
|
|
|
5709
|
); |
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
787
|
|
|
|
|
|
|
} |
788
|
|
|
|
|
|
|
|
789
|
|
|
|
|
|
|
sub makeList |
790
|
28
|
|
|
28
|
0
|
54
|
{ my ($self, $path, $st) = @_; |
791
|
27
|
|
50
|
27
|
|
60
|
sub { my $tree = shift // return undef; |
792
|
27
|
100
|
|
|
|
135
|
my $node |
|
|
50
|
|
|
|
|
|
793
|
|
|
|
|
|
|
= UNIVERSAL::isa($tree, 'XML::LibXML::Node') ? $tree |
794
|
|
|
|
|
|
|
: ref $tree ? $tree->node : undef; |
795
|
27
|
100
|
|
|
|
78
|
my $v = ref $tree ? $tree->textContent : $tree; |
796
|
27
|
|
|
|
|
101
|
[ grep defined, map $st->($_, $node), split " ", $v ]; |
797
|
28
|
|
|
|
|
110
|
}; |
798
|
|
|
|
|
|
|
} |
799
|
|
|
|
|
|
|
|
800
|
|
|
|
|
|
|
sub makeFacetsList |
801
|
11
|
|
|
11
|
0
|
23
|
{ my ($self, $path, $st, $info, $early, $late) = @_; |
802
|
11
|
|
|
|
|
24
|
my @e = grep defined, @$early; |
803
|
11
|
|
|
|
|
24
|
my @l = grep defined, @$late; |
804
|
|
|
|
|
|
|
|
805
|
|
|
|
|
|
|
# enumeration and pattern are probably rare |
806
|
|
|
|
|
|
|
@e or return sub { |
807
|
7
|
|
50
|
7
|
|
10
|
my $values = $st->(@_) // return; |
808
|
7
|
|
|
|
|
27
|
$_->($values) for @l; |
809
|
4
|
|
|
|
|
7
|
$values; |
810
|
11
|
100
|
|
|
|
50
|
}; |
811
|
|
|
|
|
|
|
|
812
|
4
|
50
|
|
4
|
|
9
|
sub { defined $_[0] or return undef; |
813
|
4
|
50
|
|
|
|
15
|
my $list = ref $_[0] ? $_[0]->textContent : $_[0]; |
814
|
4
|
|
|
|
|
13
|
$_->($list) for @e; |
815
|
3
|
|
50
|
|
|
7
|
my $values = $st->($_[0]) // return; |
816
|
3
|
|
|
|
|
7
|
$_->($values) for @l; |
817
|
3
|
|
|
|
|
6
|
$values; |
818
|
4
|
|
|
|
|
21
|
}; |
819
|
|
|
|
|
|
|
} |
820
|
|
|
|
|
|
|
|
821
|
|
|
|
|
|
|
sub makeFacets |
822
|
94
|
|
|
94
|
0
|
216
|
{ my ($self, $path, $st, $info, $early, $late) = @_; |
823
|
94
|
100
|
100
|
|
|
322
|
@$early || @$late or return $st; |
824
|
|
|
|
|
|
|
|
825
|
93
|
100
|
|
|
|
200
|
unless(@$early) |
826
|
|
|
|
|
|
|
{ return sub { |
827
|
73
|
|
|
73
|
|
126
|
my $v = $st->(shift); |
828
|
72
|
50
|
|
|
|
1374
|
defined $v or return undef; |
829
|
72
|
|
|
|
|
214
|
$v = $_->($v) for @$late; |
830
|
47
|
|
|
|
|
217
|
$v; |
831
|
79
|
|
|
|
|
408
|
}; |
832
|
|
|
|
|
|
|
} |
833
|
|
|
|
|
|
|
|
834
|
|
|
|
|
|
|
sub { |
835
|
12
|
|
|
12
|
|
18
|
my $v = shift; |
836
|
12
|
50
|
|
|
|
29
|
if(@$early) |
837
|
12
|
50
|
|
|
|
28
|
{ return if !defined $v; |
838
|
12
|
|
|
|
|
42
|
$v = $_->($v) for @$early; |
839
|
|
|
|
|
|
|
} |
840
|
9
|
|
50
|
|
|
21
|
$v = $st->($v) // return undef; |
841
|
9
|
|
|
|
|
22
|
$v = $_->($v) for @$late; |
842
|
9
|
|
|
|
|
15
|
$v; |
843
|
14
|
|
|
|
|
82
|
}; |
844
|
|
|
|
|
|
|
} |
845
|
|
|
|
|
|
|
|
846
|
|
|
|
|
|
|
sub makeUnion |
847
|
19
|
|
|
19
|
0
|
44
|
{ my ($self, $path, @types) = @_; |
848
|
26
|
|
50
|
26
|
|
91
|
sub { my $tree = shift // return undef; |
849
|
26
|
100
|
|
|
|
48
|
for(@types) { my $v = try { $_->($tree) }; $@ or return $v } |
|
39
|
|
|
|
|
203
|
|
|
39
|
|
|
|
|
8895
|
|
|
39
|
|
|
|
|
3647
|
|
850
|
3
|
100
|
|
|
|
27
|
my $text = ref $tree ? $tree->textContent : $tree; |
851
|
|
|
|
|
|
|
|
852
|
3
|
50
|
|
|
|
10
|
substr $text, 20, -5, '...' if length($text) > 50; |
853
|
3
|
|
|
|
|
9
|
error __x"no match for `{text}' in union at {path}" |
854
|
|
|
|
|
|
|
, text => $text, path => $path; |
855
|
19
|
|
|
|
|
65
|
}; |
856
|
|
|
|
|
|
|
} |
857
|
|
|
|
|
|
|
|
858
|
|
|
|
|
|
|
# Attributes |
859
|
|
|
|
|
|
|
|
860
|
|
|
|
|
|
|
sub makeAttributeRequired |
861
|
12
|
|
|
12
|
0
|
45
|
{ my ($self, $path, $ns, $tag, $label, $do) = @_; |
862
|
12
|
|
|
12
|
|
63
|
sub { my $node = $_[0]->getAttributeNodeNS($ns, $tag); |
863
|
12
|
100
|
|
|
|
51
|
defined $node |
864
|
|
|
|
|
|
|
or error __x"attribute `{name}' is required at {path}" |
865
|
|
|
|
|
|
|
, name => $tag, path => $path; |
866
|
|
|
|
|
|
|
|
867
|
11
|
50
|
|
|
|
81
|
defined $node or return (); |
868
|
11
|
|
|
|
|
27
|
my $value = $do->($node); |
869
|
11
|
50
|
|
|
|
110
|
defined $value ? ($label => $value) : (); |
870
|
12
|
|
|
|
|
79
|
}; |
871
|
|
|
|
|
|
|
} |
872
|
|
|
|
|
|
|
|
873
|
|
|
|
|
|
|
sub makeAttributeProhibited |
874
|
3
|
|
|
3
|
0
|
10
|
{ my ($self, $path, $ns, $tag, $label, $do) = @_; |
875
|
3
|
|
|
3
|
|
12
|
sub { my $node = $_[0]->getAttributeNodeNS($ns, $tag); |
876
|
3
|
100
|
|
|
|
11
|
defined $node or return (); |
877
|
1
|
|
|
|
|
6
|
error __x"attribute `{name}' is prohibited at {path}" |
878
|
|
|
|
|
|
|
, name => $tag, path => $path; |
879
|
0
|
|
|
|
|
0
|
(); |
880
|
3
|
|
|
|
|
11
|
}; |
881
|
|
|
|
|
|
|
} |
882
|
|
|
|
|
|
|
|
883
|
|
|
|
|
|
|
sub makeAttribute |
884
|
93
|
|
|
93
|
0
|
225
|
{ my ($self, $path, $ns, $tag, $label, $do) = @_; |
885
|
92
|
|
|
92
|
|
522
|
sub { my $node = $_[0]->getAttributeNodeNS($ns, $tag); |
886
|
92
|
100
|
|
|
|
266
|
defined $node or return (); |
887
|
70
|
|
|
|
|
138
|
my $val = $do->($node); |
888
|
68
|
50
|
|
|
|
384
|
defined $val ? ($label => $val) : (); |
889
|
93
|
|
|
|
|
1946
|
}; |
890
|
|
|
|
|
|
|
} |
891
|
|
|
|
|
|
|
|
892
|
|
|
|
|
|
|
sub makeAttributeDefault |
893
|
7
|
|
|
7
|
0
|
25
|
{ my ($self, $path, $ns, $tag, $label, $do, $default) = @_; |
894
|
|
|
|
|
|
|
|
895
|
7
|
|
|
|
|
13
|
my $mode = $self->{default_values}; |
896
|
|
|
|
|
|
|
$mode eq 'IGNORE' |
897
|
|
|
|
|
|
|
and return sub |
898
|
3
|
|
|
3
|
|
27
|
{ my $node = $_[0]->getAttributeNodeNS($ns, $tag); |
899
|
7
|
100
|
|
|
|
23
|
defined $node ? ($label => $do->($node)) : () }; |
|
3
|
100
|
|
|
|
13
|
|
900
|
|
|
|
|
|
|
|
901
|
5
|
|
|
|
|
10
|
my $def = $do->($default); |
902
|
|
|
|
|
|
|
|
903
|
|
|
|
|
|
|
$mode eq 'EXTEND' |
904
|
|
|
|
|
|
|
and return sub |
905
|
4
|
|
|
4
|
|
16
|
{ my $node = $_[0]->getAttributeNodeNS($ns, $tag); |
906
|
4
|
100
|
|
|
|
14
|
($label => ($node ? $do->($node) : $def)) |
907
|
5
|
100
|
|
|
|
32
|
}; |
908
|
|
|
|
|
|
|
|
909
|
|
|
|
|
|
|
$mode eq 'MINIMAL' |
910
|
|
|
|
|
|
|
and return sub |
911
|
3
|
|
|
3
|
|
9
|
{ my $node = $_[0]->getAttributeNodeNS($ns, $tag); |
912
|
3
|
100
|
|
|
|
6
|
my $v = $node ? $do->($node) : $def; |
913
|
3
|
100
|
66
|
|
|
21
|
!defined $v || $v eq $def ? () : ($label => $v); |
914
|
2
|
50
|
|
|
|
12
|
}; |
915
|
|
|
|
|
|
|
|
916
|
0
|
|
|
|
|
0
|
error __x"illegal default_values mode `{mode}'", mode => $mode; |
917
|
|
|
|
|
|
|
} |
918
|
|
|
|
|
|
|
|
919
|
|
|
|
|
|
|
sub makeAttributeFixed |
920
|
5
|
|
|
5
|
0
|
15
|
{ my ($self, $path, $ns, $tag, $label, $do, $fixed) = @_; |
921
|
5
|
|
|
|
|
12
|
my $def = $do->($fixed); |
922
|
|
|
|
|
|
|
|
923
|
5
|
100
|
|
5
|
|
31
|
sub { my $node = $_[0]->getAttributeNodeNS($ns, $tag) |
924
|
|
|
|
|
|
|
or return ($label => $def); |
925
|
|
|
|
|
|
|
|
926
|
4
|
|
|
|
|
31
|
my $value = $do->($node); |
927
|
4
|
100
|
66
|
|
|
42
|
defined $value && $value eq $def |
928
|
|
|
|
|
|
|
or error __x"value of attribute `{tag}' is fixed to `{fixed}', not `{value}' at {path}" |
929
|
|
|
|
|
|
|
, tag => $tag, fixed => $def, value => $value, path => $path; |
930
|
|
|
|
|
|
|
|
931
|
3
|
|
|
|
|
22
|
($label => $def); |
932
|
5
|
|
|
|
|
25
|
}; |
933
|
|
|
|
|
|
|
} |
934
|
|
|
|
|
|
|
|
935
|
|
|
|
|
|
|
# SubstitutionGroups |
936
|
|
|
|
|
|
|
|
937
|
|
|
|
|
|
|
sub makeSubstgroup |
938
|
9
|
|
|
9
|
0
|
44
|
{ my ($self, $path, $base, %do) = @_; |
939
|
9
|
50
|
|
0
|
|
86
|
keys %do or return bless sub { () }, 'BLOCK'; |
|
0
|
|
|
|
|
0
|
|
940
|
|
|
|
|
|
|
|
941
|
|
|
|
|
|
|
bless |
942
|
14
|
|
|
14
|
|
25
|
sub { my $tree = shift; |
943
|
14
|
50
|
|
|
|
55
|
my $type = ($tree ? $tree->currentType : undef) |
|
|
50
|
|
|
|
|
|
944
|
|
|
|
|
|
|
or error __x"no data for substitution group {type} at {path}" |
945
|
|
|
|
|
|
|
, type => $base, path => $path, class => 'misfit'; |
946
|
|
|
|
|
|
|
|
947
|
14
|
100
|
|
|
|
48
|
my $do = $do{$type} or return (); |
948
|
11
|
|
|
|
|
29
|
my @subst = $do->[1]($tree->descend); |
949
|
10
|
50
|
|
|
|
34
|
@subst or return (); |
950
|
|
|
|
|
|
|
|
951
|
10
|
|
|
|
|
104
|
$tree->nextChild; |
952
|
10
|
|
|
|
|
38
|
($do->[0] => $subst[1]); # key-rewrite |
953
|
9
|
|
|
|
|
63
|
}, 'BLOCK'; |
954
|
|
|
|
|
|
|
} |
955
|
|
|
|
|
|
|
|
956
|
|
|
|
|
|
|
# anyAttribute |
957
|
|
|
|
|
|
|
|
958
|
|
|
|
|
|
|
sub makeAnyAttribute |
959
|
5
|
|
|
5
|
0
|
11
|
{ my ($self, $path, $handler, $yes, $no, $process) = @_; |
960
|
5
|
100
|
|
|
|
40
|
return () unless defined $handler; |
961
|
|
|
|
|
|
|
|
962
|
4
|
100
|
|
|
|
5
|
my %yes = map +($_ => 1), @{$yes || []}; |
|
4
|
|
|
|
|
21
|
|
963
|
4
|
100
|
|
|
|
8
|
my %no = map +($_ => 1), @{$no || []}; |
|
4
|
|
|
|
|
12
|
|
964
|
|
|
|
|
|
|
|
965
|
|
|
|
|
|
|
# Takes all, before filtering |
966
|
|
|
|
|
|
|
my $all = |
967
|
4
|
|
|
4
|
|
8
|
sub { my @result; |
968
|
4
|
|
|
|
|
17
|
foreach my $attr ($_[0]->attributes) |
969
|
16
|
100
|
|
|
|
92
|
{ $attr->isa('XML::LibXML::Attr') or next; |
970
|
8
|
|
50
|
|
|
42
|
my $ns = $attr->namespaceURI || $_[0]->namespaceURI || ''; |
971
|
8
|
100
|
100
|
|
|
24
|
next if keys %yes && !$yes{$ns}; |
972
|
7
|
100
|
100
|
|
|
19
|
next if keys %no && $no{$ns}; |
973
|
|
|
|
|
|
|
|
974
|
6
|
|
|
|
|
22
|
push @result, pack_type($ns, $attr->localName) => $attr; |
975
|
|
|
|
|
|
|
} |
976
|
4
|
|
|
|
|
29
|
@result; |
977
|
4
|
|
|
|
|
18
|
}; |
978
|
|
|
|
|
|
|
|
979
|
4
|
|
|
|
|
16
|
weaken $self; |
980
|
|
|
|
|
|
|
|
981
|
|
|
|
|
|
|
# Create filter if requested |
982
|
|
|
|
|
|
|
my $run = $handler eq 'TAKE_ALL' ? $all |
983
|
|
|
|
|
|
|
: ref $handler ne 'CODE' |
984
|
|
|
|
|
|
|
? error(__x"any_attribute handler `{got}' not understood", got => $handler) |
985
|
1
|
|
|
1
|
|
3
|
: sub { my @attrs = $all->(@_); |
986
|
1
|
|
|
|
|
3
|
my @result; |
987
|
1
|
|
|
|
|
3
|
while(@attrs) |
988
|
2
|
|
|
|
|
5
|
{ my ($type, $data) = (shift @attrs, shift @attrs); |
989
|
2
|
|
|
|
|
6
|
my ($label, $out) = $handler->($type, $data, $path, $self); |
990
|
2
|
100
|
|
|
|
1213
|
push @result, $label, $out if defined $label; |
991
|
|
|
|
|
|
|
} |
992
|
1
|
|
|
|
|
7
|
@result; |
993
|
4
|
50
|
|
|
|
16
|
}; |
|
|
100
|
|
|
|
|
|
994
|
|
|
|
|
|
|
|
995
|
4
|
|
|
|
|
13
|
bless $run, 'ANY'; |
996
|
|
|
|
|
|
|
} |
997
|
|
|
|
|
|
|
|
998
|
|
|
|
|
|
|
# anyElement |
999
|
|
|
|
|
|
|
|
1000
|
|
|
|
|
|
|
sub makeAnyElement |
1001
|
5
|
|
|
5
|
0
|
14
|
{ my ($self, $path, $handler, $yes, $no, $process, $min, $max) = @_; |
1002
|
5
|
|
100
|
|
|
15
|
$handler ||= 'SKIP_ALL'; |
1003
|
|
|
|
|
|
|
|
1004
|
5
|
100
|
|
|
|
7
|
my %yes = map +($_ => 1), @{$yes || []}; |
|
5
|
|
|
|
|
24
|
|
1005
|
5
|
100
|
|
|
|
10
|
my %no = map +($_ => 1), @{$no || []}; |
|
5
|
|
|
|
|
17
|
|
1006
|
|
|
|
|
|
|
|
1007
|
|
|
|
|
|
|
# Takes all, before filtering |
1008
|
|
|
|
|
|
|
my $any = ($max eq 'unbounded' || $max > 1) |
1009
|
|
|
|
|
|
|
? sub |
1010
|
5
|
50
|
|
5
|
|
12
|
{ my $tree = shift or return (); |
1011
|
5
|
|
|
|
|
5
|
my $count = 0; |
1012
|
5
|
|
|
|
|
6
|
my %result; |
1013
|
5
|
|
33
|
|
|
10
|
while( (my $child = $tree->currentChild) |
|
|
|
66
|
|
|
|
|
1014
|
|
|
|
|
|
|
&& ($max eq 'unbounded' || $count < $max)) |
1015
|
2
|
|
50
|
|
|
21
|
{ my $ns = $child->namespaceURI || ''; |
1016
|
2
|
100
|
50
|
|
|
7
|
$yes{$ns} or last if keys %yes; |
1017
|
2
|
100
|
50
|
|
|
8
|
$no{$ns} and last if keys %no; |
1018
|
|
|
|
|
|
|
|
1019
|
2
|
|
|
|
|
8
|
my $k = pack_type $ns, $child->localName; |
1020
|
2
|
|
|
|
|
4
|
push @{$result{$k}}, $child; |
|
2
|
|
|
|
|
6
|
|
1021
|
2
|
|
|
|
|
2
|
$count++; |
1022
|
2
|
|
|
|
|
3
|
$tree->nextChild; |
1023
|
|
|
|
|
|
|
} |
1024
|
|
|
|
|
|
|
|
1025
|
5
|
50
|
|
|
|
15
|
$count >= $min |
1026
|
|
|
|
|
|
|
or error __x"too few any elements, requires {min} and got {found}" |
1027
|
|
|
|
|
|
|
, min => $min, found => $count; |
1028
|
|
|
|
|
|
|
|
1029
|
5
|
|
|
|
|
17
|
%result; |
1030
|
|
|
|
|
|
|
} |
1031
|
|
|
|
|
|
|
: sub |
1032
|
0
|
0
|
|
0
|
|
0
|
{ my $tree = shift or return (); |
1033
|
0
|
0
|
|
|
|
0
|
my $child = $tree->currentChild or return (); |
1034
|
0
|
|
0
|
|
|
0
|
my $ns = $child->namespaceURI || ''; |
1035
|
|
|
|
|
|
|
|
1036
|
0
|
0
|
0
|
|
|
0
|
(!keys %yes || $yes{$ns}) && !(keys %no && $no{$ns}) |
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
1037
|
|
|
|
|
|
|
or return (); |
1038
|
|
|
|
|
|
|
|
1039
|
0
|
|
|
|
|
0
|
$tree->nextChild; |
1040
|
0
|
|
|
|
|
0
|
(type_of_node($child), $child); |
1041
|
5
|
50
|
33
|
|
|
29
|
}; |
1042
|
|
|
|
|
|
|
|
1043
|
5
|
|
|
|
|
14
|
bless $any, 'ANY'; |
1044
|
|
|
|
|
|
|
|
1045
|
|
|
|
|
|
|
# I would like to weaken here, but "ANY" needs the whole compiler structure |
1046
|
|
|
|
|
|
|
# intact: someone has to catch it. |
1047
|
|
|
|
|
|
|
# weaken $self; |
1048
|
|
|
|
|
|
|
|
1049
|
|
|
|
|
|
|
# Create filter if requested |
1050
|
|
|
|
|
|
|
my $run |
1051
|
|
|
|
|
|
|
= $handler eq 'TAKE_ALL' ? $any |
1052
|
1
|
|
|
1
|
|
2
|
: $handler eq 'SKIP_ALL' ? sub { $any->(@_); () } |
|
1
|
|
|
|
|
3
|
|
1053
|
|
|
|
|
|
|
: ref $handler ne 'CODE' |
1054
|
|
|
|
|
|
|
? error(__x"any_element handler `{got}' not understood", got => $handler) |
1055
|
0
|
|
|
0
|
|
0
|
: sub { my @elems = $any->(@_); |
1056
|
0
|
|
|
|
|
0
|
my @result; |
1057
|
0
|
|
|
|
|
0
|
while(@elems) |
1058
|
0
|
|
|
|
|
0
|
{ my ($type, $data) = (shift @elems, shift @elems); |
1059
|
0
|
|
|
|
|
0
|
my ($label, $out) = $handler->($type, $data, $path, $self); |
1060
|
0
|
0
|
|
|
|
0
|
push @result, $label, $out if defined $label; |
1061
|
|
|
|
|
|
|
} |
1062
|
0
|
|
|
|
|
0
|
@result; |
1063
|
5
|
0
|
|
|
|
11
|
}; |
|
|
50
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
1064
|
|
|
|
|
|
|
|
1065
|
5
|
|
|
|
|
30
|
bless $run, 'ANY'; |
1066
|
|
|
|
|
|
|
} |
1067
|
|
|
|
|
|
|
|
1068
|
|
|
|
|
|
|
# xsi:type handling |
1069
|
|
|
|
|
|
|
|
1070
|
|
|
|
|
|
|
sub makeXsiTypeSwitch($$$$) |
1071
|
3
|
|
|
3
|
0
|
8
|
{ my ($self, $where, $elem, $default_type, $types) = @_; |
1072
|
|
|
|
|
|
|
|
1073
|
|
|
|
|
|
|
sub { |
1074
|
7
|
50
|
|
7
|
|
16
|
my $tree = shift or return; |
1075
|
7
|
50
|
|
|
|
13
|
my $node = $tree->node or return; |
1076
|
7
|
|
|
|
|
64
|
my $type = $node->getAttributeNS(SCHEMA2001i, 'type'); |
1077
|
7
|
|
|
|
|
94
|
my ($alt, $code); |
1078
|
7
|
100
|
|
|
|
14
|
if($type) |
1079
|
6
|
50
|
|
|
|
19
|
{ my ($pre, $local) = $type =~ /(.*?)\:(.*)/ ? ($1, $2) : ('',$type); |
1080
|
6
|
|
|
|
|
27
|
$alt = pack_type $node->lookupNamespaceURI($pre), $local; |
1081
|
6
|
50
|
|
|
|
22
|
$code = $types->{$alt} |
1082
|
|
|
|
|
|
|
or error __x"specified xsi:type list for `{default}' does not contain `{got}'" |
1083
|
|
|
|
|
|
|
, default => $default_type, got => $type; |
1084
|
|
|
|
|
|
|
} |
1085
|
|
|
|
|
|
|
else |
1086
|
1
|
|
|
|
|
3
|
{ ($alt, $code) = ($default_type, $types->{$default_type}); |
1087
|
|
|
|
|
|
|
} |
1088
|
|
|
|
|
|
|
|
1089
|
7
|
|
|
|
|
11
|
my ($t, $d) = $code->($tree); |
1090
|
7
|
100
|
|
|
|
19
|
defined $t or return (); |
1091
|
|
|
|
|
|
|
|
1092
|
6
|
50
|
|
|
|
16
|
$d = { _ => $d } if ref $d ne 'HASH'; |
1093
|
6
|
|
33
|
|
|
34
|
$d->{XSI_TYPE} ||= $alt; |
1094
|
6
|
|
|
|
|
14
|
($t, $d); |
1095
|
3
|
|
|
|
|
26
|
}; |
1096
|
|
|
|
|
|
|
} |
1097
|
|
|
|
|
|
|
|
1098
|
|
|
|
|
|
|
# any kind of hook |
1099
|
|
|
|
|
|
|
|
1100
|
|
|
|
|
|
|
sub makeHook($$$$$$$) |
1101
|
11
|
|
|
11
|
0
|
28
|
{ my ($self, $path, $r, $tag, $before, $replace, $after, $fulltype) = @_; |
1102
|
11
|
50
|
100
|
|
|
53
|
return $r unless $before || $replace || $after; |
|
|
|
66
|
|
|
|
|
1103
|
|
|
|
|
|
|
|
1104
|
1
|
|
|
1
|
|
4
|
return sub { ($_[0]->node->localName => 'SKIPPED') } |
1105
|
11
|
100
|
66
|
|
|
37
|
if $replace && grep $_ eq 'SKIP', @$replace; |
1106
|
|
|
|
|
|
|
|
1107
|
10
|
50
|
|
|
|
20
|
my @replace = $replace ? map $self->_decodeReplace($path,$_),@$replace : (); |
1108
|
10
|
100
|
|
|
|
24
|
my @before = $before ? map $self->_decodeBefore($path,$_), @$before : (); |
1109
|
10
|
50
|
|
|
|
41
|
my @after = $after ? map $self->_decodeAfter($path,$_), @$after : (); |
1110
|
|
|
|
|
|
|
|
1111
|
10
|
|
|
|
|
37
|
weaken $self; |
1112
|
|
|
|
|
|
|
|
1113
|
|
|
|
|
|
|
sub |
1114
|
10
|
50
|
|
10
|
|
23
|
{ my $tree = shift or return (); |
1115
|
10
|
|
|
|
|
21
|
my $xml = $tree->node; |
1116
|
10
|
|
|
|
|
20
|
foreach (@before) |
1117
|
3
|
|
|
|
|
6
|
{ $xml = $_->($xml, $path, $fulltype); |
1118
|
3
|
50
|
|
|
|
15
|
defined $xml or return (); |
1119
|
|
|
|
|
|
|
} |
1120
|
|
|
|
|
|
|
|
1121
|
10
|
|
|
|
|
35
|
my $process = sub { $r->($tree->descend($xml)) }; |
|
10
|
|
|
|
|
19
|
|
1122
|
10
|
50
|
|
|
|
24
|
my @h = @replace |
1123
|
|
|
|
|
|
|
? map $_->($xml, $self, $path, $tag, $process, $fulltype), @replace |
1124
|
|
|
|
|
|
|
: $process->(); |
1125
|
|
|
|
|
|
|
|
1126
|
10
|
50
|
|
|
|
31
|
@h or return (); |
1127
|
10
|
50
|
|
|
|
74
|
my $h = @h==1 ? $h[0] : $h[1]; # detect simpleType |
1128
|
10
|
|
|
|
|
16
|
foreach my $after (@after) |
1129
|
13
|
|
|
|
|
42
|
{ $h = $after->($xml, $h, $path, $fulltype); |
1130
|
13
|
50
|
|
|
|
3690
|
defined $h or return (); |
1131
|
|
|
|
|
|
|
} |
1132
|
10
|
|
|
|
|
45
|
($tag => $h); |
1133
|
10
|
|
|
|
|
59
|
}; |
1134
|
|
|
|
|
|
|
} |
1135
|
|
|
|
|
|
|
|
1136
|
|
|
|
|
|
|
sub _decodeBefore($$) |
1137
|
3
|
|
|
3
|
|
6
|
{ my ($self, $path, $call) = @_; |
1138
|
3
|
50
|
|
|
|
10
|
return $call if ref $call eq 'CODE'; |
1139
|
|
|
|
|
|
|
|
1140
|
0
|
|
|
0
|
|
0
|
$call eq 'PRINT_PATH' ? sub {print "$_[1]\n"; $_[0] } |
|
0
|
|
|
|
|
0
|
|
1141
|
0
|
0
|
|
|
|
0
|
: error __x"labeled before hook `{call}' undefined for READER", call=>$call; |
1142
|
|
|
|
|
|
|
} |
1143
|
|
|
|
|
|
|
|
1144
|
|
|
|
|
|
|
sub _decodeReplace($$) |
1145
|
0
|
|
|
0
|
|
0
|
{ my ($self, $path, $call) = @_; |
1146
|
0
|
0
|
|
|
|
0
|
return $call if ref $call eq 'CODE'; |
1147
|
|
|
|
|
|
|
|
1148
|
0
|
|
|
0
|
|
0
|
$call eq 'XML_NODE' ? sub { ($_[3] => $_[0]) } # don't parse XML |
1149
|
0
|
0
|
|
|
|
0
|
: error __x"labeled replace hook `{call}' undefined for READER",call=>$call; |
1150
|
|
|
|
|
|
|
} |
1151
|
|
|
|
|
|
|
|
1152
|
|
|
|
|
|
|
my %after = |
1153
|
|
|
|
|
|
|
( PRINT_PATH => sub {print "$_[2]\n"; $_[1] } |
1154
|
|
|
|
|
|
|
, INCLUDE_PATH => sub { my $h = $_[1]; |
1155
|
|
|
|
|
|
|
$h = { _ => $h } if ref $h ne 'HASH'; |
1156
|
|
|
|
|
|
|
$h->{_PATH} = $_[0]; |
1157
|
|
|
|
|
|
|
$h; |
1158
|
|
|
|
|
|
|
} |
1159
|
|
|
|
|
|
|
, XML_NODE => sub { my $h = $_[1]; |
1160
|
|
|
|
|
|
|
$h = { _ => $h } if ref $h ne 'HASH'; |
1161
|
|
|
|
|
|
|
$h->{_XML_NODE} = $_[0]; |
1162
|
|
|
|
|
|
|
$h; |
1163
|
|
|
|
|
|
|
} |
1164
|
|
|
|
|
|
|
, ELEMENT_ORDER => sub { my ($xml, $h) = @_; |
1165
|
|
|
|
|
|
|
$h = { _ => $h } if ref $h ne 'HASH'; |
1166
|
|
|
|
|
|
|
my @order = map type_of_node($_) |
1167
|
|
|
|
|
|
|
, grep $_->isa('XML::LibXML::Element'), $xml->childNodes; |
1168
|
|
|
|
|
|
|
$h->{_ELEMENT_ORDER} = \@order; |
1169
|
|
|
|
|
|
|
$h; |
1170
|
|
|
|
|
|
|
} |
1171
|
|
|
|
|
|
|
, ATTRIBUTE_ORDER => sub { my ($xml, $h) = @_; |
1172
|
|
|
|
|
|
|
$h = { _ => $h } if ref $h ne 'HASH'; |
1173
|
|
|
|
|
|
|
my @order = map $_->nodeName, $xml->attributes; |
1174
|
|
|
|
|
|
|
$h->{_ATTRIBUTE_ORDER} = \@order; |
1175
|
|
|
|
|
|
|
$h; |
1176
|
|
|
|
|
|
|
} |
1177
|
|
|
|
|
|
|
, NODE_TYPE => sub { my ($xml, $h) = @_; |
1178
|
|
|
|
|
|
|
$h = { _ => $h } if ref $h ne 'HASH'; |
1179
|
|
|
|
|
|
|
$h->{_NODE_TYPE} = type_of_node $xml; |
1180
|
|
|
|
|
|
|
$h; |
1181
|
|
|
|
|
|
|
} |
1182
|
|
|
|
|
|
|
); |
1183
|
|
|
|
|
|
|
|
1184
|
|
|
|
|
|
|
sub _decodeAfter($$) |
1185
|
13
|
|
|
13
|
|
21
|
{ my ($self, $path, $call) = @_; |
1186
|
13
|
100
|
|
|
|
35
|
return $call if ref $call eq 'CODE'; |
1187
|
|
|
|
|
|
|
|
1188
|
|
|
|
|
|
|
# The 'after' can be called on a single. In that case, turn it into |
1189
|
|
|
|
|
|
|
# a HASH for additional information. |
1190
|
6
|
50
|
|
|
|
13
|
my $dec = $after{$call} |
1191
|
|
|
|
|
|
|
or error __x"labeled after hook `{call}' undefined for READER" |
1192
|
|
|
|
|
|
|
, call=> $call; |
1193
|
|
|
|
|
|
|
|
1194
|
6
|
|
|
|
|
15
|
$dec; |
1195
|
|
|
|
|
|
|
} |
1196
|
|
|
|
|
|
|
|
1197
|
|
|
|
|
|
|
sub makeBlocked($$$) |
1198
|
19
|
|
|
19
|
0
|
34
|
{ my ($self, $where, $class, $type) = @_; |
1199
|
19
|
|
33
|
|
|
41
|
my $err_type = $self->prefixed($type) || $type; |
1200
|
|
|
|
|
|
|
|
1201
|
|
|
|
|
|
|
# errors are produced in class=misfit to allow other choices to succeed. |
1202
|
|
|
|
|
|
|
$class eq 'anyType' |
1203
|
5
|
|
|
5
|
|
20
|
? { st => sub { error __x"use of `{type}' blocked at {where}" |
1204
|
|
|
|
|
|
|
, type => $err_type, where => $where, _class => 'misfit'; |
1205
|
|
|
|
|
|
|
}} |
1206
|
|
|
|
|
|
|
: $class eq 'simpleType' |
1207
|
2
|
|
|
2
|
|
9
|
? { st => sub { error __x"use of {class} `{type}' blocked at {where}" |
1208
|
|
|
|
|
|
|
, class => $class, type => $err_type, where => $where |
1209
|
|
|
|
|
|
|
, _class => 'misfit'; |
1210
|
|
|
|
|
|
|
}} |
1211
|
|
|
|
|
|
|
: $class eq 'complexType' |
1212
|
|
|
|
|
|
|
? { elems => [] } |
1213
|
|
|
|
|
|
|
: $class eq 'ref' |
1214
|
0
|
|
|
0
|
|
|
? { st => sub { error __x"use of referenced `{type}' blocked at {where}" |
1215
|
|
|
|
|
|
|
, type => $err_type, where => $where, _class => 'misfit'; |
1216
|
|
|
|
|
|
|
}} |
1217
|
19
|
50
|
|
|
|
171
|
: panic "blocking of $class for $type not implemented"; |
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
1218
|
|
|
|
|
|
|
} |
1219
|
|
|
|
|
|
|
|
1220
|
|
|
|
|
|
|
#----------------------------------- |
1221
|
|
|
|
|
|
|
|
1222
|
|
|
|
|
|
|
|
1223
|
|
|
|
|
|
|
1; |