blib/lib/XML/Pastor/SimpleType.pm

filter: Statementstmt 21.8% 26 / 119 Branchbran 0.0% 0 / 94 Conditioncond 0.0% 0 / 17 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 66.6% 8 / 12 Podpod 25.0% 1 / 4 totaltotal 13.6% 35 / 257 CC 68 SCAR 76.3
CC
68
Cov
11%
xml_validate_value
2970.0
normalize_whitespace
132.0
xml_validate_ancestors
30.0
CRAP 2050.5
↑ Summary blib/lib/XML/Pastor/Type.pm »
1 10 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶no warnings qw(uninitialized);
Subroutine BEGIN: called CC=1 SCAR=0.0
5
6 #======================================================
7 package XML::Pastor::SimpleType;
8
9 10 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 10 ▶use XML::Pastor::Type;
Subroutine BEGIN: called CC=1 SCAR=0.0
11
12 10 ▶use Scalar::Util qw(reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 10 ▶use XML::Pastor::Util qw(getAttributeHash getChildrenHashDOM);
Subroutine BEGIN: called CC=1 SCAR=0.0
14
15 10 our @ISA = qw(XML::Pastor::Type);
16
17
18
19
20
21 #----------------------------------------------
22 # xml_validate_value
23 #----------------------------------------------
24 0 ▶sub xml_validate_value {
Subroutine xml_validate_value: not called CC=54 SCAR=80.0
Pod undocumented
25 0 my $self = shift;
26 0 ▶ my $path = shift || '';
Condition0%0/2
shift() || ''
l!l
00
MC/DC0%0/1
shift() || ''
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
27 0 my $type = $self->XmlSchemaType();
28 0 my $value = $self->__value;
29 0 $value = $self->normalize_whitespace($value);
30
31 0 ▶ unless (defined $type) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $type) 0 0 0
32 0 ▶ return ($self->xml_validate_further(@_) && $self->xml_validate_ancestors(@_));
Condition0%0/3
$self->xml_validate_further(@_) && $self->xml_validate_ancestors(@_)
!ll&&!rl&&r
000
MC/DC0%0/2
$self->xml_validate_further(@_) && $self->xml_validate_ancestors(@_)
$self->xml_validate_further(@_) $self->xml_validate_ancestors(@_)
Truth table0%0/3
A && B
A= $self->xml_validate_further(@_)B= $self->xml_validate_ancestors(@_)
ABresult
0X0
100
111
33 }
34
35 0 ▶ if (defined(my $prop = $type->length)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $prop = $type->length)) 0 0 0
36 0 ▶ $prop = (reftype($prop) eq 'ARRAY') ? $prop : [$prop];
Branch: 1 branch
BranchTrueFalseTotal
builtin::reftype($prop) eq 'ARRAY' ? : 0 0 0
37 0 foreach my $len (@$prop) {
38 0 ▶ ($len == length($value)) or die "Pastor : Validate : $path : Length must be exactly '$len' for value '$value'";
Branch: 1 branch
BranchTrueFalseTotal
$len == length $value or die "Pastor : Validate : $path : Length must be exactly '${len}' for value '${value}'" 0 0 0
39 }
40 }
41
42 0 ▶ if (defined(my $prop = $type->minLength)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $prop = $type->minLength)) 0 0 0
43 0 ▶ $prop = (reftype($prop) eq 'ARRAY') ? $prop : [$prop];
Branch: 1 branch
BranchTrueFalseTotal
builtin::reftype($prop) eq 'ARRAY' ? : 0 0 0
44 0 foreach my $minLen (@$prop) {
45 0 ▶ (length($value) >= $minLen) or die "Pastor : Validate : $path : Length must be minimum '$minLen' for value '$value'";
Branch: 1 branch
BranchTrueFalseTotal
length $value >= $minLen or die "Pastor : Validate : $path : Length must be minimum '${minLen}' for value '${value}'" 0 0 0
46 }
47 }
48
49 0 ▶ if (defined(my $prop = $type->maxLength)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $prop = $type->maxLength)) 0 0 0
50 0 ▶ $prop = (reftype($prop) eq 'ARRAY') ? $prop : [$prop];
Branch: 1 branch
BranchTrueFalseTotal
builtin::reftype($prop) eq 'ARRAY' ? : 0 0 0
51 0 foreach my $maxLen (@$prop) {
52 0 ▶ (length($value) <= $maxLen) or die "Pastor : Validate : $path : Length must be maximum '$maxLen' for value '$value'";
Branch: 1 branch
BranchTrueFalseTotal
length $value <= $maxLen or die "Pastor : Validate : $path : Length must be maximum '${maxLen}' for value '${value}'" 0 0 0
53 }
54 }
55
56 0 ▶ if (defined(my $prop = $type->regex)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $prop = $type->regex)) 0 0 0
57 0 ▶ $prop = (reftype($prop) eq 'ARRAY') ? $prop : [$prop];
Branch: 1 branch
BranchTrueFalseTotal
builtin::reftype($prop) eq 'ARRAY' ? : 0 0 0
58 0 my $pass=0;
59 0 foreach my $regex (@$prop) {
60 0 ▶ if ($value =~ /$regex/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /$regex/) 0 0 0
61 0 $pass =1;
62 0 last;
63 }
64 }
65 0 ▶ $pass or die "Pastor : Validate : $path : Value does not match any of the given regexes. Value is '$value'";
Branch: 1 branch
BranchTrueFalseTotal
$pass or die "Pastor : Validate : $path : Value does not match any of the given regexes. Value is '${value}'" 0 0 0
66 }
67
68 0 ▶ if (defined(my $prop = $type->pattern)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $prop = $type->pattern)) 0 0 0
69 0 ▶ $prop = (reftype($prop) eq 'ARRAY') ? $prop : [$prop];
Branch: 1 branch
BranchTrueFalseTotal
builtin::reftype($prop) eq 'ARRAY' ? : 0 0 0
70 0 my $pass=0;
71 0 foreach my $pattern (@$prop) {
72 0 ▶ if ($value =~ /$pattern/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /$pattern/) 0 0 0
73 0 $pass =1;
74 0 last;
75 }
76 }
77 0 ▶ $pass or die "Pastor : Validate : $path : Value does not match any of the given patterns. Value is '$value'";
Branch: 1 branch
BranchTrueFalseTotal
$pass or die "Pastor : Validate : $path : Value does not match any of the given patterns. Value is '${value}'" 0 0 0
78 }
79
80
81 0 ▶ if (defined(my $enumeration = $type->enumeration)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $enumeration = $type->enumeration)) 0 0 0
82 0 ▶ (exists $enumeration->{$value}) or die "Pastor : Validate : $path : Not in the permitted enumeration : value '$value'";
Branch: 1 branch
BranchTrueFalseTotal
exists $enumeration->{$value} or die "Pastor : Validate : $path : Not in the permitted enumeration : value '${value}'" 0 0 0
83 }
84
85
86 0 ▶ if (defined(my $prop = $type->minInclusive)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $prop = $type->minInclusive)) 0 0 0
87 0 ▶ $prop = (reftype($prop) eq 'ARRAY') ? $prop : [$prop];
Branch: 1 branch
BranchTrueFalseTotal
builtin::reftype($prop) eq 'ARRAY' ? : 0 0 0
88 0 foreach my $min (@$prop) {
89 0 ▶ ($value >= $min) or die "Pastor : Validate : $path : Value must be at least (minimum) '$min' : But value is '$value'";
Branch: 1 branch
BranchTrueFalseTotal
$value >= $min or die "Pastor : Validate : $path : Value must be at least (minimum) '${min}' : But value is '${value}'" 0 0 0
90 }
91 }
92
93 0 ▶ if (defined(my $prop = $type->maxInclusive)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $prop = $type->maxInclusive)) 0 0 0
94 0 ▶ $prop = (reftype($prop) eq 'ARRAY') ? $prop : [$prop];
Branch: 1 branch
BranchTrueFalseTotal
builtin::reftype($prop) eq 'ARRAY' ? : 0 0 0
95 0 foreach my $max (@$prop) {
96 0 ▶ ($value <= $max) or die "Pastor : Validate : $path : Value must be at most (maximum) '$max' : But value is '$value'";
Branch: 1 branch
BranchTrueFalseTotal
$value <= $max or die "Pastor : Validate : $path : Value must be at most (maximum) '${max}' : But value is '${value}'" 0 0 0
97 }
98 }
99
100 0 ▶ if (defined(my $prop = $type->minExclusive)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $prop = $type->minExclusive)) 0 0 0
101 0 ▶ $prop = (reftype($prop) eq 'ARRAY') ? $prop : [$prop];
Branch: 1 branch
BranchTrueFalseTotal
builtin::reftype($prop) eq 'ARRAY' ? : 0 0 0
102 0 foreach my $min (@$prop) {
103 0 ▶ ($value > $min) or die "Pastor : Validate : $path : Value must be greater than '$min' : But value is '$value'";
Branch: 1 branch
BranchTrueFalseTotal
$value > $min or die "Pastor : Validate : $path : Value must be greater than '${min}' : But value is '${value}'" 0 0 0
104 }
105 }
106
107 0 ▶ if (defined(my $prop = $type->maxExclusive)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $prop = $type->maxExclusive)) 0 0 0
108 0 ▶ $prop = (reftype($prop) eq 'ARRAY') ? $prop : [$prop];
Branch: 1 branch
BranchTrueFalseTotal
builtin::reftype($prop) eq 'ARRAY' ? : 0 0 0
109 0 foreach my $max (@$prop) {
110 0 ▶ ($value < $max) or die "Pastor : Validate : $path : Value must be less than '$max' : But value is '$value'";
Branch: 1 branch
BranchTrueFalseTotal
$value < $max or die "Pastor : Validate : $path : Value must be less than '${max}' : But value is '${value}'" 0 0 0
111 }
112 }
113
114
115 # Digits part is shamelessly copied from XML::Validator::Schema by Sam Tregar
116 0 ▶ if (defined($type->totalDigits) || defined($type->fractionDigits)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $type->totalDigits or defined $type->fractionDigits) 0 0 0
Condition0%0/3
defined $type->totalDigits or defined $type->fractionDigits
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $type->totalDigits or defined $type->fractionDigits
defined $type->totalDigits defined $type->fractionDigits
Truth table0%0/3
A or B
A= defined $type->totalDigitsB= defined $type->fractionDigits
ABresult
1X1
011
000
117 # strip leading and trailing zeros for numeric constraints
118 0 my $digits = $value;
119 0 $digits =~ s/^([+-]?)0*(\d*\.?\d*?)0*$/$1$2/g;
120
121 0 ▶ if (defined(my $prop=$type->totalDigits)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $prop = $type->totalDigits)) 0 0 0
122 0 ▶ $prop = (reftype($prop) eq 'ARRAY') ? $prop : [$prop];
Branch: 1 branch
BranchTrueFalseTotal
builtin::reftype($prop) eq 'ARRAY' ? : 0 0 0
123 0 foreach my $tdigits (@$prop) {
124 0 ▶ die "Pastor : Validate : $path : Value has more total digits than the allowed '$tdigits'"
Branch: 1 branch
BranchTrueFalseTotal
if $digits =~ tr/0-9// > $tdigits 0 0 0
125 if $digits =~ tr!0-9!! > $tdigits;
126 }
127 }
128
129 0 ▶ if (defined(my $prop=$type->fractionDigits)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $prop = $type->fractionDigits)) 0 0 0
130 0 ▶ $prop = (reftype($prop) eq 'ARRAY') ? $prop : [$prop];
Branch: 1 branch
BranchTrueFalseTotal
builtin::reftype($prop) eq 'ARRAY' ? : 0 0 0
131 0 foreach my $fdigits (@$prop) {
132 0 ▶ die "Pastor : Validate : $path : Value has more fraction digits than the allowed '$fdigits'"
Branch: 1 branch
BranchTrueFalseTotal
if $digits =~ /\.\d{$fdigits}\d/ 0 0 0
133 if $digits =~ /\.\d{$fdigits}\d/;
134 }
135 }
136 }
137
138
139 0 return 1;
140 }
141
142
143
144 #-----------------------------------------------------------------------------
145 # By default, this just returns TRUE. But it could be overriden by descendants (like 'date').
146 #-----------------------------------------------------------------------------
147 0 ▶sub xml_validate_further {
Subroutine xml_validate_further: not called CC=1 SCAR=6.9
Pod documented
148 0 return 1;
149 }
150
151
152 #-----------------------------------------------------------------------------
153 # Validate the ancestors. Base classes need to be validated.
154 #-----------------------------------------------------------------------------
155 0 ▶sub xml_validate_ancestors {
Subroutine xml_validate_ancestors: not called CC=5 SCAR=34.0
Pod undocumented
156 0 my $self = shift;
157 0 my $value = $self->__value;
158 0 my @ancestors = $self->get_ancestors();
159
160 0 foreach my $class (@ancestors) {
161 0 ▶ next unless (UNIVERSAL::can($class, 'new') && UNIVERSAL::can($class, 'xml_validate'));
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::can($class, "new") and UNIVERSAL::can($class, "xml_validate") 0 0 0
Condition0%0/3
UNIVERSAL::can($class, "new") and UNIVERSAL::can($class, "xml_validate")
!ll&&!rl&&r
000
MC/DC0%0/2
UNIVERSAL::can($class, "new") and UNIVERSAL::can($class, "xml_validate")
UNIVERSAL::can($class, "new") UNIVERSAL::can($class, "xml_validate")
Truth table0%0/3
A and B
A= UNIVERSAL::can($class, "new")B= UNIVERSAL::can($class, "xml_validate")
ABresult
0X0
100
111
162
163 0 my $obj=$class->new(__value => $value);
164 0 ▶ return 0 unless $obj->xml_validate(@_);
Branch: 1 branch
BranchTrueFalseTotal
unless $obj->xml_validate(@_) 0 0 0
165 }
166
167 0 return 1;
168 }
169
170
171 #-----------------------------------------------------------------------------
172 # Normalize white space.
173 #-----------------------------------------------------------------------------
174 0 ▶sub normalize_whitespace {
Subroutine normalize_whitespace: not called CC=11 SCAR=48.8
Pod undocumented
175 0 my $self = shift;
176 0 my $value = shift;
177 0 my $type = $self->XmlSchemaType();
178
179 0 ▶ if (defined($type) and defined(my $prop = $type->whiteSpace)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $type and defined(my $prop = $type->whiteSpace)) { } 0 0 0
Condition0%0/3
defined $type and defined(my $prop = $type->whiteSpace)
!ll&&!rl&&r
000
MC/DC0%0/2
defined $type and defined(my $prop = $type->whiteSpace)
defined $type defined(my $prop = $type->whiteSpace)
Truth table0%0/3
A and B
A= defined $typeB= defined(my $prop = $type->whiteSpace)
ABresult
0X0
100
111
180 0 ▶ $prop = (reftype($prop) eq 'ARRAY') ? $prop : [$prop];
Branch: 1 branch
BranchTrueFalseTotal
builtin::reftype($prop) eq 'ARRAY' ? : 0 0 0
181 0 foreach my $ws (@$prop) {
182 0 ▶ if ($ws =~ /^replace$/i) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ws =~ /^replace$/i) { } 0 0 0
elsif ($ws =~ /^collapse$/i) { } 0 0 0
183 0 $value =~ s![\t\n\r]! !g;
184 } elsif ($ws =~ /^collapse$/i) {
185 0 $value =~ s!\s+! !g;
186 0 $value =~ s!^\s!!g;
187 0 $value =~ s!\s$!!g;
188 }
189 0 return $value; # only the first one gets treated!
190 }
191 }else {
192 0 my @ancestors = $self->get_ancestors();
193 0 foreach my $class(@ancestors) {
194 0 ▶ next unless UNIVERSAL::can($class, 'normalize_whitespace') && UNIVERSAL::can($class, 'new');
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::can($class, "normalize_whitespace") and UNIVERSAL::can($class, "new") 0 0 0
Condition0%0/3
UNIVERSAL::can($class, "normalize_whitespace") and UNIVERSAL::can($class, "new")
!ll&&!rl&&r
000
MC/DC0%0/2
UNIVERSAL::can($class, "normalize_whitespace") and UNIVERSAL::can($class, "new")
UNIVERSAL::can($class, "normalize_whitespace") UNIVERSAL::can($class, "new")
Truth table0%0/3
A and B
A= UNIVERSAL::can($class, "normalize_whitespace")B= UNIVERSAL::can($class, "new")
ABresult
0X0
100
111
195 0 my $object = $class->new(__value=>$value);
196 0 my $nvalue = $object->normalize_whitespace($value);
197
198 0 ▶ return $nvalue if ($nvalue ne $value);
Branch: 1 branch
BranchTrueFalseTotal
if $nvalue ne $value 0 0 0
199 }
200 }
201
202 0 return $value;
203 }
204
205 10 1;
206
207 __END__
↑ Summary blib/lib/XML/Pastor/Type.pm »