| 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::Instance; | ||
| 10 | use vars '$VERSION'; | ||
| 11 | $VERSION = '1.61'; | ||
| 12 | |||
| 13 | |||
| 14 | use warnings; | ||
| 15 | use strict; | ||
| 16 | |||
| 17 | use Log::Report 'xml-compile'; | ||
| 18 | use XML::Compile::Schema::Specs; | ||
| 19 | use XML::Compile::Util qw/pack_type unpack_type/; | ||
| 20 | use Scalar::Util qw/weaken/; | ||
| 21 | |||
| 22 | my @defkinds = qw/element attribute simpleType complexType | ||
| 23 | attributeGroup group/; | ||
| 24 | my %defkinds = map +($_ => 1), @defkinds; | ||
| 25 | |||
| 26 | |||
| 27 | sub new($@) | ||
| 28 | { my $class = shift; | ||
| 29 | (bless {}, $class)->init( {top => @_} ); | ||
| 30 | } | ||
| 31 | |||
| 32 | sub init($) | ||
| 33 | { my ($self, $args) = @_; | ||
| 34 | my $top = $args->{top}; | ||
| 35 | defined $top && $top->isa('XML::LibXML::Node') | ||
| 36 | or panic "instance is based on XML node"; | ||
| 37 | |||
| 38 | $self->{filename} = $args->{filename}; | ||
| 39 | $self->{source} = $args->{source}; | ||
| 40 | $self->{$_} = {} for @defkinds, 'sgs', 'import'; | ||
| 41 | $self->{include} = []; | ||
| 42 | |||
| 43 | $self->_collectTypes($top, $args); | ||
| 44 | $self; | ||
| 45 | } | ||
| 46 | |||
| 47 | |||
| 48 | sub targetNamespace { shift->{tns} } | ||
| 49 | sub schemaNamespace { shift->{xsd} } | ||
| 50 | sub schemaInstance { shift->{xsi} } | ||
| 51 | sub source { shift->{source} } | ||
| 52 | sub filename { shift->{filename} } | ||
| 53 | sub schema { shift->{schema} } | ||
| 54 | |||
| 55 | |||
| 56 | sub tnses() {keys %{shift->{tnses}}} | ||
| 57 | |||
| 58 | |||
| 59 | sub sgs() { shift->{sgs} } | ||
| 60 | |||
| 61 | |||
| 62 | sub type($) { $_[0]->{types}{$_[1]} } | ||
| 63 | |||
| 64 | |||
| 65 | sub element($) { $_[0]->{element}{$_[1]} } | ||
| 66 | |||
| 67 | |||
| 68 | sub elements() { keys %{shift->{element}} } | ||
| 69 | sub attributes() { keys %{shift->{attributes}} } | ||
| 70 | sub attributeGroups() { keys %{shift->{attributeGroup}} } | ||
| 71 | sub groups() { keys %{shift->{group}} } | ||
| 72 | sub simpleTypes() { keys %{shift->{simpleType}} } | ||
| 73 | sub complexTypes() { keys %{shift->{complexType}} } | ||
| 74 | |||
| 75 | |||
| 76 | sub types() { ($_[0]->simpleTypes, $_[0]->complexTypes) } | ||
| 77 | |||
| 78 | |||
| 79 | my %skip_toplevel = map +($_ => 1), qw/annotation notation redefine/; | ||
| 80 | |||
| 81 | sub _collectTypes($$) | ||
| 82 | { my ($self, $schema, $args) = @_; | ||
| 83 | |||
| 84 | $schema->localName eq 'schema' | ||
| 85 | or panic "requires schema element"; | ||
| 86 | |||
| 87 | my $xsd = $self->{xsd} = $schema->namespaceURI || '<none>'; | ||
| 88 | if(length $xsd) | ||
| 89 | { my $def = $self->{def} | ||
| 90 | = XML::Compile::Schema::Specs->predefinedSchema($xsd) | ||
| 91 | or error __x"schema namespace `{namespace}' not (yet) supported" | ||
| 92 | , namespace => $xsd; | ||
| 93 | |||
| 94 | $self->{xsi} = $def->{uri_xsi}; | ||
| 95 | } | ||
| 96 | |||
| 97 | my $tns; | ||
| 98 | if($tns = $args->{target_namespace}) | ||
| 99 | { $schema->removeAttribute('targetNamespace'); | ||
| 100 | $schema->setAttribute(targetNamespace => $tns); | ||
| 101 | } | ||
| 102 | else | ||
| 103 | { $tns = $schema->getAttribute('targetNamespace') || ''; | ||
| 104 | } | ||
| 105 | $self->{tns} = $tns; | ||
| 106 | |||
| 107 | $self->{efd} = $args->{element_form_default} | ||
| 108 | || $schema->getAttribute('elementFormDefault') | ||
| 109 | || 'unqualified'; | ||
| 110 | |||
| 111 | $self->{afd} = $args->{attribute_form_default} | ||
| 112 | || $schema->getAttribute('attributeFormDefault') | ||
| 113 | || 'unqualified'; | ||
| 114 | |||
| 115 | $self->{tnses} = {}; # added when used | ||
| 116 | $self->{types} = {}; | ||
| 117 | |||
| 118 | $self->{schema} = $schema; | ||
| 119 | weaken($self->{schema}); | ||
| 120 | |||
| 121 | NODE: | ||
| 122 | foreach my $node ($schema->childNodes) | ||
| 123 | { next unless $node->isa('XML::LibXML::Element'); | ||
| 124 | my $local = $node->localName; | ||
| 125 | my $myns = $node->namespaceURI || ''; | ||
| 126 | $myns eq $xsd | ||
| 127 | or error __x"schema element `{name}' not in schema namespace {ns} but {other}" | ||
| 128 | , name => $local, ns => $xsd, other => ($myns || '<none>'); | ||
| 129 | |||
| 130 | next | ||
| 131 | if $skip_toplevel{$local}; | ||
| 132 | |||
| 133 | if($local eq 'import') | ||
| 134 | { my $namespace = $node->getAttribute('namespace') || $tns; | ||
| 135 | my $location = $node->getAttribute('schemaLocation') || ''; | ||
| 136 | push @{$self->{import}{$namespace}}, $location; | ||
| 137 | next NODE; | ||
| 138 | } | ||
| 139 | |||
| 140 | if($local eq 'include') | ||
| 141 | { my $location = $node->getAttribute('schemaLocation') | ||
| 142 | or error __x"include requires schemaLocation attribute at line {linenr}" | ||
| 143 | , linenr => $node->line_number; | ||
| 144 | |||
| 145 | push @{$self->{include}}, $location; | ||
| 146 | next NODE; | ||
| 147 | } | ||
| 148 | |||
| 149 | unless($defkinds{$local}) | ||
| 150 | { mistake __x"ignoring unknown definition class {class}" | ||
| 151 | , class => $local; | ||
| 152 | next; | ||
| 153 | } | ||
| 154 | |||
| 155 | my $name = $node->getAttribute('name') | ||
| 156 | or error __x"schema component {local} without name at line {linenr}" | ||
| 157 | , local => $local, linenr => $node->line_number; | ||
| 158 | |||
| 159 | my $tns = $node->getAttribute('targetNamespace') || $tns; | ||
| 160 | my $type = pack_type $tns, $name; | ||
| 161 | $self->{tnses}{$tns}++; | ||
| 162 | $self->{$local}{$type} = $node; | ||
| 163 | |||
| 164 | if(my $sg = $node->getAttribute('substitutionGroup')) | ||
| 165 | { my ($prefix, $l) = $sg =~ m/:/ ? split(/:/, $sg, 2) : ('',$sg); | ||
| 166 | my $base = pack_type $node->lookupNamespaceURI($prefix), $l; | ||
| 167 | push @{$self->{sgs}{$base}}, $type; | ||
| 168 | } | ||
| 169 | } | ||
| 170 | |||
| 171 | $self; | ||
| 172 | } | ||
| 173 | |||
| 174 | |||
| 175 | sub includeLocations() { @{shift->{include}} } | ||
| 176 | |||
| 177 | |||
| 178 | sub imports() { keys %{shift->{import}} } | ||
| 179 | |||
| 180 | |||
| 181 | sub importLocations($) | ||
| 182 | { my $locs = $_[0]->{import}{$_[1]}; | ||
| 183 | $locs ? @$locs : (); | ||
| 184 | } | ||
| 185 | |||
| 186 | |||
| 187 | sub printIndex(;$) | ||
| 188 | { my $self = shift; | ||
| 189 | my $fh = @_ % 2 ? shift : select; | ||
| 190 | my %args = @_; | ||
| 191 | |||
| 192 | $fh->print("namespace: ", $self->targetNamespace, "\n"); | ||
| 193 | if(defined(my $filename = $self->filename)) | ||
| 194 | { $fh->print(" filename: $filename\n"); | ||
| 195 | } | ||
| 196 | elsif(defined(my $source = $self->source)) | ||
| 197 | { $fh->print(" source: $source\n"); | ||
| 198 | } | ||
| 199 | |||
| 200 | my @kinds | ||
| 201 | = ! defined $args{kinds} ? @defkinds | ||
| 202 | : ref $args{kinds} eq 'ARRAY' ? @{$args{kinds}} | ||
| 203 | : $args{kinds}; | ||
| 204 | |||
| 205 | my $list_abstract | ||
| 206 | = exists $args{list_abstract} ? $args{list_abstract} : 1; | ||
| 207 | |||
| 208 | foreach my $kind (@kinds) | ||
| 209 | { my $table = $self->{$kind}; | ||
| 210 | keys %$table or next; | ||
| 211 | $fh->print(" definitions of ${kind}s:\n") if @kinds > 1; | ||
| 212 | |||
| 213 | foreach (sort keys %$table) | ||
| 214 | { my $info = $self->find($kind, $_); | ||
| 215 | my ($ns, $name) = unpack_type $_; | ||
| 216 | next if $info->{abstract} && ! $list_abstract; | ||
| 217 | my $abstract = $info->{abstract} ? ' [abstract]' : ''; | ||
| 218 | my $final = $info->{final} ? ' [final]' : ''; | ||
| 219 | $fh->print(" $name$abstract$final\n"); | ||
| 220 | } | ||
| 221 | } | ||
| 222 | } | ||
| 223 | |||
| 224 | |||
| 225 | sub find($$) | ||
| 226 | { my ($self, $kind, $full) = @_; | ||
| 227 | my $node = $self->{$kind}{$full} | ||
| 228 | or return; | ||
| 229 | |||
| 230 | return $node # translation of XML node into info is cached | ||
| 231 | if ref $node eq 'HASH'; | ||
| 232 | |||
| 233 | my %info = (type => $kind, node => $node, full => $full); | ||
| 234 | @info{'ns', 'name'} = unpack_type $full; | ||
| 235 | |||
| 236 | $self->{$kind}{$full} = \%info; | ||
| 237 | |||
| 238 | my $abstract = $node->getAttribute('abstract') || ''; | ||
| 239 | $info{abstract} = $abstract eq 'true' || $abstract eq '1'; | ||
| 240 | |||
| 241 | my $final = $node->getAttribute('final') || ''; | ||
| 242 | $info{final} = $final eq 'true' || $final eq '1'; | ||
| 243 | |||
| 244 | my $local = $node->localName; | ||
| 245 | if($local eq 'element') { $info{efd} = $node->getAttribute('form') } | ||
| 246 | elsif($local eq 'attribute'){ $info{afd} = $node->getAttribute('form') } | ||
| 247 | $info{efd} ||= $self->{efd}; # both needed for nsContext | ||
| 248 | $info{afd} ||= $self->{afd}; | ||
| 249 | \%info; | ||
| 250 | } | ||
| 251 | |||
| 252 | 1; |