| 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; | ||
| 10 | use vars '$VERSION'; | ||
| 11 | $VERSION = '1.60'; | ||
| 12 | |||
| 13 | |||
| 14 | use warnings; | ||
| 15 | use strict; | ||
| 16 | no warnings 'recursion'; # trees can be quite deep | ||
| 17 | |||
| 18 | # Errors are either in _class 'usage': called with request | ||
| 19 | # or 'schema': syntax error in schema | ||
| 20 | |||
| 21 | use Log::Report 'xml-compile', syntax => 'SHORT'; | ||
| 22 | use List::Util qw/first max/; | ||
| 23 | |||
| 24 | use XML::Compile::Schema::Specs; | ||
| 25 | use XML::Compile::Schema::BuiltInFacets; | ||
| 26 | use XML::Compile::Schema::BuiltInTypes qw/%builtin_types/; | ||
| 27 | use XML::Compile::Util qw/pack_type unpack_type type_of_node SCHEMA2001 | ||
| 28 | unpack_id/; | ||
| 29 | use XML::Compile::Iterator (); | ||
| 30 | |||
| 31 | my %translators = | ||
| 32 | ( READER => 'XML::Compile::Translate::Reader' | ||
| 33 | , WRITER => 'XML::Compile::Translate::Writer' | ||
| 34 | , TEMPLATE => 'XML::Compile::Translate::Template' | ||
| 35 | ); | ||
| 36 | |||
| 37 | # Elements from the schema to ignore: remember, we are collecting data | ||
| 38 | # from the schema, but only use selective items to produce processors. | ||
| 39 | # All the sub-elements of these will be ignored automatically | ||
| 40 | # Don't known whether we ever need the notation... maybe | ||
| 41 | my $assertions = qr/assert|report/; | ||
| 42 | my $id_constraints = qr/unique|key|keyref/; | ||
| 43 | my $ignore_elements = qr/^(?:notation|annotation|$id_constraints|$assertions)$/; | ||
| 44 | |||
| 45 | my $particle_blocks = qr/^(?:sequence|choice|all|group)$/; | ||
| 46 | my $attribute_defs = qr/^(?:attribute|attributeGroup|anyAttribute)$/; | ||
| 47 | |||
| 48 | |||
| 49 | sub new($@) | ||
| 50 | { my ($baseclass, $trans) = (shift, shift); | ||
| 51 | my $class = $translators{$trans} | ||
| 52 | or error __x"translator back-end {name} not defined", name => $trans; | ||
| 53 | |||
| 54 | eval "require $class"; | ||
| 55 | fault $@ if $@; | ||
| 56 | |||
| 57 | (bless {}, $class)->init( {@_} ); | ||
| 58 | } | ||
| 59 | |||
| 60 | sub init($) | ||
| 61 | { my ($self, $args) = @_; | ||
| 62 | $self->{nss} = $args->{nss} or panic "no namespace tables"; | ||
| 63 | $self->{prefixes} = $args->{prefixes} || {}; | ||
| 64 | $self; | ||
| 65 | } | ||
| 66 | |||
| 67 | |||
| 68 | sub register($) | ||
| 69 | { my ($class, $name) = @_; | ||
| 70 | UNIVERSAL::isa($class, __PACKAGE__) | ||
| 71 | or error __x"back-end {class} does not extend {base}" | ||
| 72 | , class => $class, base => __PACKAGE__; | ||
| 73 | $translators{$name} = $class; | ||
| 74 | } | ||
| 75 | |||
| 76 | |||
| 77 | # may disappear, so not documented publicly (yet) | ||
| 78 | sub actsAs($) { panic "not implemented" } | ||
| 79 | |||
| 80 | #-------- | ||
| 81 | |||
| 82 | sub compile($@) | ||
| 83 | { my ($self, $item, %args) = @_; | ||
| 84 | @$self{keys %args} = values %args; # dirty. Always all the same fields | ||
| 85 | |||
| 86 | my $path = $self->prefixed($item, 1) || $item; | ||
| 87 | ref $item | ||
| 88 | and panic "expecting an item as point to start at $path"; | ||
| 89 | |||
| 90 | my $hooks = $self->{hooks} ||= []; | ||
| 91 | my $typemap = $self->{typemap} ||= {}; | ||
| 92 | $self->typemapToHooks($hooks, $typemap); | ||
| 93 | |||
| 94 | $self->{blocked_nss} | ||
| 95 | = $self->decodeBlocked(delete $self->{block_namespace}); | ||
| 96 | |||
| 97 | my $nsp = $self->namespaces; | ||
| 98 | foreach my $t (keys %$typemap) | ||
| 99 | { $nsp->find(complexType => $t) || $nsp->find(simpleType => $t) | ||
| 100 | or error __x"complex or simpleType {type} for typemap unknown" | ||
| 101 | , type => $t; | ||
| 102 | } | ||
| 103 | |||
| 104 | if(my $def = $self->namespaces->findID($item)) | ||
| 105 | { my $node = $def->{node}; | ||
| 106 | my $name = $node->localName; | ||
| 107 | $item = $def->{full}; | ||
| 108 | } | ||
| 109 | |||
| 110 | delete $self->{_created}; | ||
| 111 | my $produce = $self->topLevel($path, $item, 1); | ||
| 112 | delete $self->{_created}; | ||
| 113 | |||
| 114 | my $in = $self->{include_namespaces} | ||
| 115 | or return $produce; | ||
| 116 | |||
| 117 | $self->makeWrapperNs($path, $produce, $self->{prefixes}, $in); | ||
| 118 | } | ||
| 119 | |||
| 120 | sub assertType($$$$) | ||
| 121 | { my ($self, $where, $field, $type, $value) = @_; | ||
| 122 | my $checker = $builtin_types{$type}{check}; | ||
| 123 | unless(defined $checker) | ||
| 124 | { mistake "useless assert for type $type"; | ||
| 125 | return; | ||
| 126 | } | ||
| 127 | |||
| 128 | return if $checker->($value); | ||
| 129 | |||
| 130 | error __x"field {field} contains '{value}' which is not a valid {type} at {where}" | ||
| 131 | , field => $field, value => $value, type => $type, where => $where | ||
| 132 | , _class => 'usage'; | ||
| 133 | |||
| 134 | } | ||
| 135 | |||
| 136 | sub extendAttrs($@) | ||
| 137 | { my ($self, $in, $add) = @_; | ||
| 138 | |||
| 139 | if(my $a = $add->{attrs}) | ||
| 140 | { # new attrs overrule old definitions (restrictions) | ||
| 141 | my (@attrs, %code); | ||
| 142 | my @all = (@{$in->{attrs} || []}, @{$add->{attrs} || []}); | ||
| 143 | while(@all) | ||
| 144 | { my ($type, $code) = (shift @all, shift @all); | ||
| 145 | if($code{$type}) | ||
| 146 | { $attrs[$code{$type}] = $code; | ||
| 147 | } | ||
| 148 | else | ||
| 149 | { push @attrs, $type => $code; | ||
| 150 | $code{$type} = $#attrs; | ||
| 151 | } | ||
| 152 | } | ||
| 153 | $in->{attrs} = \@attrs; | ||
| 154 | } | ||
| 155 | |||
| 156 | # doing this correctly is too complex for now | ||
| 157 | unshift @{$in->{attrs_any}}, @{$add->{attrs_any}} if $add->{attrs_any}; | ||
| 158 | $in; | ||
| 159 | } | ||
| 160 | |||
| 161 | sub isTrue($) { $_[1] eq '1' || $_[1] eq 'true' } | ||
| 162 | |||
| 163 | # Find the namespace use details of a certain top-level element or | ||
| 164 | # attribute. | ||
| 165 | sub nsContext($) | ||
| 166 | { my ($self, $def) = @_; | ||
| 167 | $def or return {}; | ||
| 168 | |||
| 169 | my $tns = $def->{ns}; | ||
| 170 | |||
| 171 | # top elements are to be qualified unless there is no targetNamespace | ||
| 172 | my %context = (tns => $tns, qual_top => ($tns ? 1 : 0)); | ||
| 173 | |||
| 174 | my $el_qual = $def->{efd} eq 'qualified'; | ||
| 175 | if(exists $self->{elements_qualified}) | ||
| 176 | { my $qual = $self->{elements_qualified} || 0; | ||
| 177 | if($qual eq 'TOP') | ||
| 178 | { $tns or error __x"application requires that element `{name}' has a targetNamespace" | ||
| 179 | , name => $def->{full}; | ||
| 180 | } | ||
| 181 | else | ||
| 182 | { $el_qual = $qual eq 'ALL' ? 1 : $qual eq 'NONE' ? 0 : $qual; | ||
| 183 | } | ||
| 184 | $context{qual_top} = 0 if $qual eq 'NONE'; | ||
| 185 | } | ||
| 186 | $context{qual_elem} = $el_qual; | ||
| 187 | |||
| 188 | my $at_qual = $def->{afd} eq 'qualified'; | ||
| 189 | if(exists $self->{attributes_qualified}) | ||
| 190 | { my $qual = $self->{attributes_qualified} || 0; | ||
| 191 | if($qual eq 'TOP') | ||
| 192 | { $tns or error __x"application requires that attibute `{name}' has a targetNamespace", name => $def->{full}; | ||
| 193 | } | ||
| 194 | else | ||
| 195 | { $at_qual = $qual eq 'ALL' ? 1 : $qual eq 'NONE' ? 0 : $qual; | ||
| 196 | } | ||
| 197 | } | ||
| 198 | $context{qual_attr} = $at_qual; | ||
| 199 | |||
| 200 | \%context; | ||
| 201 | } | ||
| 202 | |||
| 203 | sub namespaces() { $_[0]->{nss} } | ||
| 204 | |||
| 205 | sub topLevel($$;$) | ||
| 206 | { my ($self, $path, $fullname, $is_root) = @_; | ||
| 207 | |||
| 208 | # built-in types have to be handled differently. | ||
| 209 | my $internal = XML::Compile::Schema::Specs->builtInType(undef, $fullname | ||
| 210 | , sloppy_integers => $self->{sloppy_integers} | ||
| 211 | , sloppy_floats => $self->{sloppy_floats} | ||
| 212 | , json_friendly => $self->{json_friendly} | ||
| 213 | ); | ||
| 214 | |||
| 215 | if($internal) | ||
| 216 | { my $builtin = $self->makeBuiltin($fullname, undef | ||
| 217 | , $fullname, $internal, $self->{check_values}); | ||
| 218 | my $builder = $self->actsAs('WRITER') | ||
| 219 | ? sub { $_[0]->createTextNode($builtin->(@_)) } | ||
| 220 | : $builtin; | ||
| 221 | return $self->makeElementWrapper($path, $builder); | ||
| 222 | } | ||
| 223 | |||
| 224 | my $nss = $self->namespaces; | ||
| 225 | my $top = $nss->find(element => $fullname) | ||
| 226 | || $nss->find(attribute => $fullname) | ||
| 227 | or error __x(( $fullname eq $path | ||
| 228 | ? N__"cannot find element or attribute `{name}'" | ||
| 229 | : N__"cannot find element or attribute `{name}' at {where}" | ||
| 230 | ), name => $fullname, where => $path, _class => 'usage'); | ||
| 231 | |||
| 232 | # filter the nodes in the schema which are to be processed | ||
| 233 | my $node = $top->{node}; | ||
| 234 | my $schemans = $node->namespaceURI; | ||
| 235 | my $tree = XML::Compile::Iterator->new($node, $path, sub | ||
| 236 | { my $n = shift; | ||
| 237 | $n->isa('XML::LibXML::Element') | ||
| 238 | && $n->namespaceURI eq $schemans | ||
| 239 | && $n->localName !~ $ignore_elements | ||
| 240 | }); | ||
| 241 | |||
| 242 | delete $self->{_nest}; # reset recursion administration | ||
| 243 | |||
| 244 | local $self->{_context} = $self->nsContext($top); | ||
| 245 | my $name = $node->localName; | ||
| 246 | my $data; | ||
| 247 | if($name eq 'element') | ||
| 248 | { my ($label, $make) = $self->element($tree, $is_root); | ||
| 249 | $data = $self->makeElementWrapper($path, $make) if $make; | ||
| 250 | } | ||
| 251 | elsif($name eq 'attribute') | ||
| 252 | { my $make = $self->attribute($tree); | ||
| 253 | $data = $self->makeAttributeWrapper($path, $make) if $make; | ||
| 254 | } | ||
| 255 | else | ||
| 256 | { error __x"top-level `{full}' is not an element or attribute but {name} at {where}" | ||
| 257 | , full => $fullname, name => $name, where => $tree->path | ||
| 258 | , _class => 'usage'; | ||
| 259 | } | ||
| 260 | |||
| 261 | $data; | ||
| 262 | } | ||
| 263 | |||
| 264 | sub typeByName($$$) | ||
| 265 | { my ($self, $where, $tree, $typename) = @_; | ||
| 266 | |||
| 267 | my $node = $tree->node; | ||
| 268 | |||
| 269 | # | ||
| 270 | # Try to detect a built-in type | ||
| 271 | # | ||
| 272 | |||
| 273 | my $def = XML::Compile::Schema::Specs->builtInType($node, $typename | ||
| 274 | , sloppy_integers => $self->{sloppy_integers} | ||
| 275 | , sloppy_floats => $self->{sloppy_floats} | ||
| 276 | , json_friendly => $self->{json_friendly} | ||
| 277 | ); | ||
| 278 | |||
| 279 | if($def) | ||
| 280 | { # Is built-in | ||
| 281 | my $st = $self->makeBuiltin($where, $node, $typename, $def, $self->{check_values}); | ||
| 282 | |||
| 283 | return +{ st => $st, is_list => $def->{is_list} }; | ||
| 284 | } | ||
| 285 | |||
| 286 | # | ||
| 287 | # not a schema standard type | ||
| 288 | # | ||
| 289 | my $top = $self->namespaces->find(complexType => $typename) | ||
| 290 | || $self->namespaces->find(simpleType => $typename) | ||
| 291 | or error __x"cannot find type {type} at {where}" | ||
| 292 | , type => $typename, where => $where, _class => 'usage'; | ||
| 293 | |||
| 294 | local $self->{_context} = $self->nsContext($top); | ||
| 295 | my $typeimpl = $tree->descend($top->{node}); | ||
| 296 | |||
| 297 | my $typedef = $top->{type}; | ||
| 298 | $typedef eq 'simpleType' ? $self->simpleType($typeimpl) | ||
| 299 | : $typedef eq 'complexType' ? $self->complexType($typeimpl) | ||
| 300 | : error __x"expecting simple- or complexType, not '{type}' at {where}" | ||
| 301 | , type => $typedef, where => $tree->path, _class => 'schema'; | ||
| 302 | } | ||
| 303 | |||
| 304 | sub simpleType($;$) | ||
| 305 | { my ($self, $tree, $in_list) = @_; | ||
| 306 | |||
| 307 | $tree->nrChildren==1 | ||
| 308 | or error __x"simpleType must have exactly one child at {where}" | ||
| 309 | , where => $tree->path, _class => 'schema'; | ||
| 310 | |||
| 311 | my $child = $tree->firstChild; | ||
| 312 | my $name = $child->localName; | ||
| 313 | my $nest = $tree->descend($child); | ||
| 314 | |||
| 315 | # Full content: | ||
| 316 | # annotation? | ||
| 317 | # , (restriction | list | union) | ||
| 318 | |||
| 319 | my $type | ||
| 320 | = $name eq 'restriction' ? $self->simpleRestriction($nest, $in_list) | ||
| 321 | : $name eq 'list' ? $self->simpleList($nest) | ||
| 322 | : $name eq 'union' ? $self->simpleUnion($nest) | ||
| 323 | : error __x"simpleType contains '{local}', must be restriction, list, or union at {where}" | ||
| 324 | , local => $name, where => $tree->path, _class => 'schema'; | ||
| 325 | |||
| 326 | delete @$type{'attrs','attrs_any'}; # spec says ignore attrs | ||
| 327 | $type; | ||
| 328 | } | ||
| 329 | |||
| 330 | sub simpleList($) | ||
| 331 | { my ($self, $tree) = @_; | ||
| 332 | |||
| 333 | # attributes: id, itemType = QName | ||
| 334 | # content: annotation?, simpleType? | ||
| 335 | |||
| 336 | my $per_item; | ||
| 337 | my $node = $tree->node; | ||
| 338 | my $where = $tree->path . '#list'; | ||
| 339 | |||
| 340 | if(my $type = $node->getAttribute('itemType')) | ||
| 341 | { $tree->nrChildren==0 | ||
| 342 | or error __x"list with both itemType and content at {where}" | ||
| 343 | , where => $where, _class => 'schema'; | ||
| 344 | |||
| 345 | my $typename = $self->rel2abs($where, $node, $type); | ||
| 346 | $per_item = $self->blocked($where, simpleType => $typename) | ||
| 347 | || $self->typeByName($where, $tree, $typename); | ||
| 348 | } | ||
| 349 | else | ||
| 350 | { $tree->nrChildren==1 | ||
| 351 | or error __x"list expects one simpleType child at {where}" | ||
| 352 | , where => $where, _class => 'schema'; | ||
| 353 | |||
| 354 | $tree->currentLocal eq 'simpleType' | ||
| 355 | or error __x"list can only have a simpleType child at {where}" | ||
| 356 | , where => $where, _class => 'schema'; | ||
| 357 | |||
| 358 | $per_item = $self->simpleType($tree->descend, 1); | ||
| 359 | } | ||
| 360 | |||
| 361 | my $st = $per_item->{st} | ||
| 362 | or panic "list did not produce a simple type at $where"; | ||
| 363 | |||
| 364 | $per_item->{st} = $self->makeList($where, $st); | ||
| 365 | $per_item->{is_list} = 1; | ||
| 366 | $per_item; | ||
| 367 | } | ||
| 368 | |||
| 369 | sub simpleUnion($) | ||
| 370 | { my ($self, $tree) = @_; | ||
| 371 | |||
| 372 | # attributes: id, memberTypes = List of QName | ||
| 373 | # content: annotation?, simpleType* | ||
| 374 | |||
| 375 | my $node = $tree->node; | ||
| 376 | my $where = $tree->path . '#union'; | ||
| 377 | |||
| 378 | # Normal error handling switched off, and check_values must be on | ||
| 379 | # When check_values is off, we may decide later to treat that as | ||
| 380 | # string, which is faster but not 100% safe, where int 2 may be | ||
| 381 | # formatted as float 1.999 | ||
| 382 | |||
| 383 | local $self->{check_values} = 1; | ||
| 384 | |||
| 385 | my @types; | ||
| 386 | if(my $members = $node->getAttribute('memberTypes')) | ||
| 387 | { foreach my $union (split " ", $members) | ||
| 388 | { my $typename = $self->rel2abs($where, $node, $union); | ||
| 389 | my $type = $self->blocked($where, simpleType => $typename) | ||
| 390 | || $self->typeByName($where, $tree, $typename); | ||
| 391 | my $st = $type->{st} | ||
| 392 | or error __x"union only of simpleTypes, but {type} is complex at {where}" | ||
| 393 | , type => $typename, where => $where, _class => 'schema'; | ||
| 394 | |||
| 395 | push @types, $st; | ||
| 396 | } | ||
| 397 | } | ||
| 398 | |||
| 399 | foreach my $child ($tree->childs) | ||
| 400 | { my $name = $child->localName; | ||
| 401 | $name eq 'simpleType' | ||
| 402 | or error __x"only simpleType's within union, found {local} at {where}" | ||
| 403 | , local => $name, where => $where, _class => 'schema'; | ||
| 404 | |||
| 405 | my $ctype = $self->simpleType($tree->descend($child), 0); | ||
| 406 | push @types, $ctype->{st}; | ||
| 407 | } | ||
| 408 | |||
| 409 | my $do = $self->makeUnion($where, @types); | ||
| 410 | { st => $do, is_union => 1 }; | ||
| 411 | } | ||
| 412 | |||
| 413 | sub simpleRestriction($$) | ||
| 414 | { my ($self, $tree, $in_list) = @_; | ||
| 415 | |||
| 416 | # attributes: id, base = QName | ||
| 417 | # content: annotation?, simpleType?, facet* | ||
| 418 | |||
| 419 | my $node = $tree->node; | ||
| 420 | my $where = $tree->path . '#sres'; | ||
| 421 | |||
| 422 | my ($base, $typename); | ||
| 423 | if(my $basename = $node->getAttribute('base')) | ||
| 424 | { $typename = $self->rel2abs($where, $node, $basename); | ||
| 425 | $base = $self->blocked($where, simpleType => $typename) | ||
| 426 | || $self->typeByName($where, $tree, $typename); | ||
| 427 | } | ||
| 428 | else | ||
| 429 | { my $simple = $tree->firstChild | ||
| 430 | or error __x"no base in simple-restriction, so simpleType required at {where}" | ||
| 431 | , where => $where, _class => 'schema'; | ||
| 432 | |||
| 433 | $simple->localName eq 'simpleType' | ||
| 434 | or error __x"simpleType expected, because there is no base attribute at {where}" | ||
| 435 | , where => $where, _class => 'schema'; | ||
| 436 | |||
| 437 | $base = $self->simpleType($tree->descend($simple, 'st')); | ||
| 438 | |||
| 439 | if((my $r) = $simple->getChildrenByLocalName('restriction')) { | ||
| 440 | # <simpleType><restriction><simpleType><restriction base=xxx></simpleType>@facets</simpleType> | ||
| 441 | my $basename = $r->getAttribute('base'); | ||
| 442 | $typename = $self->rel2abs($where, $r, $basename) if $r; | ||
| 443 | } | ||
| 444 | |||
| 445 | $tree->nextChild; | ||
| 446 | } | ||
| 447 | |||
| 448 | my $st = $base->{st} | ||
| 449 | or error __x"simple-restriction is not a simpleType at {where}" | ||
| 450 | , where => $where, _class => 'schema'; | ||
| 451 | |||
| 452 | my $do = $self->applySimpleFacets($tree, $st | ||
| 453 | , $in_list || $base->{is_list}, $typename); | ||
| 454 | |||
| 455 | $tree->currentChild | ||
| 456 | and error __x"elements left at tail at {where}" | ||
| 457 | , where => $tree->path, _class => 'schema'; | ||
| 458 | |||
| 459 | +{ st => $do }; | ||
| 460 | } | ||
| 461 | |||
| 462 | # Early=lexical space, Late=value space | ||
| 463 | my %facets_early = map +($_ => 1), qw/whiteSpace pattern/; | ||
| 464 | #my %facets_late = map +($_ => 1), qw/totalDigits maxScale minScale enumeration | ||
| 465 | # maxInclusive maxExclusive minInclusive minExclusive fractionDigits | ||
| 466 | # length minLength maxLength/; | ||
| 467 | |||
| 468 | my $qname_type = pack_type SCHEMA2001, 'QName'; | ||
| 469 | |||
| 470 | sub applySimpleFacets($$$$) | ||
| 471 | { my ($self, $tree, $st, $is_list, $type) = @_; | ||
| 472 | my $nss = $self->{nss}; | ||
| 473 | |||
| 474 | # partial | ||
| 475 | # content: facet* | ||
| 476 | # facet = minExclusive | minInclusive | maxExclusive | maxInclusive | ||
| 477 | # | totalDigits | fractionDigits | maxScale | minScale | length | ||
| 478 | # | minLength | maxLength | enumeration | whiteSpace | pattern | ||
| 479 | |||
| 480 | my $where = $tree->path . '#facet'; | ||
| 481 | my (%facets, $is_qname); | ||
| 482 | for(my $child = $tree->currentChild; $child; $child = $tree->nextChild) | ||
| 483 | { my $facet = $child->localName; | ||
| 484 | last if $facet =~ $attribute_defs; | ||
| 485 | |||
| 486 | my $value = $child->getAttribute('value'); | ||
| 487 | defined $value | ||
| 488 | or error __x"no value for facet `{facet}' at {where}" | ||
| 489 | , facet => $facet, where => $where, _class => 'schema'; | ||
| 490 | |||
| 491 | if($facet eq 'enumeration') | ||
| 492 | { $is_qname = $nss->doesExtend($type, $qname_type) | ||
| 493 | unless defined $is_qname; | ||
| 494 | |||
| 495 | if($is_qname) | ||
| 496 | { # rewrite prefixed values into "{ns}local" | ||
| 497 | my ($prefix, $local) | ||
| 498 | = $value =~ m/\:/ ? split(/\:/, $value, 2) : ('', $value); | ||
| 499 | my $ns = $child->lookupNamespaceURI($prefix); | ||
| 500 | $value = pack_type $ns, $local; | ||
| 501 | $self->_registerNSprefix($prefix, $ns, 1); | ||
| 502 | } | ||
| 503 | |||
| 504 | push @{$facets{enumeration}}, $value; | ||
| 505 | } | ||
| 506 | elsif($facet eq 'pattern') { push @{$facets{pattern}}, $value } | ||
| 507 | elsif(!exists $facets{$facet}) { $facets{$facet} = $value } | ||
| 508 | else | ||
| 509 | { error __x"facet `{facet}' defined twice at {where}" | ||
| 510 | , facet => $facet, where => $where, _class => 'schema'; | ||
| 511 | } | ||
| 512 | } | ||
| 513 | |||
| 514 | return $st | ||
| 515 | if $self->{ignore_facets} || !keys %facets; | ||
| 516 | |||
| 517 | my %facets_info = %facets; | ||
| 518 | |||
| 519 | # | ||
| 520 | # new facets overrule all of the base-class | ||
| 521 | # | ||
| 522 | |||
| 523 | if(defined $facets{totalDigits} && defined $facets{fractionDigits}) | ||
| 524 | { my $td = delete $facets{totalDigits}; | ||
| 525 | my $fd = delete $facets{fractionDigits}; | ||
| 526 | $facets{_totalFracDigits} = [$td, $fd]; | ||
| 527 | } | ||
| 528 | |||
| 529 | my (@early, @late); | ||
| 530 | my $action = $self->actsAs('WRITER') ? 'WRITER' : 'READER'; | ||
| 531 | foreach my $facet (keys %facets) | ||
| 532 | { my $h = builtin_facet($where, $self, $facet | ||
| 533 | , $facets{$facet}, $is_list, $type, $nss, $action) or next; | ||
| 534 | |||
| 535 | if($facets_early{$facet}) | ||
| 536 | { push @early, $h } | ||
| 537 | else { push @late, $h } | ||
| 538 | } | ||
| 539 | |||
| 540 | $is_list | ||
| 541 | ? $self->makeFacetsList($where, $st, \%facets_info, \@early, \@late) | ||
| 542 | : $self->makeFacets($where, $st, \%facets_info, \@early, \@late); | ||
| 543 | } | ||
| 544 | |||
| 545 | sub element($;$) | ||
| 546 | { my ($self, $tree, $is_root) = @_; | ||
| 547 | |||
| 548 | # attributes: abstract, default, fixed, form, id, maxOccurs, minOccurs | ||
| 549 | # , name, nillable, ref, substitutionGroup, targetNamespace, type | ||
| 550 | # ignored: block, final, targetNamespace additional restrictions | ||
| 551 | # content: annotation? | ||
| 552 | # , (simpleType | complexType)? | ||
| 553 | # , (unique | key | keyref)* | ||
| 554 | |||
| 555 | my $node = $tree->node; | ||
| 556 | my $parent = $node->parentNode; | ||
| 557 | my $is_global= $parent | ||
| 558 | && $parent->isa('XML::LibXML::Element') | ||
| 559 | && $parent->localname eq 'schema'; | ||
| 560 | |||
| 561 | my $where = $tree->path; | ||
| 562 | |||
| 563 | my $name = $node->getAttribute('name') | ||
| 564 | or error __x"element has no name nor ref at {where}" | ||
| 565 | , where => $where, _class => 'schema'; | ||
| 566 | $self->assertType($where, name => NCName => $name); | ||
| 567 | |||
| 568 | # Full name based on current context. This might be a global name | ||
| 569 | # or a local name. | ||
| 570 | |||
| 571 | my $context = $self->{_context}; | ||
| 572 | |||
| 573 | # Determine the context of this element. When it is a global, we need | ||
| 574 | # to set-up a new context until end-of-function. | ||
| 575 | |||
| 576 | my $abstract = 0; | ||
| 577 | my ($qual, $ns, $fullname); | ||
| 578 | |||
| 579 | if($is_global) | ||
| 580 | { $ns = $node->getAttribute('targetNamespace') | ||
| 581 | || $parent->getAttribute('targetNamespace'); | ||
| 582 | $fullname= pack_type $ns, $name; | ||
| 583 | my $def = $self->namespaces->find(element => $fullname); | ||
| 584 | $context = $self->nsContext($def); | ||
| 585 | $qual = $context->{qual_top}; | ||
| 586 | |||
| 587 | # abstract elements are not to be used in messages. | ||
| 588 | $abstract = $self->{abstract_types} eq 'ACCEPT' ? 0 : $def->{abstract}; | ||
| 589 | } | ||
| 590 | else | ||
| 591 | { $qual = $context->{qual_elem}; | ||
| 592 | $ns = $node->getAttribute('targetNamespace') || $context->{tns}; | ||
| 593 | $fullname = pack_type $ns, $name; | ||
| 594 | } | ||
| 595 | |||
| 596 | if(my $form = $node->getAttribute('form')) | ||
| 597 | { $qual | ||
| 598 | = $form eq 'qualified' ? 1 | ||
| 599 | : $form eq 'unqualified' ? 0 | ||
| 600 | : error __x"form must be (un)qualified, not `{form}' at {where}" | ||
| 601 | , form => $form, where => $where, _class => 'schema'; | ||
| 602 | } | ||
| 603 | |||
| 604 | local $self->{_context} = $context if $is_global; | ||
| 605 | my $nodetype = $qual ? $fullname : $name; | ||
| 606 | |||
| 607 | # SubstitionGroups | ||
| 608 | # We know the type of the message root, so do not need to look for | ||
| 609 | # alternative sgs (and it wouldn't work anyway) | ||
| 610 | |||
| 611 | my @sgs; | ||
| 612 | @sgs = $self->namespaces->findSgMembers($node->localName, $fullname) | ||
| 613 | unless $is_root; | ||
| 614 | |||
| 615 | # Handle re-usable fragments, fight against combinatorial explosions | ||
| 616 | |||
| 617 | my $nodeid = $node->unique_key; #$node->nodePath.'#'.$fullname; | ||
| 618 | if(my $already = $self->{_created}{$nodeid}) | ||
| 619 | { # We cannot cache compile subst-group handlers, because sgs using | ||
| 620 | # elements which were already compiled into sgs does not work. | ||
| 621 | $already = $self->substitutionGroup($tree, $fullname, $nodetype | ||
| 622 | , $already, \@sgs) if @sgs; | ||
| 623 | return ($nodetype, $already); | ||
| 624 | } | ||
| 625 | |||
| 626 | # Detect recursion | ||
| 627 | # Very complicated: recursively nested structures. It is less of a | ||
| 628 | # problem when you handle in run-time what you see... but we here | ||
| 629 | # have to be prepared for everything. | ||
| 630 | |||
| 631 | if(exists $self->{_nest}{$nodeid}) | ||
| 632 | { my $outer = \$self->{_nest}{$nodeid}; | ||
| 633 | my $nested = sub { $$outer->(@_) }; | ||
| 634 | |||
| 635 | # The code must be blessed in the right class, to be compiled | ||
| 636 | # correctly inside its parent. | ||
| 637 | bless $nested, 'BLOCK' if @sgs; | ||
| 638 | |||
| 639 | return ($nodetype, $nested); | ||
| 640 | } | ||
| 641 | $self->{_nest}{$nodeid} = undef; | ||
| 642 | |||
| 643 | # Construct XML tag to use | ||
| 644 | |||
| 645 | my $trans = $qual ? 'makeTagQualified' : 'makeTagUnqualified'; | ||
| 646 | my $tag = $self->$trans($where, $node, $name, $ns); | ||
| 647 | |||
| 648 | # Construct type processor | ||
| 649 | |||
| 650 | my ($comptype, $comps); | ||
| 651 | my $nr_childs = $tree->nrChildren; | ||
| 652 | if(my $isa = $node->getAttribute('type')) | ||
| 653 | { # explicitly names type | ||
| 654 | $nr_childs==0 | ||
| 655 | or error __x"no childs expected with attribute `type' at {where}" | ||
| 656 | , where => $where, _class => 'schema'; | ||
| 657 | |||
| 658 | $comptype = $self->rel2abs($where, $node, $isa); | ||
| 659 | $comps = $self->blocked($where, anyType => $comptype) | ||
| 660 | || $self->typeByName($where, $tree, $comptype); | ||
| 661 | } | ||
| 662 | elsif($nr_childs==0) | ||
| 663 | { # default type for substGroups is type of base-class | ||
| 664 | my $base_node = $node; | ||
| 665 | local $self->{_context}; | ||
| 666 | while(my $subst = $base_node->getAttribute('substitutionGroup')) | ||
| 667 | { my $subst_elem = $self->rel2abs($where, $base_node, $subst); | ||
| 668 | my $base_elem = $self->namespaces->find(element => $subst_elem); | ||
| 669 | $self->{_context} = $self->nsContext($base_elem); | ||
| 670 | $base_node = $base_elem->{node}; | ||
| 671 | my $isa = $base_node->getAttribute('type') | ||
| 672 | or next; | ||
| 673 | |||
| 674 | $comptype = $self->rel2abs($where, $base_node, $isa); | ||
| 675 | $comps = $self->blocked($where, complexType => $comptype) | ||
| 676 | || $self->typeByName($where, $tree, $comptype); | ||
| 677 | last; | ||
| 678 | } | ||
| 679 | unless($comptype) | ||
| 680 | { # no type found, so anyType | ||
| 681 | $comptype = $self->anyType($node); | ||
| 682 | $comps = $self->typeByName($where, $tree, $comptype); | ||
| 683 | } | ||
| 684 | } | ||
| 685 | elsif($nr_childs!=1) | ||
| 686 | { error __x"expected is only one child node at {where}" | ||
| 687 | , where => $where, _class => 'schema'; | ||
| 688 | } | ||
| 689 | else # nameless types | ||
| 690 | { my $child = $tree->firstChild; | ||
| 691 | my $local = $child->localname; | ||
| 692 | my $nest = $tree->descend($child); | ||
| 693 | |||
| 694 | ($comps, $comptype) | ||
| 695 | = $local eq 'simpleType' | ||
| 696 | ? ($self->simpleType($nest, 0), 'unnamed simple') | ||
| 697 | : $local eq 'complexType' | ||
| 698 | ? ($self->complexType($nest), 'unnamed complex') | ||
| 699 | : error __x"illegal element child `{name}' at {where}" | ||
| 700 | , name => $local, where => $where, _class => 'schema'; | ||
| 701 | } | ||
| 702 | |||
| 703 | my ($st, $elems, $attrs, $attrs_any) | ||
| 704 | = @$comps{ qw/st elems attrs attrs_any/ }; | ||
| 705 | $_ ||= [] for $elems, $attrs, $attrs_any; | ||
| 706 | |||
| 707 | # Construct basic element handler | ||
| 708 | |||
| 709 | my $is_simple = defined $st; | ||
| 710 | my $nillable = $self->isTrue($node->getAttribute('nillable') || 'false'); | ||
| 711 | |||
| 712 | my $elem_handler | ||
| 713 | = $comps->{mixed} ? 'makeMixedElement' | ||
| 714 | : ! $is_simple ? 'makeComplexElement' # other complexType | ||
| 715 | : (@$attrs || @$attrs_any) ? 'makeTaggedElement' # complex/simpleContent | ||
| 716 | : 'makeSimpleElement'; | ||
| 717 | |||
| 718 | my $r = $self->$elem_handler | ||
| 719 | ( $where, $tag, ($st||$elems), $attrs, $attrs_any, $comptype, $nillable); | ||
| 720 | |||
| 721 | # Add defaults and stuff | ||
| 722 | my $default = $node->getAttributeNode('default'); | ||
| 723 | my $fixed = $node->getAttributeNode('fixed'); | ||
| 724 | |||
| 725 | $default && $fixed | ||
| 726 | and error __x"element can not have default and fixed at {where}" | ||
| 727 | , where => $tree->path, _class => 'schema'; | ||
| 728 | |||
| 729 | my $value | ||
| 730 | = $default ? $default->textContent | ||
| 731 | : $fixed ? $fixed->textContent | ||
| 732 | : undef; | ||
| 733 | |||
| 734 | my $generate | ||
| 735 | = $abstract ? 'makeElementAbstract' | ||
| 736 | : $default ? 'makeElementDefault' | ||
| 737 | : $fixed ? 'makeElementFixed' | ||
| 738 | : 'makeElement'; | ||
| 739 | |||
| 740 | my $do = $self->$generate($where, $ns, $nodetype, $r, $value, $tag); | ||
| 741 | |||
| 742 | # hrefs are used by SOAP-RPC | ||
| 743 | $do = $self->makeElementHref($where, $ns, $nodetype, $do) | ||
| 744 | if $self->{permit_href} && $self->actsAs('READER'); | ||
| 745 | |||
| 746 | # Implement hooks | ||
| 747 | my ($before, $replace, $after) | ||
| 748 | = $self->findHooks($where, $comptype, $node); | ||
| 749 | |||
| 750 | $do = $self->makeHook($where,$do,$tag,$before,$replace,$after,$comptype) | ||
| 751 | if $before || $replace || $after; | ||
| 752 | |||
| 753 | $do = $self->xsiType($tree, $node, $name, $comptype, $do) | ||
| 754 | if $comptype && $self->{xsi_type}{$comptype}; | ||
| 755 | |||
| 756 | $do = $self->addTypeAttribute($comptype, $do) | ||
| 757 | if $self->{xsi_type_everywhere} && $comptype !~ /^unnamed /; | ||
| 758 | |||
| 759 | $self->{_created}{$nodeid} = $do; | ||
| 760 | |||
| 761 | $do = $self->substitutionGroup($tree, $fullname, $nodetype, $do, \@sgs) | ||
| 762 | if @sgs; | ||
| 763 | |||
| 764 | # handle recursion | ||
| 765 | # this must look very silly to you... however, this is resolving | ||
| 766 | # recursive schemas: this way nested use of the same element | ||
| 767 | # definition will catch the code reference of the outer definition. | ||
| 768 | $self->{_nest}{$nodeid} = $do; | ||
| 769 | delete $self->{_nest}{$nodeid}; # clean the outer definition | ||
| 770 | |||
| 771 | ($nodetype, $do); | ||
| 772 | } | ||
| 773 | |||
| 774 | sub particle($) | ||
| 775 | { my ($self, $tree) = @_; | ||
| 776 | |||
| 777 | my $node = $tree->node; | ||
| 778 | my $local = $node->localName; | ||
| 779 | my $where = $tree->path; | ||
| 780 | |||
| 781 | my $min = $node->getAttribute('minOccurs'); | ||
| 782 | my $max = $node->getAttribute('maxOccurs'); | ||
| 783 | |||
| 784 | unless(defined $min) | ||
| 785 | { $min = ($self->actsAs('WRITER') || $self->{default_values} ne 'EXTEND') | ||
| 786 | && ($node->getAttribute('default') || $node->getAttribute('fixed')) | ||
| 787 | ? 0 : 1; | ||
| 788 | } | ||
| 789 | |||
| 790 | $min = 0 if $self->{interpret_nillable_as_optional} | ||
| 791 | && $self->isTrue($node->getAttribute('nillable') || 'false'); | ||
| 792 | |||
| 793 | # default attribute in writer means optional, but we want to see | ||
| 794 | # them in the reader, to see the value. | ||
| 795 | |||
| 796 | defined $max or $max = 1; | ||
| 797 | $max = 'unbounded' | ||
| 798 | if $max ne 'unbounded' && $max > 1 && !$self->{check_occurs}; | ||
| 799 | |||
| 800 | $min = 0 | ||
| 801 | if $max eq 'unbounded' && !$self->{check_occurs}; | ||
| 802 | |||
| 803 | return $self->anyElement($tree, $min, $max) | ||
| 804 | if $local eq 'any'; | ||
| 805 | |||
| 806 | my ($label, $process) | ||
| 807 | = $local eq 'element' ? $self->particleElement($tree) | ||
| 808 | : $local eq 'group' ? $self->particleGroup($tree) | ||
| 809 | : $local =~ $particle_blocks ? $self->particleBlock($tree) | ||
| 810 | : error __x"unknown particle type '{name}' at {where}" | ||
| 811 | , name => $local, where => $tree->path, _class => 'schema'; | ||
| 812 | |||
| 813 | defined $label | ||
| 814 | or return (); | ||
| 815 | |||
| 816 | if(ref $process eq 'BLOCK') | ||
| 817 | { my $key = $self->keyRewrite($label); | ||
| 818 | my $multi = $self->blockLabel($local, $key); | ||
| 819 | return $self->makeBlockHandler($where, $label, $min, $max | ||
| 820 | , $process, $local, $multi); | ||
| 821 | } | ||
| 822 | |||
| 823 | # only elements left | ||
| 824 | my $required; | ||
| 825 | my $key = $self->keyRewrite($label); | ||
| 826 | $required = $self->makeRequired($where, $key, $process) if $min!=0; | ||
| 827 | |||
| 828 | ($self->actsAs('READER') ? $label : $key) => | ||
| 829 | $self->makeElementHandler($where, $key, $min,$max, $required, $process); | ||
| 830 | } | ||
| 831 | |||
| 832 | sub particleElement($) | ||
| 833 | { my ($self, $tree) = @_; | ||
| 834 | |||
| 835 | my $node = $tree->node; | ||
| 836 | if(my $ref = $node->getAttribute('ref')) | ||
| 837 | { my $where = $tree->path . "/$ref"; | ||
| 838 | my $refname = $self->rel2abs($tree, $node, $ref); | ||
| 839 | return () if $self->blocked($where, ref => $refname); | ||
| 840 | |||
| 841 | my $def = $self->namespaces->find(element => $refname) | ||
| 842 | or error __x"cannot find ref element '{name}' at {where}" | ||
| 843 | , name => $refname, where => $where, _class => 'schema'; | ||
| 844 | |||
| 845 | return $self->element($tree->descend($def->{node} | ||
| 846 | , $self->prefixed($refname, 1))); | ||
| 847 | } | ||
| 848 | |||
| 849 | my $name = $node->getAttribute('name'); | ||
| 850 | $self->element($tree->descend($node, $name)); | ||
| 851 | } | ||
| 852 | |||
| 853 | # blockLabel KIND, LABEL | ||
| 854 | # Particle blocks, like `sequence' and `choice', which have a maxOccurs | ||
| 855 | # (maximum occurrence) which is 2 of more, are represented by an ARRAY | ||
| 856 | # of HASHs. The label with such a block is derived from its first element. | ||
| 857 | # This function determines how. | ||
| 858 | # seq_address sequence get seq_ prepended | ||
| 859 | # cho_gender choices get cho_ before them | ||
| 860 | # all_money an all block can also be repreated in spec >1.1 | ||
| 861 | # gr_people group refers to a block of above type, but | ||
| 862 | # that type is not reflected in the name | ||
| 863 | |||
| 864 | my %block_abbrev = qw/sequence seq_ choice cho_ all all_ group gr_/; | ||
| 865 | sub blockLabel($$) | ||
| 866 | { my ($self, $kind, $label) = @_; | ||
| 867 | return $label if $kind eq 'element'; | ||
| 868 | |||
| 869 | $label =~ s/^(?:seq|cho|all|gr)_//; | ||
| 870 | $block_abbrev{$kind} . (unpack_type $label)[1]; | ||
| 871 | } | ||
| 872 | |||
| 873 | sub particleGroup($) | ||
| 874 | { my ($self, $tree) = @_; | ||
| 875 | |||
| 876 | # attributes: id, maxOccurs, minOccurs, name, ref | ||
| 877 | # content: annotation?, (all|choice|sequence)? | ||
| 878 | # apparently, a group can not refer to a group... well.. | ||
| 879 | |||
| 880 | my $node = $tree->node; | ||
| 881 | my $where = $tree->path . '#group'; | ||
| 882 | my $ref = $node->getAttribute('ref') | ||
| 883 | or error __x"group without ref at {where}" | ||
| 884 | , where => $where, _class => 'schema'; | ||
| 885 | |||
| 886 | my $typename = $self->rel2abs($where, $node, $ref); | ||
| 887 | if(my $blocked = $self->blocked($where, ref => $typename)) | ||
| 888 | { return ($typename, $blocked); | ||
| 889 | } | ||
| 890 | |||
| 891 | my $dest = $self->namespaces->find(group => $typename) | ||
| 892 | or error __x"cannot find group `{name}' at {where}" | ||
| 893 | , name => $typename, where => $where, _class => 'schema'; | ||
| 894 | |||
| 895 | my $group = $tree->descend($dest->{node}, $self->prefixed($typename, 1)); | ||
| 896 | return () if $group->nrChildren==0; | ||
| 897 | |||
| 898 | $group->nrChildren==1 | ||
| 899 | or error __x"only one particle block expected in group `{name}' at {where}" | ||
| 900 | , name => $typename, where => $where, _class => 'schema'; | ||
| 901 | |||
| 902 | my $local = $group->currentLocal; | ||
| 903 | $local =~ m/^(?:all|choice|sequence)$/ | ||
| 904 | or error __x"illegal group member `{name}' at {where}" | ||
| 905 | , name => $local, where => $where, _class => 'schema'; | ||
| 906 | |||
| 907 | my ($blocklabel, $code) = $self->particleBlock($group->descend); | ||
| 908 | ($typename, $code); | ||
| 909 | } | ||
| 910 | |||
| 911 | sub particleBlock($) | ||
| 912 | { my ($self, $tree) = @_; | ||
| 913 | |||
| 914 | my $node = $tree->node; | ||
| 915 | my @pairs = map $self->particle($tree->descend($_)), $tree->childs; | ||
| 916 | @pairs or return (); | ||
| 917 | |||
| 918 | # label is name of first component, only needed when maxOcc > 1 | ||
| 919 | my $label = $pairs[0]; | ||
| 920 | my $blocktype = $node->localName; | ||
| 921 | |||
| 922 | my $call = 'make'.ucfirst $blocktype; | ||
| 923 | ($label => $self->$call($tree->path, @pairs)); | ||
| 924 | } | ||
| 925 | |||
| 926 | sub xsiType($$$$$) | ||
| 927 | { my ($self, $tree, $node, $name, $type, $base) = @_; | ||
| 928 | |||
| 929 | my %alt = ($type => $base); | ||
| 930 | |||
| 931 | foreach my $alttype (@{$self->{xsi_type}{$type}}) | ||
| 932 | { next if $alttype eq $type; | ||
| 933 | |||
| 934 | my ($ns, $local) = unpack_type $alttype; | ||
| 935 | my $prefix = $node->lookupNamespacePrefix($ns); | ||
| 936 | defined $prefix | ||
| 937 | or $prefix = $self->_registerNSprefix(undef, $ns, 1); | ||
| 938 | |||
| 939 | my $type = length $prefix ? "$prefix:$local" : $local; | ||
| 940 | |||
| 941 | # do not accidentally use the default namespace, when there | ||
| 942 | # may also be namespace-less types used. | ||
| 943 | my $doc = $node->ownerDocument; | ||
| 944 | my $altnode = $doc->createElement('element'); | ||
| 945 | $altnode->setNamespace(SCHEMA2001, 'temp1234', 1); | ||
| 946 | $altnode->setNamespace($ns, $prefix); | ||
| 947 | $altnode->setAttribute(name => $name); | ||
| 948 | $altnode->setAttribute(type => $type); | ||
| 949 | |||
| 950 | delete $self->{_created}{$altnode->unique_key}; # clean nesting cache | ||
| 951 | (undef, $alt{$alttype}) = $self->element($tree->descend($altnode)); | ||
| 952 | } | ||
| 953 | $self->makeXsiTypeSwitch($tree->path, $name, $type, \%alt); | ||
| 954 | } | ||
| 955 | |||
| 956 | sub substitutionGroup($$$$$) | ||
| 957 | { my ($self, $tree, $fullname, $label, $base, $sgs) = @_; | ||
| 958 | |||
| 959 | if(Log::Report->needs('TRACE')) # dump table of substgroup alternatives | ||
| 960 | { my $labelrw = $self->keyRewrite($label); | ||
| 961 | my @full = sort map $_->{full}, @$sgs; | ||
| 962 | my $longest = max map length, @full; | ||
| 963 | my @c = map sprintf("%-${longest}s %s",$_,$self->keyRewrite($_)), @full; | ||
| 964 | local $" = "\n "; | ||
| 965 | trace "substitutionGroup $fullname$\"BASE=$label ($labelrw)$\"@c"; | ||
| 966 | } | ||
| 967 | |||
| 968 | my @elems; | ||
| 969 | push @elems, $label => [$self->keyRewrite($label), $base] if $base; | ||
| 970 | |||
| 971 | foreach my $subst (@$sgs) | ||
| 972 | { my ($l, $d) = $self->element($tree->descend($subst->{node}), 1); | ||
| 973 | push @elems, $l => [$self->keyRewrite($l), $d] if defined $d; | ||
| 974 | } | ||
| 975 | |||
| 976 | $self->makeSubstgroup($tree->path.'#subst', $fullname, @elems); | ||
| 977 | } | ||
| 978 | |||
| 979 | sub keyRewrite($;$) | ||
| 980 | { my $self = shift; | ||
| 981 | my ($ns, $key) = @_==1 ? unpack_type($_[0]) : @_; | ||
| 982 | my $oldkey = $key; | ||
| 983 | |||
| 984 | foreach my $r ( @{$self->{rewrite}} ) | ||
| 985 | { if(ref $r eq 'HASH') | ||
| 986 | { my $full = pack_type $ns, $key; | ||
| 987 | $key = $r->{$full} if defined $r->{$full}; | ||
| 988 | $key = $r->{$key} if defined $r->{$key}; | ||
| 989 | } | ||
| 990 | elsif(ref $r eq 'CODE') | ||
| 991 | { $key = $r->($ns, $key); | ||
| 992 | } | ||
| 993 | elsif($r eq 'UNDERSCORES') | ||
| 994 | { $key =~ s/-/_/g; | ||
| 995 | } | ||
| 996 | elsif($r eq 'SIMPLIFIED') | ||
| 997 | { $key =~ s/-/_/g; | ||
| 998 | $key =~ s/\W//g; | ||
| 999 | $key = lc $key; | ||
| 1000 | } | ||
| 1001 | elsif($r eq 'PREFIXED') | ||
| 1002 | { my $p = $self->{prefixes}; | ||
| 1003 | my $prefix = $p->{$ns} ? $p->{$ns}{prefix} : ''; | ||
| 1004 | $key = $prefix . '_' . $key if $prefix ne ''; | ||
| 1005 | } | ||
| 1006 | elsif($r =~ m/^PREFIXED\(\s*(.*?)\s*\)$/) | ||
| 1007 | { my @l = split /\s*\,\s*/, $1; | ||
| 1008 | my $p = $self->{prefixes}; | ||
| 1009 | my $prefix = $p->{$ns} ? $p->{$ns}{prefix} : ''; | ||
| 1010 | $key = $prefix . '_' . $key if grep {$prefix eq $_} @l; | ||
| 1011 | } | ||
| 1012 | else | ||
| 1013 | { error __x"key rewrite `{got}' not understood", got => $r; | ||
| 1014 | } | ||
| 1015 | } | ||
| 1016 | |||
| 1017 | trace "rewrote type @_ to $key" | ||
| 1018 | if $key ne $oldkey; | ||
| 1019 | |||
| 1020 | $key; | ||
| 1021 | } | ||
| 1022 | |||
| 1023 | sub prefixed($;$) | ||
| 1024 | { my ($self, $qname, $hide) = @_; | ||
| 1025 | my ($ns, $local) = unpack_type $qname; | ||
| 1026 | defined $ns or return $qname; | ||
| 1027 | |||
| 1028 | my $pn = $self->{prefixes}{$ns} or return; | ||
| 1029 | $pn->{used}++ unless $hide ; | ||
| 1030 | length $pn->{prefix} ? "$pn->{prefix}:$local" : $local; | ||
| 1031 | } | ||
| 1032 | |||
| 1033 | sub prefixForNamespace($) | ||
| 1034 | { my ($self, $ns) = @_; | ||
| 1035 | my $def = $self->{prefixes}{$ns} or return; | ||
| 1036 | $def->{prefix}; | ||
| 1037 | } | ||
| 1038 | |||
| 1039 | sub attribute($) | ||
| 1040 | { my ($self, $tree) = @_; | ||
| 1041 | |||
| 1042 | # attributes: default, fixed, form, id, name, ref, type, use | ||
| 1043 | # content: annotation?, simpleType? | ||
| 1044 | |||
| 1045 | my $node = $tree->node; | ||
| 1046 | my $parent = $node->parentNode; | ||
| 1047 | my $is_global= $parent && $parent->localname eq 'schema'; | ||
| 1048 | my $where = $tree->path; | ||
| 1049 | |||
| 1050 | my $context = $self->{_context}; | ||
| 1051 | |||
| 1052 | if(my $refattr = $node->getAttribute('ref')) | ||
| 1053 | { | ||
| 1054 | my $refname = $self->rel2abs($tree, $node, $refattr); | ||
| 1055 | return () if $self->blocked($where, ref => $refname); | ||
| 1056 | |||
| 1057 | my $def = $self->namespaces->find(attribute => $refname) | ||
| 1058 | or error __x"cannot find attribute {name} at {where}" | ||
| 1059 | , name => $refname, where => $where, _class => 'schema'; | ||
| 1060 | |||
| 1061 | local $self->{_context} = $def; | ||
| 1062 | return $self->attribute($tree->descend($def->{node})); | ||
| 1063 | } | ||
| 1064 | |||
| 1065 | # Not a ref to attribute | ||
| 1066 | my $name = $node->getAttribute('name') | ||
| 1067 | or error __x"attribute without name at {where}", where => $where; | ||
| 1068 | $where .= '/@'.$name; | ||
| 1069 | $self->assertType($where, name => NCName => $name); | ||
| 1070 | |||
| 1071 | my ($qual, $ns, $fullname); | ||
| 1072 | if($is_global) | ||
| 1073 | { $ns = $node->getAttribute('targetNamespace') | ||
| 1074 | || $parent->getAttribute('targetNamespace'); | ||
| 1075 | $fullname= pack_type $ns, $name; | ||
| 1076 | my $def = $self->namespaces->find(attribute => $fullname); | ||
| 1077 | $context = $self->nsContext($def); | ||
| 1078 | $qual = $context->{qual_top}; | ||
| 1079 | } | ||
| 1080 | else | ||
| 1081 | { $qual = $context->{qual_attr}; | ||
| 1082 | $ns = $context->{tns}; | ||
| 1083 | $fullname= pack_type $ns, $name; | ||
| 1084 | } | ||
| 1085 | local $self->{_context} = $context if $is_global; | ||
| 1086 | |||
| 1087 | if(my $form = $node->getAttribute('form')) | ||
| 1088 | { $qual | ||
| 1089 | = $form eq 'qualified' ? 1 | ||
| 1090 | : $form eq 'unqualified' ? 0 | ||
| 1091 | : error __x"form must be (un)qualified, not `{form}' at {where}" | ||
| 1092 | , form => $form, where => $where, _class => 'schema'; | ||
| 1093 | } | ||
| 1094 | |||
| 1095 | # no default prefixes for attributes | ||
| 1096 | error __x"attribute namespace {ns} cannot be the default namespace" | ||
| 1097 | , ns => $ns | ||
| 1098 | if $qual && $ns && $self->prefixForNamespace($ns) eq ''; | ||
| 1099 | |||
| 1100 | my ($type, $typeattr); | ||
| 1101 | if($tree->nrChildren==1) | ||
| 1102 | { $tree->currentLocal eq 'simpleType' | ||
| 1103 | or error __x"attribute child can only be `simpleType', not `{found}' at {where}" | ||
| 1104 | , found => $tree->currentLocal, where => $where | ||
| 1105 | , _class => 'schema'; | ||
| 1106 | |||
| 1107 | $type = $self->simpleType($tree->descend); | ||
| 1108 | } | ||
| 1109 | else | ||
| 1110 | { $name = $node->getAttribute('name') | ||
| 1111 | or error __x"attribute without name or ref at {where}" | ||
| 1112 | , where => $where, _class => 'schema'; | ||
| 1113 | |||
| 1114 | $typeattr = $node->getAttribute('type'); | ||
| 1115 | } | ||
| 1116 | |||
| 1117 | unless($type) | ||
| 1118 | { my $typename = defined $typeattr | ||
| 1119 | ? $self->rel2abs($where, $node, $typeattr) | ||
| 1120 | : $self->anyType($node); | ||
| 1121 | |||
| 1122 | $type = $self->blocked($where, simpleType => $typename) | ||
| 1123 | || $self->typeByName($where, $tree, $typename); | ||
| 1124 | } | ||
| 1125 | |||
| 1126 | my $st = $type->{st} | ||
| 1127 | or error __x"attribute not based in simple value type at {where}" | ||
| 1128 | , where => $where, _class => 'schema'; | ||
| 1129 | |||
| 1130 | my $trans = $qual ? 'makeTagQualified' : 'makeTagUnqualified'; | ||
| 1131 | my $qns = $qual ? $context->{tns} : ''; | ||
| 1132 | my $tag = $self->$trans($where, $node, $name, $qns); | ||
| 1133 | |||
| 1134 | my $use = $node->getAttribute('use') || ''; | ||
| 1135 | $use =~ m/^(?:optional|required|prohibited|)$/ | ||
| 1136 | or error __x"attribute use is required, optional or prohibited (not '{use}') at {where}" | ||
| 1137 | , use => $use, where => $where, _class => 'schema'; | ||
| 1138 | |||
| 1139 | my $default = $node->getAttributeNode('default'); | ||
| 1140 | my $fixed = $node->getAttributeNode('fixed'); | ||
| 1141 | |||
| 1142 | my $generate | ||
| 1143 | = defined $default ? 'makeAttributeDefault' | ||
| 1144 | : defined $fixed ? 'makeAttributeFixed' | ||
| 1145 | : $use eq 'required' ? 'makeAttributeRequired' | ||
| 1146 | : $use eq 'prohibited'? 'makeAttributeProhibited' | ||
| 1147 | : 'makeAttribute'; | ||
| 1148 | |||
| 1149 | my $value = defined $default ? $default : $fixed; | ||
| 1150 | my $label = $self->keyRewrite($qns, $name); | ||
| 1151 | my $do = $self->$generate($where, $qns, $tag, $label, $st, $value); | ||
| 1152 | defined $do ? ($label => $do) : (); | ||
| 1153 | } | ||
| 1154 | |||
| 1155 | sub attributeGroup($) | ||
| 1156 | { my ($self, $tree) = @_; | ||
| 1157 | |||
| 1158 | # attributes: id, ref = QName | ||
| 1159 | # content: annotation? | ||
| 1160 | |||
| 1161 | my $node = $tree->node; | ||
| 1162 | my $where = $tree->path; | ||
| 1163 | my $ref = $node->getAttribute('ref') | ||
| 1164 | or error __x"attributeGroup use without ref at {where}" | ||
| 1165 | , where => $tree->path, _class => 'schema'; | ||
| 1166 | |||
| 1167 | my $typename = $self->rel2abs($where, $node, $ref); | ||
| 1168 | return () if $self->blocked($where, ref => $typename); | ||
| 1169 | |||
| 1170 | my $def = $self->namespaces->find(attributeGroup => $typename) | ||
| 1171 | or error __x"cannot find attributeGroup {name} at {where}" | ||
| 1172 | , name => $typename, where => $where, _class => 'schema'; | ||
| 1173 | |||
| 1174 | local $self->{tns} = $def->{ns}; | ||
| 1175 | $self->attributeList($tree->descend($def->{node})); | ||
| 1176 | } | ||
| 1177 | |||
| 1178 | # Don't known how to handle notQName | ||
| 1179 | sub anyAttribute($) | ||
| 1180 | { my ($self, $tree) = @_; | ||
| 1181 | |||
| 1182 | # attributes: id | ||
| 1183 | # , namespace = ##any|##other| List of (anyURI|##targetNamespace|##local) | ||
| 1184 | # , notNamespace = List of (anyURI|##targetNamespace|##local) | ||
| 1185 | # ignored attributes | ||
| 1186 | # , notQName = List of QName | ||
| 1187 | # , processContents = lax|skip|strict | ||
| 1188 | # content: annotation? | ||
| 1189 | |||
| 1190 | my $node = $tree->node; | ||
| 1191 | my $where = $tree->path . '@any'; | ||
| 1192 | |||
| 1193 | my $handler = $self->{any_attribute}; | ||
| 1194 | my $namespace = $node->getAttribute('namespace') || '##any'; | ||
| 1195 | my $not_ns = $node->getAttribute('notNamespace'); | ||
| 1196 | my $process = $node->getAttribute('processContents') || 'strict'; | ||
| 1197 | |||
| 1198 | warn "HELP: please explain me how to handle notQName" | ||
| 1199 | if $^W && $node->getAttribute('notQName'); | ||
| 1200 | |||
| 1201 | my ($yes, $no) = $self->translateNsLimits($namespace, $not_ns); | ||
| 1202 | my $do = $self->makeAnyAttribute($where, $handler, $yes, $no, $process); | ||
| 1203 | defined $do ? $do : (); | ||
| 1204 | } | ||
| 1205 | |||
| 1206 | sub anyElement($$$) | ||
| 1207 | { my ($self, $tree, $min, $max) = @_; | ||
| 1208 | |||
| 1209 | # attributes: id, maxOccurs, minOccurs, | ||
| 1210 | # , namespace = ##any|##other| List of (anyURI|##targetNamespace|##local) | ||
| 1211 | # , notNamespace = List of (anyURI|##targetNamespace|##local) | ||
| 1212 | # ignored attributes | ||
| 1213 | # , notQName = List of QName | ||
| 1214 | # , processContents = lax|skip|strict | ||
| 1215 | # content: annotation? | ||
| 1216 | |||
| 1217 | my $node = $tree->node; | ||
| 1218 | my $where = $tree->path . '#any'; | ||
| 1219 | my $handler = $self->{any_element}; | ||
| 1220 | |||
| 1221 | my $namespace = $node->getAttribute('namespace') || '##any'; | ||
| 1222 | my $not_ns = $node->getAttribute('notNamespace'); | ||
| 1223 | my $process = $node->getAttribute('processContents') || 'strict'; | ||
| 1224 | |||
| 1225 | info "HELP: please explain me how to handle notQName" | ||
| 1226 | if $^W && $node->getAttribute('notQName'); | ||
| 1227 | |||
| 1228 | my ($yes, $no) = $self->translateNsLimits($namespace, $not_ns); | ||
| 1229 | (any => $self->makeAnyElement($where, $handler, $yes, $no | ||
| 1230 | , $process, $min, $max)); | ||
| 1231 | } | ||
| 1232 | |||
| 1233 | sub translateNsLimits($$) | ||
| 1234 | { my ($self, $include, $exclude) = @_; | ||
| 1235 | |||
| 1236 | # namespace = ##any|##other| List of (anyURI|##targetNamespace|##local) | ||
| 1237 | # notNamespace = List of (anyURI |##targetNamespace|##local) | ||
| 1238 | # handling of ##local ignored: only full namespaces are supported for now | ||
| 1239 | |||
| 1240 | return (undef, []) if $include eq '##any'; | ||
| 1241 | |||
| 1242 | my $tns = $self->{_context}{tns}; | ||
| 1243 | return (undef, [$tns]) if $include eq '##other'; | ||
| 1244 | |||
| 1245 | my @return; | ||
| 1246 | foreach my $list ($include, $exclude) | ||
| 1247 | { my @list; | ||
| 1248 | if(defined $list && length $list) | ||
| 1249 | { foreach my $uri (split " ", $list) | ||
| 1250 | { push @list | ||
| 1251 | , $uri eq '##targetNamespace' ? $tns | ||
| 1252 | : $uri eq '##local' ? () | ||
| 1253 | : $uri; | ||
| 1254 | } | ||
| 1255 | } | ||
| 1256 | push @return, @list ? \@list : undef; | ||
| 1257 | } | ||
| 1258 | |||
| 1259 | @return; | ||
| 1260 | } | ||
| 1261 | |||
| 1262 | sub complexType($) | ||
| 1263 | { my ($self, $tree) = @_; | ||
| 1264 | |||
| 1265 | # abstract, block, final, id, mixed, name, defaultAttributesApply | ||
| 1266 | # Full content: | ||
| 1267 | # annotation? | ||
| 1268 | # , ( simpleContent | ||
| 1269 | # | complexContent | ||
| 1270 | # | ( (group|all|choice|sequence)? | ||
| 1271 | # , (attribute|attributeGroup)* | ||
| 1272 | # , anyAttribute? | ||
| 1273 | # ) | ||
| 1274 | # ) | ||
| 1275 | # , (assert | report)* | ||
| 1276 | |||
| 1277 | my $node = $tree->node; | ||
| 1278 | my $mixed = $self->isTrue($node->getAttribute('mixed') || 'false'); | ||
| 1279 | undef $mixed | ||
| 1280 | if $self->{mixed_elements} eq 'STRUCTURAL'; | ||
| 1281 | |||
| 1282 | my $first = $tree->firstChild | ||
| 1283 | or return {elems => [], mixed => $mixed}; | ||
| 1284 | |||
| 1285 | my $name = $first->localName; | ||
| 1286 | return $self->complexBody($tree, $mixed) | ||
| 1287 | if $name =~ $particle_blocks || $name =~ $attribute_defs; | ||
| 1288 | |||
| 1289 | $tree->nrChildren==1 | ||
| 1290 | or error __x"expected is single simpleContent or complexContent at {where}" | ||
| 1291 | , where => $tree->path, _class => 'schema'; | ||
| 1292 | |||
| 1293 | return $self->simpleContent($tree->descend($first)) | ||
| 1294 | if $name eq 'simpleContent'; | ||
| 1295 | |||
| 1296 | return $self->complexContent($tree->descend($first), $mixed) | ||
| 1297 | if $name eq 'complexContent'; | ||
| 1298 | |||
| 1299 | error __x"complexType contains particles, simpleContent or complexContent, not `{name}' at {where}" | ||
| 1300 | , name => $name, where => $tree->path, _class => 'schema'; | ||
| 1301 | } | ||
| 1302 | |||
| 1303 | sub complexBody($$) | ||
| 1304 | { my ($self, $tree, $mixed) = @_; | ||
| 1305 | |||
| 1306 | $tree->currentChild | ||
| 1307 | or return (); | ||
| 1308 | |||
| 1309 | # partial | ||
| 1310 | # (group|all|choice|sequence)? | ||
| 1311 | # , ((attribute|attributeGroup)* | ||
| 1312 | # , anyAttribute? | ||
| 1313 | |||
| 1314 | my @elems; | ||
| 1315 | if($tree->currentLocal =~ $particle_blocks) | ||
| 1316 | { push @elems, $self->particle($tree->descend); # unless $mixed; | ||
| 1317 | $tree->nextChild; | ||
| 1318 | } | ||
| 1319 | |||
| 1320 | my @attrs = $self->attributeList($tree); | ||
| 1321 | |||
| 1322 | defined $tree->currentChild | ||
| 1323 | and error __x"trailing non-attribute `{name}' at {where}" | ||
| 1324 | , name => $tree->currentChild->localName, where => $tree->path | ||
| 1325 | , _class => 'schema'; | ||
| 1326 | |||
| 1327 | {elems => \@elems, mixed => $mixed, @attrs}; | ||
| 1328 | } | ||
| 1329 | |||
| 1330 | sub attributeList($) | ||
| 1331 | { my ($self, $tree) = @_; | ||
| 1332 | |||
| 1333 | # partial content | ||
| 1334 | # ((attribute|attributeGroup)* | ||
| 1335 | # , anyAttribute? | ||
| 1336 | |||
| 1337 | my $where = $tree->path; | ||
| 1338 | |||
| 1339 | my (@attrs, @any); | ||
| 1340 | for(my $attr = $tree->currentChild; defined $attr; $attr = $tree->nextChild) | ||
| 1341 | { my $name = $attr->localName; | ||
| 1342 | if($name eq 'attribute') | ||
| 1343 | { push @attrs, $self->attribute($tree->descend); | ||
| 1344 | } | ||
| 1345 | elsif($name eq 'attributeGroup') | ||
| 1346 | { my %group = $self->attributeGroup($tree->descend); | ||
| 1347 | push @attrs, @{$group{attrs}}; | ||
| 1348 | push @any, @{$group{attrs_any}}; | ||
| 1349 | } | ||
| 1350 | else { last } | ||
| 1351 | } | ||
| 1352 | |||
| 1353 | # officially only one: don't believe that | ||
| 1354 | while($tree->currentLocal eq 'anyAttribute') | ||
| 1355 | { push @any, $self->anyAttribute($tree->descend); | ||
| 1356 | $tree->nextChild; | ||
| 1357 | } | ||
| 1358 | |||
| 1359 | (attrs => \@attrs, attrs_any => \@any); | ||
| 1360 | } | ||
| 1361 | |||
| 1362 | sub simpleContent($) | ||
| 1363 | { my ($self, $tree) = @_; | ||
| 1364 | |||
| 1365 | # attributes: id | ||
| 1366 | # content: annotation?, (restriction | extension) | ||
| 1367 | |||
| 1368 | $tree->nrChildren==1 | ||
| 1369 | or error __x"need one simpleContent child at {where}" | ||
| 1370 | , where => $tree->path, _class => 'schema'; | ||
| 1371 | |||
| 1372 | my $name = $tree->currentLocal; | ||
| 1373 | return $self->simpleContentExtension($tree->descend) | ||
| 1374 | if $name eq 'extension'; | ||
| 1375 | |||
| 1376 | return $self->simpleContentRestriction($tree->descend) | ||
| 1377 | if $name eq 'restriction'; | ||
| 1378 | |||
| 1379 | error __x"simpleContent needs extension or restriction, not `{name}' at {where}" | ||
| 1380 | , name => $name, where => $tree->path, _class => 'schema'; | ||
| 1381 | } | ||
| 1382 | |||
| 1383 | sub simpleContentExtension($) | ||
| 1384 | { my ($self, $tree) = @_; | ||
| 1385 | |||
| 1386 | # attributes: id, base = QName | ||
| 1387 | # content: annotation? | ||
| 1388 | # , (attribute | attributeGroup)* | ||
| 1389 | # , anyAttribute? | ||
| 1390 | # , (assert | report)* | ||
| 1391 | |||
| 1392 | my $node = $tree->node; | ||
| 1393 | my $where = $tree->path . '#sext'; | ||
| 1394 | |||
| 1395 | my $base = $node->getAttribute('base'); | ||
| 1396 | my $typename = defined $base ? $self->rel2abs($where, $node, $base) | ||
| 1397 | : $self->anyType($node); | ||
| 1398 | |||
| 1399 | my $basetype = $self->blocked($where, simpleType => $typename) | ||
| 1400 | || $self->typeByName($where, $tree, $typename); | ||
| 1401 | defined $basetype->{st} | ||
| 1402 | or error __x"base of simpleContent not simple at {where}" | ||
| 1403 | , where => $where, _class => 'schema'; | ||
| 1404 | |||
| 1405 | $self->extendAttrs($basetype, {$self->attributeList($tree)}); | ||
| 1406 | |||
| 1407 | $tree->currentChild | ||
| 1408 | and error __x"elements left at tail at {where}" | ||
| 1409 | , where => $tree->path, _class => 'schema'; | ||
| 1410 | |||
| 1411 | $basetype; | ||
| 1412 | } | ||
| 1413 | |||
| 1414 | sub simpleContentRestriction($$) | ||
| 1415 | { my ($self, $tree) = @_; | ||
| 1416 | |||
| 1417 | # attributes id, base = QName | ||
| 1418 | # content: annotation? | ||
| 1419 | # , (simpleType?, facet*)? | ||
| 1420 | # , (attribute | attributeGroup)*, anyAttribute? | ||
| 1421 | # , (assert | report)* | ||
| 1422 | |||
| 1423 | my $node = $tree->node; | ||
| 1424 | my $where = $tree->path . '#cres'; | ||
| 1425 | |||
| 1426 | my ($type, $typename); | ||
| 1427 | my $first = $tree->currentLocal || ''; | ||
| 1428 | if($first eq 'simpleType') | ||
| 1429 | { $type = $self->simpleType($tree->descend); | ||
| 1430 | $tree->nextChild; | ||
| 1431 | } | ||
| 1432 | elsif(my $basename = $node->getAttribute('base')) | ||
| 1433 | { $typename = $self->rel2abs($where, $node, $basename); | ||
| 1434 | $type = $self->blocked($where, simpleType => $type) | ||
| 1435 | || $self->typeByName($where, $tree, $typename); | ||
| 1436 | } | ||
| 1437 | else | ||
| 1438 | { error __x"no base in complex-restriction, so simpleType required at {where}" | ||
| 1439 | , where => $where, _class => 'schema'; | ||
| 1440 | } | ||
| 1441 | |||
| 1442 | my $st = $type->{st} | ||
| 1443 | or error __x"not a simpleType in simpleContent/restriction at {where}" | ||
| 1444 | , where => $where, _class => 'schema'; | ||
| 1445 | |||
| 1446 | $type->{st} = $self->applySimpleFacets($tree, $st, 0, $typename); | ||
| 1447 | |||
| 1448 | $self->extendAttrs($type, {$self->attributeList($tree)}); | ||
| 1449 | |||
| 1450 | $tree->currentChild | ||
| 1451 | and error __x"elements left at tail at {where}" | ||
| 1452 | , where => $where, _class => 'schema'; | ||
| 1453 | |||
| 1454 | $type; | ||
| 1455 | } | ||
| 1456 | |||
| 1457 | sub complexContent($$) | ||
| 1458 | { my ($self, $tree, $mixed) = @_; | ||
| 1459 | |||
| 1460 | # attributes: id, mixed = boolean | ||
| 1461 | # content: annotation?, (restriction | extension) | ||
| 1462 | |||
| 1463 | my $node = $tree->node; | ||
| 1464 | if(my $m = $node->getAttribute('mixed')) | ||
| 1465 | { $mixed = $self->isTrue($m) | ||
| 1466 | if $self->{mixed_elements} ne 'STRUCTURAL'; | ||
| 1467 | } | ||
| 1468 | |||
| 1469 | $tree->nrChildren == 1 | ||
| 1470 | or error __x"only one complexContent child expected at {where}" | ||
| 1471 | , where => $tree->path, _class => 'schema'; | ||
| 1472 | |||
| 1473 | my $name = $tree->currentLocal; | ||
| 1474 | error __x"complexContent needs extension or restriction, not `{name}' at {where}" | ||
| 1475 | , name => $name, where => $tree->path, _class => 'schema' | ||
| 1476 | if $name ne 'extension' && $name ne 'restriction'; | ||
| 1477 | |||
| 1478 | $tree = $tree->descend; | ||
| 1479 | $node = $tree->node; | ||
| 1480 | my $base = $node->getAttribute('base') || $self->anyType($node); | ||
| 1481 | my $type = {}; | ||
| 1482 | my $where = $tree->path . '#cce'; | ||
| 1483 | |||
| 1484 | if($base !~ m/\banyType$/) | ||
| 1485 | { my $typename = $self->rel2abs($where, $node, $base); | ||
| 1486 | if($type = $self->blocked($where, complexType => $typename)) | ||
| 1487 | { # blocked base type | ||
| 1488 | } | ||
| 1489 | else | ||
| 1490 | { my $typedef = $self->namespaces->find(complexType => $typename) | ||
| 1491 | or error __x"unknown base type '{type}' at {where}" | ||
| 1492 | , type => $typename, where => $tree->path, _class => 'schema'; | ||
| 1493 | |||
| 1494 | local $self->{_context} = $self->nsContext($typedef); | ||
| 1495 | $type = $self->complexType($tree->descend($typedef->{node})); | ||
| 1496 | } | ||
| 1497 | } | ||
| 1498 | |||
| 1499 | my $own = $self->complexBody($tree, $mixed); | ||
| 1500 | $self->extendAttrs($type, $own); | ||
| 1501 | |||
| 1502 | if($name eq 'extension') | ||
| 1503 | { push @{$type->{elems}}, @{$own->{elems} || []}; | ||
| 1504 | } | ||
| 1505 | else # restriction | ||
| 1506 | { $type->{elems} = $own->{elems}; | ||
| 1507 | } | ||
| 1508 | |||
| 1509 | $type->{mixed} ||= $own->{mixed}; | ||
| 1510 | $type; | ||
| 1511 | } | ||
| 1512 | |||
| 1513 | # | ||
| 1514 | # Helper routines | ||
| 1515 | # | ||
| 1516 | |||
| 1517 | # print $self->rel2abs($path, $node, '{ns}type') -> '{ns}type' | ||
| 1518 | # print $self->rel2abs($path, $node, 'prefix:type') -> '{ns-of-prefix}type' | ||
| 1519 | |||
| 1520 | sub rel2abs($$$) | ||
| 1521 | { my ($self, $where, $node, $type) = @_; | ||
| 1522 | return $type if substr($type, 0, 1) eq '{'; | ||
| 1523 | |||
| 1524 | my ($prefix, $local) = $type =~ m/^(.+?)\:(.*)/ ? ($1, $2) : ('', $type); | ||
| 1525 | my $uri = $node->lookupNamespaceURI($prefix); | ||
| 1526 | $self->_registerNSprefix($prefix, $uri, 0) if $uri; | ||
| 1527 | |||
| 1528 | error __x"No namespace for prefix `{prefix}' in `{type}' at {where}" | ||
| 1529 | , prefix => $prefix, type => $type, where => $where, _class => 'schema' | ||
| 1530 | if length $prefix && !defined $uri; | ||
| 1531 | |||
| 1532 | pack_type $uri, $local; | ||
| 1533 | } | ||
| 1534 | |||
| 1535 | sub _registerNSprefix($$$) | ||
| 1536 | { my ($self, $prefix, $uri, $used) = @_; | ||
| 1537 | my $table = $self->{prefixes}; | ||
| 1538 | |||
| 1539 | if(my $u = $table->{$uri}) # namespace already has a prefix | ||
| 1540 | { $u->{used} += $used; | ||
| 1541 | return $u->{prefix}; | ||
| 1542 | } | ||
| 1543 | |||
| 1544 | my %prefs = map +($_->{prefix} => 1), values %$table; | ||
| 1545 | my $take; | ||
| 1546 | if(defined $prefix && !$prefs{$prefix}) { $take = $prefix } | ||
| 1547 | elsif(!$prefs{''}) { $take = '' } | ||
| 1548 | else | ||
| 1549 | { # prefix already in use; create a new x\d+ prefix | ||
| 1550 | my $count = 0; | ||
| 1551 | $count++ while exists $prefs{"x$count"}; | ||
| 1552 | $take = 'x'.$count; | ||
| 1553 | } | ||
| 1554 | $table->{$uri} = {prefix => $take, uri => $uri, used => $used}; | ||
| 1555 | $take; | ||
| 1556 | } | ||
| 1557 | |||
| 1558 | sub anyType($) | ||
| 1559 | { my ($self, $node) = @_; | ||
| 1560 | pack_type $node->namespaceURI, 'anyType'; | ||
| 1561 | } | ||
| 1562 | |||
| 1563 | sub findHooks($$$) | ||
| 1564 | { my ($self, $path, $type, $node) = @_; | ||
| 1565 | # where is before, replace, after | ||
| 1566 | |||
| 1567 | my %hooks; | ||
| 1568 | foreach my $hook (@{$self->{hooks}}) | ||
| 1569 | { my $match; | ||
| 1570 | |||
| 1571 | $match++ | ||
| 1572 | if !$hook->{path} && !$hook->{id} | ||
| 1573 | && !$hook->{type} && !$hook->{extends}; | ||
| 1574 | |||
| 1575 | if(!$match && $hook->{path}) | ||
| 1576 | { my $p = $hook->{path}; | ||
| 1577 | $match++ | ||
| 1578 | if first {ref $_ eq 'Regexp' ? $path =~ $_ : $path eq $_} | ||
| 1579 | ref $p eq 'ARRAY' ? @$p : $p; | ||
| 1580 | } | ||
| 1581 | |||
| 1582 | my $id = !$match && $hook->{id} && $node->getAttribute('id'); | ||
| 1583 | if($id) | ||
| 1584 | { my $i = $hook->{id}; | ||
| 1585 | $match++ | ||
| 1586 | if first {ref $_ eq 'Regexp' ? $id =~ $_ : $id eq $_} | ||
| 1587 | ref $i eq 'ARRAY' ? @$i : $i; | ||
| 1588 | } | ||
| 1589 | |||
| 1590 | if(!$match && defined $type && $hook->{type}) | ||
| 1591 | { my $t = $hook->{type}; | ||
| 1592 | my ($ns, $local) = unpack_type $type; | ||
| 1593 | $match++ | ||
| 1594 | if first {ref $_ eq 'Regexp' ? $type =~ $_ | ||
| 1595 | : substr($_,0,1) eq '{' ? $type eq $_ | ||
| 1596 | : $local eq $_ | ||
| 1597 | } ref $t eq 'ARRAY' ? @$t : $t; | ||
| 1598 | } | ||
| 1599 | |||
| 1600 | if(!$match && defined $type && $hook->{extends}) | ||
| 1601 | { $match++ if $self->{nss}->doesExtend($type, $hook->{extends}); | ||
| 1602 | } | ||
| 1603 | |||
| 1604 | $match or next; | ||
| 1605 | |||
| 1606 | foreach my $where ( qw/before replace after/ ) | ||
| 1607 | { my $w = $hook->{$where} or next; | ||
| 1608 | push @{$hooks{$where}}, ref $w eq 'ARRAY' ? @$w : $w; | ||
| 1609 | } | ||
| 1610 | } | ||
| 1611 | |||
| 1612 | @hooks{ qw/before replace after/ }; | ||
| 1613 | } | ||
| 1614 | |||
| 1615 | # Namespace blocks, in most cases because the schema refers to an | ||
| 1616 | # older version of itself, which is deprecated. | ||
| 1617 | # performance is important, because it is called increadably often. | ||
| 1618 | |||
| 1619 | sub decodeBlocked($) | ||
| 1620 | { my ($self, $what) = @_; | ||
| 1621 | defined $what or return; | ||
| 1622 | my @blocked; # code-refs called with ($type, $ns, $local, $path) | ||
| 1623 | foreach my $w (ref $what eq 'ARRAY' ? @$what : $what) | ||
| 1624 | { push @blocked, | ||
| 1625 | !ref $w ? sub { $_[0] eq $w || $_[1] eq $w } | ||
| 1626 | : ref $w eq 'HASH' | ||
| 1627 | ? sub { defined $w->{$_[0]} ? $w->{$_[0]} : $w->{$_[1]} } | ||
| 1628 | : ref $what eq 'CODE' ? $w | ||
| 1629 | : error __x"blocking rule with {what} not supported", what => $w; | ||
| 1630 | } | ||
| 1631 | \@blocked; | ||
| 1632 | } | ||
| 1633 | |||
| 1634 | sub blocked($$$) | ||
| 1635 | { my ($self, $path, $class, $type) = @_; | ||
| 1636 | # $class = simpleType, complexType, or ref | ||
| 1637 | @{$self->{blocked_nss}} or return (); | ||
| 1638 | |||
| 1639 | my ($ns, $local) = unpack_type $type; | ||
| 1640 | my $is_blocked; | ||
| 1641 | foreach my $blocked ( @{$self->{blocked_nss}} ) | ||
| 1642 | { $is_blocked = $blocked->($type, $ns, $local, $path); | ||
| 1643 | last if defined $is_blocked; | ||
| 1644 | } | ||
| 1645 | $is_blocked or return; | ||
| 1646 | |||
| 1647 | trace "$type of $class is blocked"; | ||
| 1648 | $self->makeBlocked($path, $class, $type); | ||
| 1649 | } | ||
| 1650 | |||
| 1651 | sub addTypeAttribute($$) | ||
| 1652 | { my ($self, $type, $call) = @_; | ||
| 1653 | $call; | ||
| 1654 | } | ||
| 1655 | |||
| 1656 | #------------ | ||
| 1657 | |||
| 1658 | 1; |