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