blib/lib/XML/Pastor/Type.pm

filter: Statementstmt 47.3% 193 / 408 Branchbran 32.3% 68 / 210 Conditioncond 31.8% 21 / 66 MC/DCmcdc 20.5% 8 / 39 Subroutinesub 53.3% 24 / 45 Podpod 72.7% 24 / 33 totaltotal 42.2% 338 / 801 CC 140 SCAR 51.7
CC
140
Cov
41%
xml_validate
930.0
grab
110.0
to_xml
56.0
CRAP 175.8
« blib/lib/XML/Pastor/SimpleType.pm ↑ Summary blib/lib/XML/Pastor/Pastorize.pm »
1
2 #======================================================
3 package XML::Pastor::Type;
4
5 10 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 10 ▶no warnings qw(uninitialized);
Subroutine BEGIN: called CC=1 SCAR=0.0
9
10 10 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 10 ▶use Class::Data::Inheritable;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 10 ▶use Class::Accessor;
Subroutine BEGIN: called CC=1 SCAR=0.0
13
14 10 our @ISA = qw(Class::Accessor Class::Data::Inheritable);
15
16 10 ▶use Scalar::Util qw(reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 10 ▶use XML::Pastor::Util qw(getAttributeHash getChildrenHashDOM);
Subroutine BEGIN: called CC=1 SCAR=0.0
18
19
20 10 XML::Pastor::Type->mk_classdata('XmlSchemaType');
21 10 XML::Pastor::Type->mk_accessors(qw(__value));
22
23 use overload
24 10 ▶ '""' => \&stringify,
Subroutine BEGIN: called CC=1 SCAR=0.0
25 '0+' => \&numify,
26 'bool' => \&boolify,
27 '<=>' => \&num_cmp,
28 10 'cmp' => \&str_cmp;
29
30
31
32
33 #----------------------------------------------
34 # Accepts a single parameter or a hash.
35 # If single parameter, then it is taken to be the value.
36 #----------------------------------------------
37 193 ▶sub new {
Subroutine new: called CC=3 SCAR=11.3
Pod documented
38 193 my $proto = shift;
39 193 ▶ my $class = ref($proto) || $proto;
Condition66%2/3
ref $proto || $proto
l!l&&r!l&&!r
71860
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table66%2/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
40 193 ▶ my $self = (@_ == 1) ? {__value=>$_[0]} : {@_};
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 0 193 193
41
42 193 return bless $self, $class;
43 }
44
45 #----------------------------------------------
46 # String value.
47 #----------------------------------------------
48 215 ▶sub stringify {
Subroutine stringify: called CC=1 SCAR=0.0
Pod undocumented
49 215 my $self = shift;
50 215 return "" . $self->__value(); # force stringification on value
51 }
52
53 #----------------------------------------------
54 # Numeric value.
55 #----------------------------------------------
56 0 ▶sub numify {
Subroutine numify: not called CC=1 SCAR=6.9
Pod undocumented
57 0 my $self = shift;
58 0 return +$self->__value(); # force numerification on value
59 }
60
61 #----------------------------------------------
62 # Boolean value.
63 #----------------------------------------------
64 0 ▶sub boolify {
Subroutine boolify: not called CC=1 SCAR=6.9
Pod undocumented
65 0 my $self = shift;
66 0 my $val = $self->__value();
67
68 0 return $val;
69 }
70
71 #----------------------------------------------
72 # Numerical comparison
73 #----------------------------------------------
74 0 ▶sub num_cmp {
Subroutine num_cmp: not called CC=3 SCAR=24.8
Pod undocumented
75 0 my $self = shift;
76 0 my $arg = shift;
77 0 my $numval = $self->numify;
78 0 ▶ return undef unless defined($numval);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $numval 0 0 0
79 0 ▶ return undef unless defined(+$arg);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $arg 0 0 0
80
81 0 return ($numval <=> $arg);
82 }
83
84 #----------------------------------------------
85 # String comparison
86 #----------------------------------------------
87 20 ▶sub str_cmp {
Subroutine str_cmp: called CC=3 SCAR=11.2
Pod undocumented
88 20 my $self = shift;
89 20 my $arg = shift;
90 20 my $strval = $self->stringify;
91 20 ▶ return undef unless defined($strval);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $strval 0 20 20
92 20 ▶ return undef unless defined($arg);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $arg 0 20 20
93
94 20 return ($strval cmp "$arg");
95 }
96
97 #----------------------------------------------
98 # set -- Overriden from Class::Accessor
99 #----------------------------------------------
100 108 ▶sub set {
Subroutine set: called CC=9 SCAR=37.7
Pod documented
101 108 my($self, $key) = splice(@_, 0, 2);
102
103 108 ▶ if(@_ == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 1) { } 108 0 108
elsif (@_ > 1) { } 0 0 0
104 108 my $value = $_[0];
105
106 108 ▶ if (UNIVERSAL::can($value, 'to_xml_dom') || ($key eq '__value')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::can($value, 'to_xml_dom') or $key eq '__value') { } 108 0 108
Condition33%1/3
UNIVERSAL::can($value, "to_xml_dom") or $key eq "__value"
l!l&&r!l&&!r
01080
MC/DC0%0/2
UNIVERSAL::can($value, "to_xml_dom") or $key eq "__value"
UNIVERSAL::can($value, "to_xml_dom") $key eq "__value"
Truth table33%1/3
A or B
A= UNIVERSAL::can($value, "to_xml_dom")B= $key eq "__value"
ABresult
1X1
011
000
107 # If the value is an XLM dommable value, then just assign it.
108 108 $self->{$key} = $value;
109 }else {
110 # Otherwise, see if the field alreday exists, and if it can support a __value accessor
111 0 ▶ if ((exists($self->{$key})) && UNIVERSAL::can($self->{$key}, '__value')) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{$key} and UNIVERSAL::can($self->{$key}, '__value')) { } 0 0 0
Condition0%0/3
exists $self->{$key} and UNIVERSAL::can($self->{$key}, "__value")
!ll&&!rl&&r
000
MC/DC0%0/2
exists $self->{$key} and UNIVERSAL::can($self->{$key}, "__value")
exists $self->{$key} UNIVERSAL::can($self->{$key}, "__value")
Truth table0%0/3
A and B
A= exists $self->{$key}B= UNIVERSAL::can($self->{$key}, "__value")
ABresult
0X0
100
111
112 # Yes. Assign it to the value.
113 0 $self->{$key}->__value($value);
114 }else {
115 # No. See if we can get the class name of the field.
116 0 my $class = $self->xml_field_class();
117 0 ▶ if (defined($class) && UNIVERSAL::can($class, '__value')) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $class and UNIVERSAL::can($class, '__value')) { } 0 0 0
Condition0%0/3
defined $class and UNIVERSAL::can($class, "__value")
!ll&&!rl&&r
000
MC/DC0%0/2
defined $class and UNIVERSAL::can($class, "__value")
defined $class UNIVERSAL::can($class, "__value")
Truth table0%0/3
A and B
A= defined $classB= UNIVERSAL::can($class, "__value")
ABresult
0X0
100
111
118 # Create the field and set the value
119 0 $self->{$key} = $class->new(__value => $value);
120 }else {
121 # Just revert to the default of assigning it directly.
122 0 $self->{$key} = $value;
123 }
124 }
125 }
126 }
127 elsif(@_ > 1) {
128 0 $self->{$key} = [@_];
129 }
130 else {
131 0 $self->_croak("Wrong number of arguments received");
132 }
133 }
134
135
136 #------------------------------------------------------
137 # CLASS METHOD
138 # Return the Perl class name of a given field (element or attribute).
139 #------------------------------------------------------
140 0 ▶sub xml_field_class($$) {
Subroutine xml_field_class: not called CC=6 SCAR=37.4
Pod documented
141 0 my $self = shift;
142 0 my $field = shift;
143 0 my $class;
144
145 0 my $type = $self->XmlSchemaType();
146
147 # Try with elements
148 0 ▶ if (UNIVERSAL::can($type, 'effectiveElementInfo')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::can($type, "effectiveElementInfo")) 0 0 0
149 0 my $elementInfo = $type->effectiveElementInfo();
150 0 ▶ if (defined(my $element = $elementInfo->{$field})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $element = $elementInfo->{$field})) 0 0 0
151 0 $class= $element->class();
152 }
153 # Return the class if we already have it.
154 0 ▶ return $class if ($class);
Branch: 1 branch
BranchTrueFalseTotal
if $class 0 0 0
155 }
156
157
158 # Try with attributes
159 0 ▶ if (UNIVERSAL::can($type, 'effectiveAttributeInfo')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::can($type, "effectiveAttributeInfo")) 0 0 0
160 0 my $attribInfo = $type->effectiveAttributeInfo();
161 0 my $attribPfx = $type->attributePrefix();
162 0 my $afield = $field;
163 0 $afield =~ s/^$attribPfx//;
164 0 ▶ if (defined(my $attrib = $attribInfo->{$afield})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $attrib = $attribInfo->{$afield})) 0 0 0
165 0 $class= $attrib->class();
166 }
167 }
168
169 0 return $class;
170 }
171
172 #------------------------------------------------------
173 # CLASS METHOD
174 # Return the singletonness of a given field.
175 #------------------------------------------------------
176 0 ▶sub is_xml_field_singleton($$) {
Subroutine is_xml_field_singleton: not called CC=5 SCAR=34.0
Pod documented
177 0 my $self = shift;
178 0 my $field = shift;
179
180 0 my $type = $self->XmlSchemaType();
181
182 # Try with elements
183 0 ▶ if (UNIVERSAL::can($type, 'effectiveElementInfo')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::can($type, "effectiveElementInfo")) 0 0 0
184 0 my $elementInfo = $type->effectiveElementInfo();
185 0 ▶ if (defined(my $element = $elementInfo->{$field})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $element = $elementInfo->{$field})) 0 0 0
186 0 return $element->isSingleton();
187 }
188 }
189
190 # Try with attributes
191 0 ▶ if (UNIVERSAL::can($type, 'effectiveAttributeInfo')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::can($type, "effectiveAttributeInfo")) 0 0 0
192 0 my $attribInfo = $type->effectiveAttributeInfo();
193 0 my $attribPfx = $type->attributePrefix();
194 0 my $afield = $field;
195 0 $afield =~ s/^$attribPfx//;
196 0 ▶ if (defined(my $attrib = $attribInfo->{$afield})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $attrib = $attribInfo->{$afield})) 0 0 0
197 0 return 1; # An attribute is always singleton.
198 }
199 }
200
201 # unkown
202 0 return undef;
203 }
204
205 #------------------------------------------------------
206 # CLASS METHOD
207 # Return the multiplicity of a given field.
208 #------------------------------------------------------
209 0 ▶sub is_xml_field_multiple($$) {
Subroutine is_xml_field_multiple: not called CC=1 SCAR=6.9
Pod documented
210 0 my $self = shift;
211 0 return !$self->is_xml_field_singleton(@_);
212 }
213
214 #------------------------------------------------------
215 # OBJECT METHOD
216 # Get/set the text content.
217 #------------------------------------------------------
218 0 ▶sub xml_text_content { &__value;}
Subroutine xml_text_content: not called CC=1 SCAR=6.9
Pod undocumented
219
220 #------------------------------------------------------
221 # Grab a field. The difference from 'get'is that 'grab' will create
222 # the field if it doesn't exist (correctly typed).
223 #------------------------------------------------------
224 0 ▶sub grab($) {
Subroutine grab: not called CC=10 SCAR=47.0
Pod documented
225 0 my $self = shift;
226 0 my $field = shift;
227
228 0 ▶ return $self->{$field} if ( defined($self->{$field}) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{$field} 0 0 0
229 0 ▶ return undef unless (UNIVERSAL::can($self, "XmlSchemaType"));
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::can($self, "XmlSchemaType") 0 0 0
230
231 0 my $type = $self->XmlSchemaType();
232
233 # Try with elements
234 0 ▶ if (UNIVERSAL::can($type, 'effectiveElementInfo')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::can($type, "effectiveElementInfo")) 0 0 0
235 0 my $elementInfo = $type->effectiveElementInfo();
236 0 ▶ if (defined(my $element = $elementInfo->{$field})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $element = $elementInfo->{$field})) 0 0 0
237 0 my $class= $element->class();
238 0 ▶ if (defined($class)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $class) 0 0 0
239 0 my $result;
240
241 0 ▶ if ($element->isSingleton()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($element->isSingleton) { } 0 0 0
242 # singleton
243 0 $result = $self->{$field} = $class->new();
244 }else {
245 # multiplicity
246
247 # What to do? Should we return an empty array or one having an object in it?
248 0 $result = $self->{$field} = XML::Pastor::NodeArray->new();
249 # $result = $self->{$field} = XML::Pastor::NodeArray->new($class->new());
250 }
251 0 return $result;
252 }
253 }
254 }
255
256 # Try with attributes
257 0 ▶ if (UNIVERSAL::can($type, 'effectiveAttributeInfo')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::can($type, "effectiveAttributeInfo")) 0 0 0
258 0 my $attribInfo = $type->effectiveAttributeInfo();
259 0 my $attribPfx = $type->attributePrefix();
260 0 my $afield = $field;
261 0 $afield =~ s/^$attribPfx//;
262
263 0 ▶ if (defined(my $attrib = $attribInfo->{$afield})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $attrib = $attribInfo->{$afield})) 0 0 0
264 0 my $class= $attrib->class();
265 0 ▶ if (defined($class)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $class) 0 0 0
266 0 my $result = $self->{$attribPfx . $afield}=$class->new();
267 0 return $result;
268 }
269 }
270 }
271
272 0 return undef;
273 }
274
275 #----------------------------------------------
276 0 ▶sub is_xml_valid {
Subroutine is_xml_valid: not called CC=2 SCAR=17.9
Pod documented
277 0 my $self = shift;
278 0 my $result;
279
280 0 eval { $result = $self->xml_validate(@_); };
281
282 0 ▶ return 0 if ($@); # if we died along the way...
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
283 0 return $result;
284 }
285
286
287 #----------------------------------------------
288 # xml_validate
289 #----------------------------------------------
290 0 ▶sub xml_validate {
Subroutine xml_validate: not called CC=30 SCAR=68.4
Pod documented
291 0 my $self = shift;
292 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
293 0 my $type = $self->XmlSchemaType();
294
295 0 ▶ unless ($path) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($path) 0 0 0
296 0 ▶ if (UNIVERSAL::can($self, "XmlSchemaElement")) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::can($self, "XmlSchemaElement")) 0 0 0
297 0 my $xmlSchemaElement = $self->XmlSchemaElement;
298 0 $path = $xmlSchemaElement->name();
299 }
300 }
301
302 0 ▶ if (UNIVERSAL::can($type, 'effectiveAttributes')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::can($type, "effectiveAttributes")) 0 0 0
303 0 my $attributes = $type->effectiveAttributes();
304 0 my $attribInfo = $type->effectiveAttributeInfo();
305 0 ▶ my $attribPfx = $type->attributePrefix() || '';
Condition0%0/2
$type->attributePrefix || ''
l!l
00
MC/DC0%0/1
$type->attributePrefix || ''
$type->attributePrefix
Truth table0%0/2
A
A= $type->attributePrefix
Aresult
11
00
306
307 0 foreach my $attribName (@$attributes) {
308 0 my $attrib = $attribInfo->{$attribName};
309 0 my $value = $self->{$attribPfx . $attribName};
310
311 0 ▶ unless ( defined($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 0 0 0
312 0 ▶ if ($attrib->use =~ /required/io) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attrib->use =~ /required/io) { } 0 0 0
313 0 die "Pastor : Validate : $path : required attribute '$attribName' is missing!";
314 }else {
315 0 next;
316 }
317 }
318
319 0 my $obj;
320 0 ▶ if (UNIVERSAL::can($value, 'xml_validate')){
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::can($value, 'xml_validate')) { } 0 0 0
321 0 $obj=$value;
322 }else {
323 0 my $class = $attrib->class;
324 0 $obj = $class->new(__value=>$value);
325 }
326
327 0 ▶ return 0 unless $obj->xml_validate($path . "/@" . $attribName);
Branch: 1 branch
BranchTrueFalseTotal
unless $obj->xml_validate($path . '/@' . $attribName) 0 0 0
328 }
329 }
330
331
332 # Elements
333 0 ▶ if (UNIVERSAL::can($type, 'effectiveElements')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::can($type, "effectiveElements")) 0 0 0
334 0 my $elements = $type->effectiveElements();
335 0 my $elementInfo = $type->effectiveElementInfo();
336 0 foreach my $elemName (@$elements) {
337 0 my $items = $self->{$elemName};
338 0 ▶ $items = [] unless defined($items);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $items 0 0 0
339 0 ▶ $items = [$items] unless (reftype($items) eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::reftype($items) eq "ARRAY" 0 0 0
340
341 0 my $element = $elementInfo->{$elemName};
342
343 0 ▶ if (defined($element->minOccurs) && (@$items < $element->minOccurs) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $element->minOccurs and @$items < $element->minOccurs) 0 0 0
Condition0%0/3
defined $element->minOccurs and @$items < $element->minOccurs
!ll&&!rl&&r
000
MC/DC0%0/2
defined $element->minOccurs and @$items < $element->minOccurs
defined $element->minOccurs @$items < $element->minOccurs
Truth table0%0/3
A and B
A= defined $element->minOccursB= @$items < $element->minOccurs
ABresult
0X0
100
111
344 0 die "Pastor : Validate : $path : Element '$elemName' must occur at least '" . $element->minOccurs . "' times whereas it occurs only '" . scalar(@$items) . "' times!";
345 }
346
347 0 ▶ if (defined($element->maxOccurs) && ($element->maxOccurs !~ /unbounded/io) && (@$items > $element->maxOccurs) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $element->maxOccurs and not $element->maxOccurs =~ /unbounded/io and @$items > $element->maxOccurs) 0 0 0
Condition0%0/6
defined $element->maxOccurs and not $element->maxOccurs =~ /unbounded/io and @$items > $element->maxOccurs
!ll&&!rl&&r
000
defined $element->maxOccurs and not $element->maxOccurs =~ /unbounded/io
!ll&&!rl&&r
000
MC/DC0%0/3
defined $element->maxOccurs and not $element->maxOccurs =~ /unbounded/io and @$items > $element->maxOccurs
defined $element->maxOccurs not $element->maxOccurs =~ /unbounded/io @$items > $element->maxOccurs
Truth table0%0/4
A and not(B) and C
A= defined $element->maxOccursB= not $element->maxOccurs =~ /unbounded/ioC= @$items > $element->maxOccurs
ABCresult
0XX0
10X0
1100
1111
348 0 die "Pastor : Validate : $path : Element '$elemName' must occur at most '" . $element->maxOccurs . "' times whereas it occurs '" . scalar(@$items) . "' times!";
349 }
350
351 0 foreach my $item (@$items) {
352 0 ▶ if (UNIVERSAL::can($item, 'xml_validate')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::can($item, 'xml_validate')) { } 0 0 0
353 # The item can validate itself, no problem.
354 0 ▶ return 0 unless $item->xml_validate($path . "/$elemName");
Branch: 1 branch
BranchTrueFalseTotal
unless $item->xml_validate($path . "/$elemName") 0 0 0
355 }else {
356 0 my $class = $element->class;
357 0 ▶ if (UNIVERSAL::isa($class, "XML::Pastor::SimpleType")) {
Branch: 2 branches
BranchTrueFalseTotal
if (UNIVERSAL::isa($class, 'XML::Pastor::SimpleType')) { } 0 0 0
elsif (UNIVERSAL::isa($class, 'XML::Pastor::ComplexType') and builtin::reftype($item) eq 'HASH') { } 0 0 0
358 # Item should be of SimpleType, but it is not. Fix it and then validate.
359 0 my $obj = $class->new(__value => "$item");
360 0 ▶ return 0 unless $obj->xml_validate($path . "/$elemName");
Branch: 1 branch
BranchTrueFalseTotal
unless $obj->xml_validate($path . "/$elemName") 0 0 0
361 ▶ }elsif (UNIVERSAL::isa($class, "XML::Pastor::ComplexType") && (reftype($item) eq 'HASH')) {
Condition0%0/3
UNIVERSAL::isa($class, "XML::Pastor::ComplexType") and builtin::reftype($item) eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
UNIVERSAL::isa($class, "XML::Pastor::ComplexType") and builtin::reftype($item) eq "HASH"
UNIVERSAL::isa($class, "XML::Pastor::ComplexType") builtin::reftype($item) eq "HASH"
Truth table0%0/3
A and B
A= UNIVERSAL::isa($class, "XML::Pastor::ComplexType")B= builtin::reftype($item) eq "HASH"
ABresult
0X0
100
111
362 # Item should be of ComplexType, but it is just Hash. Fix it and then validate.
363 0 my $obj = $class->new(%$item);
364 0 ▶ return 0 unless $obj->xml_validate($path . "/$elemName");
Branch: 1 branch
BranchTrueFalseTotal
unless $obj->xml_validate($path . "/$elemName") 0 0 0
365 }else {
366 0 die "Pastor : Validate : $path : Don't know how to validate '$elemName' (not a known or convertable type)";
367 }
368 }
369
370 }
371 }
372 }
373
374 0 ▶ return ( $self->xml_validate_value(@_) &&
Condition0%0/6
$self->xml_validate_value(@_) && $self->xml_validate_further(@_) && $self->xml_validate_ancestors(@_)
!ll&&!rl&&r
000
$self->xml_validate_value(@_) and $self->xml_validate_further(@_)
!ll&&!rl&&r
000
MC/DC0%0/3
$self->xml_validate_value(@_) && $self->xml_validate_further(@_) && $self->xml_validate_ancestors(@_)
$self->xml_validate_value(@_) $self->xml_validate_further(@_) $self->xml_validate_ancestors(@_)
Truth table0%0/4
A and B && C
A= $self->xml_validate_value(@_)B= $self->xml_validate_further(@_)C= $self->xml_validate_ancestors(@_)
ABCresult
0XX0
10X0
1100
1111
375 $self->xml_validate_further(@_) &&
376 $self->xml_validate_ancestors(@_));
377 }
378
379 #----------------------------------------------
380 0 ▶sub xml_validate_further {
Subroutine xml_validate_further: not called CC=1 SCAR=6.9
Pod documented
381 0 return 1; # to be overriden!
382 }
383
384 #----------------------------------------------
385 0 ▶sub xml_validate_ancestors {
Subroutine xml_validate_ancestors: not called CC=1 SCAR=6.9
Pod undocumented
386 0 return 1; # to be overriden!
387 }
388
389 #----------------------------------------------
390 0 ▶sub xml_validate_value {
Subroutine xml_validate_value: not called CC=1 SCAR=6.9
Pod undocumented
391 0 return 1; # to be overriden!
392 }
393
394 #-----------------------------------------------------------------------------
395 # CLASS METHOD. Obtain the ancestors of this class.
396 #-----------------------------------------------------------------------------
397 0 ▶sub get_ancestors {
Subroutine get_ancestors: not called CC=2 SCAR=13.4
Pod undocumented
398 0 my $self = shift;
399 0 my @ancestors;
400
401 {
402 0 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.2
403 0 ▶ my $cls = ref ($self) || $self;
Condition0%0/3
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
404 0 @ancestors = @{ $cls . '::ISA' };
405 }
406 0 return (@ancestors);
407 }
408
409
410 #----------------------------------------------
411 # from_dom (CONSTRUCTOR)
412 #----------------------------------------------
413 115 ▶sub from_xml_dom {
Subroutine from_xml_dom: called CC=17 SCAR=29.5
Pod documented
414 115 my $self = shift->new(); # This method behaves as a constructor.
415 115 my $node = shift;
416 115 my $type = $self->XmlSchemaType();
417 115 my $verbose = 0;
418
419 # If we have encountered a document, just recurse into the ROOT element.
420 115 ▶ if (UNIVERSAL::isa($node, "XML::LibXML::Document")) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($node, "XML::LibXML::Document")) 7 108 115
421 7 return $self->from_xml_dom($node->documentElement());
422 }
423
424
425 108 ▶ if (UNIVERSAL::isa($node, "XML::LibXML::Text")) {
Branch: 3 branches
BranchTrueFalseTotal
if (UNIVERSAL::isa($node, 'XML::LibXML::Text')) { } 0 108 108
elsif (UNIVERSAL::isa($node, 'XML::LibXML::Attr')) { } 0 108 108
elsif (UNIVERSAL::isa($node, 'XML::LibXML::Element')) { } 108 0 108
426 0 $self->__value($node->nodeValue());
427 0 return $self;
428 }elsif (UNIVERSAL::isa($node, "XML::LibXML::Attr")) {
429 0 $self->__value($node->value());
430 0 return $self;
431 }elsif (UNIVERSAL::isa($node, "XML::LibXML::Element")) {
432 108 $self->__value($node->textContent());
433
434 108 ▶ print STDERR "**** SimpleType:from_xml_dom : " . $node->localName() .": " . $self->__value . "\n" if ($verbose >=9);
Branch: 1 branch
BranchTrueFalseTotal
if $verbose >= 9 0 108 108
435
436 # This is a secret place where we put the node name in case we need it later.
437 # TODO : Namespaces
438 108 $self->{'._nodeName_'} = $node->localName();
439
440 108 ▶ if (UNIVERSAL::can($type, 'effectiveAttributeInfo')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::can($type, "effectiveAttributeInfo")) 68 40 108
441 # Get the attributes from DOM into self
442 68 my $attribs = getAttributeHash($node);
443 68 my $attribInfo = $type->effectiveAttributeInfo();
444 68 ▶ my $attribPfx = $type->attributePrefix() || '';
Condition50%1/2
$type->attributePrefix || ''
l!l
680
MC/DC0%0/1
$type->attributePrefix || ''
$type->attributePrefix
Truth table50%1/2
A
A= $type->attributePrefix
Aresult
11
00
445
446 68 foreach my $attribName (@{$type->effectiveAttributes()}) {
447 81 ▶ next unless ( defined($attribs->{$attribName}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $attribs->{$attribName} 3 78 81
448
449 78 my $class = $attribInfo->{$attribName}->class();
450 78 ▶ print "\nfrom_xml_dom : Attribute = $attribName, Class = $class" if ($verbose >= 7);
Branch: 1 branch
BranchTrueFalseTotal
if $verbose >= 7 0 78 78
451 78 $self->{$attribPfx . $attribName} = $class->new(__value => $attribs->{$attribName});
452 }
453 }
454
455
456 # Get the child elements from DOM into self
457 108 ▶ if (UNIVERSAL::can($type, 'effectiveElementInfo')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::can($type, "effectiveElementInfo")) 68 40 108
458 68 my $children = getChildrenHashDOM($node);
459 68 my $elemInfo = $type->effectiveElementInfo();
460
461 68 foreach my $elemName (@{$type->effectiveElements()}) {
462 103 ▶ next unless ( defined($children->{$elemName}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $children->{$elemName} 30 73 103
463
464 73 ▶ my $elem = $elemInfo->{$elemName} or croak("Undefined child element for '$elemName'!");
Branch: 1 branch
BranchTrueFalseTotal
my $elem = $elemInfo->{$elemName} or croak("Undefined child element for '${elemName}'!") 73 0 73
465 73 ▶ my $class = $elem->class() or croak("Undefined class for '$elemName'!");
Branch: 1 branch
BranchTrueFalseTotal
my $class = $elem->class or croak("Undefined class for '${elemName}'!") 73 0 73
466 73 my $childNodes = $children->{$elemName};
467
468 73 ▶ if ($elem->isSingleton()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem->isSingleton) { } 63 10 73
469 # singleton
470 63 $self->{$elemName} = $class->from_xml_dom($childNodes->[0]);
471 }else {
472 # multiplicity
473 10 $self->{$elemName} = XML::Pastor::NodeArray->new(map {$class->from_xml_dom($_)} @$childNodes);
474 }
475 }
476 }
477 108 return $self;
478 }
479 0 return undef;
480 }
481
482 #-------------------------------------------------------------------
483 # CONSTRUCTOR
484 # Parse an XML resource (string, file or URL) and return the object that represents it.
485 #-------------------------------------------------------------------
486 3 ▶sub from_xml {
Subroutine from_xml: called CC=8 SCAR=29.9
Pod documented
487 3 my $self = shift;
488 3 my $resource = shift;
489
490 3 SWITCH: for ($resource) {
491 0 ▶ UNIVERSAL::isa($_, "XML::LibXml::Document") and do {return $self->from_xml_dom($resource, @_); };
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($_, "XML::LibXml::Document") and do { return $self->from_xml_dom($resource, @_) } 0 3 3
492 0 ▶ UNIVERSAL::isa($_, "XML::LibXml::Element") and do {return $self->from_xml_dom($resource, @_); };
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($_, "XML::LibXml::Element") and do { return $self->from_xml_dom($resource, @_) } 0 3 3
493 0 ▶ UNIVERSAL::isa($_, "IO::Handle") and do {return $self->from_xml_fh($resource, @_); };
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($_, "IO::Handle") and do { return $self->from_xml_fh($resource, @_) } 0 3 3
494 3 ▶ UNIVERSAL::isa($_, "URI") and do {return $self->from_xml_url($resource, @_); };
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($_, "URI") and do { return $self->from_xml_url($resource, @_) } 3 0 3
495 0 ▶ /^(http|https|ftp|file):/i and do {return $self->from_xml_url($resource, @_); };
Branch: 1 branch
BranchTrueFalseTotal
/^(http|https|ftp|file):/i and do { return $self->from_xml_url($resource, @_) } 0 0 0
496 0 ▶ /<\// and do {return $self->from_xml_string($resource, @_) };
Branch: 1 branch
BranchTrueFalseTotal
m[</] and do { return $self->from_xml_string($resource, @_) } 0 0 0
497 0 OTHERWISE: return $self->from_xml_file($resource, @_);
498 }
499 }
500
501 #-------------------------------------------------------------------
502 # CONSTRUCTOR
503 # Parse an XML file and return the object that represents it.
504 #-------------------------------------------------------------------
505 4 ▶sub from_xml_file {
Subroutine from_xml_file: called CC=2 SCAR=7.0
Pod documented
506 4 my $self = shift;
507 4 my $file = shift;
508
509 4 ▶ return undef unless ($file);
Branch: 1 branch
BranchTrueFalseTotal
unless $file 0 4 4
510 4 my $parser = XML::LibXML->new();
511 4 my $dom = $parser->parse_file($file);
512 4 return $self->from_xml_dom($dom);
513 }
514
515 #-------------------------------------------------------------------
516 # CONSTRUCTOR
517 # Parse an XML file handle and return the object that represents it.
518 #-------------------------------------------------------------------
519 0 ▶sub from_xml_fh {
Subroutine from_xml_fh: not called CC=1 SCAR=6.9
Pod documented
520 0 my $self = shift;
521 0 my $fh = shift;
522
523 0 my $parser = XML::LibXML->new();
524 0 my $dom = $parser->parse_fh($fh);
525 0 return $self->from_xml_dom($dom);
526 }
527
528 #-------------------------------------------------------------------
529 # CONSTRUCTOR
530 # Parse an XML fragment string and return the object that represents it.
531 #-------------------------------------------------------------------
532 0 ▶sub from_xml_fragment {
Subroutine from_xml_fragment: not called CC=2 SCAR=17.9
Pod documented
533 0 my $self = shift;
534 0 my $str = shift;
535
536 0 ▶ return undef unless ($str);
Branch: 1 branch
BranchTrueFalseTotal
unless $str 0 0 0
537 0 return $self->from_xml_string('<?xml version="1.0"?>' . "\n".$str);
538 }
539
540 #-------------------------------------------------------------------
541 # CONSTRUCTOR
542 # Parse an XML string and return the object that represents it.
543 #-------------------------------------------------------------------
544 0 ▶sub from_xml_string {
Subroutine from_xml_string: not called CC=2 SCAR=17.9
Pod documented
545 0 my $self = shift;
546 0 my $str = shift;
547
548 0 ▶ return undef unless ($str);
Branch: 1 branch
BranchTrueFalseTotal
unless $str 0 0 0
549 0 my $parser = XML::LibXML->new();
550 0 my $dom = $parser->parse_string($str);
551 0 return $self->from_xml_dom($dom);
552 }
553
554 #-------------------------------------------------------------------
555 # CONSTRUCTOR
556 # Parse an XML internet resource and return the object that represents it.
557 #-------------------------------------------------------------------
558 3 ▶sub from_xml_url {
Subroutine from_xml_url: called CC=3 SCAR=11.1
Pod documented
559 3 my $self = shift;
560 3 my $url = shift;
561
562 3 ▶ return undef unless ($url);
Branch: 1 branch
BranchTrueFalseTotal
unless $url 0 3 3
563
564 3 my $ua = LWP::UserAgent->new;
565 3 $ua->agent("Pastor/0.1 ");
566
567 # Create a request
568 3 my $req = HTTP::Request->new(GET => $url);
569
570 # Pass request to the user agent and get a response back
571 3 my $res = $ua->request($req);
572
573 # Check the outcome of the response
574 3 ▶ unless ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->is_success) 0 3 3
575 0 die "Pastor: ComplexType : from_xml_url : cannot GET from URL '$url' : " . $res->status_line . "\n";
576 }
577
578 3 my $str = $res->content;
579 3 my $parser = XML::LibXML->new();
580 3 my $dom = $parser->parse_string($str);
581 3 return $self->from_xml_dom($dom);
582 }
583
584
585 #-------------------------------------------------------------------
586 # Convert to object to XML and PUT it to the resource that is passed.
587 #-------------------------------------------------------------------
588 0 ▶sub to_xml {
Subroutine to_xml: not called CC=7 SCAR=40.3
Pod documented
589 0 my $self = shift;
590 0 my $resource = shift;
591
592 0 SWITCH: for ($resource) {
593 0 ▶ UNIVERSAL::isa($_, "IO::Handle") and do {return $self->to_xml_fh($resource, @_); };
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($_, "IO::Handle") and do { return $self->to_xml_fh($resource, @_) } 0 0 0
594 0 ▶ UNIVERSAL::isa($_, "URI") and do {return $self->to_xml_url($resource, @_); };
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($_, "URI") and do { return $self->to_xml_url($resource, @_) } 0 0 0
595 0 ▶ !defined($resource) || (reftype($resource) eq 'SCALAR') and do {return ($$resource = $self->to_xml_string(@_)); };
Branch: 1 branch
BranchTrueFalseTotal
!defined($resource) || builtin::reftype($resource) eq "SCALAR" and do { return $$resource = $self->to_xml_string(@_) } 0 0 0
Condition0%0/3
not defined $resource or builtin::reftype($resource) eq "SCALAR"
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $resource or builtin::reftype($resource) eq "SCALAR"
not defined $resource builtin::reftype($resource) eq "SCALAR"
Truth table0%0/3
not(A) or B
A= not defined $resourceB= builtin::reftype($resource) eq "SCALAR"
ABresult
1X1
011
000
596 0 ▶ /^(http|https|ftp|file):/i and do {return $self->to_xml_url($resource, @_); };
Branch: 1 branch
BranchTrueFalseTotal
/^(http|https|ftp|file):/i and do { return $self->to_xml_url($resource, @_) } 0 0 0
597 0 OTHERWISE: return $self->to_xml_file($resource, @_);
598 }
599 }
600
601
602 #-------------------------------------------------------------
603 4 ▶sub to_xml_dom_document {
Subroutine to_xml_dom_document: called CC=2 SCAR=7.6
Pod documented
604 4 my $self = shift;
605 4 my $node = $self->to_xml_dom(@_);
606
607 4 ▶ return $node->ownerDocument if (defined($node));
Branch: 1 branch
BranchTrueFalseTotal
if defined $node 4 0 4
608 0 return undef;
609 }
610
611
612 #-------------------------------------------------------------------
613 # Convert the object to an XML string and write to the given file handle.
614 #-------------------------------------------------------------------
615 0 ▶sub to_xml_fh {
Subroutine to_xml_fh: not called CC=1 SCAR=6.9
Pod documented
616 0 my $self = shift;
617 0 my $handle = shift;
618 0 my $str = $self->to_xml_string(@_);
619 0 print $handle $str;
620 0 return $str;
621 }
622
623 #-------------------------------------------------------------------
624 # Convert the object to an XML string and write to the given file.
625 #-------------------------------------------------------------------
626 4 ▶sub to_xml_file {
Subroutine to_xml_file: called CC=3 SCAR=11.1
Pod documented
627 4 my $self = shift;
628 4 ▶ my $file = shift or die "Pastor : to_xml_file : need a file name!\n";
Branch: 1 branch
BranchTrueFalseTotal
my $file = shift() or die "Pastor : to_xml_file : need a file name!\n" 4 0 4
629 4 my $str = $self->to_xml_string(@_);
630
631 4 my ($volume,$directories,$filebase) = File::Spec->splitpath( $file );
632 4 File::Path::mkpath($volume.$directories);
633 4 ▶ my $handle = IO::File->new($file, "w") or die "Pastor : ComplexType : to_xml_file : Can't open file : $file\n";
Branch: 1 branch
BranchTrueFalseTotal
my $handle = "IO::File"->new($file, "w") or die "Pastor : ComplexType : to_xml_file : Can't open file : $file\n" 4 0 4
634
635 4 print $handle $str;
636 4 $handle->close();
637 4 return $str;
638 }
639
640 #-------------------------------------------------------------------
641 # Convert the object to an XML fragment (without the <?xml ...> part)
642 #-------------------------------------------------------------------
643 0 ▶sub to_xml_fragment {
Subroutine to_xml_fragment: not called CC=1 SCAR=6.9
Pod documented
644 0 my $self = shift;
645
646 0 my $dom = $self->to_xml_dom(@_);
647 0 return $dom->toString(1);
648 }
649
650 #-------------------------------------------------------------------
651 # Convert the object to an XML string.
652 #-------------------------------------------------------------------
653 4 ▶sub to_xml_string {
Subroutine to_xml_string: called CC=1 SCAR=0.0
Pod documented
654 4 my $self = shift;
655
656 4 my $dom = $self->to_xml_dom_document(@_);
657 4 return $dom->toString(1);
658
659 }
660
661
662
663 #-------------------------------------------------------------------
664 # Convert the object to an XML string and PUT it to the given URL.
665 #-------------------------------------------------------------------
666 0 ▶sub to_xml_url {
Subroutine to_xml_url: not called CC=3 SCAR=24.8
Pod documented
667 0 my $self = shift;
668 0 ▶ my $url = shift or die "Pastor : to_xml_url : need a URL!\n";
Branch: 1 branch
BranchTrueFalseTotal
my $url = shift() or die "Pastor : to_xml_url : need a URL!\n" 0 0 0
669 0 my $str = $self->to_xml_string(@_);
670
671 0 my $ua = LWP::UserAgent->new;
672 0 $ua->agent("Pastor/0.1 ");
673
674 # Create a request
675 0 my $req = HTTP::Request->new(PUT => $url);
676 0 $req->content($str);
677
678 # Pass request to the user agent and get a response back
679 0 my $res = $ua->request($req);
680
681 # Check the outcome of the response
682 0 ▶ unless ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->is_success) 0 0 0
683 0 die "Pastor: ComplexType : to_xml_url : cannot PUT to URL '$url' : " . $res->status_line . "\n";
684 }
685 0 return $str;
686 }
687
688
689 #-------------------------------------------------------------
690 # Convert the data in this object to a DOM element and return the DOM tree.
691 # Uses heavily the type information in XmlSchemaType.
692 #-------------------------------------------------------------
693 58 ▶sub to_xml_dom {
Subroutine to_xml_dom: called CC=35 SCAR=37.3
Pod documented
694 58 my $self = shift;
695 58 my $args = {@_};
696 58 my $doc = $args->{doc};
697 58 my $name = $args->{name};
698 58 my $type = $self->XmlSchemaType();
699 58 my $node;
700 my $doc_new;
701 58 my $targetNamespace;
702 58 my $verbose = 0;
703
704 58 ▶ unless ( defined($doc) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $doc) 4 54 58
705 4 ▶ my $encoding = $args->{encoding} || 'UTF-8';
Condition100%2/2
$args->{'encoding'} || 'UTF-8'
l!l
22
MC/DC100%1/1
$args->{'encoding'} || 'UTF-8'
$args->{'encoding'}
Truth table100%2/2
A
A= $args->{'encoding'}
Aresult
11
00
706 4 ▶ my $version = $args->{version} || '1.0';
Condition50%1/2
$args->{'version'} || '1.0'
l!l
04
MC/DC0%0/1
$args->{'version'} || '1.0'
$args->{'version'}
Truth table50%1/2
A
A= $args->{'version'}
Aresult
11
00
707
708 4 $doc = $args->{doc}= XML::LibXML::Document->new($version, $encoding);
709 4 $doc_new = 1;
710 }
711
712 58 ▶ unless ($name) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($name) 4 54 58
713 4 ▶ if (UNIVERSAL::can($self, "XmlSchemaElement")) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::can($self, 'XmlSchemaElement')) { } 4 0 4
714 4 my $xmlSchemaElement = $self->XmlSchemaElement;
715 4 $name = $xmlSchemaElement->name();
716 }else {
717 0 $name = $self->{'._nodeName_'};
718 }
719 }
720
721 # We absolutely need a name
722 58 ▶ $name or die "Pastor: to_xml_dom : Element needs a name!\n";
Branch: 1 branch
BranchTrueFalseTotal
$name or die "Pastor: to_xml_dom : Element needs a name!\n" 58 0 58
723
724 58 ▶ print STDERR "**** to_xml_dom : " . $name . "\n" if ($verbose >=9);
Branch: 1 branch
BranchTrueFalseTotal
if $verbose >= 9 0 58 58
725
726 # Get the target name-space.
727 58 ▶ if (UNIVERSAL::can($self, "XmlSchemaElement")) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::can($self, "XmlSchemaElement")) 4 54 58
728 4 my $xmlSchemaElement = $self->XmlSchemaElement;
729 4 ▶ $targetNamespace=$xmlSchemaElement->targetNamespace if ($xmlSchemaElement->scope =~ /global/i);
Branch: 1 branch
BranchTrueFalseTotal
if $xmlSchemaElement->scope =~ /global/i 4 0 4
730 }
731
732 58 ▶ if (!$targetNamespace && UNIVERSAL::can($self, "XmlSchemaType")) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $targetNamespace and UNIVERSAL::can($self, "XmlSchemaType")) 54 4 58
Condition66%2/3
not $targetNamespace and UNIVERSAL::can($self, "XmlSchemaType")
!ll&&!rl&&r
4054
MC/DC50%1/2
not $targetNamespace and UNIVERSAL::can($self, "XmlSchemaType")
not $targetNamespace UNIVERSAL::can($self, "XmlSchemaType")
Truth table66%2/3
not(A) and B
A= not $targetNamespaceB= UNIVERSAL::can($self, "XmlSchemaType")
ABresult
0X0
100
111
733 54 my $type = $self->XmlSchemaType;
734 54 $targetNamespace = $type->targetNamespace;
735 }
736
737
738 # Create the node
739 58 ▶ if ($targetNamespace) {
Branch: 1 branch
BranchTrueFalseTotal
if ($targetNamespace) { } 38 20 58
740 38 $node=$doc->createElementNS($targetNamespace, $name);
741 }else {
742 20 $node=$doc->createElement($name);
743 }
744
745 58 ▶ $doc->setDocumentElement($node) if ($doc_new);
Branch: 1 branch
BranchTrueFalseTotal
if $doc_new 4 54 58
746
747 # Attributes
748 58 ▶ if(UNIVERSAL::can($type, 'effectiveAttributes')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::can($type, "effectiveAttributes")) 38 20 58
749 38 my $attributes = $type->effectiveAttributes();
750 38 ▶ my $attribPfx = $type->attributePrefix() || '';
Condition50%1/2
$type->attributePrefix || ''
l!l
380
MC/DC0%0/1
$type->attributePrefix || ''
$type->attributePrefix
Truth table50%1/2
A
A= $type->attributePrefix
Aresult
11
00
751
752 38 foreach my $attribName (@$attributes) {
753 50 my $field = $attribPfx . $attribName;
754 50 my $value = $self->{$field};
755 50 ▶ next unless defined($value);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 2 48 50
756 48 $node->setAttribute($attribName, "" . $value); # force stringification.
757 }
758 }
759
760 58 ▶ if (UNIVERSAL::can($self, '__value')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::can($self, "__value")) 58 0 58
761 58 ▶ my $isSimpleContent = UNIVERSAL::can($type, 'isSimpleContent') && $type->isSimpleContent;
Condition100%3/3
UNIVERSAL::can($type, 'isSimpleContent') && $type->isSimpleContent
!ll&&!rl&&r
20344
MC/DC100%2/2
UNIVERSAL::can($type, 'isSimpleContent') && $type->isSimpleContent
UNIVERSAL::can($type, 'isSimpleContent') $type->isSimpleContent
Truth table100%3/3
A && B
A= UNIVERSAL::can($type, 'isSimpleContent')B= $type->isSimpleContent
ABresult
0X0
100
111
762 58 my $isSimpleType = UNIVERSAL::isa($type, 'XML::Pastor::Schema::SimpleType');
763
764 58 ▶ if($self->__value() && ($isSimpleContent || $isSimpleType)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->__value and $isSimpleContent || $isSimpleType) 24 34 58
Condition100%6/6
$self->__value and $isSimpleContent || $isSimpleType
!ll&&!rl&&r
102424
$isSimpleContent or $isSimpleType
l!l&&r!l&&!r
42024
MC/DC100%3/3
$self->__value and $isSimpleContent || $isSimpleType
$self->__value $isSimpleContent $isSimpleType
Truth table100%4/4
A and B or C
A= $self->__valueB= $isSimpleContentC= $isSimpleType
ABCresult
0XX0
1000
11X1
1011
765 24 $node->appendChild( XML::LibXML::Text->new( $self->__value . "" ) ); # stringify
766 }
767 }
768
769 #Elements
770 58 ▶ if (UNIVERSAL::can($type, 'effectiveElements') && UNIVERSAL::can($type, 'effectiveElementInfo')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::can($type, "effectiveElements") and UNIVERSAL::can($type, "effectiveElementInfo")) 38 20 58
Condition66%2/3
UNIVERSAL::can($type, "effectiveElements") and UNIVERSAL::can($type, "effectiveElementInfo")
!ll&&!rl&&r
20038
MC/DC50%1/2
UNIVERSAL::can($type, "effectiveElements") and UNIVERSAL::can($type, "effectiveElementInfo")
UNIVERSAL::can($type, "effectiveElements") UNIVERSAL::can($type, "effectiveElementInfo")
Truth table66%2/3
A and B
A= UNIVERSAL::can($type, "effectiveElements")B= UNIVERSAL::can($type, "effectiveElementInfo")
ABresult
0X0
100
111
771 38 my $elements = $type->effectiveElements();
772 38 my $elementInfo = $type->effectiveElementInfo();
773 38 foreach my $elemName (@$elements) {
774 58 my $value = $self->{$elemName};
775 58 ▶ next unless defined($value);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 20 38 58
776 38 ▶ $value = [$value] unless (reftype($value) eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::reftype($value) eq "ARRAY" 34 4 38
777 38 my $element = $elementInfo->{$elemName};
778
779 38 foreach my $item (@$value) {
780 54 my $obj = $item;
781 54 my $class = $element->class;
782 54 ▶ if (!UNIVERSAL::can($item, "to_xml_dom")) {
Branch: 1 branch
BranchTrueFalseTotal
unless (UNIVERSAL::can($item, "to_xml_dom")) 0 54 54
783 0 ▶ if ( (reftype($item) eq 'HASH') && UNIVERSAL::isa($class, "XML::Pastor::ComplexType")){
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::reftype($item) eq 'HASH' and UNIVERSAL::isa($class, 'XML::Pastor::ComplexType')) { } 0 0 0
elsif (UNIVERSAL::isa($class, 'XML::Pastor::SimpleType')) { } 0 0 0
Condition0%0/3
builtin::reftype($item) eq "HASH" and UNIVERSAL::isa($class, "XML::Pastor::ComplexType")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::reftype($item) eq "HASH" and UNIVERSAL::isa($class, "XML::Pastor::ComplexType")
builtin::reftype($item) eq "HASH" UNIVERSAL::isa($class, "XML::Pastor::ComplexType")
Truth table0%0/3
A and B
A= builtin::reftype($item) eq "HASH"B= UNIVERSAL::isa($class, "XML::Pastor::ComplexType")
ABresult
0X0
100
111
784 # Item should be of ComplexType, but it is just Hash. Fix it and then do the job.
785 0 $obj = $class->new(%$item);
786 }elsif (UNIVERSAL::isa($class, "XML::Pastor::SimpleType")){
787 # Item should be of SimpleType. Fix it and then do the job.
788 0 $obj = $class->new(__value => "$item");
789 }else {
790 0 die "Pastor : to_xml_dom : Don't know how to transform '$elemName' into DOM (not a known or convertable type)";
791 }
792 }
793
794 54 ▶ if (defined(my $childNode = $self->_childToDom(doc=>$doc, name=>$elemName, child=>$obj))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $childNode = $self->_childToDom("doc", $doc, "name", $elemName, "child", $obj))) 54 0 54
795 54 $node->appendChild($childNode);
796 }
797 54 ▶ last if ($element->isSingleton()); # singleton
Branch: 1 branch
BranchTrueFalseTotal
if $element->isSingleton 34 20 54
798 }
799 }
800 }
801
802 58 return $node;
803 }
804
805
806 #-------------------------------------------------------------------
807 54 ▶sub _childToDom {
Subroutine _childToDom: called CC=4 SCAR=20.3
808 54 my $self = shift;
809 54 my $args = {@_};
810 54 my $child = $args->{child};
811 54 my $doc = $args->{doc};
812
813 # If the child can "to_xml_dom", then just return that
814 54 ▶ if (UNIVERSAL::can($child, "to_xml_dom")) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::can($child, "to_xml_dom")) 54 0 54
815 54 return $child->to_xml_dom(@_);
816 }
817
818 # Otherwise, we'll just stringify the child and return an element with a text node.
819 0 ▶ my $name = $args->{name} or die "Pastor: _childToDom : Child node needs a name!\n";
Branch: 1 branch
BranchTrueFalseTotal
my $name = $args->{"name"} or die "Pastor: _childToDom : Child node needs a name!\n" 0 0 0
820 0 ▶ $doc or die "Pastor: _childToDom : Need a DOM Document!\n";
Branch: 1 branch
BranchTrueFalseTotal
$doc or die "Pastor: _childToDom : Need a DOM Document!\n" 0 0 0
821
822 # TODO : Namespaces
823 0 my $node = $doc->createElement($name);
824
825 0 my $text= $child . ""; # stringify
826
827 # if ($text =~~ /[<>]/) {
828 # has special XML characters. Must be put in a CDATA section
829 # $node->appendChild( XML::LibXML::CDATASection->new( $child . "" ) ); # stringify
830 # } else {
831 # Normal text.
832 0 $node->appendChild( XML::LibXML::Text->new( $child . "" ) ); # stringify
833 # }
834
835 0 return $node;
836 }
837
838
839 10 1;
840
841 __END__
« blib/lib/XML/Pastor/SimpleType.pm ↑ Summary blib/lib/XML/Pastor/Pastorize.pm »