Schema/ModelNode.pm

filter: Statementstmt 92.5% 137 / 148 Branchbran 80.9% 34 / 42 Conditioncond 85.1% 23 / 27 MC/DCmcdc 43.7% 7 / 16 Subroutinesub 90.3% 28 / 31 Podpod 0.0% 0 / 4 totaltotal 85.4% 229 / 268 CC 32 SCAR 18.6
CC
32
Cov
89%
parse
14.9
_qual
10.0
_compile
7.0
CRAP 6.4
« Schema/Attribute.pm ↑ Summary Schema/ElementRefNode.pm »
1 package XML::Validator::Schema::ModelNode;
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 ▶use base 'XML::Validator::Schema::Node';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use constant DEBUG => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use XML::Validator::Schema::Util qw(_err _attr);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 XML:Validator::Schema::ModelNode
13  
14 =head1 DESCRIPTION
15  
16 Objects of this class represent the content models encountered while
17 parsing a schema. After a model is completely parsed it is compiled
18 into a regular expression and a human-readbale description and
19 assigned to the element or complex type's 'model' attribute.
20  
21 =cut
22  
23 # parse a model based on a <sequence>, <choice>, <all> or <union> returning the
24 # appropriate subclass
25 58 ▶sub parse {
Subroutine parse: called CC=14 SCAR=27.0
Pod undocumented
26 58 my ($pkg, $data) = @_;
27 58 my $name = $data->{LocalName};
28 58 ▶ croak("Unknown model type '$name'")
Branch: 1 branch
BranchTrueFalseTotal
unless $name eq "sequence" or $name eq "choice" or $name eq "all" or $name eq "union" 0 58 58
Condition88%8/9
$name eq "sequence" or $name eq "choice" or $name eq "all" or $name eq "union"
l!l&&r!l&&!r
5530
$name eq "sequence" or $name eq "choice" or $name eq "all"
l!l&&r!l&&!r
4963
$name eq "sequence" or $name eq "choice"
l!l&&r!l&&!r
4369
MC/DC0%0/4
$name eq "sequence" or $name eq "choice" or $name eq "all" or $name eq "union"
$name eq "sequence" $name eq "choice" $name eq "all" $name eq "union"
Truth table80%4/5
A or B or C or D
A= $name eq "sequence"B= $name eq "choice"C= $name eq "all"D= $name eq "union"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
29 unless $name eq 'sequence' or $name eq 'choice' or $name eq 'all'
30 or $name eq 'union';
31  
32 # construct as appropriate
33 58 $pkg = "XML::Validator::Schema::" . ucfirst($name) . "ModelNode";
34 58 my $self = $pkg->new();
35
36 58 my $min = _attr($data, 'minOccurs');
37 58 ▶ $min = 1 unless defined $min;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $min 56 2 58
38 58 ▶ _err("Invalid value for minOccurs '$min' found in <$name>.")
Branch: 1 branch
BranchTrueFalseTotal
unless $min =~ /^\d+$/ 0 58 58
39 unless $min =~ /^\d+$/;
40 58 $self->{min} = $min;
41  
42 58 my $max = _attr($data, 'maxOccurs');
43 58 ▶ $max = 1 unless defined $max;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $max 53 5 58
44 58 ▶ _err("Invalid value for maxOccurs '$max' found in <$name>.")
Branch: 1 branch
BranchTrueFalseTotal
unless $max =~ /^\d+$/ or $max eq "unbounded" 0 58 58
Condition66%2/3
$max =~ /^\d+$/ or $max eq "unbounded"
l!l&&r!l&&!r
5530
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 58 $self->{max} = $max;
47  
48 58 ▶ if ($name eq 'all') {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "all") 6 52 58
49 _err("Found <all> with minOccurs neither 0 nor 1.")
50 6 ▶ unless $self->{min} eq '1' or $self->{min} eq '0';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"min"} eq 1 or $self->{"min"} eq 0 0 6 6
Condition66%2/3
$self->{"min"} eq 1 or $self->{"min"} eq 0
l!l&&r!l&&!r
510
MC/DC0%0/2
$self->{"min"} eq 1 or $self->{"min"} eq 0
$self->{"min"} eq 1 $self->{"min"} eq 0
Truth table66%2/3
A or B
A= $self->{"min"} eq 1B= $self->{"min"} eq 0
ABresult
1X1
011
000
51 _err("Found <all> with maxOccurs not 1.")
52 6 ▶ unless $self->{max} eq '1';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"max"} eq 1 0 6 6
53 }
54  
55 58 return $self;
56 }
57  
58 # compile a tree of elements and model nodes into a single model node
59 # attached to the containing element. This results in a tree
60 # containing only elements and the element having a 'model' object
61 # with working check_model() and check_final_model() methods.
62 52 ▶sub compile {
Subroutine compile: called CC=2 SCAR=6.9
Pod undocumented
63 52 my $self = shift;
64 52 my $root = $self->mother;
65  
66 # the root will get assigned all the ElementNodes composing the model.
67 52 $root->clear_daughters;
68  
69 # get two regular expressions, one for verifying the final
70 # composition of the tree and the other for detecting problems
71 # mid-model
72 52 my ($final_re, $running_re, $desc) = $self->_compile($root);
73  
74 51 $self->{description} = $desc;
75  
76 # hold onto the strings if debugging
77 51 $self->{final_re_string} = $final_re if DEBUG;
78 51 $self->{running_re_string} = $running_re if DEBUG;
79 51 print STDERR "Compile <$root->{name}> content model to:\n\t/$self->{final_re_string}/\n\t/$self->{running_re_string}\n\t$self->{description}\n\n"
80 if DEBUG;
81  
82 # compile the regular expressions
83 51 eval {
84 51 $self->{final_re} = qr/^$final_re$/;
85 51 $self->{running_re} = qr/^$running_re$/;
86 };
87 51 ▶ croak("Problem compiling content model '<$root->{name}>' into regular expression: $@") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 51 51
88  
89 # finished
90 51 $self->clear_daughters;
91 51 $root->{model} = $self;
92 }
93  
94 # recursive worker for compilation of content models. returns three
95 # text fragments - ($final_re, $running_re, $description)
96 55 ▶sub _compile {
Subroutine _compile: called CC=7 SCAR=19.5
97 55 my ($self, $root) = @_;
98 55 my @final_parts;
99 my @running_parts;
100 55 my @desc_parts;
101  
102 55 foreach my $d ($self->daughters) {
103 162 ▶ if ($d->isa('XML::Validator::Schema::ElementNode')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($d->isa('XML::Validator::Schema::ElementNode')) { } 159 3 162
elsif ($d->isa('XML::Validator::Schema::ModelNode')) { } 3 0 3
104 159 my $re_name = quotemeta('<' . $d->{name} . '>');
105 159 my $qual = _qual($d->{min}, $d->{max});
106 159 ▶ my $re = length($qual) ? '(?:' . $re_name . ")$qual" : $re_name;
Branch: 1 branch
BranchTrueFalseTotal
length $qual ? : 56 103 159
107 159 push @final_parts, $re;
108  
109 159 ▶ my $running_qual = _qual($d->{min} eq '0' ? 0 : 1, $d->{max});
Branch: 1 branch
BranchTrueFalseTotal
$d->{'min'} eq '0' ? : 42 117 159
110 159 ▶ my $running_re = length($running_qual) ? '(?:' . $re_name . ")$running_qual" : $re_name;
Branch: 1 branch
BranchTrueFalseTotal
length $running_qual ? : 56 103 159
111 159 push @running_parts, $running_re;
112  
113 159 push @desc_parts, $d->{name} . $qual;
114  
115 # push onto root's daughter list
116 159 $root->add_daughter($d);
117  
118 } elsif ($d->isa('XML::Validator::Schema::ModelNode')) {
119 # recurse
120 3 my ($final_part, $running_part, $desc)
121 = $d->_compile($root);
122 3 push @final_parts, $final_part;
123 3 push @running_parts, $running_part;
124 3 push @desc_parts, $desc;
125 } else {
126 0 croak("What's a " . ref($d) . " doing here?");
127 }
128 }
129
130 # combine parts into a regex matching the final and running contents
131 54 my $final_re = $self->_combine_final_parts(\@final_parts);
132 54 my $running_re = $self->_combine_running_parts(\@running_parts);
133 54 my $desc = $self->_combine_desc_parts(\@desc_parts);
134  
135 54 return ($final_re, $running_re, $desc);
136 }
137  
138 # assign a qualifier based on min/max
139 481 ▶sub _qual {
Subroutine _qual: called CC=10 SCAR=23.1
140 481 my ($min, $max) = @_;
141 481 ▶ return "" if $min eq '1' and $max eq '1';
Branch: 1 branch
BranchTrueFalseTotal
if $min eq 1 and $max eq 1 353 128 481
Condition100%3/3
$min eq 1 and $max eq 1
!ll&&!rl&&r
8741353
MC/DC100%2/2
$min eq 1 and $max eq 1
$min eq 1 $max eq 1
Truth table100%3/3
A and B
A= $min eq 1B= $max eq 1
ABresult
0X0
100
111
142 128 ▶ return "+" if $min eq '1' and $max eq 'unbounded';
Branch: 1 branch
BranchTrueFalseTotal
if $min eq 1 and $max eq "unbounded" 32 96 128
Condition100%3/3
$min eq 1 and $max eq "unbounded"
!ll&&!rl&&r
87932
MC/DC100%2/2
$min eq 1 and $max eq "unbounded"
$min eq 1 $max eq "unbounded"
Truth table100%3/3
A and B
A= $min eq 1B= $max eq "unbounded"
ABresult
0X0
100
111
143 96 ▶ return "?" if $min eq '0' and $max eq '1';
Branch: 1 branch
BranchTrueFalseTotal
if $min eq 0 and $max eq 1 54 42 96
Condition100%3/3
$min eq 0 and $max eq 1
!ll&&!rl&&r
103254
MC/DC100%2/2
$min eq 0 and $max eq 1
$min eq 0 $max eq 1
Truth table100%3/3
A and B
A= $min eq 0B= $max eq 1
ABresult
0X0
100
111
144 42 ▶ return "*" if $min eq '0' and $max eq 'unbounded';
Branch: 1 branch
BranchTrueFalseTotal
if $min eq 0 and $max eq "unbounded" 32 10 42
Condition66%2/3
$min eq 0 and $max eq "unbounded"
!ll&&!rl&&r
10032
MC/DC50%1/2
$min eq 0 and $max eq "unbounded"
$min eq 0 $max eq "unbounded"
Truth table66%2/3
A and B
A= $min eq 0B= $max eq "unbounded"
ABresult
0X0
100
111
145 10 ▶ return "{$min,}" if $max eq 'unbounded';
Branch: 1 branch
BranchTrueFalseTotal
if $max eq "unbounded" 0 10 10
146 10 return "{$min,$max}";
147 }
148  
149 # method to check a final content model
150 129 ▶sub check_final_model {
Subroutine check_final_model: called CC=2 SCAR=6.9
Pod undocumented
151 129 my ($self, $this_name, $names_ref) = @_;
152  
153 # prepare names for regex
154 129 my $names = join('', map { '<' . $_ . '>' } @$names_ref);
155  
156 129 print STDERR "Checking element string: '$names' against ".
157 "'$self->{final_re_string}'\n" if DEBUG;
158  
159 # do the match and return an error if necessary
160 129 ▶ if ($names !~ /$self->{final_re}/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $names =~ /$self->{"final_re"}/) 15 114 129
161 15 _err("Contents of element '$this_name' do not match content model '$self->{description}'.");
162 }
163 }
164  
165 # method to check content model in mid-parse. will succeed if the set
166 # of names constitute at least a prefix of the required content model.
167 377 ▶sub check_model {
Subroutine check_model: called CC=2 SCAR=6.9
Pod undocumented
168 377 my ($self, $this_name, $names_ref) = @_;
169  
170 # prepare names for regex
171 377 my $names = join('', map { '<' . $_ . '>' } @$names_ref);
172  
173 377 print STDERR "Checking element string: '$names' against ".
174 "'$self->{running_re_string}'\n" if DEBUG;
175  
176 # do the match and blame $names[-1] for failures
177 377 ▶ if ($names !~ /$self->{running_re}/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $names =~ /$self->{"running_re"}/) 16 361 377
178 16 _err("Inside element '$this_name', element '$names_ref->[-1]' does not match content model '$self->{description}'.");
179 }
180 }
181  
182 package XML::Validator::Schema::SequenceModelNode;
183 5 ▶use base 'XML::Validator::Schema::ModelNode';
Subroutine BEGIN: called CC=1 SCAR=0.0
184  
185 48 ▶sub _combine_final_parts {
Subroutine _combine_final_parts: called CC=1 SCAR=0.0
186 48 my ($self, $parts) = @_;
187  
188 # build final re
189 my $re = '(?:' . join('', @$parts) . ')' .
190 48 XML::Validator::Schema::ModelNode::_qual($self->{min}, $self->{max});
191  
192 48 return $re;
193 }
194  
195 48 ▶sub _combine_running_parts {
Subroutine _combine_running_parts: called CC=1 SCAR=0.0
196 48 my ($self, $parts) = @_;
197  
198 # build running re
199 48 my $re = join('', map { "(?:$_" } @$parts) .
200 ")?" x @$parts;
201 48 $re =~ s!\?$!!;
202 48 $re .= XML::Validator::Schema::ModelNode::_qual($self->{min},$self->{max});
203 48 return $re;
204 }
205  
206 43 ▶sub _combine_desc_parts {
Subroutine _combine_desc_parts: called CC=1 SCAR=0.0
207 43 my ($self, $parts) = @_;
208  
209 # build description
210 my $desc = '(' . join(',', @$parts) . ')'
211 43 . XML::Validator::Schema::ModelNode::_qual($self->{min}, $self->{max});
212  
213 43 return $desc;
214 }
215  
216 package XML::Validator::Schema::ChoiceModelNode;
217 5 ▶use base 'XML::Validator::Schema::ModelNode';
Subroutine BEGIN: called CC=1 SCAR=0.0
218  
219 12 ▶sub _combine_final_parts {
Subroutine _combine_final_parts: called CC=1 SCAR=0.0
220 12 my ($self, $parts) = @_;
221  
222 # build final re
223 30 my $re = '(?:' . join('|', map { '(?:'. $_ . ')' } @$parts) . ')' .
224 12 XML::Validator::Schema::ModelNode::_qual($self->{min}, $self->{max});
225  
226 12 return $re;
227 }
228  
229 6 ▶sub _combine_running_parts {
Subroutine _combine_running_parts: called CC=1 SCAR=0.0
230 6 my ($self, $parts) = @_;
231  
232 # build running re
233 my $re = '(?:' . $self->_combine_final_parts($parts) . ')' .
234 6 XML::Validator::Schema::ModelNode::_qual($self->{min}, $self->{max});
235  
236 6 return $re;
237 }
238  
239 6 ▶sub _combine_desc_parts {
Subroutine _combine_desc_parts: called CC=1 SCAR=0.0
240 6 my ($self, $parts) = @_;
241  
242 # build description
243 my $desc = '(' . join('|', @$parts) . ')' .
244 6 XML::Validator::Schema::ModelNode::_qual($self->{min}, $self->{max});
245  
246 6 return $desc;
247 }
248  
249 package XML::Validator::Schema::UnionModelNode;
250 5 ▶use base 'XML::Validator::Schema::ModelNode';
Subroutine BEGIN: called CC=1 SCAR=0.0
251  
252 0 ▶sub _combine_final_parts {
Subroutine _combine_final_parts: not called CC=1 SCAR=6.9
253 0 my ($self, $parts) = @_;
254  
255 # build final re
256 0 my $re = '(?:' . join('|', map { '(?:'. $_ . ')' } @$parts) . ')' .
257 0 XML::Validator::Schema::ModelNode::_qual($self->{min}, $self->{max});
258  
259 0 return $re;
260 }
261  
262 0 ▶sub _combine_running_parts {
Subroutine _combine_running_parts: not called CC=1 SCAR=6.9
263 0 my ($self, $parts) = @_;
264  
265 # build running re
266 my $re = '(?:' . $self->_combine_final_parts($parts) . ')' .
267 0 XML::Validator::Schema::ModelNode::_qual($self->{min}, $self->{max});
268  
269 0 return $re;
270 }
271  
272 0 ▶sub _combine_desc_parts {
Subroutine _combine_desc_parts: not called CC=1 SCAR=6.9
273 0 my ($self, $parts) = @_;
274  
275 # build description
276 my $desc = '(' . join('|', @$parts) . ')' .
277 0 XML::Validator::Schema::ModelNode::_qual($self->{min}, $self->{max});
278  
279 0 return $desc;
280 }
281  
282  
283 package XML::Validator::Schema::AllModelNode;
284 5 ▶use base 'XML::Validator::Schema::SequenceModelNode';
Subroutine BEGIN: called CC=1 SCAR=0.0
285  
286 # an all is just a sequence that doesn't care about ordering and only
287 # accepts min/max of 0/1
288  
289 5 ▶sub _combine_final_parts {
Subroutine _combine_final_parts: called CC=1 SCAR=0.0
290 5 my ($self, $parts) = @_;
291 5 return $self->SUPER::_combine_final_parts([sort sort_parts @$parts]);
292 }
293  
294 5 ▶sub _combine_running_parts {
Subroutine _combine_running_parts: called CC=1 SCAR=0.0
295 5 my ($self, $parts) = @_;
296 5 return $self->SUPER::_combine_running_parts([sort sort_parts @$parts]);
297 }
298  
299 5 ▶sub _combine_desc_parts {
Subroutine _combine_desc_parts: called CC=1 SCAR=0.0
300 5 my ($self, $parts) = @_;
301  
302 # build description
303 5 my $desc = '(' . join('&', @$parts) . ')';
304  
305 5 return $desc;
306 }
307  
308 # running model check not possible for all, right?
309 28 ▶sub check_model {}
Subroutine check_model: called CC=1 SCAR=0.0
310  
311 17 ▶sub check_final_model {
Subroutine check_final_model: called CC=1 SCAR=0.0
312 17 my ($self, $this_name, $names_ref) = @_;
313 17 $self->SUPER::check_final_model($this_name, [sort @$names_ref]);
314 }
315  
316 18 ▶sub sort_parts {
Subroutine sort_parts: called CC=1 SCAR=0.0
317 18 my( $a_element ) = $a =~ /<(.*?)\\>/;
318 18 my( $b_element ) = $b =~ /<(.*?)\\>/;
319 18 $a_element cmp $b_element;
320 }
321  
322 5 1;
« Schema/Attribute.pm ↑ Summary Schema/ElementRefNode.pm »