Schema/ElementNode.pm

filter: Statementstmt 97.0% 99 / 102 Branchbran 89.0% 57 / 64 Conditioncond 81.8% 18 / 22 MC/DCmcdc 57.1% 8 / 14 Subroutinesub 100% 11 / 11 Podpod 14.2% 1 / 7 totaltotal 88.1% 194 / 220 CC 45 SCAR 21.8
CC
45
Cov
93%
check_attributes
13.0
check_contents
12.4
parse
8.1
CRAP 8.8
« Schema/SimpleType.pm ↑ Summary Schema/Attribute.pm »
1 package XML::Validator::Schema::ElementNode;
2 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 =head1 NAME
6  
7 XML::Validator::Schema::ElementNode - an element node in a schema object
8  
9 =head1 DESCRIPTION
10  
11 This is an internal module used by XML::Validator::Schema to represent
12 element nodes derived from XML Schema documents.
13  
14 =cut
15  
16 5 ▶use base qw(XML::Validator::Schema::Node);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 5 ▶use XML::Validator::Schema::Util qw(_attr _err);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 # create a node based on the contents of an <element> found in the
20 # schema document
21 212 ▶sub parse {
Subroutine parse: called CC=8 SCAR=20.9
Pod undocumented
22 212 my ($pkg, $data) = @_;
23 212 my $self = $pkg->new();
24  
25 212 my $name = _attr($data, 'name');
26 212 ▶ _err('Found element without a name.')
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 212 212
27 unless $name;
28 212 $self->name($name);
29  
30 212 my $type_name = _attr($data, 'type');
31 212 ▶ if ($type_name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type_name) 86 126 212
32 86 $self->{unresolved_type} = 1;
33 86 $self->{type_name} = $type_name;
34 }
35  
36 212 my $min = _attr($data, 'minOccurs');
37 212 ▶ $min = 1 unless defined $min;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $min 161 51 212
38 212 ▶ _err("Invalid value for minOccurs '$min' found in <$name>.")
Branch: 1 branch
BranchTrueFalseTotal
unless $min =~ /^\d+$/ 0 212 212
39 unless $min =~ /^\d+$/;
40 212 $self->{min} = $min;
41  
42 212 my $max = _attr($data, 'maxOccurs');
43 212 ▶ $max = 1 unless defined $max;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $max 179 33 212
44 212 ▶ _err("Invalid value for maxOccurs '$max' found in <$name>.")
Branch: 1 branch
BranchTrueFalseTotal
unless $max =~ /^\d+$/ or $max eq "unbounded" 0 212 212
Condition66%2/3
$max =~ /^\d+$/ or $max eq "unbounded"
l!l&&r!l&&!r
188240
MC/DC0%0/2
$max =~ /^\d+$/ or $max eq "unbounded"
$max =~ /^\d+$/ $max eq "unbounded"
Truth table66%2/3
A or B
A= $max =~ /^\d+$/B= $max eq "unbounded"
ABresult
1X1
011
000
45 unless $max =~ /^\d+$/ or $max eq 'unbounded';
46 212 $self->{max} = $max;
47  
48 212 return $self;
49 }
50  
51 # override add_daughter to check parent-specific requirements
52 380 ▶sub add_daughter {
Subroutine add_daughter: called CC=7 SCAR=19.7
Pod documented
53 380 my ($self, $d) = @_;
54  
55 # check that min/mix are 0 or 1 for 'all' contents
56 380 ▶ if ($self->{is_all} and $d->isa('XML::Validator::Schema::ElementNode')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"is_all"} and $d->isa("XML::Validator::Schema::ElementNode")) 12 368 380
Condition100%3/3
$self->{"is_all"} and $d->isa("XML::Validator::Schema::ElementNode")
!ll&&!rl&&r
367112
MC/DC100%2/2
$self->{"is_all"} and $d->isa("XML::Validator::Schema::ElementNode")
$self->{"is_all"} $d->isa("XML::Validator::Schema::ElementNode")
Truth table100%3/3
A and B
A= $self->{"is_all"}B= $d->isa("XML::Validator::Schema::ElementNode")
ABresult
0X0
100
111
57 _err("Element '$d->{name}' must have minOccurs of 0 or 1 because it is within an <all>.")
58 12 ▶ unless ($d->{min} eq '0' or $d->{min} eq '1');
Branch: 1 branch
BranchTrueFalseTotal
unless $d->{"min"} eq 0 or $d->{"min"} eq 1 0 12 12
Condition66%2/3
$d->{"min"} eq 0 or $d->{"min"} eq 1
l!l&&r!l&&!r
2100
MC/DC0%0/2
$d->{"min"} eq 0 or $d->{"min"} eq 1
$d->{"min"} eq 0 $d->{"min"} eq 1
Truth table66%2/3
A or B
A= $d->{"min"} eq 0B= $d->{"min"} eq 1
ABresult
1X1
011
000
59 _err("Element '$d->{name}' must have maxOccurs of 0 or 1 because it is within an <all>.")
60 12 ▶ unless ($d->{max} eq '0' or $d->{max} eq '1');
Branch: 1 branch
BranchTrueFalseTotal
unless $d->{"max"} eq 0 or $d->{"max"} eq 1 1 11 12
Condition66%2/3
$d->{"max"} eq 0 or $d->{"max"} eq 1
l!l&&r!l&&!r
0111
MC/DC50%1/2
$d->{"max"} eq 0 or $d->{"max"} eq 1
$d->{"max"} eq 0 $d->{"max"} eq 1
Truth table66%2/3
A or B
A= $d->{"max"} eq 0B= $d->{"max"} eq 1
ABresult
1X1
011
000
61 }
62  
63 379 return $self->SUPER::add_daughter($d);
64 }
65  
66 # check contents of an element against declared type
67 895 ▶sub check_contents {
Subroutine check_contents: called CC=12 SCAR=25.1
Pod undocumented
68 895 my ($self, $contents) = @_;
69  
70 # do type check if a type is declared
71 895 ▶ if ($self->{type}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'type'}) { } 201 694 895
elsif ($self->{'is_complex'} and $contents =~ /\S/) { } 1 949 950
72  
73 # Union isn't really a simple type. In a sense it isn't a type
74 # at all, if it is, it sure as hell isn't simple. It's just
75 # a rather laissez-faire view of what the type might be.
76 # Hence I've not handled union in SimpleType::check. As it's
77 # not handled directly in SimpleType, I've bastardized the usage
78 # of $self->{type} to just contain a string effectively indicating
79 # that it is an exception
80  
81 201 my ( $ok, $msg);
82 201 ▶ if ($self->{type} eq 'union' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'type'} eq 'union') { } 6 195 201
83 # it only has to match one of the member types:
84 6 ▶ if ( not defined($self->{members}) ){
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{'members'}) { } 0 6 6
85 0 die "Internal error: I aint got no members\n";
86 } else {
87 6 ▶ if (@{$self->{members}} == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"members"};} == 0) 0 6 6
88 0 _err("Element '$self->{name}' is a union with no members.");
89 }
90 }
91  
92 6 my $types = '';
93 6 $ok = 0;
94 6 foreach my $m ( @{$self->{members}} ) {
95 10 ▶ if ( not my $x = ref($m) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (my $x = ref $m) 0 10 10
96 0 die ("Internal error, that isn't a reference\n");
97 }
98 10 ( $ok, $msg ) = $m->{type}->check($contents);
99 10 ▶ last if $ok;
Branch: 1 branch
BranchTrueFalseTotal
if $ok 4 6 10
100 6 $types .= ' '.$m->{type}->{base}->{name};
101 }
102  
103 6 ▶ if ( not $ok ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 2 4 6
104 # Just giving the error for the last one checked isn't
105 # really that helpful. We need to make it explicit that
106 # NONE of the tests succeeded.
107 2 $msg = "content does not match any of the union base types".
108 " [ $types ]";
109 }
110 } else {
111 195 ($ok, $msg) = $self->{type}->check($contents);
112 }
113 201 ▶ _err("Illegal value '$contents' in element <$self->{name}>, $msg")
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 26 175 201
114 unless $ok;
115 }
116  
117 # mixed content isn't supported, so all complex elements must be
118 # element only or have nothing but whitespace between the elements
119 ▶ elsif ($self->{is_complex} and $contents =~ /\S/) {
Condition100%3/3
$self->{"is_complex"} and $contents =~ /\S/
!ll&&!rl&&r
2564371
MC/DC100%2/2
$self->{"is_complex"} and $contents =~ /\S/
$self->{"is_complex"} $contents =~ /\S/
Truth table100%3/3
A and B
A= $self->{"is_complex"}B= $contents =~ /\S/
ABresult
0X0
100
111
120 1 _err("Illegal character data found in element <$self->{name}>.");
121 }
122 }
123  
124 # check if a given name is a legal child, and return it if it is
125 566 ▶sub check_daughter {
Subroutine check_daughter: called CC=6 SCAR=17.9
Pod undocumented
126 566 my ($self, $name) = @_;
127 566 my ($daughter) = grep { $_->{name} eq $name } ($self->daughters);
128  
129 # doesn't even exist?
130 566 ▶ _err("Found unexpected <$name> inside <$self->{name}>. This is not a valid child element.")
Branch: 1 branch
BranchTrueFalseTotal
unless $daughter 8 558 566
131 unless $daughter;
132  
133 # push on
134 558 ▶ push @{$self->{memory} ||= []}, $name;
Condition100%2/2
$self->{"memory"} ||= []
l!l
46494
MC/DC100%1/1
$self->{"memory"} ||= []
$self->{"memory"}
Truth table100%2/2
A
A= $self->{"memory"}
Aresult
11
00
135  
136 # check model
137 $self->{model}->check_model($self->{name}, $self->{memory})
138 558 ▶ if $self->{model};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"model"} 398 160 558
139  
140 # does this daughter have a valid type? if not, attempt to elaborate
141 545 ▶ if ($daughter->{unresolved_type}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($daughter->{"unresolved_type"}) 22 523 545
142 22 $self->root->complete_type($daughter);
143 22 ($daughter) = grep { $_->{name} eq $name } ($self->daughters);
144 }
145
146 # is this daughter a dangling ref? if so, complete it
147 545 ▶ if ($daughter->{unresolved_ref}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($daughter->{"unresolved_ref"}) 1 544 545
148 1 $self->root->complete_ref($daughter);
149 1 ($daughter) = grep { $_->{name} eq $name } ($self->daughters);
150 }
151  
152 545 return $daughter;
153 }
154  
155 # check that attributes are kosher
156 545 ▶sub check_attributes {
Subroutine check_attributes: called CC=13 SCAR=25.6
Pod undocumented
157 545 my ($self, $data) = @_;
158  
159 # get lists required and allowed attributes
160 545 my (@required, %allowed);
161 545 ▶ foreach my $attr (@{$self->{attr} || []}) {
Condition100%2/2
$self->{'attr'} || []
l!l
76469
MC/DC100%1/1
$self->{'attr'} || []
$self->{'attr'}
Truth table100%2/2
A
A= $self->{'attr'}
Aresult
11
00
162 97 $allowed{$attr->{name}} = $attr;
163 97 ▶ push(@required, $attr->{name}) if $attr->{required};
Branch: 1 branch
BranchTrueFalseTotal
if $attr->{"required"} 24 73 97
164 }
165  
166 # check attributes
167 545 my %saw;
168 545 foreach my $jcname (keys %$data) {
169 43 my $attr = $data->{$jcname};
170  
171 # attributes in the http://www.w3.org/2001/XMLSchema-instance
172 # namespace are processing instructions, not part of the
173 # document to be validated
174 next if $attr->{NamespaceURI}
175 43 ▶ eq 'http://www.w3.org/2001/XMLSchema-instance';
Branch: 1 branch
BranchTrueFalseTotal
if $attr->{"NamespaceURI"} eq "http://www.w3.org/2001/XMLSchema-instance" 2 41 43
176  
177 # attributes in http://www.w3.org/2000/xmlns/ are namespace
178 # declarations and don't concern us
179 41 ▶ next if $attr->{NamespaceURI} eq 'http://www.w3.org/2000/xmlns/';
Branch: 1 branch
BranchTrueFalseTotal
if $attr->{"NamespaceURI"} eq "http://www.w3.org/2000/xmlns/" 2 39 41
180  
181 39 my $name = $attr->{LocalName};
182 39 my $obj = $allowed{$name};
183 39 ▶ _err("Illegal attribute '$name' found in <$self->{name}>.")
Branch: 1 branch
BranchTrueFalseTotal
unless $obj 1 38 39
184 unless $obj;
185 38 $saw{$name} = 1;
186
187 # does this obj have an incomplete type? complete it
188 38 ▶ if ($obj->{unresolved_type}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj->{"unresolved_type"}) 1 37 38
189 1 $self->root->complete_attr_type($obj);
190 }
191  
192 # check value, if attribute is typed
193 38 ▶ if ($obj->{type}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj->{"type"}) 34 4 38
194 34 my ($ok, $msg) = $obj->{type}->check($attr->{Value});
195 34 ▶ _err("Illegal value '$attr->{Value}' for attribute '$name' in <$self->{name}>, $msg")
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 7 27 34
196 unless $ok;
197 }
198 }
199
200 # make sure all required attributes are present
201 537 foreach my $name (@required) {
202 _err("Missing required attribute '$name' in <$self->{name}>.")
203 20 ▶ unless $saw{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless $saw{$name} 3 17 20
204 }
205 }
206  
207 # finish
208 249 ▶sub compile {
Subroutine compile: called CC=3 SCAR=11.0
Pod undocumented
209 249 my $self = shift;
210  
211 249 ▶ if ($self->daughters and
Branch: 1 branch
BranchTrueFalseTotal
if ($self->daughters and ($self->daughters)[0]->isa("XML::Validator::Schema::ModelNode")) 48 201 249
Condition66%2/3
$self->daughters and ($self->daughters)[0]->isa("XML::Validator::Schema::ModelNode")
!ll&&!rl&&r
201048
MC/DC50%1/2
$self->daughters and ($self->daughters)[0]->isa("XML::Validator::Schema::ModelNode")
$self->daughters ($self->daughters)[0]->isa("XML::Validator::Schema::ModelNode")
Truth table66%2/3
A and B
A= $self->daughtersB= ($self->daughters)[0]->isa("XML::Validator::Schema::ModelNode")
ABresult
0X0
100
111
212 ($self->daughters)[0]->isa('XML::Validator::Schema::ModelNode')) {
213 48 ($self->daughters)[0]->compile;
214 }
215 }
216  
217 # forget about the past
218 1330 ▶sub clear_memory {
Subroutine clear_memory: called CC=2 SCAR=6.9
Pod undocumented
219 1330 ▶ @{$_[0]->{memory}} = () if $_[0]->{memory};
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]{"memory"} 335 995 1330
220 }
221  
222  
223 5 1;
« Schema/SimpleType.pm ↑ Summary Schema/Attribute.pm »