| 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::Writer; | ||
| 10 | use vars '$VERSION'; | ||
| 11 | $VERSION = '1.60'; | ||
| 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/blessed weaken/; | ||
| 23 | use Encode qw/encode/; | ||
| 24 | |||
| 25 | use XML::LibXML; | ||
| 26 | use XML::Compile::Util qw/pack_type unpack_type type_of_node SCHEMA2001i | ||
| 27 | SCHEMA2001 odd_elements even_elements/; | ||
| 28 | |||
| 29 | |||
| 30 | # Each action implementation returns a code reference, which will be | ||
| 31 | # used to do the run-time work. The principle of closures is used to | ||
| 32 | # keep the important information. Be sure that you understand closures | ||
| 33 | # before you attempt to change anything. | ||
| 34 | # | ||
| 35 | # The returned writer subroutines will always be called | ||
| 36 | # $writer->($doc, $value) | ||
| 37 | |||
| 38 | sub actsAs($) { $_[1] eq 'WRITER' } | ||
| 39 | |||
| 40 | sub makeTagQualified | ||
| 41 | { # my ($self, $path, $node, $local, $ns) = @_; | ||
| 42 | # my $prefix = $self->_registerNSprefix('', $ns, 1); | ||
| 43 | # length($prefix) ? "$prefix:$local" : $local; | ||
| 44 | my $prefix = $_[0]->_registerNSprefix('', $_[4], 1); | ||
| 45 | length($prefix) ? "$prefix:$_[3]" : $_[3]; | ||
| 46 | } | ||
| 47 | |||
| 48 | sub makeTagUnqualified | ||
| 49 | { # my ($self, $path, $node, $local, $ns) = @_; | ||
| 50 | # $local; | ||
| 51 | $_[3]; | ||
| 52 | } | ||
| 53 | |||
| 54 | sub _typemapClass($$) | ||
| 55 | { my ($self, $type, $class) = @_; | ||
| 56 | |||
| 57 | no strict 'refs'; | ||
| 58 | keys %{$class.'::'} | ||
| 59 | or error __x"class {pkg} for typemap {type} is not loaded" | ||
| 60 | , pkg => $class, type => $type; | ||
| 61 | |||
| 62 | $class->can('toXML') | ||
| 63 | or error __x"class {pkg} does not implement toXML(), required for typemap {type}" | ||
| 64 | , pkg => $class, type => $type; | ||
| 65 | |||
| 66 | sub { | ||
| 67 | my ($doc, $values, $path) = @_; | ||
| 68 | UNIVERSAL::isa($values, $class) ? $values->toXML($type, $doc) : $values; | ||
| 69 | }; | ||
| 70 | } | ||
| 71 | |||
| 72 | sub _typemapObject($$) | ||
| 73 | { my ($self, $type, $object) = @_; | ||
| 74 | |||
| 75 | $object->can('toXML') | ||
| 76 | or error __x"object of class {pkg} does not implement toXML(), required for typemap {type}" | ||
| 77 | , pkg => ref($object), type => $type; | ||
| 78 | |||
| 79 | sub { | ||
| 80 | my ($doc, $values, $path) = @_; | ||
| 81 | blessed($values) ? $object->toXML($values, $type, $doc) : $values; | ||
| 82 | }; | ||
| 83 | } | ||
| 84 | |||
| 85 | sub typemapToHooks($$) | ||
| 86 | { my ($self, $hooks, $typemap) = @_; | ||
| 87 | while(my($type, $action) = each %$typemap) | ||
| 88 | { defined $action or next; | ||
| 89 | my $hook; | ||
| 90 | if(!ref $action) | ||
| 91 | { $hook = $self->_typemapClass($type, $action); | ||
| 92 | trace "created writer hook for type $type to class $action"; | ||
| 93 | } | ||
| 94 | elsif(ref $action eq 'CODE') | ||
| 95 | { $hook = sub { | ||
| 96 | my ($doc, $values, $path) = @_; | ||
| 97 | blessed($values) | ||
| 98 | ? $action->(WRITER => $values, $type, $doc) | ||
| 99 | : $values; | ||
| 100 | }; | ||
| 101 | trace "created writer hook for type $type to CODE"; | ||
| 102 | } | ||
| 103 | else | ||
| 104 | { $hook = $self->_typemapObject($type, $action); | ||
| 105 | trace "created reader hook for type $type to object"; | ||
| 106 | |||
| 107 | } | ||
| 108 | |||
| 109 | push @$hooks, +{action => 'WRITER', type => $type, before => $hook}; | ||
| 110 | } | ||
| 111 | $hooks; | ||
| 112 | } | ||
| 113 | |||
| 114 | sub makeElementWrapper | ||
| 115 | { my ($self, $path, $processor) = @_; | ||
| 116 | sub { | ||
| 117 | my ($doc, $data) = @_; | ||
| 118 | UNIVERSAL::isa($doc, 'XML::LibXML::Document') | ||
| 119 | or error __x"first argument of call to writer must be an XML::LibXML::Document"; | ||
| 120 | |||
| 121 | |||
| 122 | my $top = $processor->(@_); | ||
| 123 | $doc->indexElements; | ||
| 124 | $top; | ||
| 125 | }; | ||
| 126 | } | ||
| 127 | *makeAttributeWrapper = \&makeElementWrapper; | ||
| 128 | |||
| 129 | sub makeWrapperNs | ||
| 130 | { my ($self, $path, $processor, $index, $filter) = @_; | ||
| 131 | my @entries; | ||
| 132 | $filter = sub {$_[2]} if ref $filter ne 'CODE'; # only the used | ||
| 133 | |||
| 134 | foreach my $entry (sort {$a->{prefix} cmp $b->{prefix}} values %$index) | ||
| 135 | { # ANY components are frustrating this | ||
| 136 | $filter->($entry->{uri}, $entry->{prefix}, $entry->{used}) or next; | ||
| 137 | push @entries, [ $entry->{uri}, $entry->{prefix} ]; | ||
| 138 | $entry->{used} = 0; | ||
| 139 | } | ||
| 140 | |||
| 141 | @entries or return $processor; | ||
| 142 | |||
| 143 | sub { | ||
| 144 | my $node = $processor->(@_) or return (); | ||
| 145 | UNIVERSAL::isa($node, 'XML::LibXML::Element') | ||
| 146 | or return $node; | ||
| 147 | $node->setNamespace(@$_, 0) for @entries; | ||
| 148 | $node; | ||
| 149 | }; | ||
| 150 | } | ||
| 151 | |||
| 152 | sub makeSequence($@) | ||
| 153 | { my ($self, $path, @pairs) = @_; | ||
| 154 | |||
| 155 | if(@pairs==2) | ||
| 156 | { my ($take, $do) = @pairs; | ||
| 157 | |||
| 158 | return | ||
| 159 | ref $do eq 'ANY' ? bless(sub { $do->(@_) }, 'BLOCK') | ||
| 160 | : ref $do eq 'BLOCK' ? $do | ||
| 161 | : bless sub { | ||
| 162 | my ($doc, $values) = @_; | ||
| 163 | defined $values or return; | ||
| 164 | $do->($doc, delete $values->{$take}); | ||
| 165 | }, 'BLOCK'; | ||
| 166 | } | ||
| 167 | |||
| 168 | bless sub { | ||
| 169 | my ($doc, $values) = @_; | ||
| 170 | defined $values or return; | ||
| 171 | |||
| 172 | my @res; | ||
| 173 | my @do = @pairs; | ||
| 174 | while(@do) | ||
| 175 | { my ($take, $do) = (shift @do, shift @do); | ||
| 176 | push @res | ||
| 177 | , ref $do eq 'BLOCK' ? $do->($doc, $values) | ||
| 178 | : ref $do eq 'ANY' ? $do->($doc, $values) | ||
| 179 | : $do->($doc, delete $values->{$take}); | ||
| 180 | } | ||
| 181 | @res; | ||
| 182 | }, 'BLOCK'; | ||
| 183 | } | ||
| 184 | |||
| 185 | sub makeChoice($@) | ||
| 186 | { my ($self, $path) = (shift, shift); | ||
| 187 | my (%do, @specials); | ||
| 188 | while(@_) # protect order of specials | ||
| 189 | { my ($el, $do) = (shift, shift); | ||
| 190 | if(ref $do eq 'BLOCK' || ref $do eq 'ANY') | ||
| 191 | { push @specials, $do } | ||
| 192 | else { $do{$el} = $do } | ||
| 193 | } | ||
| 194 | |||
| 195 | if(!@specials && keys %do==1) | ||
| 196 | { my ($take, $do) = %do; | ||
| 197 | return bless sub { | ||
| 198 | my ($doc, $values) = @_; | ||
| 199 | defined $values && defined $values->{$take} | ||
| 200 | ? $do->($doc, delete $values->{$take}) : (); | ||
| 201 | }, 'BLOCK'; | ||
| 202 | } | ||
| 203 | |||
| 204 | bless sub { | ||
| 205 | my ($doc, $values) = @_; | ||
| 206 | defined $values or return (); | ||
| 207 | foreach my $take (keys %do) | ||
| 208 | { | ||
| 209 | #warn "TAKE($take) = ", (defined $values->{$take} ? 'defined' : "undef"); | ||
| 210 | return $do{$take}->($doc, delete $values->{$take}) | ||
| 211 | if defined $values->{$take}; | ||
| 212 | } | ||
| 213 | |||
| 214 | #warn "TRY SPECIALS"; | ||
| 215 | my $starter = keys %$values; | ||
| 216 | foreach (@specials) | ||
| 217 | { my @d = try { $_->($doc, $values) }; | ||
| 218 | if(my $f = $@->wasFatal(class => 'misfit')) | ||
| 219 | { # misfit error is ok, if nothing consumed | ||
| 220 | my $err = $@; | ||
| 221 | trace "misfit $path ".$@->wasFatal->message; | ||
| 222 | $err->reportAll if $starter != keys %$values; | ||
| 223 | next; | ||
| 224 | } | ||
| 225 | elsif(defined $@) {$@->reportAll} | ||
| 226 | |||
| 227 | return @d; | ||
| 228 | } | ||
| 229 | |||
| 230 | #warn "BLURK!"; | ||
| 231 | # blurk... any element with minOccurs=0 or default? | ||
| 232 | foreach (values %do) | ||
| 233 | { my @d = try { $_->($doc, undef) }; | ||
| 234 | return @d if !$@ && @d; | ||
| 235 | } | ||
| 236 | foreach (@specials) | ||
| 237 | { my @d = try { $_->($doc, undef) }; | ||
| 238 | if($@->wasFatal(class => 'misfit')) | ||
| 239 | { $@->reportAll if $starter != keys %$values; | ||
| 240 | next; | ||
| 241 | } | ||
| 242 | elsif(defined $@) {$@->reportAll} | ||
| 243 | return @d; | ||
| 244 | } | ||
| 245 | |||
| 246 | (); | ||
| 247 | }, 'BLOCK'; | ||
| 248 | } | ||
| 249 | |||
| 250 | sub makeAll($@) | ||
| 251 | { my ($self, $path, @pairs) = @_; | ||
| 252 | |||
| 253 | if(@pairs==2 && !ref $pairs[1]) | ||
| 254 | { my ($take, $do) = @pairs; | ||
| 255 | return bless sub { | ||
| 256 | my ($doc, $values) = @_; | ||
| 257 | $do->($doc, delete $values->{$take}); | ||
| 258 | }, 'BLOCK'; | ||
| 259 | } | ||
| 260 | |||
| 261 | return bless sub { | ||
| 262 | my ($doc, $values) = @_; | ||
| 263 | |||
| 264 | my @res; | ||
| 265 | my @do = @pairs; | ||
| 266 | while(@do) | ||
| 267 | { my ($take, $do) = (shift @do, shift @do); | ||
| 268 | push @res | ||
| 269 | , ref $do eq 'BLOCK' || ref $do eq 'ANY' | ||
| 270 | ? $do->($doc, $values) | ||
| 271 | : $do->($doc, delete $values->{$take}); | ||
| 272 | } | ||
| 273 | @res; | ||
| 274 | }, 'BLOCK'; | ||
| 275 | } | ||
| 276 | |||
| 277 | # | ||
| 278 | ## Element | ||
| 279 | # | ||
| 280 | |||
| 281 | # see comment BlockHandler: undef means zero but success | ||
| 282 | sub makeElementHandler | ||
| 283 | { my ($self, $path, $label, $min,$max, $required, $optional) = @_; | ||
| 284 | $max eq "0" and return sub {}; | ||
| 285 | |||
| 286 | if($min==0 && $max eq 'unbounded') | ||
| 287 | { return | ||
| 288 | sub { my ($doc, $values) = @_; | ||
| 289 | my @values = ref $values eq 'ARRAY' ? @$values | ||
| 290 | : defined $values ? $values : (); | ||
| 291 | @values ? map {$optional->($doc,$_)} @values : (undef); | ||
| 292 | }; | ||
| 293 | } | ||
| 294 | |||
| 295 | if($max eq 'unbounded') | ||
| 296 | { return | ||
| 297 | sub { my ($doc, $values) = @_; | ||
| 298 | my @values = ref $values eq 'ARRAY' ? @$values | ||
| 299 | : defined $values ? $values : (); | ||
| 300 | my @d = ( (map { $required->($doc, shift @values) } 1..$min) | ||
| 301 | , (map { $optional->($doc, $_) } @values) ); | ||
| 302 | @d ? @d : (undef); | ||
| 303 | }; | ||
| 304 | } | ||
| 305 | |||
| 306 | return sub { my @d = $optional->(@_); @d ? @d : undef } | ||
| 307 | if $min==0 && $max==1; | ||
| 308 | |||
| 309 | return $required | ||
| 310 | if $min==1 && $max==1; | ||
| 311 | |||
| 312 | sub { | ||
| 313 | my ($doc, $values) = @_; | ||
| 314 | my @values | ||
| 315 | = ref $values eq 'ARRAY' ? @$values : defined $values ? $values : (); | ||
| 316 | |||
| 317 | @values <= $max | ||
| 318 | or error "too many elements for `{tag}', max {max} found {nr} at {path}" | ||
| 319 | , tag => $label, max => $max, nr => (scalar @values) | ||
| 320 | , path => $path; | ||
| 321 | |||
| 322 | my @d = map { $required->($doc, shift @values) } 1..$min; | ||
| 323 | push @d, $optional->($doc, shift @values) | ||
| 324 | while @values && @d < $max; | ||
| 325 | |||
| 326 | @d ? @d : (undef); | ||
| 327 | }; | ||
| 328 | } | ||
| 329 | |||
| 330 | # To reflect the difference between a block which did not "succeed hence | ||
| 331 | # produced nothing", and "did succeed by producing nothing" (minOccurs=0) | ||
| 332 | # the later is represented by an undef value. | ||
| 333 | sub makeBlockHandler | ||
| 334 | { my ($self, $path, $label, $min, $max, $process, $kind, $multi) = @_; | ||
| 335 | |||
| 336 | if($min==0 && $max eq 'unbounded') | ||
| 337 | { my $code = sub { | ||
| 338 | my $doc = shift; | ||
| 339 | my $values = $_[0] ? delete shift->{$multi} : undef; | ||
| 340 | ref $values eq 'ARRAY' ? (map {$process->($doc, {%$_})} @$values) | ||
| 341 | : defined $values ? $process->($doc, $values) | ||
| 342 | : (undef); | ||
| 343 | }; | ||
| 344 | return ($multi, bless($code, 'BLOCK')); | ||
| 345 | } | ||
| 346 | |||
| 347 | if($max eq 'unbounded') | ||
| 348 | { my $code = sub { | ||
| 349 | my $doc = shift; | ||
| 350 | my $values = $_[0] ? delete shift->{$multi} : undef; | ||
| 351 | my @values = ref $values eq 'ARRAY' ? @$values | ||
| 352 | : defined $values ? $values : (); | ||
| 353 | |||
| 354 | @values >= $min | ||
| 355 | or error __x"too few blocks for `{tag}' specified, got {found} need {min} at {path}" | ||
| 356 | , tag => $multi, found => scalar @values | ||
| 357 | , min => $min, path => $path, _class => 'misfit'; | ||
| 358 | |||
| 359 | map {$process->($doc, {%$_}) } @values; | ||
| 360 | }; | ||
| 361 | return ($multi, bless($code, 'BLOCK')); | ||
| 362 | } | ||
| 363 | |||
| 364 | if($min==0 && $max==1) | ||
| 365 | { my $code = sub { | ||
| 366 | my ($doc, $values) = @_; | ||
| 367 | my @values = ref $values eq 'ARRAY' ? @$values | ||
| 368 | : defined $values ? $values : (); | ||
| 369 | @values <= 1 | ||
| 370 | or error __x"only one block value for `{tag}', not {count} at {path}" | ||
| 371 | , tag => $multi, count => scalar @values | ||
| 372 | , path => $path, _class => 'misfit'; | ||
| 373 | |||
| 374 | # @values ? $process->($doc, $values[0]) : undef; | ||
| 375 | @values or return undef; | ||
| 376 | |||
| 377 | my $starter = keys %$values; | ||
| 378 | my @d = try { $process->($doc, $values[0]) }; | ||
| 379 | $@->wasFatal(class => 'misfit') && $starter==keys %$values | ||
| 380 | or $@->reportAll; | ||
| 381 | @d; | ||
| 382 | }; | ||
| 383 | return ($label, bless($code, 'BLOCK')); | ||
| 384 | } | ||
| 385 | |||
| 386 | if($min==1 && $max==1) | ||
| 387 | { my $code = sub { | ||
| 388 | my @d = $process->(@_); | ||
| 389 | @d or error __x"no match for required block `{tag}' at {path}" | ||
| 390 | , tag => $multi, path => $path, _class => 'misfit'; | ||
| 391 | @d; | ||
| 392 | }; | ||
| 393 | return ($label, bless($code, 'BLOCK')); | ||
| 394 | } | ||
| 395 | |||
| 396 | my $opt = $max - $min; | ||
| 397 | my $code = sub { | ||
| 398 | my $doc = shift; | ||
| 399 | my $values = delete shift->{$multi}; | ||
| 400 | my @values = ref $values eq 'ARRAY' ? @$values | ||
| 401 | : defined $values ? $values : (); | ||
| 402 | |||
| 403 | @values >= $min && @values <= $max | ||
| 404 | or error __x"found {found} blocks for `{tag}', must be between {min} and {max} inclusive at {path}" | ||
| 405 | , tag => $multi, min => $min, max => $max, path => $path | ||
| 406 | , found => scalar @values, _class => 'misfit'; | ||
| 407 | |||
| 408 | map { $process->($doc, {%$_}) } @values; | ||
| 409 | }; | ||
| 410 | |||
| 411 | ($multi, bless($code, 'BLOCK')); | ||
| 412 | } | ||
| 413 | |||
| 414 | sub makeRequired | ||
| 415 | { my ($self, $path, $label, $do) = @_; | ||
| 416 | my $req = sub { | ||
| 417 | my @nodes = $do->(@_); | ||
| 418 | return @nodes if @nodes; | ||
| 419 | |||
| 420 | error __x"required data for block (starts with `{tag}') missing at {path}" | ||
| 421 | , tag => $label, path => $path, _class => 'misfit' | ||
| 422 | if ref $do eq 'BLOCK'; | ||
| 423 | |||
| 424 | error __x"required value for element `{tag}' missing at {path}" | ||
| 425 | , tag => $label, path => $path, _class => 'misfit'; | ||
| 426 | }; | ||
| 427 | bless $req, 'BLOCK' if ref $do eq 'BLOCK'; | ||
| 428 | $req; | ||
| 429 | } | ||
| 430 | |||
| 431 | sub makeElement | ||
| 432 | { my ($self, $path, $ns, $childname, $do) = @_; | ||
| 433 | sub { defined $_[1] ? $do->(@_) : () }; | ||
| 434 | } | ||
| 435 | |||
| 436 | sub makeElementFixed | ||
| 437 | { my ($self, $path, $ns, $childname, $do, $fixed) = @_; | ||
| 438 | $fixed = $fixed->value if ref $fixed; | ||
| 439 | |||
| 440 | sub { | ||
| 441 | my ($doc, $value) = @_; | ||
| 442 | my $ret = defined $value ? $do->($doc, $value) : return; | ||
| 443 | return $ret | ||
| 444 | if defined $ret && $ret->textContent eq $fixed; | ||
| 445 | |||
| 446 | defined $ret | ||
| 447 | or error __x"required element `{name}' with fixed value `{fixed}' missing at {path}" | ||
| 448 | , name => $childname, fixed => $fixed, path => $path, | ||
| 449 | , _class => 'misfit'; | ||
| 450 | |||
| 451 | error __x"element `{name}' has value fixed to `{fixed}', got `{value}' at {path}" | ||
| 452 | , name => $childname, fixed => $fixed | ||
| 453 | , value => $ret->textContent, path => $path, _class => 'misfit'; | ||
| 454 | }; | ||
| 455 | } | ||
| 456 | |||
| 457 | sub makeElementDefault | ||
| 458 | { my ($self, $path, $ns, $childname, $do, $default) = @_; | ||
| 459 | my $mode = $self->{default_values}; | ||
| 460 | |||
| 461 | $mode eq 'IGNORE' | ||
| 462 | and return sub { defined $_[1] ? $do->(@_) : () }; | ||
| 463 | |||
| 464 | $mode eq 'EXTEND' | ||
| 465 | and return sub { $do->($_[0], (defined $_[1] ? $_[1] : $default)) }; | ||
| 466 | |||
| 467 | $mode eq 'MINIMAL' | ||
| 468 | and return sub { defined $_[1] && $_[1] ne $default ? $do->(@_) : () }; | ||
| 469 | |||
| 470 | error __x"illegal default_values mode `{mode}'", mode => $mode; | ||
| 471 | } | ||
| 472 | |||
| 473 | sub makeElementAbstract | ||
| 474 | { my ($self, $path, $ns, $childname, $do, $default) = @_; | ||
| 475 | sub { defined $_[1] or return (); | ||
| 476 | error __x"attempt to instantiate abstract element `{name}' at {where}" | ||
| 477 | , name => $childname, where => $path; | ||
| 478 | }; | ||
| 479 | } | ||
| 480 | |||
| 481 | # | ||
| 482 | # complexType/ComplexContent | ||
| 483 | # | ||
| 484 | |||
| 485 | sub nil($) | ||
| 486 | { my ($self, $path) = @_; | ||
| 487 | $self->makeTagQualified($path, undef, 'nil', SCHEMA2001i); | ||
| 488 | } | ||
| 489 | |||
| 490 | sub makeComplexElement | ||
| 491 | { my ($self, $path, $tag, $elems, $attrs, $any_attr,undef, $is_nillable) = @_; | ||
| 492 | my @elems = odd_elements @$elems; | ||
| 493 | my @attrs = @$attrs; | ||
| 494 | my $tags = join ', ', grep defined | ||
| 495 | , even_elements(@$elems), even_elements(@attrs); | ||
| 496 | my @anya = @$any_attr; | ||
| 497 | my $iut = $self->{ignore_unused_tags}; | ||
| 498 | my $nilattr = $is_nillable ? $self->nil($path) : undef; | ||
| 499 | |||
| 500 | return | ||
| 501 | sub | ||
| 502 | { my ($doc, $data) = @_; | ||
| 503 | $data = { _ => 'NIL' } if $data eq 'NIL'; | ||
| 504 | |||
| 505 | return $doc->importNode($data) | ||
| 506 | if UNIVERSAL::isa($data, 'XML::LibXML::Element'); | ||
| 507 | |||
| 508 | unless(UNIVERSAL::isa($data, 'HASH')) | ||
| 509 | { defined $data | ||
| 510 | or error __x"complex `{tag}' requires data at {path}" | ||
| 511 | , tag => $tag, path => $path, _class => 'misfit'; | ||
| 512 | |||
| 513 | error __x"complex `{tag}' requires a HASH of input data, not `{got}' at {path}" | ||
| 514 | , tag => $tag, got => (ref $data || $data), path => $path; | ||
| 515 | } | ||
| 516 | |||
| 517 | my $copy = { %$data }; # do not destroy callers hash | ||
| 518 | |||
| 519 | my @childs = ($is_nillable && (delete $copy->{_} || '') eq 'NIL') | ||
| 520 | ? $doc->createAttribute($nilattr => 'true') | ||
| 521 | : map($_->($doc, $copy), @elems); | ||
| 522 | |||
| 523 | for(my $i=0; $i<@attrs; $i+=2) | ||
| 524 | { push @childs, $attrs[$i+1]->($doc, delete $copy->{$attrs[$i]}); | ||
| 525 | } | ||
| 526 | |||
| 527 | push @childs, $_->($doc, $copy) | ||
| 528 | for @anya; | ||
| 529 | |||
| 530 | if(%$copy) | ||
| 531 | { my @not_used | ||
| 532 | = defined $iut ? (grep $_ !~ $iut, keys %$copy) : keys %$copy; | ||
| 533 | |||
| 534 | if(@not_used) | ||
| 535 | { trace "available tags are: $tags"; | ||
| 536 | mistake __xn "tag `{tags}' not used at {path}" | ||
| 537 | , "unused tags {tags} at {path}" | ||
| 538 | , scalar @not_used, tags => [sort @not_used], path => $path; | ||
| 539 | } | ||
| 540 | } | ||
| 541 | |||
| 542 | my $node = $doc->createElement($tag); | ||
| 543 | |||
| 544 | foreach my $child (@childs) | ||
| 545 | { defined $child or next; | ||
| 546 | if(ref $child) | ||
| 547 | { next if UNIVERSAL::isa($child, 'XML::LibXML::Text') | ||
| 548 | && $child->data eq ''; | ||
| 549 | } | ||
| 550 | else | ||
| 551 | { length $child or next; | ||
| 552 | $child = XML::LibXML::Text->new($child); | ||
| 553 | } | ||
| 554 | $node->addChild($child); | ||
| 555 | } | ||
| 556 | |||
| 557 | $node; | ||
| 558 | }; | ||
| 559 | } | ||
| 560 | |||
| 561 | # | ||
| 562 | # complexType/simpleContent | ||
| 563 | # | ||
| 564 | |||
| 565 | sub makeTaggedElement | ||
| 566 | { my ($self, $path, $tag, $st, $attrs, $attrs_any,undef, $is_nillable) = @_; | ||
| 567 | my @attrs = @$attrs; | ||
| 568 | my @anya = @$attrs_any; | ||
| 569 | my $nilattr = $is_nillable ? $self->nil($path) : undef; | ||
| 570 | |||
| 571 | return sub { | ||
| 572 | my ($doc, $data) = @_; | ||
| 573 | return $doc->importNode($data) | ||
| 574 | if UNIVERSAL::isa($data, 'XML::LibXML::Element'); | ||
| 575 | |||
| 576 | my $copy = UNIVERSAL::isa($data,'HASH') ? {%$data} : {_ => $data}; | ||
| 577 | my $content = delete $copy->{_}; | ||
| 578 | |||
| 579 | my ($node, @childs); | ||
| 580 | if(UNIVERSAL::isa($content, 'XML::LibXML::Node')) | ||
| 581 | { $node = $doc->importNode($content); | ||
| 582 | } | ||
| 583 | elsif($is_nillable && $content eq 'NIL') | ||
| 584 | { push @childs, $doc->createAttribute($nilattr => 'true'); | ||
| 585 | } | ||
| 586 | elsif(defined $content) | ||
| 587 | { push @childs, $st->($doc, $content); | ||
| 588 | } | ||
| 589 | |||
| 590 | for(my $i=0; $i<@attrs; $i+=2) | ||
| 591 | { push @childs, $attrs[$i+1]->($doc, delete $copy->{$attrs[$i]}); | ||
| 592 | } | ||
| 593 | |||
| 594 | push @childs, $_->($doc, $copy) | ||
| 595 | for @anya; | ||
| 596 | |||
| 597 | if(my @not_used = sort keys %$copy) | ||
| 598 | { error __xn "tag `{tags}' not processed at {path}" | ||
| 599 | , "unprocessed tags {tags} at {path}" | ||
| 600 | , scalar @not_used, tags => \@not_used, path => $path; | ||
| 601 | } | ||
| 602 | |||
| 603 | $node or @childs or return (); | ||
| 604 | $node ||= $doc->createElement($tag); | ||
| 605 | $node->addChild | ||
| 606 | ( UNIVERSAL::isa($_, 'XML::LibXML::Node') ? $_ | ||
| 607 | : $doc->createTextNode(defined $_ ? $_ : '')) | ||
| 608 | for @childs; | ||
| 609 | $node; | ||
| 610 | }; | ||
| 611 | } | ||
| 612 | |||
| 613 | # | ||
| 614 | # complexType mixed or complexContent mixed | ||
| 615 | # | ||
| 616 | |||
| 617 | sub makeMixedElement | ||
| 618 | { my ($self, $path, $tag, $elems, $attrs, $attrs_any,undef, $is_nillable) =@_; | ||
| 619 | my @attrs = @$attrs; | ||
| 620 | my @anya = @$attrs_any; | ||
| 621 | my $nilattr = $is_nillable ? $self->nil($path) : undef; | ||
| 622 | (my $locname = $tag) =~ s/.*\://; | ||
| 623 | |||
| 624 | my $mixed = $self->{mixed_elements}; | ||
| 625 | if($mixed eq 'ATTRIBUTES') { ; } | ||
| 626 | elsif($mixed eq 'STRUCTURAL') | ||
| 627 | { # mixed_element eq STRUCTURAL is handled earlier | ||
| 628 | panic "mixed structural handled as normal element"; | ||
| 629 | } | ||
| 630 | else { error __x"unknown mixed_elements value `{value}'", value => $mixed } | ||
| 631 | |||
| 632 | if(!@attrs && !@anya) | ||
| 633 | { return | ||
| 634 | sub { my ($doc, $data) = @_; | ||
| 635 | my $node = ref $data eq 'HASH' ? $data->{_} : $data; | ||
| 636 | return $doc->importNode($node) | ||
| 637 | if UNIVERSAL::isa($node, 'XML::LibXML::Element'); | ||
| 638 | error __x"mixed `{tag}' requires XML::LibXML::Node, not `{found}' at {path}" | ||
| 639 | , tag => $tag, found => $data, path => $path; | ||
| 640 | }; | ||
| 641 | } | ||
| 642 | |||
| 643 | my $iut = $self->{ignore_unused_tags}; | ||
| 644 | sub { my ($doc, $data) = @_; | ||
| 645 | defined $data or return; | ||
| 646 | |||
| 647 | return $doc->importNode($data) | ||
| 648 | if UNIVERSAL::isa($data, 'XML::LibXML::Element'); | ||
| 649 | |||
| 650 | my $copy = UNIVERSAL::isa($data, 'HASH') ? {%$data} : {_ => $data}; | ||
| 651 | my $content = delete $copy->{_}; | ||
| 652 | defined $content or return; | ||
| 653 | |||
| 654 | #XXX there are no regression test for these options | ||
| 655 | my $node; | ||
| 656 | if(blessed $content && $content->isa('XML::LibXML::Node')) | ||
| 657 | { $node = $doc->importNode($content); | ||
| 658 | } | ||
| 659 | elsif($is_nillable && $content eq 'NIL') | ||
| 660 | { # nillable element | ||
| 661 | $node = $doc->createElement($tag); | ||
| 662 | $node->setAttribute($nilattr => 'true'); | ||
| 663 | } | ||
| 664 | elsif($content =~ /\<.*?\>|\&\w+\;/) | ||
| 665 | { # XXX I do not know a way to fill text nodes without getting | ||
| 666 | # entity encoding on them. Apparently, libxml2 has a | ||
| 667 | # xmlStringTextNoenc, which is not provided in XML::LibXML. | ||
| 668 | # Now, we need the expensive roundtrip, via the parser. | ||
| 669 | |||
| 670 | my $frag = XML::LibXML->new | ||
| 671 | ->parse_balanced_chunk(encode utf8 => $content); | ||
| 672 | |||
| 673 | $node = $frag->firstChild; | ||
| 674 | if($node->localName ne $locname) | ||
| 675 | { # <nodes> --> <$locname>$nodes</$locname> | ||
| 676 | my $c = $node; | ||
| 677 | $node = $doc->createElement($tag); | ||
| 678 | $node->appendChild($frag); | ||
| 679 | } | ||
| 680 | } | ||
| 681 | else | ||
| 682 | { # Plain text | ||
| 683 | $node = $doc->createElement($tag); | ||
| 684 | $node->appendText($content); | ||
| 685 | } | ||
| 686 | |||
| 687 | my @childs; | ||
| 688 | for(my $i=0; $i<@attrs; $i+=2) | ||
| 689 | { push @childs, $attrs[$i+1]->($doc, delete $copy->{$attrs[$i]}); | ||
| 690 | } | ||
| 691 | |||
| 692 | push @childs, $_->($doc, $copy) | ||
| 693 | for @anya; | ||
| 694 | |||
| 695 | if(%$copy) | ||
| 696 | { my @not_used | ||
| 697 | = defined $iut ? (grep $_ !~ $iut, keys %$copy) : keys %$copy; | ||
| 698 | |||
| 699 | if(my @not_used = sort keys %$copy) | ||
| 700 | { error __xn "tag `{tags}' not processed at {path}" | ||
| 701 | , "unprocessed tags {tags} at {path}", scalar @not_used | ||
| 702 | , tags => [sort @not_used], path => $path; | ||
| 703 | } | ||
| 704 | } | ||
| 705 | |||
| 706 | @childs or return $node; | ||
| 707 | $node->addChild | ||
| 708 | ( ref $_ && $_->isa('XML::LibXML::Node') ? $_ | ||
| 709 | : $doc->createTextNode(defined $_ ? $_ : '')) | ||
| 710 | for @childs; | ||
| 711 | $node; | ||
| 712 | }; | ||
| 713 | } | ||
| 714 | |||
| 715 | # | ||
| 716 | # simpleType | ||
| 717 | # | ||
| 718 | |||
| 719 | sub makeSimpleElement | ||
| 720 | { my ($self, $path, $tag, $st, undef, undef, undef, $is_nillable) = @_; | ||
| 721 | my $nilattr = $is_nillable ? $self->nil($path) : undef; | ||
| 722 | |||
| 723 | sub { | ||
| 724 | my ($doc, $data) = @_; | ||
| 725 | return $doc->importNode($data) | ||
| 726 | if UNIVERSAL::isa($data, 'XML::LibXML::Element'); | ||
| 727 | $data = $data->{_} | ||
| 728 | if ref $data eq 'HASH'; | ||
| 729 | |||
| 730 | my $value = ($is_nillable && $data eq 'NIL') | ||
| 731 | ? $doc->createAttribute($nilattr => 'true') | ||
| 732 | : $st->($doc, $data); | ||
| 733 | |||
| 734 | defined $value | ||
| 735 | or return (); | ||
| 736 | |||
| 737 | my $node = $doc->createElement($tag); | ||
| 738 | error __x"expected single value for {tag}, but got {type}" | ||
| 739 | , tag => $tag, type => ref($value) | ||
| 740 | if ref $value eq 'ARRAY' || ref $value eq 'HASH'; | ||
| 741 | |||
| 742 | $node->addChild | ||
| 743 | ( UNIVERSAL::isa($value, 'XML::LibXML::Node') ? $value | ||
| 744 | : $doc->createTextNode(defined $value ? $value : '')); | ||
| 745 | $node; | ||
| 746 | }; | ||
| 747 | } | ||
| 748 | |||
| 749 | sub makeBuiltin | ||
| 750 | { my ($self, $path, $node, $type, $def, $check_values) = @_; | ||
| 751 | my $check = $check_values ? $def->{check} : undef; | ||
| 752 | my $err = $path eq $type | ||
| 753 | ? N__"illegal value `{value}' for type {type}" | ||
| 754 | : N__"illegal value `{value}' for type {type} at {path}"; | ||
| 755 | |||
| 756 | my $format = $def->{format}; | ||
| 757 | my $trans = $self->{prefixes}; | ||
| 758 | |||
| 759 | $check | ||
| 760 | ? ( defined $format | ||
| 761 | ? sub { defined $_[1] or return undef; | ||
| 762 | my $value = $format->($_[1], $trans, $path); | ||
| 763 | return $value if defined $value && $check->($value); | ||
| 764 | error __x$err, value => $value, type => $type, path => $path; | ||
| 765 | } | ||
| 766 | : sub { return $_[1] if !defined $_[1] || $check->($_[1]); | ||
| 767 | error __x$err, value => $_[1], type => $type, path => $path; | ||
| 768 | } | ||
| 769 | ) | ||
| 770 | : ( defined $format | ||
| 771 | ? sub { defined $_[1] ? $format->($_[1], $trans, $path) : undef } | ||
| 772 | : sub { $_[1] } | ||
| 773 | ); | ||
| 774 | } | ||
| 775 | |||
| 776 | # simpleType | ||
| 777 | |||
| 778 | sub makeList | ||
| 779 | { my ($self, $path, $st) = @_; | ||
| 780 | sub { my ($doc, $v) = @_; | ||
| 781 | defined $v or return undef; | ||
| 782 | join ' ', grep defined, map {$st->($doc, $_)} | ||
| 783 | ref $v eq 'ARRAY' ? @$v : $v; | ||
| 784 | }; | ||
| 785 | } | ||
| 786 | |||
| 787 | sub makeFacetsList | ||
| 788 | { my ($self, $path, $st, $info, $early, $late) = @_; | ||
| 789 | my @e = grep defined, @$early; | ||
| 790 | my @l = grep defined, @$late; | ||
| 791 | sub { my ($doc, $v) = @_; | ||
| 792 | defined $v or return undef; | ||
| 793 | $_->($v) for @l; | ||
| 794 | my $list = join ' ', map $st->($doc, $_), ref $v eq 'ARRAY' ? @$v : $v; | ||
| 795 | defined $list && length $list or return; | ||
| 796 | do { $list = $_->($list) } for @e; | ||
| 797 | $list; | ||
| 798 | } | ||
| 799 | } | ||
| 800 | |||
| 801 | sub makeFacets | ||
| 802 | { my ($self, $path, $st, $info, $early, $late) = @_; | ||
| 803 | @$early || @$late or return $st; | ||
| 804 | sub { my ($doc, $v) = @_; | ||
| 805 | defined $v or return undef; | ||
| 806 | $v = $_->($v) for @$late; | ||
| 807 | $v = $st->($doc, $v); | ||
| 808 | defined $v or return undef; | ||
| 809 | $v = $_->($v) for @$early; | ||
| 810 | $v; | ||
| 811 | }; | ||
| 812 | } | ||
| 813 | |||
| 814 | sub makeUnion | ||
| 815 | { my ($self, $path, @types) = @_; | ||
| 816 | sub { my ($doc, $value) = @_; | ||
| 817 | defined $value or return undef; | ||
| 818 | for(@types) {my $v = try { $_->($doc, $value) }; $@ or return $v } | ||
| 819 | |||
| 820 | substr $value, 20, -5, '...' if length($value) > 50; | ||
| 821 | error __x"no match for `{text}' in union at {path}" | ||
| 822 | , text => $value, path => $path; | ||
| 823 | }; | ||
| 824 | } | ||
| 825 | |||
| 826 | sub makeSubstgroup | ||
| 827 | { my ($self, $path, $type, %done) = @_; | ||
| 828 | |||
| 829 | keys %done or return bless sub { () }, 'BLOCK'; | ||
| 830 | my %do = map { @$_ } values %done; | ||
| 831 | |||
| 832 | bless | ||
| 833 | sub { my ($doc, $values) = @_; | ||
| 834 | #warn "SUBST($type) AVAILABLE ARE ", join ', ', keys %do; | ||
| 835 | foreach my $take (keys %do) | ||
| 836 | { my $subst = delete $values->{$take}; | ||
| 837 | defined $subst or next; | ||
| 838 | |||
| 839 | return $do{$take}->($doc, $subst); | ||
| 840 | } | ||
| 841 | (); | ||
| 842 | }, 'BLOCK'; | ||
| 843 | } | ||
| 844 | |||
| 845 | # Attributes | ||
| 846 | |||
| 847 | sub makeAttributeRequired | ||
| 848 | { my ($self, $path, $ns, $tag, $label, $do) = @_; | ||
| 849 | |||
| 850 | sub { my $value = $do->(@_); | ||
| 851 | return $_[0]->createAttribute($tag, $value) | ||
| 852 | if defined $value; | ||
| 853 | |||
| 854 | error __x"attribute `{tag}' is required at {path}" | ||
| 855 | , tag => $tag, path => $path; | ||
| 856 | }; | ||
| 857 | } | ||
| 858 | |||
| 859 | sub makeAttributeProhibited | ||
| 860 | { my ($self, $path, $ns, $tag, $label, $do) = @_; | ||
| 861 | |||
| 862 | sub { my $value = $do->(@_); | ||
| 863 | defined $value or return (); | ||
| 864 | |||
| 865 | error __x"attribute `{tag}' is prohibited at {path}" | ||
| 866 | , tag => $tag, path => $path; | ||
| 867 | }; | ||
| 868 | } | ||
| 869 | |||
| 870 | sub makeAttribute | ||
| 871 | { my ($self, $path, $ns, $tag, $label, $do) = @_; | ||
| 872 | sub { my $value = $do->(@_); | ||
| 873 | defined $value ? $_[0]->createAttribute($tag, $value) : (); | ||
| 874 | }; | ||
| 875 | } | ||
| 876 | |||
| 877 | sub makeAttributeDefault | ||
| 878 | { my ($self, $path, $ns, $tag, $label, $do, $default_node) = @_; | ||
| 879 | |||
| 880 | my $mode = $self->{default_values}; | ||
| 881 | $mode eq 'IGNORE' | ||
| 882 | and return sub | ||
| 883 | { my $value = $do->(@_); | ||
| 884 | defined $value ? $_[0]->createAttribute($tag, $value) : (); | ||
| 885 | }; | ||
| 886 | |||
| 887 | my $default = $default_node->value; | ||
| 888 | $mode eq 'EXTEND' | ||
| 889 | and return sub | ||
| 890 | { my $value = $do->(@_); | ||
| 891 | defined $value or $value = $default; | ||
| 892 | $_[0]->createAttribute($tag, $value); | ||
| 893 | }; | ||
| 894 | |||
| 895 | $mode eq 'MINIMAL' | ||
| 896 | and return sub | ||
| 897 | { my $value = $do->(@_); | ||
| 898 | return () if defined $value && $value eq $default; | ||
| 899 | $_[0]->createAttribute($tag, $value); | ||
| 900 | }; | ||
| 901 | |||
| 902 | error __x"illegal default_values mode `{mode}'", mode => $mode; | ||
| 903 | } | ||
| 904 | |||
| 905 | sub makeAttributeFixed | ||
| 906 | { my ($self, $path, $ns, $tag, $label, $do, $fixed) = @_; | ||
| 907 | $fixed = $fixed->value if ref $fixed; | ||
| 908 | |||
| 909 | sub { my ($doc, $value) = @_; | ||
| 910 | defined $value or return (); | ||
| 911 | |||
| 912 | $value eq $fixed | ||
| 913 | or error __x"value of attribute `{tag}' is fixed to `{fixed}', not `{got}' at {path}" | ||
| 914 | , tag => $tag, got => $value, fixed => $fixed, path => $path; | ||
| 915 | |||
| 916 | $doc->createAttribute($tag, $fixed); | ||
| 917 | }; | ||
| 918 | } | ||
| 919 | |||
| 920 | # any | ||
| 921 | |||
| 922 | sub _split_any_list($$$) | ||
| 923 | { my ($path, $type, $v) = @_; | ||
| 924 | my @nodes = ref $v eq 'ARRAY' ? @$v : defined $v ? $v : return ([], []); | ||
| 925 | my (@attrs, @elems); | ||
| 926 | |||
| 927 | foreach my $node (@nodes) | ||
| 928 | { UNIVERSAL::isa($node, 'XML::LibXML::Node') | ||
| 929 | or error __x"elements for 'any' are XML::LibXML nodes, not {string} at {path}" | ||
| 930 | , string => $node, path => $path; | ||
| 931 | |||
| 932 | if($node->isa('XML::LibXML::Attr')) | ||
| 933 | { push @attrs, $node; | ||
| 934 | next; | ||
| 935 | } | ||
| 936 | |||
| 937 | if($node->isa('XML::LibXML::Element')) | ||
| 938 | { push @elems, $node; | ||
| 939 | next; | ||
| 940 | } | ||
| 941 | |||
| 942 | error __x"an XML::LibXML::Element or ::Attr is expected as 'any' or 'anyAttribute value with {type}, but a {kind} was found at {path}" | ||
| 943 | , type => $type, kind => ref $node, path => $path; | ||
| 944 | } | ||
| 945 | |||
| 946 | return (\@attrs, \@elems); | ||
| 947 | } | ||
| 948 | |||
| 949 | sub makeAnyAttribute | ||
| 950 | { my ($self, $path, $handler, $yes, $no, $process) = @_; | ||
| 951 | my %yes = map +($_ => 1), @{$yes || []}; | ||
| 952 | my %no = map +($_ => 1), @{$no || []}; | ||
| 953 | my $prefs = $self->{prefixes}; | ||
| 954 | |||
| 955 | weaken $self; | ||
| 956 | |||
| 957 | bless | ||
| 958 | sub { my ($doc, $values) = @_; | ||
| 959 | |||
| 960 | my @res; | ||
| 961 | foreach my $label (sort keys %$values) | ||
| 962 | { my ($type, $ns, $local); | ||
| 963 | if(substr($label, 0, 1) eq '{') | ||
| 964 | { ($ns, $local) = unpack_type $label; | ||
| 965 | $type = $label; | ||
| 966 | } | ||
| 967 | elsif(index($label, ':') >= 0) | ||
| 968 | { (my $prefix, $local) = split ':', $label, 2; | ||
| 969 | my $match = first {$_->{prefix} eq $prefix} values %$prefs; | ||
| 970 | my $ns = $match ? $match->{uri} : undef; | ||
| 971 | $type = pack_type $ns, $local; | ||
| 972 | } | ||
| 973 | else {next} # not fully qualified, not an 'any' | ||
| 974 | |||
| 975 | $yes{$ns} or next if keys %yes; | ||
| 976 | $no{$ns} and next if keys %no; | ||
| 977 | |||
| 978 | my $value = delete $values->{$label} or next; | ||
| 979 | my ($attrs, $elems) = _split_any_list $path, $type, $value; | ||
| 980 | |||
| 981 | $values->{$type} = $elems if @$elems; | ||
| 982 | @$attrs or next; | ||
| 983 | |||
| 984 | foreach my $node (@$attrs) | ||
| 985 | { my $nodetype = type_of_node $node; | ||
| 986 | next if $nodetype eq $type; | ||
| 987 | |||
| 988 | error __x"provided 'anyAttribute' node has type {type}, but labeled with {other} at {path}" | ||
| 989 | , type => $nodetype, other => $type, path => $path | ||
| 990 | } | ||
| 991 | |||
| 992 | push @res, @$attrs; | ||
| 993 | } | ||
| 994 | @res; | ||
| 995 | }, 'ANY'; | ||
| 996 | } | ||
| 997 | |||
| 998 | sub makeAnyElement | ||
| 999 | { my ($self, $path, $handler, $yes, $no, $process, $min, $max) = @_; | ||
| 1000 | my %yes = map +($_ => 1), @{$yes || []}; | ||
| 1001 | my %no = map +($_ => 1), @{$no || []}; | ||
| 1002 | my $prefs = $self->{prefixes}; | ||
| 1003 | |||
| 1004 | $handler ||= 'SKIP_ALL'; | ||
| 1005 | weaken $self; | ||
| 1006 | |||
| 1007 | bless | ||
| 1008 | sub { my ($doc, $values) = @_; | ||
| 1009 | my @res; | ||
| 1010 | |||
| 1011 | foreach my $label (sort keys %$values) | ||
| 1012 | { my ($type, $ns, $local); | ||
| 1013 | if(substr($label, 0, 1) eq '{') | ||
| 1014 | { ($ns, $local) = unpack_type $label; | ||
| 1015 | $type = $label; | ||
| 1016 | } | ||
| 1017 | elsif(index($label, ':') >= 0) | ||
| 1018 | { (my $prefix, $local) = split ':', $label, 2; | ||
| 1019 | my $match = first {$_->{prefix} eq $prefix} values %$prefs; | ||
| 1020 | $ns = $match ? $match->{uri} : undef; | ||
| 1021 | $type = pack_type $ns, $local; | ||
| 1022 | } | ||
| 1023 | else {next} # not fully qualified, not an 'any' | ||
| 1024 | |||
| 1025 | $yes{$ns} or next if keys %yes; | ||
| 1026 | $no{$ns} and next if keys %no; | ||
| 1027 | |||
| 1028 | my $value = delete $values->{$label} or next; | ||
| 1029 | my ($attrs, $elems) = _split_any_list $path, $type, $value; | ||
| 1030 | |||
| 1031 | $values->{$type} = $attrs if @$attrs; | ||
| 1032 | @$elems or next; | ||
| 1033 | |||
| 1034 | foreach my $node (@$elems) | ||
| 1035 | { my $nodens = $node->namespaceURI; | ||
| 1036 | defined $nodens or next; # see README.todo work-around | ||
| 1037 | |||
| 1038 | my $nodetype = type_of_node $node; | ||
| 1039 | next if $nodetype eq $type; | ||
| 1040 | |||
| 1041 | error __x"provided 'any' element node has type {type}, but labeled with {other} at {path}" | ||
| 1042 | , type => $nodetype, other => $type, path => $path | ||
| 1043 | } | ||
| 1044 | |||
| 1045 | push @res, @$elems; | ||
| 1046 | $max eq 'unbounded' || @res <= $max | ||
| 1047 | or error __x"too many 'any' elements after consuming {count} nodes of {type}, max {max} at {path}" | ||
| 1048 | , count => scalar @$elems, type => $type | ||
| 1049 | , max => $max, path => $path; | ||
| 1050 | } | ||
| 1051 | |||
| 1052 | @res >= $min | ||
| 1053 | or error __x"too few 'any' elements, got {count} for minimum {min} at {path}" | ||
| 1054 | , count => scalar @res, min => $min, path => $path; | ||
| 1055 | |||
| 1056 | @res ? @res : undef; # empty, then "0 but true" | ||
| 1057 | }, 'ANY'; | ||
| 1058 | } | ||
| 1059 | |||
| 1060 | # xsi:type handling | ||
| 1061 | |||
| 1062 | sub makeXsiTypeSwitch($$$$) | ||
| 1063 | { my ($self, $where, $elem, $default_type, $types) = @_; | ||
| 1064 | my $xsi = $self->_registerNSprefix(xsi => SCHEMA2001i, 1) . ':type'; | ||
| 1065 | my %types; | ||
| 1066 | foreach my $type (sort keys %$types) | ||
| 1067 | { my ($ns, $local) = unpack_type $type; | ||
| 1068 | my $tag = $self->makeTagQualified($where, undef, $local, $ns); | ||
| 1069 | |||
| 1070 | # register code under both prefixed and full type name | ||
| 1071 | $types{$self->prefixed($type)} = $types{$type} = [$tag,$types->{$type}]; | ||
| 1072 | } | ||
| 1073 | |||
| 1074 | sub { | ||
| 1075 | my ($doc, $values) = @_; | ||
| 1076 | ref $values eq 'HASH' && $values->{XSI_TYPE} | ||
| 1077 | or return $types{$default_type}[1]->(@_); | ||
| 1078 | |||
| 1079 | my %v = %$values; | ||
| 1080 | my $type = delete $v{XSI_TYPE}; | ||
| 1081 | my $def = $types{$type} | ||
| 1082 | or error __x"specified xsi:type list for `{default}' does not contain `{got}'" | ||
| 1083 | , default => $default_type, got => $type; | ||
| 1084 | |||
| 1085 | my ($t, $code) = @$def; | ||
| 1086 | my $node = $code->($doc, \%v) | ||
| 1087 | or return; | ||
| 1088 | $node->setAttribute($xsi, $t); | ||
| 1089 | $node; | ||
| 1090 | }; | ||
| 1091 | } | ||
| 1092 | |||
| 1093 | sub makeHook($$$$$$$) | ||
| 1094 | { my ($self, $path, $r, $tag, $before, $replace, $after, $fulltype) = @_; | ||
| 1095 | return $r unless $before || $replace || $after; | ||
| 1096 | |||
| 1097 | my $do_replace; | ||
| 1098 | if($replace) | ||
| 1099 | { return sub {()} if grep $_ eq 'SKIP', @$replace; | ||
| 1100 | |||
| 1101 | # Input for replace is Perl, output is XML... so we cannot stack them | ||
| 1102 | error __x"writer only supports one replace hook (for {type})" | ||
| 1103 | , type => $fulltype | ||
| 1104 | if @$replace > 1; | ||
| 1105 | |||
| 1106 | $do_replace = $self->_decodeReplace($path, $replace->[0]); | ||
| 1107 | } | ||
| 1108 | |||
| 1109 | my @do_before = $before ? map $self->_decodeBefore($path,$_), @$before :(); | ||
| 1110 | my @do_after = $after ? map $self->_decodeAfter($path,$_), @$after :(); | ||
| 1111 | |||
| 1112 | sub | ||
| 1113 | { my ($doc, $val) = @_; | ||
| 1114 | defined $val or return; | ||
| 1115 | foreach (@do_before) | ||
| 1116 | { $val = $_->($doc, $val, $path, $fulltype); | ||
| 1117 | defined $val or return (); | ||
| 1118 | } | ||
| 1119 | |||
| 1120 | my $xml = $do_replace | ||
| 1121 | ? $do_replace->($doc, $val, $path, $tag, $r, $fulltype) | ||
| 1122 | : $r->($doc, $val); | ||
| 1123 | defined $xml or return (); | ||
| 1124 | |||
| 1125 | foreach (@do_after) | ||
| 1126 | { $xml = $_->($doc, $xml, $path, $val, $fulltype); | ||
| 1127 | defined $xml or return (); | ||
| 1128 | } | ||
| 1129 | |||
| 1130 | $xml; | ||
| 1131 | }; | ||
| 1132 | } | ||
| 1133 | |||
| 1134 | sub _decodeBefore($$) | ||
| 1135 | { my ($self, $path, $call) = @_; | ||
| 1136 | return $call if ref $call eq 'CODE'; | ||
| 1137 | |||
| 1138 | # $code->($doc, $values, $path) | ||
| 1139 | $call eq 'PRINT_PATH' ? sub { print "$_[2]\n"; $_[1] } | ||
| 1140 | : error __x"labeled before hook `{name}' undefined for WRITER", name=>$call; | ||
| 1141 | } | ||
| 1142 | |||
| 1143 | sub _decodeReplace($$) | ||
| 1144 | { my ($self, $path, $call) = @_; | ||
| 1145 | return $call if ref $call eq 'CODE'; | ||
| 1146 | |||
| 1147 | # SKIP already handled | ||
| 1148 | # $replace->($doc, $val, $path, $tag, $replaced) | ||
| 1149 | error __x"labeled replace hook `{name}' undefined for WRITER", name=>$call; | ||
| 1150 | } | ||
| 1151 | |||
| 1152 | sub _decodeAfter($$) | ||
| 1153 | { my ($self, $path, $call) = @_; | ||
| 1154 | return $call if ref $call eq 'CODE'; | ||
| 1155 | |||
| 1156 | $call eq 'PRINT_PATH' ? sub { print "$_[2]\n"; $_[1] } | ||
| 1157 | : error __x"labeled after hook `{name}' undefined for WRITER", name=>$call; | ||
| 1158 | } | ||
| 1159 | |||
| 1160 | sub makeBlocked($$$) | ||
| 1161 | { my ($self, $where, $class, $type) = @_; | ||
| 1162 | my $err_type = $self->prefixed($type); | ||
| 1163 | |||
| 1164 | # errors are produced in class=misfit to allow other choices to succeed. | ||
| 1165 | $class eq 'anyType' | ||
| 1166 | ? { st => sub { error __x"use of `{type}' blocked at {where}" | ||
| 1167 | , type => $err_type, where => $where, _class => 'misfit'; | ||
| 1168 | }} | ||
| 1169 | : $class eq 'simpleType' | ||
| 1170 | ? { st => sub { error __x"use of {class} `{type}' blocked at {where}" | ||
| 1171 | , class => $class, type => $err_type, where => $where | ||
| 1172 | , _class => 'misfit'; | ||
| 1173 | }} | ||
| 1174 | : $class eq 'complexType' | ||
| 1175 | ? { elems => [] } | ||
| 1176 | : $class eq 'ref' | ||
| 1177 | ? { st => sub { error __x"use of referenced `{type}' blocked at {where}" | ||
| 1178 | , type => $err_type, where => $where, _class => 'misfit'; | ||
| 1179 | }} | ||
| 1180 | : panic "blocking of $class for $type not implemented"; | ||
| 1181 | } | ||
| 1182 | |||
| 1183 | sub addTypeAttribute($$) | ||
| 1184 | { my ($self, $type, $do) = @_; | ||
| 1185 | my $xsi = $self->_registerNSprefix(xsi => SCHEMA2001i, 1) . ':type'; | ||
| 1186 | my $xsd = $self->_registerNSprefix(xsd => SCHEMA2001, 1); | ||
| 1187 | my $typed = $self->prefixed($type); | ||
| 1188 | |||
| 1189 | sub { | ||
| 1190 | my $r = $do->(@_); | ||
| 1191 | $type && $r && UNIVERSAL::isa($r, 'XML::LibXML::Element') or return $r; | ||
| 1192 | return $r if $r->getAttributeNS(SCHEMA2001i, 'type'); | ||
| 1193 | $r->setAttribute($xsi, $typed); | ||
| 1194 | $r; | ||
| 1195 | }; | ||
| 1196 | } | ||
| 1197 | |||
| 1198 | #------------ | ||
| 1199 | |||
| 1200 | 1; |