| 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::Template; | ||
| 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 XML::Compile::Util | ||
| 22 | qw/odd_elements even_elements SCHEMA2001i pack_type unpack_type/; | ||
| 23 | use List::Util qw/max first/; | ||
| 24 | |||
| 25 | use vars '$VERSION'; # OODoc adds $VERSION to the script | ||
| 26 | $VERSION ||= 'undef'; | ||
| 27 | |||
| 28 | |||
| 29 | sub makeTagQualified | ||
| 30 | { my ($self, $path, $node, $local, $ns) = @_; | ||
| 31 | my $prefix = $self->_registerNSprefix('', $ns, 1); | ||
| 32 | |||
| 33 | # it is certainly not correct to do a keyRewrite here, but it works :( | ||
| 34 | $self->{_output} eq 'PERL' ? $self->keyRewrite($ns, $local) | ||
| 35 | : length $prefix ? "$prefix:$local" | ||
| 36 | : $local; | ||
| 37 | } | ||
| 38 | |||
| 39 | sub makeTagUnqualified | ||
| 40 | { my ($self, $path, $node, $local, $ns) = @_; | ||
| 41 | # $name =~ s/.*\://; | ||
| 42 | return $self->keyRewrite($ns, $local) | ||
| 43 | if $self->{_output} eq 'PERL'; | ||
| 44 | |||
| 45 | my $prefix = $self->_registerNSprefix('', $ns, 1); | ||
| 46 | length $prefix ? "$prefix:$local" : $local; | ||
| 47 | } | ||
| 48 | |||
| 49 | # Detect recursion. Based on type is best, but some schema's do not | ||
| 50 | # have named types, so tags are indexed as well. | ||
| 51 | my (%recurse_type, %reuse_type, %recurse_tag, %reuse_tag); | ||
| 52 | |||
| 53 | sub compile($@) | ||
| 54 | { my ($self, $type, %args) = @_; | ||
| 55 | $self->{_output} = $args{output}; | ||
| 56 | $self->{_style} = $args{output_style} || 1; | ||
| 57 | (%recurse_type, %reuse_type, %recurse_tag, %reuse_tag) = (); | ||
| 58 | $self->SUPER::compile($type, %args); | ||
| 59 | } | ||
| 60 | |||
| 61 | sub actsAs($) | ||
| 62 | { my ($self, $as) = @_; | ||
| 63 | ($as eq 'READER' && $self->{_output} eq 'PERL') | ||
| 64 | || ($as eq 'WRITER' && $self->{_output} eq 'XML') | ||
| 65 | } | ||
| 66 | |||
| 67 | sub makeWrapperNs($$$$$) | ||
| 68 | { my ($self, $path, $processor, $index, $filter) = @_; | ||
| 69 | |||
| 70 | my @entries; | ||
| 71 | $filter = sub {1} if ref $filter ne 'CODE'; | ||
| 72 | |||
| 73 | foreach my $entry (sort {$a->{prefix} cmp $b->{prefix}} values %$index) | ||
| 74 | { $entry->{used} or next; | ||
| 75 | my ($prefix, $uri) = @{$entry}{'prefix', 'uri'}; | ||
| 76 | $filter->($uri, $prefix) or next; | ||
| 77 | push @entries, [ $uri, $prefix ]; | ||
| 78 | $entry->{used} = 0; | ||
| 79 | } | ||
| 80 | |||
| 81 | sub { my $data = $processor->(@_) or return (); | ||
| 82 | if($self->{include_namespaces}) | ||
| 83 | { $data->{"xmlns:$_->[1]"} = $_->[0] for @entries; | ||
| 84 | } | ||
| 85 | $data; | ||
| 86 | }; | ||
| 87 | } | ||
| 88 | |||
| 89 | sub typemapToHooks($$) | ||
| 90 | { my ($self, $hooks, $typemap) = @_; | ||
| 91 | |||
| 92 | while(my($type, $action) = each %$typemap) | ||
| 93 | { defined $action or next; | ||
| 94 | |||
| 95 | my ($struct, $example) | ||
| 96 | = $action =~ s/^[\\]?\&/\$/ | ||
| 97 | ? ( "call on converter function with object" | ||
| 98 | , "$action->('WRITER', \$object, '$type', \$doc)") | ||
| 99 | : $action =~ m/^\$/ | ||
| 100 | ? ( "call on converter with object" | ||
| 101 | , "$action->toXML(\$object, '$type', \$doc)") | ||
| 102 | : ( [ "calls toXML() on $action objects", " with $type and doc" ] | ||
| 103 | , "bless({}, '$action')" ); | ||
| 104 | |||
| 105 | my $details = | ||
| 106 | { struct => $struct | ||
| 107 | , example => $example | ||
| 108 | }; | ||
| 109 | |||
| 110 | push @$hooks, { type => $type, replace => sub { $details} }; | ||
| 111 | } | ||
| 112 | |||
| 113 | $hooks; | ||
| 114 | } | ||
| 115 | |||
| 116 | sub makeElementWrapper | ||
| 117 | { my ($self, $path, $processor) = @_; | ||
| 118 | sub { $processor->() }; | ||
| 119 | } | ||
| 120 | *makeAttributeWrapper = \&makeElementWrapper; | ||
| 121 | |||
| 122 | sub _block($@) | ||
| 123 | { my ($self, $block, $path, @pairs) = @_; | ||
| 124 | bless | ||
| 125 | sub { my @elems = map { $_->() } odd_elements @pairs; | ||
| 126 | my @tags = map { $_->{tag} } @elems; | ||
| 127 | |||
| 128 | local $" = ', '; | ||
| 129 | my $struct = @tags ? "$block of @tags" | ||
| 130 | : "empty $block from ".join(" ", even_elements @pairs); | ||
| 131 | |||
| 132 | my @lines; | ||
| 133 | while(length $struct > 65) | ||
| 134 | { $struct =~ s/(.{1,60}|\S+)(?:\s+|$)//; | ||
| 135 | push @lines, $1; | ||
| 136 | } | ||
| 137 | push @lines, $struct | ||
| 138 | if length $struct; | ||
| 139 | $lines[$_] =~ s/^/ / for 1..$#lines; | ||
| 140 | |||
| 141 | { tag => $block | ||
| 142 | , elems => \@elems | ||
| 143 | , struct => \@lines | ||
| 144 | }; | ||
| 145 | }, 'BLOCK'; | ||
| 146 | } | ||
| 147 | |||
| 148 | sub makeSequence { my $self = shift; $self->_block(sequence => @_) } | ||
| 149 | sub makeChoice { my $self = shift; $self->_block(choice => @_) } | ||
| 150 | sub makeAll { my $self = shift; $self->_block(all => @_) } | ||
| 151 | |||
| 152 | sub makeBlockHandler | ||
| 153 | { my ($self, $path, $label, $min, $max, $proc, $kind, $multi) = @_; | ||
| 154 | |||
| 155 | my $code = | ||
| 156 | sub { my $data = $proc->(); | ||
| 157 | my $occur | ||
| 158 | = $max eq 'unbounded' && $min==0 ? 'occurs any number of times' | ||
| 159 | : $max ne 'unbounded' && $max==1 && $min==0 ? 'is optional' | ||
| 160 | : $max ne 'unbounded' && $max==1 && $min==1 ? '' # the usual case | ||
| 161 | : "occurs $min <= # <= $max times"; | ||
| 162 | |||
| 163 | $data->{occur} ||= $occur if $occur; | ||
| 164 | if($max ne 'unbounded' && $max==1) | ||
| 165 | { bless $data, 'BLOCK'; | ||
| 166 | } | ||
| 167 | else | ||
| 168 | { $data->{tag} = $multi; | ||
| 169 | $data->{is_array} = 1; | ||
| 170 | bless $data, 'REP-BLOCK'; | ||
| 171 | } | ||
| 172 | $data; | ||
| 173 | }; | ||
| 174 | ($label => $code); | ||
| 175 | } | ||
| 176 | |||
| 177 | sub makeElementHandler | ||
| 178 | { my ($self, $path, $label, $min, $max, $req, $opt) = @_; | ||
| 179 | sub { my $data = $opt->() or return; | ||
| 180 | my $occur | ||
| 181 | = $max eq 'unbounded' && $min==0 ? 'occurs any number of times' | ||
| 182 | : $max ne 'unbounded' && $max==1 && $min==0 ? 'is optional' | ||
| 183 | : $max ne 'unbounded' && $max==1 && $min==1 ? '' # the usual case | ||
| 184 | : "occurs $min <= # <= $max times"; | ||
| 185 | $data->{occur} ||= $occur if $occur; | ||
| 186 | $data->{is_array} = $max eq 'unbounded' || $max > 1; | ||
| 187 | $data; | ||
| 188 | }; | ||
| 189 | } | ||
| 190 | |||
| 191 | sub makeRequired | ||
| 192 | { my ($self, $path, $label, $do) = @_; | ||
| 193 | $do; | ||
| 194 | } | ||
| 195 | |||
| 196 | sub makeElementHref | ||
| 197 | { my ($self, $path, $ns, $childname, $do) = @_; | ||
| 198 | $do; | ||
| 199 | } | ||
| 200 | |||
| 201 | sub makeElement | ||
| 202 | { my ($self, $path, $ns, $childname, $do) = @_; | ||
| 203 | sub { my $h = $do->(@_); | ||
| 204 | $h->{_NAME} = $childname; | ||
| 205 | $h; | ||
| 206 | }; | ||
| 207 | } | ||
| 208 | |||
| 209 | sub makeElementDefault | ||
| 210 | { my ($self, $path, $ns, $childname, $do, $default) = @_; | ||
| 211 | sub { my $h = $do->(@_); | ||
| 212 | $h->{occur} = "defaults to '$default'"; | ||
| 213 | $h->{example} = $default; | ||
| 214 | $h; | ||
| 215 | }; | ||
| 216 | } | ||
| 217 | |||
| 218 | sub makeElementFixed | ||
| 219 | { my ($self, $path, $ns, $childname, $do, $fixed) = @_; | ||
| 220 | sub { my $h = $do->(@_); | ||
| 221 | $h->{occur} = "fixed to '$fixed'"; | ||
| 222 | $h->{example} = $fixed; | ||
| 223 | $h; | ||
| 224 | }; | ||
| 225 | } | ||
| 226 | |||
| 227 | sub makeElementAbstract | ||
| 228 | { my ($self, $path, $ns, $childname, $do) = @_; | ||
| 229 | # sub { () }; | ||
| 230 | sub { | ||
| 231 | my $h = $do->(@_); | ||
| 232 | $h->{_NAME} = $childname; | ||
| 233 | $h->{occur} = "ABSTRACT"; | ||
| 234 | $h; | ||
| 235 | }; | ||
| 236 | } | ||
| 237 | |||
| 238 | sub makeComplexElement | ||
| 239 | { my ($self, $path, $tag, $elems, $attrs, $any_attr, $type, $is_nillable)=@_; | ||
| 240 | my @elem_parts = odd_elements @$elems; | ||
| 241 | my @attr_parts = (odd_elements(@$attrs), @$any_attr); | ||
| 242 | |||
| 243 | sub { my (@attrs, @elems); | ||
| 244 | my $is_pseudo_type = $type !~ m/^{/; # like "unnamed complex" | ||
| 245 | |||
| 246 | if((!$is_pseudo_type && $recurse_type{$type}) || $recurse_tag{$tag}) | ||
| 247 | { return | ||
| 248 | +{ kind => 'complex' | ||
| 249 | , struct => 'probably a recursive complex' | ||
| 250 | , tag => $tag | ||
| 251 | , _TYPE => $type | ||
| 252 | }; | ||
| 253 | } | ||
| 254 | |||
| 255 | if((!$is_pseudo_type && $reuse_type{$type}) || $reuse_tag{$tag}) | ||
| 256 | { return | ||
| 257 | +{ kind => 'complex' | ||
| 258 | , struct => 'complex structure shown above' | ||
| 259 | , tag => $tag | ||
| 260 | , _TYPE => $type | ||
| 261 | }; | ||
| 262 | } | ||
| 263 | |||
| 264 | $recurse_type{$type}++; $recurse_tag{$tag}++; | ||
| 265 | $reuse_type{$type}++; $reuse_tag{$tag}++; | ||
| 266 | push @elems, $_->() for @elem_parts; | ||
| 267 | push @attrs, $_->() for @attr_parts; | ||
| 268 | |||
| 269 | $recurse_type{$type}--; $recurse_tag{$tag}--; | ||
| 270 | |||
| 271 | +{ kind => 'complex' | ||
| 272 | , struct => ($is_nillable ? "is nillable, as: $tag => NIL" : undef) | ||
| 273 | , tag => $tag | ||
| 274 | , attrs => \@attrs | ||
| 275 | , elems => \@elems | ||
| 276 | , _TYPE => $type | ||
| 277 | }; | ||
| 278 | }; | ||
| 279 | } | ||
| 280 | |||
| 281 | sub makeTaggedElement | ||
| 282 | { my ($self, $path, $tag, $st, $attrs, $attrs_any, $type, $is_nillable) = @_; | ||
| 283 | my @parts = (odd_elements(@$attrs), @$attrs_any); | ||
| 284 | |||
| 285 | sub { my @attrs = map $_->(), @parts; | ||
| 286 | my %simple = $st->(); | ||
| 287 | |||
| 288 | my @struct = 'string content of the container'; | ||
| 289 | push @struct, $simple{struct} if $simple{struct}; | ||
| 290 | push @struct, 'is nillable, hence value or NIL' if $is_nillable; | ||
| 291 | |||
| 292 | my %content = | ||
| 293 | ( tag => '_' | ||
| 294 | , struct => \@struct | ||
| 295 | , example => ($simple{example} || 'Hello, World!') | ||
| 296 | ); | ||
| 297 | $content{_TYPE} = $simple{_TYPE} if $simple{_TYPE}; | ||
| 298 | |||
| 299 | +{ kind => 'tagged' | ||
| 300 | , struct => "$tag is simple value with attributes" | ||
| 301 | , tag => $tag | ||
| 302 | , attrs => \@attrs | ||
| 303 | , elems => [ \%content ] | ||
| 304 | , _TYPE => $type | ||
| 305 | }; | ||
| 306 | }; | ||
| 307 | } | ||
| 308 | |||
| 309 | sub makeMixedElement | ||
| 310 | { my ($self, $path, $tag, $elems, $attrs, $attrs_any, $type, $is_nillable)=@_; | ||
| 311 | my @parts = (odd_elements(@$attrs), @$attrs_any); | ||
| 312 | |||
| 313 | my @struct = 'mixed content cannot be processed automatically'; | ||
| 314 | push @struct, 'is nillable' if $is_nillable; | ||
| 315 | |||
| 316 | my %mixed = | ||
| 317 | ( tag => '_' | ||
| 318 | , struct => \@struct | ||
| 319 | , example => "XML::LibXML::Element->new('$tag')" | ||
| 320 | ); | ||
| 321 | |||
| 322 | unless(@parts) # show simpler alternative | ||
| 323 | { $mixed{tag} = $tag; | ||
| 324 | $mixed{type} = $type; | ||
| 325 | return sub { \%mixed }; | ||
| 326 | } | ||
| 327 | |||
| 328 | sub { my @attrs = map $_->(), @parts; | ||
| 329 | +{ kind => 'mixed' | ||
| 330 | , struct => "$tag has a mixed content" | ||
| 331 | , tag => $tag | ||
| 332 | , elems => [ \%mixed ] | ||
| 333 | , attrs => \@attrs | ||
| 334 | , _TYPE => $type | ||
| 335 | }; | ||
| 336 | }; | ||
| 337 | } | ||
| 338 | |||
| 339 | sub makeSimpleElement | ||
| 340 | { my ($self, $path, $tag, $st, undef, undef, $type, $is_nillable) = @_; | ||
| 341 | sub { my @struct; | ||
| 342 | push @struct, 'is nillable, hence value or NIL' if $is_nillable; | ||
| 343 | +{ kind => 'simple' | ||
| 344 | , struct => \@struct | ||
| 345 | , tag => $tag | ||
| 346 | , $st->() | ||
| 347 | }; | ||
| 348 | }; | ||
| 349 | } | ||
| 350 | |||
| 351 | sub makeBuiltin | ||
| 352 | { my ($self, $path, $node, $type, $def, $check_values) = @_; | ||
| 353 | sub { (_TYPE=> $type, example => $def->{example}) }; | ||
| 354 | } | ||
| 355 | |||
| 356 | sub makeList | ||
| 357 | { my ($self, $path, $st) = @_; | ||
| 358 | sub { my %d = $st->(); | ||
| 359 | $d{struct} = 'a list of values, where each'; | ||
| 360 | my $example = $d{example}; | ||
| 361 | if($self->{_output} eq 'PERL') | ||
| 362 | { $example = qq("$example") if $example =~ m/[^0-9.]/; | ||
| 363 | $d{example} = "[ $example , ... ]"; | ||
| 364 | } | ||
| 365 | else | ||
| 366 | { $d{example} = "$example $example ..."; | ||
| 367 | } | ||
| 368 | %d }; | ||
| 369 | } | ||
| 370 | |||
| 371 | sub makeFacetsList | ||
| 372 | { my ($self, $path, $st, $info) = @_; | ||
| 373 | $self->makeFacets($path, $st, $info); | ||
| 374 | } | ||
| 375 | |||
| 376 | sub _ff($@) | ||
| 377 | { my ($self,$type) = (shift, shift); | ||
| 378 | my @lines = $type.':'; | ||
| 379 | while(@_) | ||
| 380 | { my $facet = shift; | ||
| 381 | $facet =~ s/\t/\t/g; | ||
| 382 | $facet = qq{"$facet"} if $facet =~ m/\s/; | ||
| 383 | push @lines, ' ' if length($lines[-1]) + length($facet) > 55; | ||
| 384 | $lines[-1] .= ' '.$facet; | ||
| 385 | } | ||
| 386 | @lines; | ||
| 387 | } | ||
| 388 | |||
| 389 | sub makeFacets | ||
| 390 | { my ($self, $path, $st, $info) = @_; | ||
| 391 | my @comment; | ||
| 392 | foreach my $k (sort keys %$info) | ||
| 393 | { my $v = $info->{$k}; | ||
| 394 | push @comment | ||
| 395 | , $k eq 'enumeration' ? $self->_ff('Enum', sort @$v) | ||
| 396 | : $k eq 'pattern' ? $self->_ff('Pattern', @$v) | ||
| 397 | : $k eq 'length' ? "fixed length of $v" | ||
| 398 | : $k eq 'maxLength' ? "length <= $v" | ||
| 399 | : $k eq 'minLength' ? "length >= $v" | ||
| 400 | : $k eq 'totalDigits' ? "total digits is $v" | ||
| 401 | : $k eq 'maxScale' ? "scale <= $v" | ||
| 402 | : $k eq 'minScale' ? "scale >= $v" | ||
| 403 | : $k eq 'maxInclusive' ? "value <= $v" | ||
| 404 | : $k eq 'maxExclusive' ? "value < $v" | ||
| 405 | : $k eq 'minInclusive' ? "value >= $v" | ||
| 406 | : $k eq 'minExclusive' ? "value > $v" | ||
| 407 | : $k eq 'fractionDigits' ? "faction digits is $v" | ||
| 408 | : $k eq 'whiteSpace' ? "white-space $v" | ||
| 409 | : "restriction? $k = $v"; | ||
| 410 | } | ||
| 411 | |||
| 412 | my %facet = (facets => \@comment, $st->()); | ||
| 413 | |||
| 414 | if(my $enum = $info->{enumeration}) | ||
| 415 | { $facet{example} = $enum->[0]; | ||
| 416 | } | ||
| 417 | |||
| 418 | sub { %facet }; | ||
| 419 | } | ||
| 420 | |||
| 421 | sub makeUnion | ||
| 422 | { my ($self, $path, @types) = @_; | ||
| 423 | sub { my @choices = map { +{$_->()} } @types; | ||
| 424 | +( kind => 'union' | ||
| 425 | , struct => "one of the following (union)" | ||
| 426 | , choice => \@choices | ||
| 427 | , example => $choices[0]->{example} | ||
| 428 | ); | ||
| 429 | }; | ||
| 430 | } | ||
| 431 | |||
| 432 | sub makeAttributeRequired | ||
| 433 | { my ($self, $path, $ns, $tag, $label, $do) = @_; | ||
| 434 | |||
| 435 | sub { +{ kind => 'attr' | ||
| 436 | , tag => $label | ||
| 437 | , occur => "attribute $tag is required" | ||
| 438 | , $do->() | ||
| 439 | }; | ||
| 440 | }; | ||
| 441 | } | ||
| 442 | |||
| 443 | sub makeAttributeProhibited | ||
| 444 | { my ($self, $path, $ns, $tag, $label, $do) = @_; | ||
| 445 | (); | ||
| 446 | } | ||
| 447 | |||
| 448 | sub makeAttribute | ||
| 449 | { my ($self, $path, $ns, $tag, $label, $do) = @_; | ||
| 450 | sub { +{ kind => 'attr' | ||
| 451 | , tag => $tag | ||
| 452 | , occur => "becomes an attribute" | ||
| 453 | , $do->() | ||
| 454 | }; | ||
| 455 | }; | ||
| 456 | } | ||
| 457 | |||
| 458 | sub makeAttributeDefault | ||
| 459 | { my ($self, $path, $ns, $tag, $label, $do) = @_; | ||
| 460 | sub { | ||
| 461 | +{ kind => 'attr' | ||
| 462 | , tag => $tag | ||
| 463 | , occur => "attribute $tag has default" | ||
| 464 | , $do->() | ||
| 465 | }; | ||
| 466 | }; | ||
| 467 | } | ||
| 468 | |||
| 469 | sub makeAttributeFixed | ||
| 470 | { my ($self, $path, $ns, $tag, $label, $do, $fixed) = @_; | ||
| 471 | my $value = $fixed->value; | ||
| 472 | |||
| 473 | sub { +{ kind => 'attr' | ||
| 474 | , tag => $tag | ||
| 475 | , occur => "attribute $tag is fixed" | ||
| 476 | , example => $value | ||
| 477 | }; | ||
| 478 | }; | ||
| 479 | } | ||
| 480 | |||
| 481 | sub makeSubstgroup | ||
| 482 | { my ($self, $path, $type, @todo) = @_; | ||
| 483 | |||
| 484 | sub { | ||
| 485 | my (@example_tags, $example_nest, %tags); | ||
| 486 | my @do = @todo; | ||
| 487 | my $group = $do[1][0]; | ||
| 488 | |||
| 489 | while(@do) | ||
| 490 | { my ($type, $info) = (shift @do, shift @do); | ||
| 491 | my ($label, $call) = @$info; | ||
| 492 | my $processed = $call->(); | ||
| 493 | my $show = ''; | ||
| 494 | if($processed->{kind} eq 'substitution group') | ||
| 495 | { # substr extended by subst, which already is formatted. | ||
| 496 | # need to extract only the indicated type info. | ||
| 497 | my $s = $processed->{struct} || []; | ||
| 498 | /^ $label (.*)/ and $show = $1 for @$s; | ||
| 499 | } | ||
| 500 | elsif(my $type = $processed->{_TYPE}) | ||
| 501 | { $show = $self->prefixed($type); | ||
| 502 | } | ||
| 503 | |||
| 504 | if($processed->{occur} && $processed->{occur} eq 'ABSTRACT') | ||
| 505 | { $show .= ' (abstract)'; | ||
| 506 | } | ||
| 507 | else | ||
| 508 | { # some complication to always produce the same tag for | ||
| 509 | # regression tests... Instance uses a HASH... | ||
| 510 | push @example_tags, $label; | ||
| 511 | $example_nest ||= $processed->{kind} eq 'simple' | ||
| 512 | ? ($processed->{example} || '...') : '{...}'; | ||
| 513 | } | ||
| 514 | |||
| 515 | $tags{$label} = $show; | ||
| 516 | } | ||
| 517 | |||
| 518 | my $longest = max map length, keys %tags; | ||
| 519 | my @lines = map sprintf(" %-${longest}s %s", $_, $tags{$_}), | ||
| 520 | sort keys %tags; | ||
| 521 | |||
| 522 | my $example_tag = (sort @example_tags)[0]; | ||
| 523 | my $example = $example_tag ? "{ $example_tag => $example_nest }" | ||
| 524 | : "undef # only abstract types known"; | ||
| 525 | |||
| 526 | my $name = $self->prefixed($type); | ||
| 527 | |||
| 528 | +{ kind => 'substitution group' | ||
| 529 | , tag => $group | ||
| 530 | , struct => [ "substitutionGroup $name", @lines ] | ||
| 531 | , example => $example | ||
| 532 | }; | ||
| 533 | }; | ||
| 534 | } | ||
| 535 | |||
| 536 | sub makeXsiTypeSwitch($$$$) | ||
| 537 | { my ($self, $where, $elem, $default_type, $types) = @_; | ||
| 538 | my @types = map " ".$self->prefixed($_), sort keys %$types; | ||
| 539 | my $deftype = $self->prefixed($default_type); | ||
| 540 | |||
| 541 | sub { +{ kind => 'xsi:type switch' | ||
| 542 | , tag => $elem | ||
| 543 | , struct => [ 'xsi:type alternatives:', @types ] | ||
| 544 | , example => "{ XSI_TYPE => '$deftype', %data }" | ||
| 545 | } | ||
| 546 | }; | ||
| 547 | } | ||
| 548 | |||
| 549 | sub makeAnyAttribute | ||
| 550 | { my ($self, $path, $handler, $yes, $no, $process) = @_; | ||
| 551 | $yes ||= []; $no ||= []; | ||
| 552 | $yes = [ map {$self->prefixed("{$_}") || $_} @$yes]; | ||
| 553 | $no = [ map {$self->prefixed("{$_}") || $_} @$no]; | ||
| 554 | my $occurs = @$yes ? "in @$yes" : @$no ? "not in @$no" : 'in any namespace'; | ||
| 555 | bless sub { +{kind => 'attr' , struct => "any attribute $occurs" | ||
| 556 | , tag => 'ANYATTR', example => 'AnySimple'} }, 'ANY'; | ||
| 557 | } | ||
| 558 | |||
| 559 | sub makeAnyElement | ||
| 560 | { my ($self, $path, $handler, $yes, $no, $process, $min, $max) = @_; | ||
| 561 | $yes ||= []; $no ||= []; | ||
| 562 | $yes = [ map {$self->prefixed("{$_}") || $_} @$yes]; | ||
| 563 | $no = [ map {$self->prefixed("{$_}") || $_} @$no]; | ||
| 564 | my $where = @$yes ? "in @$yes" : @$no ? "not in @$no" : 'in any namespace'; | ||
| 565 | |||
| 566 | my $data = +{ kind => 'element', struct => "any element $where" | ||
| 567 | , tag => "ANY", example => 'Anything' }; | ||
| 568 | my $occur | ||
| 569 | = $max eq 'unbounded' && $min==0 ? 'occurs any number of times' | ||
| 570 | : $max ne 'unbounded' && $max==1 && $min==0 ? 'is optional' | ||
| 571 | : $max ne 'unbounded' && $max==1 && $min==1 ? '' # the usual case | ||
| 572 | : "occurs $min <= # <= $max times"; | ||
| 573 | $data->{occur} ||= $occur if $occur; | ||
| 574 | $data->{is_array} = $max eq 'unbounded' || $max > 1; | ||
| 575 | |||
| 576 | bless sub { +$data }, 'ANY'; | ||
| 577 | } | ||
| 578 | |||
| 579 | sub makeHook($$$$$$$) | ||
| 580 | { my ($self, $path, $r, $tag, $before, $replace, $after, $fulltype) = @_; | ||
| 581 | |||
| 582 | return $r unless $before || $replace || $after; | ||
| 583 | |||
| 584 | error __x"template only supports one production (replace) hook" | ||
| 585 | if $replace && @$replace > 1; | ||
| 586 | |||
| 587 | return sub {()} if $replace && grep {$_ eq 'SKIP'} @$replace; | ||
| 588 | |||
| 589 | my @replace = $replace ? map {$self->_decodeReplace($path,$_)} @$replace:(); | ||
| 590 | my @before = $before ? map {$self->_decodeBefore($path,$_) } @$before :(); | ||
| 591 | my @after = $after ? map {$self->_decodeAfter($path,$_) } @$after :(); | ||
| 592 | |||
| 593 | sub | ||
| 594 | { my $doc = XML::LibXML::Document->new; | ||
| 595 | for(@before) { $_->($doc, $path, undef) or return } | ||
| 596 | |||
| 597 | my $xml = @replace ? $replace[0]->($doc, $path, $r) : $r->(); | ||
| 598 | defined $xml or return (); | ||
| 599 | |||
| 600 | for(@after) { $xml = $_->($doc, $path, $xml) or return } | ||
| 601 | $xml; | ||
| 602 | } | ||
| 603 | } | ||
| 604 | |||
| 605 | sub _decodeBefore($$) | ||
| 606 | { my ($self, $path, $call) = @_; | ||
| 607 | return $call if ref $call eq 'CODE'; | ||
| 608 | error __x"labeled before hook `{name}' undefined", name => $call; | ||
| 609 | } | ||
| 610 | |||
| 611 | sub _decodeReplace($$) | ||
| 612 | { my ($self, $path, $call) = @_; | ||
| 613 | return $call if ref $call eq 'CODE'; | ||
| 614 | |||
| 615 | if($call eq 'COLLAPSE') | ||
| 616 | { return sub | ||
| 617 | { my ($tag, $path, $do) = @_; | ||
| 618 | my $h = $do->(); | ||
| 619 | $h->{elems} = [ { struct => [ 'content collapsed' ] | ||
| 620 | , kind => 'collapsed' } ]; | ||
| 621 | delete $h->{attrs}; | ||
| 622 | $h; | ||
| 623 | }; | ||
| 624 | } | ||
| 625 | |||
| 626 | # SKIP already handled | ||
| 627 | error __x"labeled replace hook `{name}' undefined", name => $call; | ||
| 628 | } | ||
| 629 | |||
| 630 | sub _decodeAfter($$) | ||
| 631 | { my ($self, $path, $call) = @_; | ||
| 632 | return $call if ref $call eq 'CODE'; | ||
| 633 | error __x"labeled after hook `{name}' undefined", name => $call; | ||
| 634 | } | ||
| 635 | |||
| 636 | |||
| 637 | ### | ||
| 638 | ### toPerl | ||
| 639 | ### | ||
| 640 | |||
| 641 | sub toPerl($%) | ||
| 642 | { my ($self, $ast, %args) = @_; | ||
| 643 | $ast or return undef; | ||
| 644 | |||
| 645 | my @lines; | ||
| 646 | if($ast->{kind}) | ||
| 647 | { my $name = $ast->{_NAME} || $ast->{tag}; | ||
| 648 | my $pref = $self->prefixed($name); | ||
| 649 | push @lines, defined $pref | ||
| 650 | ? ("# Describing $ast->{kind} $pref", "# $name") | ||
| 651 | : "# Describing $ast->{kind} $name"; | ||
| 652 | } | ||
| 653 | |||
| 654 | push @lines | ||
| 655 | , "#" | ||
| 656 | , "# Produced by ".__PACKAGE__." version $VERSION" | ||
| 657 | , "# on ".localtime() | ||
| 658 | , "#" | ||
| 659 | , "# BE WARNED: in most cases, the example below cannot be used without" | ||
| 660 | , "# interpretation. The comments will guide you." | ||
| 661 | , "#" | ||
| 662 | unless $args{skip_header}; | ||
| 663 | |||
| 664 | # add info about name-spaces | ||
| 665 | foreach my $nsdecl (grep /^xmlns\:/, sort keys %$ast) | ||
| 666 | { push @lines, sprintf "# %-15s %s", $nsdecl, $ast->{$nsdecl} || '(none)'; | ||
| 667 | } | ||
| 668 | push @lines, '' if @lines; | ||
| 669 | |||
| 670 | # produce data tree | ||
| 671 | push @lines, $self->_perlAny($ast, \%args); | ||
| 672 | |||
| 673 | # remove leading 'type =>' | ||
| 674 | for(my $linenr = 0; $linenr < @lines; $linenr++) | ||
| 675 | { next if $lines[$linenr] =~ m/^\s*\#/; | ||
| 676 | next unless $lines[$linenr] =~ s/.*? \=\>\s*//; | ||
| 677 | $lines[$linenr] =~ m/\S/ or splice @lines, $linenr, 1; | ||
| 678 | last; | ||
| 679 | } | ||
| 680 | |||
| 681 | my $lines = join "\n", @lines; | ||
| 682 | $lines =~ s/\,?\s*$/\n/; | ||
| 683 | $lines; | ||
| 684 | } | ||
| 685 | |||
| 686 | my %seen; | ||
| 687 | sub _perlAny($$); | ||
| 688 | sub _perlAny($$) | ||
| 689 | { my ($self, $ast, $args) = @_; | ||
| 690 | |||
| 691 | my ($pref, @lines); | ||
| 692 | if($ast->{_TYPE} && $args->{show_type}) | ||
| 693 | { if($pref = $self->prefixed($ast->{_TYPE})) | ||
| 694 | { push @lines # not perfect, but a good attempt | ||
| 695 | , $pref =~ m/^[aiou]/i && $pref !~ m/^(uni|eu)/i | ||
| 696 | ? "# is an $pref" : "# is a $pref"; | ||
| 697 | } | ||
| 698 | } | ||
| 699 | |||
| 700 | if($ast->{struct} && $args->{show_struct}) | ||
| 701 | { my $struct = $ast->{struct}; | ||
| 702 | my @struct = ref $struct ? @$struct : $struct; | ||
| 703 | s/^/# /gm for @struct; | ||
| 704 | push @lines, @struct; | ||
| 705 | } | ||
| 706 | |||
| 707 | push @lines, "# $ast->{occur}" | ||
| 708 | if $ast->{occur} && $args->{show_occur}; | ||
| 709 | |||
| 710 | if($ast->{facets} && $args->{show_facets}) | ||
| 711 | { my $facets = $ast->{facets}; | ||
| 712 | my @facets = ref $facets ? @$facets : $facets; | ||
| 713 | s/^/# /gm for @facets; | ||
| 714 | push @lines, @facets; | ||
| 715 | } | ||
| 716 | |||
| 717 | my @childs; | ||
| 718 | push @childs, @{$ast->{attrs}} if $ast->{attrs}; | ||
| 719 | push @childs, @{$ast->{elems}} if $ast->{elems}; | ||
| 720 | push @childs, $ast->{body} if $ast->{body}; | ||
| 721 | |||
| 722 | my @subs; | ||
| 723 | foreach my $child (@childs) | ||
| 724 | { my @sub = $self->_perlAny($child, $args); | ||
| 725 | @sub or next; | ||
| 726 | |||
| 727 | # last line is code and gets comma | ||
| 728 | $sub[-1] =~ s/\,?\s*$/,/ | ||
| 729 | if $sub[-1] !~ m/\#\s/; | ||
| 730 | |||
| 731 | if(ref $ast ne 'BLOCK') | ||
| 732 | { s/^(.)/$args->{indent}$1/ for @sub; | ||
| 733 | } | ||
| 734 | |||
| 735 | # seperator blank, sometimes | ||
| 736 | unshift @sub, '' | ||
| 737 | if $sub[0] =~ m/^\s*[#{]/ # } | ||
| 738 | || (@subs && $subs[-1] =~ m/[}\]]\,\s*$/); | ||
| 739 | |||
| 740 | push @subs, @sub; | ||
| 741 | } | ||
| 742 | |||
| 743 | if(ref $ast eq 'REP-BLOCK') | ||
| 744 | { # repeated block | ||
| 745 | @subs or @subs = ''; | ||
| 746 | $subs[0] =~ s/^ /{ / or $subs[0] =~ s/^\s*$/{/; | ||
| 747 | if($subs[-1] =~ m/\#\s/) { push @subs, "}," } | ||
| 748 | else { $subs[-1] =~ s/$/ },/ } | ||
| 749 | } | ||
| 750 | |||
| 751 | # XML does not permit difficult tags, but we still check. | ||
| 752 | my $tag = $ast->{tag} || ''; | ||
| 753 | if(defined $tag && $tag !~ m/^[\w_][\w\d_]*$/) | ||
| 754 | { $tag =~ s/\\/\\\\/g; | ||
| 755 | $tag =~ s/'/\\'/g; | ||
| 756 | $tag = qq{'$tag'}; | ||
| 757 | } | ||
| 758 | |||
| 759 | my $kind = $ast->{kind} || ''; | ||
| 760 | if(ref $ast eq 'REP-BLOCK') | ||
| 761 | { s/^(.)/ $1/ for @subs; | ||
| 762 | $subs[0] =~ s/^ ?/[/; | ||
| 763 | push @lines, "$tag => ", @subs , ']'; | ||
| 764 | } | ||
| 765 | elsif(ref $ast eq 'BLOCK') | ||
| 766 | { push @lines, @subs; | ||
| 767 | } | ||
| 768 | elsif(@subs) | ||
| 769 | { length $subs[0] or shift @subs; | ||
| 770 | if($ast->{is_array}) | ||
| 771 | { s/^(.)/ $1/ for @subs; | ||
| 772 | $subs[0] =~ s/^[ ]{0,3}/[ {/; | ||
| 773 | if($subs[-1] =~ m/\#\s/ || $self->{_style}==2) | ||
| 774 | { push @subs, "}, ], " } | ||
| 775 | else { $subs[-1] .= ' }, ], ' } | ||
| 776 | push @lines, "$tag =>", @subs; | ||
| 777 | } | ||
| 778 | else | ||
| 779 | { $subs[0] =~ s/^ /{ /; | ||
| 780 | if($self->{_style}==2) | ||
| 781 | { push @subs, "}, "; | ||
| 782 | $subs[-1] .= "# $pref" if $pref; | ||
| 783 | } | ||
| 784 | elsif($subs[-1] =~ m/\#\s/) { push @subs, "}, " } | ||
| 785 | else { $subs[-1] .= ' },' } | ||
| 786 | push @lines, "$tag =>", @subs; | ||
| 787 | } | ||
| 788 | } | ||
| 789 | elsif($kind eq 'complex' || $kind eq 'mixed') # empty complex-type | ||
| 790 | { # if there is an "occurs", then there can always be more than one | ||
| 791 | push @lines, $tag.' => '.($ast->{occur} ? '[{},]' : '{}'); | ||
| 792 | } | ||
| 793 | elsif($kind eq 'collapsed') {;} | ||
| 794 | elsif($kind eq 'union') # union type | ||
| 795 | { foreach my $union ( @{$ast->{choice}} ) | ||
| 796 | { # remove examples | ||
| 797 | my @l = grep { m/^#/ } $self->_perlAny($union, $args); | ||
| 798 | s/^\#/# -/ for $l[0]; | ||
| 799 | s/^\#/# / for @l[1..$#l]; | ||
| 800 | push @lines, @l; | ||
| 801 | } | ||
| 802 | } | ||
| 803 | elsif(!exists $ast->{example}) | ||
| 804 | { push @lines, "$tag => 'TEMPLATE-ERROR $ast->{kind}'"; | ||
| 805 | } | ||
| 806 | |||
| 807 | my $example = $ast->{example}; | ||
| 808 | if(defined $example) | ||
| 809 | { $example = qq{"$example"} # in quotes unless | ||
| 810 | if $example !~ m/^[+-]?\d+(?:\.\d+)?$/ # numeric or | ||
| 811 | && $example !~ m/^\$/ # variable or | ||
| 812 | && $example !~ m/^bless\b/ # constructor or | ||
| 813 | && $example !~ m/^\$?[\w:]*\-\>/ # method call example | ||
| 814 | && $example !~ m/^\{.*\}$/ # anon HASH example | ||
| 815 | && $example !~ m/^\[.*\]$/; # anon ARRAY example | ||
| 816 | |||
| 817 | push @lines, "$tag => " | ||
| 818 | . ($ast->{is_array} ? "[ $example, ]" : $example); | ||
| 819 | } | ||
| 820 | @lines; | ||
| 821 | } | ||
| 822 | |||
| 823 | ### | ||
| 824 | ### toXML | ||
| 825 | ### | ||
| 826 | |||
| 827 | sub toXML($$%) | ||
| 828 | { my ($self, $doc, $ast, %args) = @_; | ||
| 829 | my $xml = $self->_xmlAny($doc, $ast, "\n$args{indent}", \%args); | ||
| 830 | |||
| 831 | UNIVERSAL::isa($xml, 'XML::LibXML::Element') | ||
| 832 | or return $xml; | ||
| 833 | |||
| 834 | # add comment | ||
| 835 | my $pkg = __PACKAGE__; | ||
| 836 | my $now = localtime(); | ||
| 837 | |||
| 838 | my $header = $doc->createComment( <<_HEADER . ' ' ); | ||
| 839 | |||
| 840 | BE WARNED: in most cases, the example below cannot be used without | ||
| 841 | interpretation. The comments will guide you. | ||
| 842 | Produced by $pkg version $VERSION | ||
| 843 | on $now | ||
| 844 | _HEADER | ||
| 845 | |||
| 846 | unless($args{skip_header}) | ||
| 847 | { $xml->insertBefore($header, $xml->firstChild); | ||
| 848 | $xml->insertBefore($doc->createTextNode("\n "), $header); | ||
| 849 | } | ||
| 850 | |||
| 851 | # I use xsi:type myself, too late for the usual "used" counter | ||
| 852 | $ast->{'xmlns:xsi'} ||= SCHEMA2001i | ||
| 853 | if $args{show_type}; | ||
| 854 | |||
| 855 | # add info about name-spaces | ||
| 856 | foreach (sort keys %$ast) | ||
| 857 | { if( m/^xmlns\:(.*)/ ) | ||
| 858 | { $xml->setNamespace($ast->{$_}, $1, 0); | ||
| 859 | } | ||
| 860 | } | ||
| 861 | |||
| 862 | $xml; | ||
| 863 | } | ||
| 864 | |||
| 865 | sub _xmlAny($$$$); | ||
| 866 | sub _xmlAny($$$$) | ||
| 867 | { my ($self, $doc, $ast, $indent, $args) = @_; | ||
| 868 | my @res; | ||
| 869 | my $xsi = $self->_registerNSprefix('xsi', SCHEMA2001i, 1) | ||
| 870 | if $args->{show_type}; | ||
| 871 | |||
| 872 | my @comment; | ||
| 873 | if($ast->{struct} && $args->{show_struct}) | ||
| 874 | { my $struct = $ast->{struct}; | ||
| 875 | push @comment, ref $struct ? @$struct : $struct; | ||
| 876 | } | ||
| 877 | |||
| 878 | push @comment, $ast->{occur} | ||
| 879 | if $ast->{occur} && $args->{show_occur}; | ||
| 880 | |||
| 881 | if($ast->{facets} && $args->{show_facets}) | ||
| 882 | { my $facets = $ast->{facets}; | ||
| 883 | push @comment, ref $facets eq 'ARRAY' ? @$facets : $facets; | ||
| 884 | } | ||
| 885 | |||
| 886 | if(defined $ast->{kind} && $ast->{kind} eq 'union') | ||
| 887 | { push @comment, map " $_->{type}", @{$ast->{choice}}; | ||
| 888 | } | ||
| 889 | |||
| 890 | my @attrs = @{$ast->{attrs} || []}; | ||
| 891 | foreach my $attr (@attrs) | ||
| 892 | { push @res, $doc->createAttribute($attr->{tag}, $attr->{example}); | ||
| 893 | my ($ns, $local) = unpack_type $attr->{_TYPE}; | ||
| 894 | my $prefix = $self->_registerNSprefix('', $ns, 1); | ||
| 895 | push @comment, "attr $attr->{tag} has type $prefix:$local" | ||
| 896 | if $args->{show_type}; | ||
| 897 | } | ||
| 898 | |||
| 899 | my $nest_indent = $indent.$args->{indent}; | ||
| 900 | if(@comment) | ||
| 901 | { my $comment = ' '.join("$nest_indent ", @comment) .' '; | ||
| 902 | push @res | ||
| 903 | , $doc->createTextNode($indent) | ||
| 904 | , $doc->createComment($comment); | ||
| 905 | } | ||
| 906 | |||
| 907 | my @elems = @{$ast->{elems} || []}; | ||
| 908 | foreach my $elem (@elems) | ||
| 909 | { if(ref $elem eq 'BLOCK' || ref $elem eq 'REP-BLOCK') | ||
| 910 | { push @res, $self->_xmlAny($doc, $elem, $indent, $args); | ||
| 911 | } | ||
| 912 | elsif($elem->{tag} eq '_') | ||
| 913 | { push @res, $doc->createTextNode($indent.$elem->{example}); | ||
| 914 | } | ||
| 915 | else | ||
| 916 | { my $node = $self->_xmlAny($doc, $elem, $nest_indent, $args); | ||
| 917 | push @res, $doc->createTextNode($indent) | ||
| 918 | if $node->isa('XML::LibXML::Element'); | ||
| 919 | push @res, $node; | ||
| 920 | } | ||
| 921 | } | ||
| 922 | |||
| 923 | (my $outdent = $indent) =~ s/$args->{indent}$//; # sorry | ||
| 924 | |||
| 925 | if(my $example = $ast->{example}) | ||
| 926 | { push @res, $doc->createTextNode | ||
| 927 | (@comment ? "$indent$example$outdent" : $example) | ||
| 928 | } | ||
| 929 | |||
| 930 | if($ast->{_TYPE} && $args->{show_type}) | ||
| 931 | { my $pref = $self->prefixed($ast->{_TYPE}); | ||
| 932 | push @res, $doc->createAttribute("$xsi:type" => $pref); | ||
| 933 | } | ||
| 934 | |||
| 935 | return @res | ||
| 936 | if wantarray; | ||
| 937 | |||
| 938 | my $node = $doc->createElement($ast->{tag}); | ||
| 939 | $node->addChild($_) for @res; | ||
| 940 | $node->appendText($outdent) if @elems; | ||
| 941 | $node; | ||
| 942 | } | ||
| 943 | |||
| 944 | sub makeBlocked($$$) | ||
| 945 | { my ($self, $where, $class, $type) = @_; | ||
| 946 | panic "namespace blocking not yet supported for Templates"; | ||
| 947 | } | ||
| 948 | |||
| 949 | |||
| 950 | 1; |