| 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::Schema::NameSpaces; | ||
| 10 | use vars '$VERSION'; | ||
| 11 | $VERSION = '1.61'; | ||
| 12 | |||
| 13 | |||
| 14 | use warnings; | ||
| 15 | use strict; | ||
| 16 | |||
| 17 | use Log::Report 'xml-compile'; | ||
| 18 | |||
| 19 | use XML::Compile::Util | ||
| 20 | qw/pack_type unpack_type pack_id unpack_id SCHEMA2001/; | ||
| 21 | |||
| 22 | use XML::Compile::Schema::BuiltInTypes qw/%builtin_types/; | ||
| 23 | |||
| 24 | |||
| 25 | sub new($@) | ||
| 26 | { my $class = shift; | ||
| 27 | (bless {}, $class)->init( {@_} ); | ||
| 28 | } | ||
| 29 | |||
| 30 | sub init($) | ||
| 31 | { my ($self, $args) = @_; | ||
| 32 | $self->{tns} = {}; | ||
| 33 | $self->{sgs} = {}; | ||
| 34 | $self->{use} = []; | ||
| 35 | $self; | ||
| 36 | } | ||
| 37 | |||
| 38 | |||
| 39 | sub list() { keys %{shift->{tns}} } | ||
| 40 | |||
| 41 | |||
| 42 | sub namespace($) | ||
| 43 | { my $nss = $_[0]->{tns}{$_[1]}; | ||
| 44 | $nss ? @$nss : (); | ||
| 45 | } | ||
| 46 | |||
| 47 | |||
| 48 | sub add(@) | ||
| 49 | { my $self = shift; | ||
| 50 | foreach my $instance (@_) | ||
| 51 | { # With the "new" targetNamespace attribute on any attribute, one | ||
| 52 | # schema may have contribute to multiple tns's. Also, I have | ||
| 53 | # encounted schema's without elements, but <import> | ||
| 54 | my @tnses = $instance->tnses; | ||
| 55 | @tnses or @tnses = '(none)'; | ||
| 56 | |||
| 57 | # newest definitions overrule earlier. | ||
| 58 | unshift @{$self->{tns}{$_}}, $instance | ||
| 59 | for @tnses; | ||
| 60 | |||
| 61 | # inventory where to find definitions which belong to some | ||
| 62 | # substitutionGroup. | ||
| 63 | while(my($base,$ext) = each %{$instance->sgs}) | ||
| 64 | { $self->{sgs}{$base}{$_} ||= $instance for @$ext; | ||
| 65 | } | ||
| 66 | } | ||
| 67 | @_; | ||
| 68 | } | ||
| 69 | |||
| 70 | |||
| 71 | sub use($) | ||
| 72 | { my $self = shift; | ||
| 73 | push @{$self->{use}}, @_; | ||
| 74 | @{$self->{use}}; | ||
| 75 | } | ||
| 76 | |||
| 77 | |||
| 78 | sub schemas($) { $_[0]->namespace($_[1]) } | ||
| 79 | |||
| 80 | |||
| 81 | sub allSchemas() | ||
| 82 | { my $self = shift; | ||
| 83 | map {$self->schemas($_)} $self->list; | ||
| 84 | } | ||
| 85 | |||
| 86 | |||
| 87 | sub find($$;$) | ||
| 88 | { my ($self, $kind) = (shift, shift); | ||
| 89 | my ($ns, $name) = (@_%2==1) ? (unpack_type shift) : (shift, shift); | ||
| 90 | my %opts = @_; | ||
| 91 | |||
| 92 | defined $ns or return undef; | ||
| 93 | my $label = pack_type $ns, $name; # re-pack unpacked for consistency | ||
| 94 | |||
| 95 | foreach my $schema ($self->schemas($ns)) | ||
| 96 | { my $def = $schema->find($kind, $label); | ||
| 97 | return $def if defined $def; | ||
| 98 | } | ||
| 99 | |||
| 100 | my $used = exists $opts{include_used} ? $opts{include_used} : 1; | ||
| 101 | $used or return undef; | ||
| 102 | |||
| 103 | foreach my $use ( @{$self->{use}} ) | ||
| 104 | { my $def = $use->namespaces->find($kind, $label, include_used => 0); | ||
| 105 | return $def if defined $def; | ||
| 106 | } | ||
| 107 | |||
| 108 | undef; | ||
| 109 | } | ||
| 110 | |||
| 111 | |||
| 112 | sub doesExtend($$) | ||
| 113 | { my ($self, $ext, $base) = @_; | ||
| 114 | return 1 if $ext eq $base; | ||
| 115 | return 0 if $ext =~ m/^unnamed /; | ||
| 116 | |||
| 117 | my ($node, $super, $subnode); | ||
| 118 | if(my $st = $self->find(simpleType => $ext)) | ||
| 119 | { # pure simple type | ||
| 120 | $node = $st->{node}; | ||
| 121 | if(($subnode) = $node->getChildrenByLocalName('restriction')) | ||
| 122 | { $super = $subnode->getAttribute('base'); | ||
| 123 | } | ||
| 124 | # list an union currently ignored | ||
| 125 | } | ||
| 126 | elsif(my $ct = $self->find(complexType => $ext)) | ||
| 127 | { $node = $ct->{node}; | ||
| 128 | # getChildrenByLocalName returns list, we know size one | ||
| 129 | if(my($sc) = $node->getChildrenByLocalName('simpleContent')) | ||
| 130 | { # tagged | ||
| 131 | if(($subnode) = $sc->getChildrenByLocalName('extension')) | ||
| 132 | { $super = $subnode->getAttribute('base'); | ||
| 133 | } | ||
| 134 | elsif(($subnode) = $sc->getChildrenByLocalName('restriction')) | ||
| 135 | { $super = $subnode->getAttribute('base'); | ||
| 136 | } | ||
| 137 | } | ||
| 138 | elsif(my($cc) = $node->getChildrenByLocalName('complexContent')) | ||
| 139 | { # real complex | ||
| 140 | if(($subnode) = $cc->getChildrenByLocalName('extension')) | ||
| 141 | { $super = $subnode->getAttribute('base'); | ||
| 142 | } | ||
| 143 | elsif(($subnode) = $cc->getChildrenByLocalName('restriction')) | ||
| 144 | { $super = $subnode->getAttribute('base'); | ||
| 145 | } | ||
| 146 | } | ||
| 147 | } | ||
| 148 | else | ||
| 149 | { # built-in | ||
| 150 | my ($ns, $local) = unpack_type $ext; | ||
| 151 | $ns eq SCHEMA2001 && $builtin_types{$local} | ||
| 152 | or error __x"cannot find {type} as simpleType or complexType" | ||
| 153 | , type => $ext; | ||
| 154 | my ($bns, $blocal) = unpack_type $base; | ||
| 155 | $ns eq $bns | ||
| 156 | or return 0; | ||
| 157 | |||
| 158 | while(my $e = $builtin_types{$local}{extends}) | ||
| 159 | { return 1 if $e eq $blocal; | ||
| 160 | $local = $e; | ||
| 161 | } | ||
| 162 | } | ||
| 163 | |||
| 164 | $super | ||
| 165 | or return 0; | ||
| 166 | |||
| 167 | my ($prefix, $local) = $super =~ m/:/ ? split(/:/,$super,2) : ('',$super); | ||
| 168 | my $supertype = pack_type $subnode->lookupNamespaceURI($prefix), $local; | ||
| 169 | |||
| 170 | $base eq $supertype ? 1 : $self->doesExtend($supertype, $base); | ||
| 171 | } | ||
| 172 | |||
| 173 | |||
| 174 | sub findTypeExtensions($) | ||
| 175 | { my ($self, $type) = @_; | ||
| 176 | |||
| 177 | my %ext; | ||
| 178 | if($self->find(simpleType => $type)) | ||
| 179 | { $self->doesExtend($_, $type) && $ext{$_}++ | ||
| 180 | for map $_->simpleTypes, $self->allSchemas; | ||
| 181 | } | ||
| 182 | elsif($self->find(complexType => $type)) | ||
| 183 | { $self->doesExtend($_, $type) && $ext{$_}++ | ||
| 184 | for map $_->complexTypes, $self->allSchemas; | ||
| 185 | } | ||
| 186 | else | ||
| 187 | { error __x"cannot find base-type {type} for extensions", type => $type; | ||
| 188 | } | ||
| 189 | sort keys %ext; | ||
| 190 | } | ||
| 191 | |||
| 192 | sub autoexpand_xsi_type($) | ||
| 193 | { my ($self, $type) = @_; | ||
| 194 | my @ext = $self->findTypeExtensions($type); | ||
| 195 | trace "discovered xsi:type choices for $type:\n ". join("\n ", @ext); | ||
| 196 | \@ext; | ||
| 197 | } | ||
| 198 | |||
| 199 | |||
| 200 | sub findSgMembers($$) | ||
| 201 | { my ($self, $class, $base) = @_; | ||
| 202 | my $s = $self->{sgs}{$base} | ||
| 203 | or return; | ||
| 204 | |||
| 205 | my @sgs; | ||
| 206 | while(my($ext, $instance) = each %$s) | ||
| 207 | { push @sgs, $instance->find($class => $ext) | ||
| 208 | , $self->findSgMembers($class, $ext); | ||
| 209 | } | ||
| 210 | @sgs; | ||
| 211 | } | ||
| 212 | |||
| 213 | |||
| 214 | sub findID($;$) | ||
| 215 | { my $self = shift; | ||
| 216 | my ($label, $ns, $id) | ||
| 217 | = @_==1 ? ($_[0], unpack_id $_[0]) : (pack_id($_[0], $_[1]), @_); | ||
| 218 | defined $ns or return undef; | ||
| 219 | |||
| 220 | my $xpc = XML::LibXML::XPathContext->new; | ||
| 221 | $xpc->registerNs(a => $ns); | ||
| 222 | |||
| 223 | my @nodes; | ||
| 224 | foreach my $fragment ($self->schemas($ns)) | ||
| 225 | { @nodes = $xpc->findnodes("/*/a:*#$id", $fragment->schema) | ||
| 226 | or next; | ||
| 227 | |||
| 228 | return $nodes[0] | ||
| 229 | if @nodes==1; | ||
| 230 | |||
| 231 | error "multiple elements with the same id {id} in {source}" | ||
| 232 | , id => $label | ||
| 233 | , source => ($fragment->filename || $fragment->source); | ||
| 234 | } | ||
| 235 | |||
| 236 | undef; | ||
| 237 | } | ||
| 238 | |||
| 239 | |||
| 240 | sub printIndex(@) | ||
| 241 | { my $self = shift; | ||
| 242 | my $fh = @_ % 2 ? shift : select; | ||
| 243 | my %opts = @_; | ||
| 244 | |||
| 245 | my $nss = delete $opts{namespace} || [$self->list]; | ||
| 246 | foreach my $nsuri (ref $nss eq 'ARRAY' ? @$nss : $nss) | ||
| 247 | { $_->printIndex($fh, %opts) for $self->namespace($nsuri); | ||
| 248 | } | ||
| 249 | |||
| 250 | my $show_used = exists $opts{include_used} ? $opts{include_used} : 1; | ||
| 251 | foreach my $use ($self->use) | ||
| 252 | { $use->printIndex(%opts, include_used => 0); | ||
| 253 | } | ||
| 254 | |||
| 255 | $self; | ||
| 256 | } | ||
| 257 | |||
| 258 | |||
| 259 | sub importIndex(%) | ||
| 260 | { my ($self, %args) = @_; | ||
| 261 | my %import; | ||
| 262 | foreach my $fragment (map $self->schemas($_), $self->list) | ||
| 263 | { foreach my $import ($fragment->imports) | ||
| 264 | { $import{$import}{$_}++ for $fragment->importLocations($import); | ||
| 265 | } | ||
| 266 | } | ||
| 267 | foreach my $ns (keys %import) | ||
| 268 | { $import{$ns} = [ grep length, keys %{$import{$ns}} ]; | ||
| 269 | } | ||
| 270 | \%import; | ||
| 271 | } | ||
| 272 | |||
| 273 | 1; |