| 1 | # Copyrights 2007-2021 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-SOAP. Meta-POD processed | ||
| 6 | # with 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::SOAP11; | ||
| 10 | use vars '$VERSION'; | ||
| 11 | $VERSION = '3.27'; | ||
| 12 | #!!! | ||
| 13 | |||
| 14 | use warnings; | ||
| 15 | use strict; | ||
| 16 | |||
| 17 | use Log::Report 'xml-compile-soap'; | ||
| 18 | |||
| 19 | use List::Util qw/first/; | ||
| 20 | use Scalar::Util qw/reftype/; | ||
| 21 | use XML::Compile::Util | ||
| 22 | qw/odd_elements SCHEMA2001 SCHEMA2001i pack_type unpack_type type_of_node/; | ||
| 23 | use XML::Compile::SOAP::Util qw/:soap11 WSDL11/; | ||
| 24 | |||
| 25 | my $simplify; | ||
| 26 | |||
| 27 | sub XML::Compile::SOAP11::Encoding::import(@) #!!! | ||
| 28 | { my ($class, %args) = @_; | ||
| 29 | $simplify = $args{simplify}; | ||
| 30 | } | ||
| 31 | |||
| 32 | |||
| 33 | sub _initRpcEnc11($$) | ||
| 34 | { my ($self, $schemas, $xsddir) = @_; | ||
| 35 | |||
| 36 | $schemas->addPrefixes('SOAP-ENC' => SOAP11ENC); | ||
| 37 | $schemas->importDefinitions("$xsddir/soap-encoding.xsd"); | ||
| 38 | |||
| 39 | $schemas->addCompileOptions( 'READERS' | ||
| 40 | , anyElement => 'TAKE_ALL' | ||
| 41 | , anyAttribute => 'TAKE_ALL' | ||
| 42 | , permit_href => 1 | ||
| 43 | ); | ||
| 44 | |||
| 45 | # this will keep the soap11 compile object alive after compilation | ||
| 46 | $schemas->addHook | ||
| 47 | ( action => 'READER' | ||
| 48 | , extends => 'SOAP-ENC:Array' | ||
| 49 | , replace => sub { $self->_dec_array_hook(@_) } | ||
| 50 | ); | ||
| 51 | |||
| 52 | $schemas->addHook | ||
| 53 | ( action => 'WRITER' | ||
| 54 | , extends => 'SOAP-ENC:Array' | ||
| 55 | , replace => sub { $self->_enc_array_hook(@_) } | ||
| 56 | ); | ||
| 57 | |||
| 58 | $self; | ||
| 59 | } | ||
| 60 | |||
| 61 | sub _reader_body_rpcenc_wrapper($$) | ||
| 62 | { my ($self, $procedure, $body) = @_; | ||
| 63 | my %trans = map +($_->[1] => [ $_->[0], $_->[2] ]), @$body; | ||
| 64 | |||
| 65 | # this should use key_rewrite, but there is no $wsdl here | ||
| 66 | # my $label = $wsdl->prefixed($procedure); | ||
| 67 | my $label = (unpack_type $procedure)[1]; | ||
| 68 | |||
| 69 | my $code = sub | ||
| 70 | { my $opnode = shift or return {}; | ||
| 71 | my @nodes = $opnode->childNodes; | ||
| 72 | my $parent = $opnode->parentNode; # href'd sometimes a level up | ||
| 73 | push @nodes, grep $_ ne $opnode, $parent->childNodes | ||
| 74 | if $parent; | ||
| 75 | |||
| 76 | $self->rpcDecode(@nodes); | ||
| 77 | }; | ||
| 78 | |||
| 79 | [ [ $label => $procedure => $code ] ]; | ||
| 80 | } | ||
| 81 | |||
| 82 | sub _writer_body_rpcenc_hook($$$$$) | ||
| 83 | { my ($self, $type, $procedure, $params, $faults) = @_; | ||
| 84 | $self->_writer_body_rpclit_hook($type, $procedure, $params, $faults); | ||
| 85 | } | ||
| 86 | |||
| 87 | #------------------ | ||
| 88 | |||
| 89 | |||
| 90 | sub startEncoding(%) | ||
| 91 | { my ($self, %args) = @_; | ||
| 92 | my $doc = $args{doc} || XML::LibXML::Document->new('1.0', 'UTF-8'); | ||
| 93 | $self->{enc} = {doc => $doc}; | ||
| 94 | $self; | ||
| 95 | } | ||
| 96 | |||
| 97 | # Currently only support 1-dim arrays | ||
| 98 | |||
| 99 | sub _enc_array_hook(@) | ||
| 100 | { my ($client, $doc, $val, $path, $tag, $r, $fulltype) = @_; | ||
| 101 | my $schema = $client->schemas; | ||
| 102 | my $nss = $schema->namespaces; | ||
| 103 | |||
| 104 | my $elem = $doc->createElement($tag); | ||
| 105 | my $encns = $schema->prefixFor(SOAP11ENC); | ||
| 106 | $elem->setAttribute('xsi:type' => "$encns:Array"); | ||
| 107 | |||
| 108 | my ($label, $items) = %$val; | ||
| 109 | my @items = ref $items eq 'ARRAY' ? @$items : $items; | ||
| 110 | |||
| 111 | my $def = $nss->find(complexType => $fulltype) | ||
| 112 | or error __x"cannot find {type} in rpc array writer hook" | ||
| 113 | , type => $fulltype; | ||
| 114 | my $defnode= $def->{node}; | ||
| 115 | my $xpc = XML::LibXML::XPathContext->new; | ||
| 116 | $xpc->registerNs(wsdl => WSDL11); | ||
| 117 | my ($atattr) = $xpc->findnodes('.//@wsdl:arrayType', $def->{node}); | ||
| 118 | |||
| 119 | my $qname = $atattr->value; | ||
| 120 | $qname =~ s/\[.*//; # strip array notation | ||
| 121 | |||
| 122 | my ($pref,$local) = $qname =~ /\:/ ? (split /\:/,$qname,2) : ('',$qname); | ||
| 123 | my $ns = $atattr->lookupNamespaceURI($pref); | ||
| 124 | my $eltype = pack_type $ns, $local; | ||
| 125 | |||
| 126 | my @nodes; | ||
| 127 | if($nss->find(element => $eltype)) | ||
| 128 | { my $w = $schema->writer($eltype); | ||
| 129 | @nodes = map $w->($doc, $_), @items; | ||
| 130 | } | ||
| 131 | else # type | ||
| 132 | { my $w = $schema->writer($eltype, is_type => 1, element => $label); | ||
| 133 | @nodes = map $w->($doc, $_), @items; | ||
| 134 | } | ||
| 135 | |||
| 136 | $elem->appendChild($_) for @nodes; | ||
| 137 | $elem->setAttribute("$encns:arrayType" => $qname.'['.@nodes.']'); | ||
| 138 | $elem; | ||
| 139 | } | ||
| 140 | |||
| 141 | |||
| 142 | sub prefixed($;$) | ||
| 143 | { my $self = shift; | ||
| 144 | $self->schemas->prefixed(@_); | ||
| 145 | } | ||
| 146 | |||
| 147 | |||
| 148 | sub enc($$$) | ||
| 149 | { my ($self, $local, $value, $id) = @_; | ||
| 150 | my $type = pack_type SOAP11ENC, $local; | ||
| 151 | $self->schemas->writer($type, include_namespaces => 0) | ||
| 152 | ->($self->{enc}{doc}, {_ => $value, id => $id} ); | ||
| 153 | } | ||
| 154 | |||
| 155 | |||
| 156 | sub typed($$$) | ||
| 157 | { my ($self, $type, $name, $value) = @_; | ||
| 158 | |||
| 159 | my $showtype; | ||
| 160 | if($type =~ s/^\{\}//) | ||
| 161 | { $showtype = $type; | ||
| 162 | } | ||
| 163 | else | ||
| 164 | { my ($tns, $tlocal) = unpack_type $type; | ||
| 165 | unless(length $tns) | ||
| 166 | { $tns = SCHEMA2001; | ||
| 167 | $type = pack_type $tns, $tlocal; | ||
| 168 | } | ||
| 169 | $showtype = $self->prefixed($tns, $tlocal); | ||
| 170 | } | ||
| 171 | |||
| 172 | my $el = $self->element($type, $name, $value); | ||
| 173 | my $typedef = $self->prefixed(SCHEMA2001i, 'type'); | ||
| 174 | $el->setAttribute($typedef, $showtype); | ||
| 175 | $el; | ||
| 176 | } | ||
| 177 | |||
| 178 | |||
| 179 | sub struct($@) | ||
| 180 | { my ($self, $type, @childs) = @_; | ||
| 181 | my $typedef = $self->prefixed($type); | ||
| 182 | my $doc = $self->{enc}{doc}; | ||
| 183 | my $struct = $doc->createElement($typedef); | ||
| 184 | $struct->addChild($_) for @childs; | ||
| 185 | $struct; | ||
| 186 | } | ||
| 187 | |||
| 188 | |||
| 189 | sub element($$$) | ||
| 190 | { my ($self, $type, $name, $value) = @_; | ||
| 191 | |||
| 192 | return $value | ||
| 193 | if UNIVERSAL::isa($value, 'XML::LibXML::Element'); | ||
| 194 | |||
| 195 | $type = $self->prefixed(SCHEMA2001, $type) | ||
| 196 | if $type !~ m/^\{|\:/; | ||
| 197 | |||
| 198 | my $doc = $self->{enc}{doc}; | ||
| 199 | my $el = $doc->createElement($name); | ||
| 200 | my $child = $self->schemas->writer($type, include_namespaces => 0) | ||
| 201 | ->($doc, $value); | ||
| 202 | $el->addChild($child) if $child; | ||
| 203 | $el; | ||
| 204 | } | ||
| 205 | |||
| 206 | |||
| 207 | my $id_count = 0; | ||
| 208 | sub href($$$) | ||
| 209 | { my ($self, $name, $to, $prefid) = @_; | ||
| 210 | my $id = $to->getAttribute('id'); | ||
| 211 | unless(defined $id) | ||
| 212 | { $id = defined $prefid ? $prefid : 'id-'.++$id_count; | ||
| 213 | $to->setAttribute(id => $id); | ||
| 214 | } | ||
| 215 | |||
| 216 | my $ename = $self->prefixed($name); | ||
| 217 | my $el = $self->{enc}{doc}->createElement($ename); | ||
| 218 | $el->setAttribute(href => "#$id"); | ||
| 219 | $el; | ||
| 220 | } | ||
| 221 | |||
| 222 | |||
| 223 | sub nil($;$) | ||
| 224 | { my $self = shift; | ||
| 225 | my ($type, $name) = @_==2 ? @_ : (undef, $_[0]); | ||
| 226 | my ($ns, $local) = unpack_type $name; | ||
| 227 | |||
| 228 | my $doc = $self->{enc}{doc}; | ||
| 229 | my $el = $ns | ||
| 230 | ? $doc->createElementNS($ns, $local) | ||
| 231 | : $doc->createElement($local); | ||
| 232 | |||
| 233 | $el->setAttribute($self->prefixed(SCHEMA2001i, 'nil'), 'true'); | ||
| 234 | $el->setAttribute($self->prefixed(SCHEMA2001i, 'type') | ||
| 235 | , $self->prefixed($type)) if $type; | ||
| 236 | |||
| 237 | $el; | ||
| 238 | } | ||
| 239 | |||
| 240 | |||
| 241 | sub array($$$@) | ||
| 242 | { my ($self, $name, $itemtype, $array, %opts) = @_; | ||
| 243 | |||
| 244 | my $enc = $self->{enc}; | ||
| 245 | my $doc = $enc->{doc}; | ||
| 246 | |||
| 247 | my $offset = $opts{offset} || 0; | ||
| 248 | my $slice = $opts{slice}; | ||
| 249 | |||
| 250 | my ($min, $size) = ($offset, scalar @$array); | ||
| 251 | $min++ while $min <= $size && !defined $array->[$min]; | ||
| 252 | |||
| 253 | my $max = defined $slice && $min+$slice-1 < $size ? $min+$slice-1 : $size; | ||
| 254 | $max-- while $min <= $max && !defined $array->[$max]; | ||
| 255 | |||
| 256 | my $sparse = 0; | ||
| 257 | for(my $i = $min; $i < $max; $i++) | ||
| 258 | { next if defined $array->[$i]; | ||
| 259 | $sparse = 1; | ||
| 260 | last; | ||
| 261 | } | ||
| 262 | |||
| 263 | my $elname = $self->prefixed(defined $name ? $name : (SOAP11ENC, 'Array')); | ||
| 264 | my $el = $doc->createElement($elname); | ||
| 265 | my $nested = $opts{nested_array} || ''; | ||
| 266 | my $type = $self->prefixed($itemtype)."$nested\[$size]"; | ||
| 267 | |||
| 268 | $el->setAttribute(id => $opts{id}) if defined $opts{id}; | ||
| 269 | my $at = $opts{array_type} ? $opts{arrayType} | ||
| 270 | : $self->prefixed(SOAP11ENC, 'arrayType'); | ||
| 271 | $el->setAttribute($at, $type) if defined $at; | ||
| 272 | |||
| 273 | if($sparse) | ||
| 274 | { my $placeition = $self->prefixed(SOAP11ENC, 'position'); | ||
| 275 | for(my $r = $min; $r <= $max; $r++) | ||
| 276 | { my $row = $array->[$r] or next; | ||
| 277 | my $node = $row->cloneNode(1); | ||
| 278 | $node->setAttribute($placeition, "[$r]"); | ||
| 279 | $el->addChild($node); | ||
| 280 | } | ||
| 281 | } | ||
| 282 | else | ||
| 283 | { $el->setAttribute($self->prefixed(SOAP11ENC, 'offset'), "[$min]") | ||
| 284 | if $min > 0; | ||
| 285 | $el->addChild($array->[$_]) for $min..$max; | ||
| 286 | } | ||
| 287 | |||
| 288 | $el; | ||
| 289 | } | ||
| 290 | |||
| 291 | |||
| 292 | sub multidim($$$@) | ||
| 293 | { my ($self, $name, $itemtype, $array, %opts) = @_; | ||
| 294 | my $enc = $self->{enc}; | ||
| 295 | my $doc = $enc->{doc}; | ||
| 296 | |||
| 297 | # determine dimensions | ||
| 298 | my @dims; | ||
| 299 | for(my $dim = $array; ref $dim eq 'ARRAY'; $dim = $dim->[0]) | ||
| 300 | { push @dims, scalar @$dim; | ||
| 301 | } | ||
| 302 | |||
| 303 | my $sparse = $self->_check_multidim($array, \@dims, ''); | ||
| 304 | my $elname = $self->prefixed(defined $name ? $name : (SOAP11ENC, 'Array')); | ||
| 305 | my $el = $doc->createElement($elname); | ||
| 306 | my $type = $self->prefixed($itemtype) . '['.join(',', @dims).']'; | ||
| 307 | |||
| 308 | $el->setAttribute(id => $opts{id}) if defined $opts{id}; | ||
| 309 | $el->setAttribute($self->prefixed(SOAP11ENC, 'arrayType'), $type); | ||
| 310 | |||
| 311 | my @data = $self->_flatten_multidim($array, \@dims, ''); | ||
| 312 | if($sparse) | ||
| 313 | { my $placeition = $self->prefixed(SOAP11ENC, 'position'); | ||
| 314 | while(@data) | ||
| 315 | { my ($place, $field) = (shift @data, shift @data); | ||
| 316 | my $node = $field->cloneNode(1); | ||
| 317 | $node->setAttribute($placeition, "[$place]"); | ||
| 318 | $el->addChild($node); | ||
| 319 | } | ||
| 320 | } | ||
| 321 | else | ||
| 322 | { $el->addChild($_) for odd_elements @data; | ||
| 323 | } | ||
| 324 | |||
| 325 | $el; | ||
| 326 | } | ||
| 327 | |||
| 328 | sub _check_multidim($$$) | ||
| 329 | { my ($self, $array, $dims, $loc) = @_; | ||
| 330 | my @dims = @$dims; | ||
| 331 | |||
| 332 | my $expected = shift @dims; | ||
| 333 | @$array <= $expected | ||
| 334 | or error __x"dimension at ({location}) is {size}, larger than size {expect} of first row" | ||
| 335 | , location => $loc, size => scalar(@$array), expect => $expected; | ||
| 336 | |||
| 337 | my $sparse = 0; | ||
| 338 | foreach (my $x = 0; $x < $expected; $x++) | ||
| 339 | { my $el = $array->[$x]; | ||
| 340 | my $cell = length $loc ? "$loc,$x" : $x; | ||
| 341 | |||
| 342 | if(!defined $el) { $sparse++ } | ||
| 343 | elsif(@dims==0) # bottom level | ||
| 344 | { UNIVERSAL::isa($el, 'XML::LibXML::Element') | ||
| 345 | or error __x"array element at ({location}) shall be a XML element or undef, is {value}" | ||
| 346 | , location => $cell, value => $el; | ||
| 347 | } | ||
| 348 | elsif(ref $el eq 'ARRAY') | ||
| 349 | { $sparse += $self->_check_multidim($el, \@dims, $cell); | ||
| 350 | } | ||
| 351 | else | ||
| 352 | { error __x"array at ({location}) expects ARRAY reference, is {value}" | ||
| 353 | , location => $cell, value => $el; | ||
| 354 | } | ||
| 355 | } | ||
| 356 | |||
| 357 | $sparse; | ||
| 358 | } | ||
| 359 | |||
| 360 | sub _flatten_multidim($$$) | ||
| 361 | { my ($self, $array, $dims, $loc) = @_; | ||
| 362 | my @dims = @$dims; | ||
| 363 | |||
| 364 | my $expected = shift @dims; | ||
| 365 | my @data; | ||
| 366 | foreach (my $x = 0; $x < $expected; $x++) | ||
| 367 | { my $el = $array->[$x]; | ||
| 368 | defined $el or next; | ||
| 369 | |||
| 370 | my $cell = length $loc ? "$loc,$x" : $x; | ||
| 371 | push @data, @dims==0 ? ($cell, $el) # deepest dim | ||
| 372 | : $self->_flatten_multidim($el, \@dims, $cell); | ||
| 373 | } | ||
| 374 | |||
| 375 | @data; | ||
| 376 | } | ||
| 377 | |||
| 378 | #-------------------------------------------------- | ||
| 379 | |||
| 380 | sub rpcDecode(@) | ||
| 381 | { my $self = shift; | ||
| 382 | my @nodes = grep $_->isa('XML::LibXML::Element'), @_; | ||
| 383 | my $data = $self->_dec(\@nodes); | ||
| 384 | |||
| 385 | #XXX MO: no idea why this is needed: | ||
| 386 | foreach my $d (@$data) | ||
| 387 | { next unless $d->{_NAME}; | ||
| 388 | $d = { $d->{_NAME} => $d }; | ||
| 389 | } | ||
| 390 | |||
| 391 | my ($index, $hrefs) = ({}, []); | ||
| 392 | $self->_dec_find_ids_hrefs($index, $hrefs, \$data); | ||
| 393 | $self->_dec_resolve_hrefs($index, $hrefs); | ||
| 394 | |||
| 395 | $data = $self->_dec_simplify_tree($data) | ||
| 396 | if $simplify; | ||
| 397 | |||
| 398 | ref $data eq 'ARRAY' | ||
| 399 | or return $data; | ||
| 400 | |||
| 401 | @$data > 1 | ||
| 402 | or return $data->[0]; | ||
| 403 | |||
| 404 | # find the root element(s) | ||
| 405 | my @roots; | ||
| 406 | for(my $i = 0; $i < @_ && $i < @$data; $i++) | ||
| 407 | { my $root = $nodes[$i]->getAttributeNS(SOAP11ENC, 'root'); | ||
| 408 | next if defined $root && $root==0; | ||
| 409 | push @roots, $data->[$i]; | ||
| 410 | } | ||
| 411 | |||
| 412 | my $root_type = @roots ? $roots[0]->{_TYPE} : undef; | ||
| 413 | |||
| 414 | # address parameters by name | ||
| 415 | # On the top-level, we can strip on level. Some elements may appear | ||
| 416 | # more than once. | ||
| 417 | my %h; | ||
| 418 | foreach my $param (@roots ? @roots : @$data) | ||
| 419 | { delete $param->{_TYPE}; | ||
| 420 | my ($k, $v) = %$param; | ||
| 421 | if(! exists $h{$k}) { $h{$k} = $v } | ||
| 422 | elsif(reftype $h{$k} eq 'ARRAY') { push @{$h{$k}}, $v } | ||
| 423 | else { $h{$k} = [ $h{$k}, $v ] } | ||
| 424 | } | ||
| 425 | |||
| 426 | $h{_TYPE} = $root_type | ||
| 427 | if $root_type; | ||
| 428 | |||
| 429 | \%h; | ||
| 430 | } | ||
| 431 | |||
| 432 | sub _dec_reader($$@) | ||
| 433 | { my ($self, $node, $type) = splice @_, 0, 3; | ||
| 434 | |||
| 435 | # We must decode the prefix from the $node context | ||
| 436 | if(substr($type, 0, 1) ne '{') | ||
| 437 | { my ($prefix, $local) = $type =~ m/^(.*?)\:(.*)/ ? ($1, $2) : ('',$type); | ||
| 438 | $type = pack_type $node->lookupNamespaceURI($prefix) // '', $local; | ||
| 439 | } | ||
| 440 | |||
| 441 | my $r = try { | ||
| 442 | $self->schemas->reader($type | ||
| 443 | , element => type_of_node($node), is_type => 1, @_); | ||
| 444 | }; | ||
| 445 | $r || sub { shift }; | ||
| 446 | } | ||
| 447 | |||
| 448 | sub _dec($;$$$) | ||
| 449 | { my ($self, $nodes, $basetype, $offset, $dims) = @_; | ||
| 450 | my $schemas = $self->schemas; | ||
| 451 | my $nss = $schemas->namespaces; | ||
| 452 | |||
| 453 | my @res; | ||
| 454 | $#res = $offset-1 if defined $offset; | ||
| 455 | |||
| 456 | foreach my $node (@$nodes) | ||
| 457 | { my $ns = $node->namespaceURI || ''; | ||
| 458 | |||
| 459 | my $label = type_of_node $node; | ||
| 460 | my $place; | ||
| 461 | if($dims) | ||
| 462 | { my $pos = $node->getAttributeNS(SOAP11ENC, 'position'); | ||
| 463 | if($pos && $pos =~ m/^\[([\d,]+)\]/ ) | ||
| 464 | { my @pos = split /\,/, $1; | ||
| 465 | $place = \$res[shift @pos]; | ||
| 466 | $place = \(($$place ||= [])->[shift @pos]) while @pos; | ||
| 467 | } | ||
| 468 | } | ||
| 469 | |||
| 470 | unless($place) | ||
| 471 | { push @res, undef; | ||
| 472 | $place = \$res[-1]; | ||
| 473 | } | ||
| 474 | |||
| 475 | if(my $href = $node->getAttribute('href') || '') | ||
| 476 | { $$place = { $label => { href => $href } }; | ||
| 477 | next; | ||
| 478 | } | ||
| 479 | |||
| 480 | if($ns ne SOAP11ENC) | ||
| 481 | { my $typedef = $node->getAttributeNS(SCHEMA2001i, 'type'); | ||
| 482 | if($typedef) | ||
| 483 | { $$place = $self->_dec_typed($node, $typedef); | ||
| 484 | next; | ||
| 485 | } | ||
| 486 | |||
| 487 | $$place = $self->_dec_other($node, $basetype); | ||
| 488 | next; | ||
| 489 | } | ||
| 490 | |||
| 491 | my $local = $node->localName; | ||
| 492 | if($local eq 'Array') | ||
| 493 | { $$place = $self->_dec_other($node, $basetype); | ||
| 494 | next; | ||
| 495 | } | ||
| 496 | |||
| 497 | $$place = $self->_dec_soapenc($node, pack_type($ns, $local)); | ||
| 498 | } | ||
| 499 | |||
| 500 | \@res; | ||
| 501 | } | ||
| 502 | |||
| 503 | sub _dec_typed($$$) | ||
| 504 | { my ($self, $node, $type, $index) = @_; | ||
| 505 | |||
| 506 | my $full = type_of_node $node; | ||
| 507 | my $read = $self->_dec_reader($node, $type); | ||
| 508 | my $child = $read->($node); | ||
| 509 | my $data = ref $child eq 'HASH' ? $child : { _ => $child }; | ||
| 510 | $data->{_TYPE} = $type; | ||
| 511 | $data->{_NAME} = type_of_node $node; | ||
| 512 | |||
| 513 | my $id = $node->getAttribute('id'); | ||
| 514 | $data->{id} = $id if defined $id; | ||
| 515 | |||
| 516 | $data; | ||
| 517 | } | ||
| 518 | |||
| 519 | sub _dec_other($$) | ||
| 520 | { my ($self, $node, $basetype) = @_; | ||
| 521 | my $local = $node->localName; | ||
| 522 | |||
| 523 | my $data; | ||
| 524 | my $type = $basetype || type_of_node $node; | ||
| 525 | my $read = try { $self->_dec_reader($node, $type) }; | ||
| 526 | if($@) | ||
| 527 | { # warn $@->wasFatal->message; #--> element not found | ||
| 528 | # Element not known, so we must autodetect the type | ||
| 529 | my @childs = grep $_->isa('XML::LibXML::Element'), $node->childNodes; | ||
| 530 | if(@childs) | ||
| 531 | { my ($childbase, $dims); | ||
| 532 | if($type =~ m/(.+?)\s*\[([\d,]+)\]$/) | ||
| 533 | { $childbase = $1; | ||
| 534 | $dims = ($2 =~ tr/,//) + 1; | ||
| 535 | } | ||
| 536 | my $dec_childs = $self->_dec(\@childs, $childbase, 0, $dims); | ||
| 537 | |||
| 538 | my $key = $local; | ||
| 539 | $key = '_' if $key eq 'Array'; # simplifies better | ||
| 540 | $data = { $key => $dec_childs } if $dec_childs; | ||
| 541 | } | ||
| 542 | else | ||
| 543 | { $data->{_} = $node->textContent; | ||
| 544 | $data->{_TYPE} = $basetype if $basetype; | ||
| 545 | } | ||
| 546 | } | ||
| 547 | else | ||
| 548 | { my @x = $read->($node); | ||
| 549 | $data = $x[0]; | ||
| 550 | $data = { _ => $data } if ref $data ne 'HASH'; | ||
| 551 | $data->{_TYPE} = $basetype if $basetype; | ||
| 552 | } | ||
| 553 | |||
| 554 | $data->{_NAME} = type_of_node $node; | ||
| 555 | |||
| 556 | my $id = $node->getAttribute('id'); | ||
| 557 | $data->{id} = $id if defined $id; | ||
| 558 | |||
| 559 | ($local => $data); | ||
| 560 | } | ||
| 561 | |||
| 562 | sub _dec_soapenc($$) | ||
| 563 | { my ($self, $node, $type) = @_; | ||
| 564 | my $reader = $self->_dec_reader($node, $type) | ||
| 565 | or return $node; | ||
| 566 | my $data = $reader->($node); | ||
| 567 | $data = { _ => $data } if ref $data ne 'HASH'; | ||
| 568 | $data->{_TYPE} = $type; | ||
| 569 | $data; | ||
| 570 | } | ||
| 571 | |||
| 572 | sub _dec_find_ids_hrefs($$$) | ||
| 573 | { my ($self, $index, $hrefs, $node) = @_; | ||
| 574 | ref $$node or return; | ||
| 575 | |||
| 576 | if(ref $$node eq 'ARRAY') | ||
| 577 | { foreach my $child (@$$node) | ||
| 578 | { $self->_dec_find_ids_hrefs($index, $hrefs, \$child); | ||
| 579 | } | ||
| 580 | } | ||
| 581 | elsif(ref $$node eq 'HASH') | ||
| 582 | { $index->{$$node->{id}} = $$node | ||
| 583 | if defined $$node->{id}; | ||
| 584 | |||
| 585 | if(my $href = $$node->{href}) | ||
| 586 | { push @$hrefs, $href => $node if $href =~ s/^#//; | ||
| 587 | } | ||
| 588 | |||
| 589 | foreach my $k (keys %$$node) | ||
| 590 | { $self->_dec_find_ids_hrefs($index, $hrefs, \( $$node->{$k} )); | ||
| 591 | } | ||
| 592 | } | ||
| 593 | elsif(UNIVERSAL::isa($$node, 'XML::LibXML::Element')) | ||
| 594 | { my $search = XML::LibXML::XPathContext->new($$node); | ||
| 595 | $index->{$_->value} = $_->getOwnerElement | ||
| 596 | for $search->findnodes('.//@id'); | ||
| 597 | |||
| 598 | # we cannot restore deep hrefs, so only top level | ||
| 599 | if(my $href = $$node->getAttribute('href')) | ||
| 600 | { push @$hrefs, $href => $node if $href =~ s/^#//; | ||
| 601 | } | ||
| 602 | } | ||
| 603 | } | ||
| 604 | |||
| 605 | sub _dec_resolve_hrefs($$) | ||
| 606 | { my ($self, $index, $hrefs) = @_; | ||
| 607 | |||
| 608 | while(@$hrefs) | ||
| 609 | { my ($to, $where) = (shift @$hrefs, shift @$hrefs); | ||
| 610 | my $dest = $index->{$to}; | ||
| 611 | unless($dest) | ||
| 612 | { warning __x"cannot find id for href {name}", name => $to; | ||
| 613 | next; | ||
| 614 | } | ||
| 615 | $$where = $dest; | ||
| 616 | } | ||
| 617 | } | ||
| 618 | |||
| 619 | sub _dec_array_hook($$$$$) | ||
| 620 | { my ($self, $node, $args, $where, $local, $r, $fulltype) = @_; | ||
| 621 | |||
| 622 | my $at = $node->getAttributeNS(SOAP11ENC, 'arrayType') | ||
| 623 | || $node->getAttributeNS(WSDL11, 'arrayType') | ||
| 624 | or return $node; | ||
| 625 | |||
| 626 | $at =~ m/^(.*) \s* \[ ([\d,]+)? \] $/x | ||
| 627 | or return $node; | ||
| 628 | |||
| 629 | my ($preftype, $dims) = ($1, $2); | ||
| 630 | my @dims = defined $dims ? split /\,/, $dims : (); | ||
| 631 | |||
| 632 | my $basetype; | ||
| 633 | if(index($preftype, ':') >= 0) | ||
| 634 | { my ($prefix, $local) = split /\:/, $preftype, 2; | ||
| 635 | $basetype = pack_type $node->lookupNamespaceURI($prefix), $local; | ||
| 636 | } | ||
| 637 | else | ||
| 638 | { $basetype = pack_type '', $preftype; | ||
| 639 | } | ||
| 640 | |||
| 641 | my $table; | ||
| 642 | if(@dims < 2) | ||
| 643 | { $table = $self->_dec_array_one($node, $basetype, $dims[0]); | ||
| 644 | } | ||
| 645 | else | ||
| 646 | { my $first = first {$_->isa('XML::LibXML::Element')} $node->childNodes; | ||
| 647 | $table = $first && $first->getAttributeNS(SOAP11ENC, 'position') | ||
| 648 | ? $self->_dec_array_multisparse($node, $basetype, \@dims) | ||
| 649 | : $self->_dec_array_multi($node, $basetype, \@dims); | ||
| 650 | } | ||
| 651 | |||
| 652 | (type_of_node($node) => $table); | ||
| 653 | } | ||
| 654 | |||
| 655 | sub _dec_array_one($$;$) | ||
| 656 | { my ($self, $node, $basetype, $size) = @_; | ||
| 657 | |||
| 658 | my $off = $node->getAttributeNS(SOAP11ENC, 'offset') || '[0]'; | ||
| 659 | $off =~ m/^\[(\d+)\]$/ or return $node; | ||
| 660 | |||
| 661 | my $offset = $1; | ||
| 662 | my @childs = grep $_->isa('XML::LibXML::Element'), $node->childNodes; | ||
| 663 | my $array = $self->_dec(\@childs, $basetype, $offset, 1); | ||
| 664 | $#$array = $size -1 if $size; # resize array to specified size | ||
| 665 | $array; | ||
| 666 | } | ||
| 667 | |||
| 668 | sub _dec_array_multisparse($$$) | ||
| 669 | { my ($self, $node, $basetype, $dims) = @_; | ||
| 670 | |||
| 671 | my @childs = grep $_->isa('XML::LibXML::Element'), $node->childNodes; | ||
| 672 | my $array = $self->_dec(\@childs, $basetype, 0, scalar(@$dims)); | ||
| 673 | $array; | ||
| 674 | } | ||
| 675 | |||
| 676 | sub _dec_array_multi($$$) | ||
| 677 | { my ($self, $node, $basetype, $dims) = @_; | ||
| 678 | |||
| 679 | my @childs = grep $_->isa('XML::LibXML::Element'), $node->childNodes; | ||
| 680 | $self->_dec_array_multi_slice(\@childs, $basetype, $dims); | ||
| 681 | } | ||
| 682 | |||
| 683 | sub _dec_array_multi_slice($$$) | ||
| 684 | { my ($self, $childs, $basetype, $dims) = @_; | ||
| 685 | if(@$dims==1) | ||
| 686 | { my @col = splice @$childs, 0, $dims->[0]; | ||
| 687 | return $self->_dec(\@col, $basetype); | ||
| 688 | } | ||
| 689 | my ($rows, @dims) = @$dims; | ||
| 690 | |||
| 691 | [map $self->_dec_array_multi_slice($childs, $basetype, \@dims), 1..$rows]; | ||
| 692 | } | ||
| 693 | |||
| 694 | sub _dec_simplify_tree($@) | ||
| 695 | { my ($self, $tree, %opts) = @_; | ||
| 696 | defined $tree or return (); | ||
| 697 | $self->{dec}{_simple_recurse} = {}; | ||
| 698 | $self->_dec_simple($tree, \%opts); | ||
| 699 | } | ||
| 700 | |||
| 701 | sub _dec_simple($$) | ||
| 702 | { my ($self, $tree, $opts) = @_; | ||
| 703 | |||
| 704 | ref $tree | ||
| 705 | or return $tree; | ||
| 706 | |||
| 707 | return $tree | ||
| 708 | if $self->{dec}{_simple_recurse}{$tree}; | ||
| 709 | |||
| 710 | $self->{dec}{_simple_recurse}{$tree}++; | ||
| 711 | |||
| 712 | if(ref $tree eq 'ARRAY') | ||
| 713 | { my @a = map $self->_dec_simple($_, $opts), @$tree; | ||
| 714 | return $a[0] if @a==1; | ||
| 715 | |||
| 716 | # array of hash with each one element becomes hash | ||
| 717 | my %out; | ||
| 718 | foreach my $hash (@a) | ||
| 719 | { ref $hash eq 'HASH' && keys %$hash==1 | ||
| 720 | or return \@a; | ||
| 721 | |||
| 722 | my ($name, $value) = each %$hash; | ||
| 723 | if(!exists $out{$name}) { $out{$name} = $value } | ||
| 724 | elsif(ref $out{$name} eq 'ARRAY') | ||
| 725 | { $out{$name} = [ $out{$name} ] # array of array: keep [] | ||
| 726 | if ref $out{$name}[0] ne 'ARRAY' && ref $value eq 'ARRAY'; | ||
| 727 | push @{$out{$name}}, $value; | ||
| 728 | } | ||
| 729 | else { $out{$name} = [ $out{$name}, $value ] } | ||
| 730 | } | ||
| 731 | return \%out; | ||
| 732 | } | ||
| 733 | |||
| 734 | ref $tree eq 'HASH' | ||
| 735 | or return $tree; | ||
| 736 | |||
| 737 | foreach my $k (keys %$tree) | ||
| 738 | { if($k =~ m/^(?:_NAME$|_TYPE$|id$)/) { delete $tree->{$k} } | ||
| 739 | elsif(ref $tree->{$k}) | ||
| 740 | { $tree->{$k} = $self->_dec_simple($tree->{$k}, $opts); | ||
| 741 | } | ||
| 742 | } | ||
| 743 | |||
| 744 | delete $self->{dec}{_simple_recurse}{$tree}; | ||
| 745 | |||
| 746 | keys(%$tree)==1 && exists $tree->{_} ? $tree->{_} : $tree; | ||
| 747 | } | ||
| 748 | |||
| 749 | 1; |