Compile/Schema/NameSpaces.pm

filter: Statementstmt 75.9% 104 / 137 Branchbran 57.5% 46 / 80 Conditioncond 18.1% 2 / 11 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 85.7% 18 / 21 Podpod 87.5% 14 / 16 totaltotal 67.6% 184 / 272 CC 60 SCAR 29.8
CC
60
Cov
67%
printIndex
72.0
importIndex
30.0
doesExtend
23.8
CRAP 19.6
« Compile/Schema.pm ↑ Summary Compile/Schema/Instance.pm »
1 # Copyrights 2006-2024 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.03.
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 50 our $VERSION = '1.64';
11 }
12  
13  
14 50 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 50 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 50 ▶use Log::Report 'xml-compile';
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 use XML::Compile::Util
20 50 ▶ qw/pack_type unpack_type pack_id unpack_id SCHEMA2001/;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 50 ▶use XML::Compile::Schema::BuiltInTypes qw/%builtin_types/;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24  
25 sub new($@)
26 52 ▶{ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
27 52 (bless {}, $class)->init( {@_} );
28 }
29  
30 sub init($)
31 52 ▶{ my ($self, $args) = @_;
Subroutine init: called CC=1 SCAR=0.0
Pod undocumented
32 52 $self->{tns} = {};
33 52 $self->{sgs} = {};
34 52 $self->{use} = [];
35 52 $self;
36 }
37  
38  
39 9 ▶sub list() { keys %{shift->{tns}} }
Subroutine list: called CC=1 SCAR=0.0
Pod documented
40  
41  
42 sub namespace($)
43 4693 ▶{ my $nss = $_[0]->{tns}{$_[1]};
Subroutine namespace: called CC=2 SCAR=6.9
Pod documented
44 4693 ▶ $nss ? @$nss : ();
Branch: 1 branch
BranchTrueFalseTotal
$nss ? : 2494 2199 4693
45 }
46  
47  
48 sub add(@)
49 58 ▶{ my $self = shift;
Subroutine add: called CC=7 SCAR=19.8
Pod documented
50 58 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 58 my @tnses = $instance->tnses;
55 58 ▶ @tnses or @tnses = '(none)';
Branch: 1 branch
BranchTrueFalseTotal
@tnses or @tnses = "(none)" 58 0 58
56  
57 # newest definitions overrule earlier.
58 58 unshift @{$self->{tns}{$_}}, $instance
59 58 for @tnses;
60  
61 # inventory where to find definitions which belong to some
62 # substitutionGroup.
63 58 ▶ while(my($base,$ext) = each %{$instance->sgs})
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$instance->sgs;}) 5 58 63
64 5 ▶ { $self->{sgs}{$base}{$_} ||= $instance for @$ext;
Condition33%1/3
$self->{"sgs"}{$base}{$_} ||= $instance
l!l&&r!l&&!r
050
MC/DC0%0/2
$self->{"sgs"}{$base}{$_} ||= $instance
$self->{"sgs"}{$base}{$_} $instance
Truth table33%1/3
A ||= B
A= $self->{"sgs"}{$base}{$_}B= $instance
ABresult
1X1
011
000
65 }
66 }
67 58 @_;
68 }
69  
70  
71 sub use($)
72 0 ▶{ my $self = shift;
Subroutine use: not called CC=1 SCAR=6.9
Pod documented
73 0 push @{$self->{use}}, @_;
74 0 @{$self->{use}};
75 }
76  
77  
78 4692 ▶sub schemas($) { $_[0]->namespace($_[1]) }
Subroutine schemas: called CC=1 SCAR=0.0
Pod documented
79  
80  
81 sub allSchemas()
82 2 ▶{ my $self = shift;
Subroutine allSchemas: called CC=1 SCAR=0.0
Pod documented
83 2 map {$self->schemas($_)} $self->list;
84 }
85  
86  
87 sub find($$;$)
88 3903 ▶{ my ($self, $kind) = (shift, shift);
Subroutine find: called CC=9 SCAR=24.1
Pod documented
89 3903 ▶ my ($ns, $name) = (@_%2==1) ? (unpack_type shift) : (shift, shift);
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 == 1 ? : 3903 0 3903
90 3903 my %opts = @_;
91  
92 3903 ▶ defined $ns or return undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $ns or return undef 3903 0 3903
93 3903 my $label = pack_type $ns, $name; # re-pack unpacked for consistency
94  
95 3903 foreach my $schema ($self->schemas($ns))
96 2550 { my $def = $schema->find($kind, $label);
97 2550 ▶ return $def if defined $def;
Branch: 1 branch
BranchTrueFalseTotal
if defined $def 2280 270 2550
98 }
99  
100 1623 ▶ my $used = exists $opts{include_used} ? $opts{include_used} : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $opts{'include_used'} ? : 0 1623 1623
101 1623 ▶ $used or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$used or return undef 1623 0 1623
102  
103 1623 foreach my $use ( @{$self->{use}} )
104 0 { my $def = $use->namespaces->find($kind, $label, include_used => 0);
105 0 ▶ return $def if defined $def;
Branch: 1 branch
BranchTrueFalseTotal
if defined $def 0 0 0
106 }
107  
108 1623 undef;
109 }
110  
111  
112 sub doesExtend($$)
113 815 ▶{ my ($self, $ext, $base) = @_;
Subroutine doesExtend: called CC=20 SCAR=31.7
Pod documented
114 815 ▶ return 1 if $ext eq $base;
Branch: 1 branch
BranchTrueFalseTotal
if $ext eq $base 15 800 815
115 800 ▶ return 0 if $ext =~ m/^unnamed /;
Branch: 1 branch
BranchTrueFalseTotal
if $ext =~ /^unnamed / 0 800 800
116  
117 800 my ($node, $super, $subnode);
118 800 ▶ if(my $st = $self->find(simpleType => $ext))
Branch: 2 branches
BranchTrueFalseTotal
if (my $st = $self->find('simpleType', $ext)) { } 86 714 800
elsif (my $ct = $self->find('complexType', $ext)) { } 4 710 714
119 { # pure simple type
120 86 $node = $st->{node};
121 86 ▶ if(($subnode) = $node->getChildrenByLocalName('restriction'))
Branch: 1 branch
BranchTrueFalseTotal
if (($subnode) = $node->getChildrenByLocalName("restriction")) 80 6 86
122 80 { $super = $subnode->getAttribute('base');
123 }
124 # list an union currently ignored
125 }
126 elsif(my $ct = $self->find(complexType => $ext))
127 4 { $node = $ct->{node};
128 # getChildrenByLocalName returns list, we know size one
129 4 ▶ if(my($sc) = $node->getChildrenByLocalName('simpleContent'))
Branch: 2 branches
BranchTrueFalseTotal
if (my($sc) = $node->getChildrenByLocalName('simpleContent')) { } 2 2 4
elsif (my($cc) = $node->getChildrenByLocalName('complexContent')) { } 2 0 2
130 { # tagged
131 2 ▶ if(($subnode) = $sc->getChildrenByLocalName('extension'))
Branch: 2 branches
BranchTrueFalseTotal
if (($subnode) = $sc->getChildrenByLocalName('extension')) { } 2 0 2
elsif (($subnode) = $sc->getChildrenByLocalName('restriction')) { } 0 0 0
132 2 { $super = $subnode->getAttribute('base');
133 }
134 elsif(($subnode) = $sc->getChildrenByLocalName('restriction'))
135 0 { $super = $subnode->getAttribute('base');
136 }
137 }
138 elsif(my($cc) = $node->getChildrenByLocalName('complexContent'))
139 { # real complex
140 2 ▶ if(($subnode) = $cc->getChildrenByLocalName('extension'))
Branch: 2 branches
BranchTrueFalseTotal
if (($subnode) = $cc->getChildrenByLocalName('extension')) { } 2 0 2
elsif (($subnode) = $cc->getChildrenByLocalName('restriction')) { } 0 0 0
141 2 { $super = $subnode->getAttribute('base');
142 }
143 elsif(($subnode) = $cc->getChildrenByLocalName('restriction'))
144 0 { $super = $subnode->getAttribute('base');
145 }
146 }
147 }
148 else
149 { # built-in
150 710 my ($ns, $local) = unpack_type $ext;
151 710 ▶ $ns eq SCHEMA2001 && $builtin_types{$local}
Branch: 1 branch
BranchTrueFalseTotal
$ns eq "http://www.w3.org/2001/XMLSchema" and $XML::Compile::Schema::NameSpaces::builtin_types{$local} or &error(&__x("cannot find {type} as simpleType or complexType", "type", $ext)) 710 0 710
Condition33%1/3
$ns eq "http://www.w3.org/2001/XMLSchema" and $XML::Compile::Schema::NameSpaces::builtin_types{$local}
!ll&&!rl&&r
00710
MC/DC0%0/2
$ns eq "http://www.w3.org/2001/XMLSchema" and $XML::Compile::Schema::NameSpaces::builtin_types{$local}
$ns eq "http://www.w3.org/2001/XMLSchema" $XML::Compile::Schema::NameSpaces::builtin_types{$local}
Truth table33%1/3
A and B
A= $ns eq "http://www.w3.org/2001/XMLSchema"B= $XML::Compile::Schema::NameSpaces::builtin_types{$local}
ABresult
0X0
100
111
152 or error __x"cannot find {type} as simpleType or complexType"
153 , type => $ext;
154 710 my ($bns, $blocal) = unpack_type $base;
155 710 ▶ $ns eq $bns
Branch: 1 branch
BranchTrueFalseTotal
$ns eq $bns or return 0 710 0 710
156 or return 0;
157  
158 710 ▶ while(my $e = $builtin_types{$local}{extends})
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = $XML::Compile::Schema::NameSpaces::builtin_types{$local}{"extends"}) 2808 698 3506
159 2808 ▶ { return 1 if $e eq $blocal;
Branch: 1 branch
BranchTrueFalseTotal
if $e eq $blocal 12 2796 2808
160 2796 $local = $e;
161 }
162 }
163  
164 788 ▶ $super
Branch: 1 branch
BranchTrueFalseTotal
$super or return 0 84 704 788
165 or return 0;
166  
167 84 ▶ my ($prefix, $local) = $super =~ m/:/ ? split(/:/,$super,2) : ('',$super);
Branch: 1 branch
BranchTrueFalseTotal
$super =~ /:/ ? : 26 58 84
168 84 my $supertype = pack_type $subnode->lookupNamespaceURI($prefix), $local;
169  
170 84 ▶ $base eq $supertype ? 1 : $self->doesExtend($supertype, $base);
Branch: 1 branch
BranchTrueFalseTotal
$base eq $supertype ? : 7 77 84
171 }
172  
173  
174 sub findTypeExtensions($)
175 2 ▶{ my ($self, $type) = @_;
Subroutine findTypeExtensions: called CC=7 SCAR=24.8
Pod documented
176  
177 2 my %ext;
178 2 ▶ if($self->find(simpleType => $type))
Branch: 2 branches
BranchTrueFalseTotal
if ($self->find('simpleType', $type)) { } 0 2 2
elsif ($self->find('complexType', $type)) { } 2 0 2
179 { $self->doesExtend($_, $type) && $ext{$_}++
180 0 ▶ for map $_->simpleTypes, $self->allSchemas;
Branch: 1 branch
BranchTrueFalseTotal
$self->doesExtend($_, $type) and ++$ext{$_} 0 0 0
181 }
182 elsif($self->find(complexType => $type))
183 { $self->doesExtend($_, $type) && $ext{$_}++
184 2 ▶ for map $_->complexTypes, $self->allSchemas;
Branch: 1 branch
BranchTrueFalseTotal
$self->doesExtend($_, $type) and ++$ext{$_} 4 0 4
185 }
186 else
187 0 { error __x"cannot find base-type {type} for extensions", type => $type;
188 }
189 2 sort keys %ext;
190 }
191  
192 sub autoexpand_xsi_type($)
193 2 ▶{ my ($self, $type) = @_;
Subroutine autoexpand_xsi_type: called CC=1 SCAR=0.0
Pod undocumented
194 2 my @ext = $self->findTypeExtensions($type);
195 2 trace "discovered xsi:type choices for $type:\n ". join("\n ", @ext);
196 2 \@ext;
197 }
198  
199  
200 sub findSgMembers($$)
201 1145 ▶{ my ($self, $class, $base) = @_;
Subroutine findSgMembers: called CC=3 SCAR=11.0
Pod documented
202 1145 ▶ my $s = $self->{sgs}{$base}
Branch: 1 branch
BranchTrueFalseTotal
my $s = $self->{"sgs"}{$base} or return 24 1121 1145
203 or return;
204  
205 24 my @sgs;
206 24 ▶ while(my($ext, $instance) = each %$s)
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$s) 40 24 64
207 40 { push @sgs, $instance->find($class => $ext)
208 , $self->findSgMembers($class, $ext);
209 }
210 24 @sgs;
211 }
212  
213  
214 sub findID($;$)
215 779 ▶{ my $self = shift;
Subroutine findID: called CC=7 SCAR=27.0
Pod documented
216 779 ▶ my ($label, $ns, $id)
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 779 0 779
217 = @_==1 ? ($_[0], unpack_id $_[0]) : (pack_id($_[0], $_[1]), @_);
218 779 ▶ defined $ns or return undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $ns or return undef 779 0 779
219  
220 779 my $xpc = XML::LibXML::XPathContext->new;
221 779 $xpc->registerNs(a => $ns);
222  
223 779 my @nodes;
224 779 foreach my $fragment ($self->schemas($ns))
225 0 ▶ { @nodes = $xpc->findnodes("/*/a:*#$id", $fragment->schema)
Branch: 1 branch
BranchTrueFalseTotal
@nodes = $xpc->findnodes("/*/a:*#$id", $fragment->schema) or next 0 0 0
226 or next;
227  
228 0 ▶ return $nodes[0]
Branch: 1 branch
BranchTrueFalseTotal
if @nodes == 1 0 0 0
229 if @nodes==1;
230  
231 0 ▶ error "multiple elements with the same id {id} in {source}"
Condition0%0/3
$fragment->filename || $fragment->source
l!l&&r!l&&!r
000
MC/DC0%0/2
$fragment->filename || $fragment->source
$fragment->filename $fragment->source
Truth table0%0/3
A || B
A= $fragment->filenameB= $fragment->source
ABresult
1X1
011
000
232 , id => $label
233 , source => ($fragment->filename || $fragment->source);
234 }
235  
236 779 undef;
237 }
238  
239  
240 sub printIndex(@)
241 0 ▶{ my $self = shift;
Subroutine printIndex: not called CC=8 SCAR=42.8
Pod documented
242 0 ▶ my $fh = @_ % 2 ? shift : select;
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 ? : 0 0 0
243 0 my %opts = @_;
244  
245 0 ▶ my $nss = delete $opts{namespace} || [$self->list];
Condition0%0/2
delete $opts{'namespace'} || [$self->list]
l!l
00
MC/DC0%0/1
delete $opts{'namespace'} || [$self->list]
delete $opts{'namespace'}
Truth table0%0/2
A
A= delete $opts{'namespace'}
Aresult
11
00
246 0 ▶ foreach my $nsuri (ref $nss eq 'ARRAY' ? @$nss : $nss)
Branch: 1 branch
BranchTrueFalseTotal
ref $nss eq 'ARRAY' ? : 0 0 0
247 0 { $_->printIndex($fh, %opts) for $self->namespace($nsuri);
248 }
249  
250 0 ▶ my $show_used = exists $opts{include_used} ? $opts{include_used} : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $opts{'include_used'} ? : 0 0 0
251 0 foreach my $use ($self->use)
252 0 { $use->printIndex(%opts, include_used => 0);
253 }
254  
255 0 $self;
256 }
257  
258  
259 sub importIndex(%)
260 0 ▶{ my ($self, %args) = @_;
Subroutine importIndex: not called CC=5 SCAR=34.0
Pod documented
261 0 my %import;
262 0 foreach my $fragment (map $self->schemas($_), $self->list)
263 0 { foreach my $import ($fragment->imports)
264 0 { $import{$import}{$_}++ for $fragment->importLocations($import);
265 }
266 }
267 0 foreach my $ns (keys %import)
268 0 { $import{$ns} = [ grep length, keys %{$import{$ns}} ];
269 }
270 0 \%import;
271 }
272  
273 50 1;
« Compile/Schema.pm ↑ Summary Compile/Schema/Instance.pm »