Compile/Schema/Instance.pm

filter: Statementstmt 87.5% 133 / 152 Branchbran 62.9% 39 / 62 Conditioncond 58.3% 28 / 48 MC/DCmcdc 25.9% 7 / 27 Subroutinesub 74.1% 23 / 31 Podpod 95.8% 23 / 24 totaltotal 73.5% 253 / 344 CC 55 SCAR 28.7
CC
55
Cov
76%
_collectTypes
37.4
printIndex
20.1
find
11.9
CRAP 17.7
« Compile/Schema/NameSpaces.pm ↑ Summary Compile/Translate/Writer.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::Instance;{
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 50 ▶use XML::Compile::Schema::Specs;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 50 ▶use XML::Compile::Util qw/pack_type unpack_type/;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 50 ▶use Scalar::Util qw/weaken/;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 50 my @defkinds = qw/element attribute simpleType complexType
23 attributeGroup group/;
24 50 my %defkinds = map +($_ => 1), @defkinds;
25  
26  
27 sub new($@)
28 58 ▶{ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
29 58 (bless {}, $class)->init( {top => @_} );
30 }
31  
32 sub init($)
33 58 ▶{ my ($self, $args) = @_;
Subroutine init: called CC=4 SCAR=14.2
Pod undocumented
34 58 my $top = $args->{top};
35 58 ▶ defined $top && $top->isa('XML::LibXML::Node')
Branch: 1 branch
BranchTrueFalseTotal
defined $top and $top->isa("XML::LibXML::Node") or &panic("instance is based on XML node") 58 0 58
Condition33%1/3
defined $top and $top->isa("XML::LibXML::Node")
!ll&&!rl&&r
0058
MC/DC0%0/2
defined $top and $top->isa("XML::LibXML::Node")
defined $top $top->isa("XML::LibXML::Node")
Truth table33%1/3
A and B
A= defined $topB= $top->isa("XML::LibXML::Node")
ABresult
0X0
100
111
36 or panic "instance is based on XML node";
37  
38 58 $self->{filename} = $args->{filename};
39 58 $self->{source} = $args->{source};
40 58 $self->{$_} = {} for @defkinds, 'sgs', 'import';
41 58 $self->{include} = [];
42  
43 58 $self->_collectTypes($top, $args);
44 58 $self;
45 }
46  
47  
48 1 ▶sub targetNamespace { shift->{tns} }
Subroutine targetNamespace: called CC=1 SCAR=0.0
Pod documented
49 0 ▶sub schemaNamespace { shift->{xsd} }
Subroutine schemaNamespace: not called CC=1 SCAR=6.9
Pod documented
50 0 ▶sub schemaInstance { shift->{xsi} }
Subroutine schemaInstance: not called CC=1 SCAR=6.9
Pod documented
51 1 ▶sub source { shift->{source} }
Subroutine source: called CC=1 SCAR=0.0
Pod documented
52 1 ▶sub filename { shift->{filename} }
Subroutine filename: called CC=1 SCAR=0.0
Pod documented
53 0 ▶sub schema { shift->{schema} }
Subroutine schema: not called CC=1 SCAR=6.9
Pod documented
54  
55  
56 58 ▶sub tnses() {keys %{shift->{tnses}}}
Subroutine tnses: called CC=1 SCAR=0.0
Pod documented
57  
58  
59 63 ▶sub sgs() { shift->{sgs} }
Subroutine sgs: called CC=1 SCAR=0.0
Pod documented
60  
61  
62 0 ▶sub type($) { $_[0]->{types}{$_[1]} }
Subroutine type: not called CC=1 SCAR=6.9
Pod documented
63  
64  
65 0 ▶sub element($) { $_[0]->{element}{$_[1]} }
Subroutine element: not called CC=1 SCAR=6.9
Pod documented
66  
67  
68 5 ▶sub elements() { keys %{shift->{element}} }
Subroutine elements: called CC=1 SCAR=0.0
Pod documented
69 1 ▶sub attributes() { keys %{shift->{attributes}} }
Subroutine attributes: called CC=1 SCAR=0.0
Pod documented
70 1 ▶sub attributeGroups() { keys %{shift->{attributeGroup}} }
Subroutine attributeGroups: called CC=1 SCAR=0.0
Pod documented
71 1 ▶sub groups() { keys %{shift->{group}} }
Subroutine groups: called CC=1 SCAR=0.0
Pod documented
72 5 ▶sub simpleTypes() { keys %{shift->{simpleType}} }
Subroutine simpleTypes: called CC=1 SCAR=0.0
Pod documented
73 7 ▶sub complexTypes() { keys %{shift->{complexType}} }
Subroutine complexTypes: called CC=1 SCAR=0.0
Pod documented
74  
75  
76 4 ▶sub types() { ($_[0]->simpleTypes, $_[0]->complexTypes) }
Subroutine types: called CC=1 SCAR=0.0
Pod documented
77  
78  
79 50 my %skip_toplevel = map +($_ => 1), qw/annotation notation redefine/;
80  
81 sub _collectTypes($$)
82 58 ▶{ my ($self, $schema, $args) = @_;
Subroutine _collectTypes: called CC=27 SCAR=36.2
83  
84 58 ▶ $schema->localName eq 'schema'
Branch: 1 branch
BranchTrueFalseTotal
$schema->localName eq "schema" or &panic("requires schema element") 58 0 58
85 or panic "requires schema element";
86  
87 58 ▶ my $xsd = $self->{xsd} = $schema->namespaceURI || '<none>';
Condition50%1/2
$schema->namespaceURI || '<none>'
l!l
580
MC/DC0%0/1
$schema->namespaceURI || '<none>'
$schema->namespaceURI
Truth table50%1/2
A
A= $schema->namespaceURI
Aresult
11
00
88 58 ▶ if(length $xsd)
Branch: 1 branch
BranchTrueFalseTotal
if (length $xsd) 58 0 58
89 { my $def = $self->{def}
90 58 ▶ = XML::Compile::Schema::Specs->predefinedSchema($xsd)
Branch: 1 branch
BranchTrueFalseTotal
my $def = $self->{"def"} = "XML::Compile::Schema::Specs"->predefinedSchema($xsd) or &error(&__x("schema namespace `{namespace}' not (yet) supported", "namespace", $xsd)) 58 0 58
91 or error __x"schema namespace `{namespace}' not (yet) supported"
92 , namespace => $xsd;
93  
94 58 $self->{xsi} = $def->{uri_xsi};
95 }
96  
97 58 my $tns;
98 58 ▶ if($tns = $args->{target_namespace})
Branch: 1 branch
BranchTrueFalseTotal
if ($tns = $args->{'target_namespace'}) { } 1 57 58
99 1 { $schema->removeAttribute('targetNamespace');
100 1 $schema->setAttribute(targetNamespace => $tns);
101 }
102 else
103 57 ▶ { $tns = $schema->getAttribute('targetNamespace') || '';
Condition100%2/2
$schema->getAttribute('targetNamespace') || ''
l!l
552
MC/DC100%1/1
$schema->getAttribute('targetNamespace') || ''
$schema->getAttribute('targetNamespace')
Truth table100%2/2
A
A= $schema->getAttribute('targetNamespace')
Aresult
11
00
104 }
105 58 $self->{tns} = $tns;
106  
107 $self->{efd} = $args->{element_form_default}
108 58 ▶ || $schema->getAttribute('elementFormDefault')
Condition100%5/5
$args->{'element_form_default'} || $schema->getAttribute('elementFormDefault') || 'unqualified'
l!l
1642
$args->{"element_form_default"} or $schema->getAttribute("elementFormDefault")
l!l&&r!l&&!r
11542
MC/DC100%2/2
$args->{'element_form_default'} || $schema->getAttribute('elementFormDefault') || 'unqualified'
$args->{"element_form_default"} $schema->getAttribute("elementFormDefault")
Truth table100%3/3
A or B
A= $args->{"element_form_default"}B= $schema->getAttribute("elementFormDefault")
ABresult
1X1
011
000
109 || 'unqualified';
110  
111 $self->{afd} = $args->{attribute_form_default}
112 58 ▶ || $schema->getAttribute('attributeFormDefault')
Condition80%4/5
$args->{'attribute_form_default'} || $schema->getAttribute('attributeFormDefault') || 'unqualified'
l!l
256
$args->{"attribute_form_default"} or $schema->getAttribute("attributeFormDefault")
l!l&&r!l&&!r
0256
MC/DC50%1/2
$args->{'attribute_form_default'} || $schema->getAttribute('attributeFormDefault') || 'unqualified'
$args->{"attribute_form_default"} $schema->getAttribute("attributeFormDefault")
Truth table66%2/3
A or B
A= $args->{"attribute_form_default"}B= $schema->getAttribute("attributeFormDefault")
ABresult
1X1
011
000
113 || 'unqualified';
114  
115 58 $self->{tnses} = {}; # added when used
116 58 $self->{types} = {};
117  
118 58 $self->{schema} = $schema;
119 58 weaken($self->{schema});
120  
121 NODE:
122 58 foreach my $node ($schema->childNodes)
123 1020 ▶ { next unless $node->isa('XML::LibXML::Element');
Branch: 1 branch
BranchTrueFalseTotal
unless $node->isa("XML::LibXML::Element") 572 448 1020
124 448 my $local = $node->localName;
125 448 ▶ my $myns = $node->namespaceURI || '';
Condition50%1/2
$node->namespaceURI || ''
l!l
4480
MC/DC0%0/1
$node->namespaceURI || ''
$node->namespaceURI
Truth table50%1/2
A
A= $node->namespaceURI
Aresult
11
00
126 448 ▶ $myns eq $xsd
Branch: 1 branch
BranchTrueFalseTotal
$myns eq $xsd or &error(&__x("schema element `{name}' not in schema namespace {ns} but {other}", "name", $local, "ns", $xsd, "other", $myns || "<none>")) 448 0 448
Condition0%0/2
$myns || '<none>'
l!l
00
MC/DC0%0/1
$myns || '<none>'
$myns
Truth table0%0/2
A
A= $myns
Aresult
11
00
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 448 ▶ if $skip_toplevel{$local};
Branch: 1 branch
BranchTrueFalseTotal
if $skip_toplevel{$local} 10 438 448
132  
133 438 ▶ if($local eq 'import')
Branch: 1 branch
BranchTrueFalseTotal
if ($local eq "import") 1 437 438
134 1 ▶ { my $namespace = $node->getAttribute('namespace') || $tns;
Condition33%1/3
$node->getAttribute('namespace') || $tns
l!l&&r!l&&!r
100
MC/DC0%0/2
$node->getAttribute('namespace') || $tns
$node->getAttribute('namespace') $tns
Truth table33%1/3
A || B
A= $node->getAttribute('namespace')B= $tns
ABresult
1X1
011
000
135 1 ▶ my $location = $node->getAttribute('schemaLocation') || '';
Condition50%1/2
$node->getAttribute('schemaLocation') || ''
l!l
10
MC/DC0%0/1
$node->getAttribute('schemaLocation') || ''
$node->getAttribute('schemaLocation')
Truth table50%1/2
A
A= $node->getAttribute('schemaLocation')
Aresult
11
00
136 1 push @{$self->{import}{$namespace}}, $location;
137 1 next NODE;
138 }
139  
140 437 ▶ if($local eq 'include')
Branch: 1 branch
BranchTrueFalseTotal
if ($local eq "include") 0 437 437
141 0 ▶ { my $location = $node->getAttribute('schemaLocation')
Branch: 1 branch
BranchTrueFalseTotal
my $location = $node->getAttribute("schemaLocation") or &error(&__x("include requires schemaLocation attribute at line {linenr}", "linenr", $node->line_number)) 0 0 0
142 or error __x"include requires schemaLocation attribute at line {linenr}"
143 , linenr => $node->line_number;
144  
145 0 push @{$self->{include}}, $location;
146 0 next NODE;
147 }
148  
149 437 ▶ unless($defkinds{$local})
Branch: 1 branch
BranchTrueFalseTotal
unless ($defkinds{$local}) 0 437 437
150 0 { mistake __x"ignoring unknown definition class {class}"
151 , class => $local;
152 0 next;
153 }
154  
155 437 ▶ my $name = $node->getAttribute('name')
Branch: 1 branch
BranchTrueFalseTotal
my $name = $node->getAttribute("name") or &error(&__x("schema component {local} without name at line {linenr}", "local", $local, "linenr", $node->line_number)) 437 0 437
156 or error __x"schema component {local} without name at line {linenr}"
157 , local => $local, linenr => $node->line_number;
158  
159 437 ▶ my $tns = $node->getAttribute('targetNamespace') || $tns;
Condition66%2/3
$node->getAttribute('targetNamespace') || $tns
l!l&&r!l&&!r
04316
MC/DC50%1/2
$node->getAttribute('targetNamespace') || $tns
$node->getAttribute('targetNamespace') $tns
Truth table66%2/3
A || B
A= $node->getAttribute('targetNamespace')B= $tns
ABresult
1X1
011
000
160 437 my $type = pack_type $tns, $name;
161 437 $self->{tnses}{$tns}++;
162 437 $self->{$local}{$type} = $node;
163  
164 437 ▶ if(my $sg = $node->getAttribute('substitutionGroup'))
Branch: 1 branch
BranchTrueFalseTotal
if (my $sg = $node->getAttribute("substitutionGroup")) 5 432 437
165 5 ▶ { my ($prefix, $l) = $sg =~ m/:/ ? split(/:/, $sg, 2) : ('',$sg);
Branch: 1 branch
BranchTrueFalseTotal
$sg =~ /:/ ? : 5 0 5
166 5 my $base = pack_type $node->lookupNamespaceURI($prefix), $l;
167 5 push @{$self->{sgs}{$base}}, $type;
168 }
169 }
170  
171 58 $self;
172 }
173  
174  
175 0 ▶sub includeLocations() { @{shift->{include}} }
Subroutine includeLocations: not called CC=1 SCAR=6.9
Pod documented
176  
177  
178 0 ▶sub imports() { keys %{shift->{import}} }
Subroutine imports: not called CC=1 SCAR=6.9
Pod documented
179  
180  
181 sub importLocations($)
182 0 ▶{ my $locs = $_[0]->{import}{$_[1]};
Subroutine importLocations: not called CC=2 SCAR=17.9
Pod documented
183 0 ▶ $locs ? @$locs : ();
Branch: 1 branch
BranchTrueFalseTotal
$locs ? : 0 0 0
184 }
185  
186  
187 sub printIndex(;$)
188 1 ▶{ my $self = shift;
Subroutine printIndex: called CC=15 SCAR=30.0
Pod documented
189 1 ▶ my $fh = @_ % 2 ? shift : select;
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 ? : 1 0 1
190 1 my %args = @_;
191  
192 1 $fh->print("namespace: ", $self->targetNamespace, "\n");
193 1 ▶ if(defined(my $filename = $self->filename))
Branch: 2 branches
BranchTrueFalseTotal
if (defined(my $filename = $self->filename)) { } 0 1 1
elsif (defined(my $source = $self->source)) { } 1 0 1
194 0 { $fh->print(" filename: $filename\n");
195 }
196 elsif(defined(my $source = $self->source))
197 1 { $fh->print(" source: $source\n");
198 }
199  
200 my @kinds
201 = ! defined $args{kinds} ? @defkinds
202 0 : ref $args{kinds} eq 'ARRAY' ? @{$args{kinds}}
203 1 ▶ : $args{kinds};
Branch: 2 branches
BranchTrueFalseTotal
!defined($args{'kinds'}) ? : 1 0 1
ref $args{'kinds'} eq 'ARRAY' ? : 0 0 0
204  
205 my $list_abstract
206 1 ▶ = exists $args{list_abstract} ? $args{list_abstract} : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'list_abstract'} ? : 0 1 1
207  
208 1 foreach my $kind (@kinds)
209 6 { my $table = $self->{$kind};
210 6 ▶ keys %$table or next;
Branch: 1 branch
BranchTrueFalseTotal
keys %$table or next 5 1 6
211 5 ▶ $fh->print(" definitions of ${kind}s:\n") if @kinds > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @kinds > 1 5 0 5
212  
213 5 foreach (sort keys %$table)
214 145 { my $info = $self->find($kind, $_);
215 145 my ($ns, $name) = unpack_type $_;
216 145 ▶ next if $info->{abstract} && ! $list_abstract;
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"abstract"} and not $list_abstract 0 145 145
Condition66%2/3
$info->{"abstract"} and not $list_abstract
!ll&&!rl&&r
14050
MC/DC0%0/2
$info->{"abstract"} and not $list_abstract
$info->{"abstract"} not $list_abstract
Truth table66%2/3
A and not(B)
A= $info->{"abstract"}B= not $list_abstract
ABresult
0X0
100
111
217 145 ▶ my $abstract = $info->{abstract} ? ' [abstract]' : '';
Branch: 1 branch
BranchTrueFalseTotal
$info->{'abstract'} ? : 5 140 145
218 145 ▶ my $final = $info->{final} ? ' [final]' : '';
Branch: 1 branch
BranchTrueFalseTotal
$info->{'final'} ? : 0 145 145
219 145 $fh->print(" $name$abstract$final\n");
220 }
221 }
222 }
223  
224  
225 sub find($$)
226 2737 ▶{ my ($self, $kind, $full) = @_;
Subroutine find: called CC=11 SCAR=24.8
Pod documented
227 2737 ▶ my $node = $self->{$kind}{$full}
Branch: 1 branch
BranchTrueFalseTotal
my $node = $self->{$kind}{$full} or return 2466 271 2737
228 or return;
229  
230 2466 ▶ return $node # translation of XML node into info is cached
Branch: 1 branch
BranchTrueFalseTotal
if ref $node eq "HASH" 2033 433 2466
231 if ref $node eq 'HASH';
232  
233 433 my %info = (type => $kind, node => $node, full => $full);
234 433 @info{'ns', 'name'} = unpack_type $full;
235  
236 433 $self->{$kind}{$full} = \%info;
237  
238 433 ▶ my $abstract = $node->getAttribute('abstract') || '';
Condition100%2/2
$node->getAttribute('abstract') || ''
l!l
7426
MC/DC100%1/1
$node->getAttribute('abstract') || ''
$node->getAttribute('abstract')
Truth table100%2/2
A
A= $node->getAttribute('abstract')
Aresult
11
00
239 433 ▶ $info{abstract} = $abstract eq 'true' || $abstract eq '1';
Condition66%2/3
$abstract eq 'true' || $abstract eq '1'
l!l&&r!l&&!r
70426
MC/DC50%1/2
$abstract eq 'true' || $abstract eq '1'
$abstract eq 'true' $abstract eq '1'
Truth table66%2/3
A || B
A= $abstract eq 'true'B= $abstract eq '1'
ABresult
1X1
011
000
240  
241 433 ▶ my $final = $node->getAttribute('final') || '';
Condition50%1/2
$node->getAttribute('final') || ''
l!l
0433
MC/DC0%0/1
$node->getAttribute('final') || ''
$node->getAttribute('final')
Truth table50%1/2
A
A= $node->getAttribute('final')
Aresult
11
00
242 433 ▶ $info{final} = $final eq 'true' || $final eq '1';
Condition33%1/3
$final eq 'true' || $final eq '1'
l!l&&r!l&&!r
00433
MC/DC0%0/2
$final eq 'true' || $final eq '1'
$final eq 'true' $final eq '1'
Truth table33%1/3
A || B
A= $final eq 'true'B= $final eq '1'
ABresult
1X1
011
000
243  
244 433 my $local = $node->localName;
245 433 ▶ if($local eq 'element') { $info{efd} = $node->getAttribute('form') }
Branch: 2 branches
BranchTrueFalseTotal
if ($local eq 'element') { } 245 188 433
elsif ($local eq 'attribute') { } 4 184 188
246 4 elsif($local eq 'attribute'){ $info{afd} = $node->getAttribute('form') }
247 433 ▶ $info{efd} ||= $self->{efd}; # both needed for nsContext
Condition33%1/3
$info{"efd"} ||= $self->{"efd"}
l!l&&r!l&&!r
04330
MC/DC0%0/2
$info{"efd"} ||= $self->{"efd"}
$info{"efd"} $self->{"efd"}
Truth table33%1/3
A ||= B
A= $info{"efd"}B= $self->{"efd"}
ABresult
1X1
011
000
248 433 ▶ $info{afd} ||= $self->{afd};
Condition33%1/3
$info{"afd"} ||= $self->{"afd"}
l!l&&r!l&&!r
04330
MC/DC0%0/2
$info{"afd"} ||= $self->{"afd"}
$info{"afd"} $self->{"afd"}
Truth table33%1/3
A ||= B
A= $info{"afd"}B= $self->{"afd"}
ABresult
1X1
011
000
249 433 \%info;
250 }
251  
252 50 1;
« Compile/Schema/NameSpaces.pm ↑ Summary Compile/Translate/Writer.pm »