PRANG/Graph/Meta/Class.pm

filter: Statementstmt 91.1% 205 / 225 Branchbran 82.2% 97 / 118 Conditioncond 70.7% 29 / 41 MC/DCmcdc 52.0% 13 / 25 Subroutinesub 95.4% 21 / 22 Podpod 55.5% 5 / 9 totaltotal 84.0% 370 / 440 SCAR 45.9
CC
81
Cov
86%
accept_childnodes
29.3
accept_attributes
17.5
add_xml_attr
14.0
CRAP 98.3
« PRANG/Marshaller.pm ↑ Summary PRANG/XMLSchema/Types.pm »
1 package PRANG::Graph::Meta::Class;
2 11 $PRANG::Graph::Meta::Class::VERSION = '0.19';
3 11 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 11 ▶use MooseX::Params::Validate;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 11 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 has 'xml_attr' =>
10 isa => "HashRef[HashRef[PRANG::Graph::Meta::Attr]]",
11 is => "ro",
12 lazy => 1,
13 required => 1,
14 default => sub {
15 31 ▶ my $self = shift;
Subroutine __ANON__: called CC=5 SCAR=16.1
16 31 my @attr = grep { $_->does("PRANG::Graph::Meta::Attr") }
17 $self->get_all_attributes;
18 31 my $default_xmlns = ""; #eval { $self->name->xmlns };
19 31 my %attr_ns;
20 31 for my $attr (@attr) {
21 29 ▶ my $xmlns = $attr->has_xmlns
Branch: 1 branch
BranchTrueFalseTotal
$attr->has_xmlns ? : 8 21 29
22 ?
23 $attr->xmlns
24 : $default_xmlns;
25 29 ▶ my $xml_name = $attr->has_xml_name
Branch: 1 branch
BranchTrueFalseTotal
$attr->has_xml_name ? : 9 20 29
26 ?
27 $attr->xml_name
28 : $attr->name;
29 29 ▶ $attr_ns{$xmlns//""}{$xml_name} = $attr;
Condition50%1/2
$xmlns // ""
l!l
290
MC/DC0%0/1
$xmlns // ""
$xmlns
Truth table50%1/2
A
A= $xmlns
Aresult
11
00
30 }
31 31 \%attr_ns;
32 11 };
33  
34 has 'xml_elements' =>
35 isa => "ArrayRef[PRANG::Graph::Meta::Element]",
36 is => "ro",
37 lazy => 1,
38 required => 1,
39 default => sub {
40 33 ▶ my $self = shift;
Subroutine __ANON__: called CC=7 SCAR=22.6
41 my @elements = grep {
42 33 $_->does("PRANG::Graph::Meta::Element")
43 } $self->get_all_attributes;
44 33 my @e_c = map { $_->associated_class->name } @elements;
45 33 my %e_c_does;
46 33 for my $parent (@e_c) {
47 76 for my $child (@e_c) {
48 430 ▶ if ( $parent eq $child ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent eq $child) { } 430 0 430
49 430 $e_c_does{$parent}{$child} = 0;
50 }
51 else {
52 0 ▶ my $cdp = $child->does($parent) ||
Condition0%0/3
$child->does($parent) || $child->isa($parent)
l!l&&r!l&&!r
000
MC/DC0%0/2
$child->does($parent) || $child->isa($parent)
$child->does($parent) $child->isa($parent)
Truth table0%0/3
A || B
A= $child->does($parent)B= $child->isa($parent)
ABresult
1X1
011
000
53 $child->isa($parent);
54 0 ▶ $e_c_does{$parent}{$child} =
Branch: 1 branch
BranchTrueFalseTotal
$cdp ? : 0 0 0
55 ( $cdp ? -1 : 1 );
56 }
57 }
58 }
59 76 [ map { $elements[$_] } sort {
60 33 ▶ $e_c_does{$e_c[$a]}{$e_c[$b]} or
Branch: 1 branch
BranchTrueFalseTotal
$e_c_does{$e_c[$a]}{$e_c[$b]} or $elements[$a]->insertion_order <=> $elements[$b]->insertion_order 95 0 95
61 (
62 $elements[$a]->insertion_order
63 <=> $elements[$b]->insertion_order
64 )
65 } 0..$#elements
66 ];
67 11 };
68  
69 has 'graph' =>
70 is => "rw",
71 isa => "PRANG::Graph::Node",
72 lazy => 1,
73 required => 1,
74 default => sub {
75 33 ▶ $_[0]->build_graph;
Subroutine __ANON__: called CC=1 SCAR=0.0
76 },
77 11 ;
78  
79 sub build_graph {
80 33 ▶ my $self = shift;
Subroutine build_graph: called CC=3 SCAR=11.0
Pod undocumented
81
82 33 my @nodes = map { $_->graph_node } @{ $self->xml_elements };
83 33 ▶ if ( @nodes != 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@nodes != 1) { } 11 22 33
elsif (@nodes) { } 22 0 22
84 11 PRANG::Graph::Seq->new(
85 members => \@nodes,
86 );
87 }
88 elsif (@nodes) {
89 22 $nodes[0];
90 }
91 }
92  
93 sub add_to_list($$) {
94 1226 ▶ if ( !defined $_[0] ) {
Subroutine add_to_list: called CC=4 SCAR=13.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $_[0]) { } 959 267 1226
95 959 $_[0] = $_[1];
96 }
97 else {
98 267 ▶ if ( (ref($_[0])||"") ne "ARRAY" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ((ref $_[0] || "") ne "ARRAY") 134 133 267
Condition100%2/2
ref $_[0] || ''
l!l
18087
MC/DC100%1/1
ref $_[0] || ''
ref $_[0]
Truth table100%2/2
A
A= ref $_[0]
Aresult
11
00
99 134 $_[0] = [ $_[0] ];
100 }
101 267 push @{ $_[0] }, $_[1];
102 }
103 }
104  
105 sub as_listref($) {
106 87 ▶ if ( ref($_[0]) eq "ARRAY" ) {
Subroutine as_listref: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'ARRAY') { } 44 43 87
107 44 $_[0]
108 }
109 else {
110 43 [ $_[0] ];
111 }
112 }
113  
114 sub as_item($) {
115 194 ▶ if ( ref($_[0]) eq "ARRAY" ) {
Subroutine as_item: called CC=2 SCAR=9.2
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'ARRAY') { } 0 194 194
116 0 die scalar(@{$_[0]})." item(s) found where 1 expected";
117 }
118 else {
119 194 $_[0];
120 }
121 }
122  
123 sub accept_attributes {
124 243 ▶ my $self = shift;
Subroutine accept_attributes: called CC=17 SCAR=28.6
Pod documented
125 243 my ( $node_attr, $context, $lax ) = pos_validated_list(
126 \@_,
127 { isa => 'ArrayRef[XML::LibXML::Attr]' },
128 { isa => 'PRANG::Graph::Context' },
129 { isa => 'Bool', optional => 1, default => 0 },
130 );
131  
132 243 my $attributes = $self->xml_attr;
133 243 my %rv;
134  
135 # process attributes
136 243 for my $attr (@$node_attr) {
137 95 my $prefix = $attr->prefix;
138 95 ▶ if ( !defined $prefix ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $prefix) 71 24 95
139 71 $prefix = "";
140 }
141 95 ▶ if ( length $prefix and !exists $context->xsi->{$prefix} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $prefix and not exists $context->xsi->{$prefix}) 0 95 95
Condition66%2/3
length $prefix and not exists $context->xsi->{$prefix}
!ll&&!rl&&r
71240
MC/DC0%0/2
length $prefix and not exists $context->xsi->{$prefix}
length $prefix not exists $context->xsi->{$prefix}
Truth table66%2/3
A and not(B)
A= length $prefixB= not exists $context->xsi->{$prefix}
ABresult
0X0
100
111
142 0 $context->exception("unknown xmlns prefix '$prefix'");
143 }
144 95 ▶ my $xmlns = $context->get_xmlns($prefix)
Branch: 1 branch
BranchTrueFalseTotal
if length $prefix 24 71 95
145 if length $prefix;
146 95 ▶ $xmlns //= "";
Condition100%2/2
$xmlns //= ""
l!l
7916
MC/DC100%1/1
$xmlns //= ""
$xmlns
Truth table100%2/2
A
A= $xmlns
Aresult
11
00
147 95 my $meta_att = $attributes->{$xmlns}{$attr->localname};
148 95 my $xmlns_att_name;
149 my $_xmlns_att_name = sub {
150 30 ▶ $xmlns_att_name = $meta_att->xmlns_attr
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$xmlns_att_name = $meta_att->xmlns_attr or $context->exception('xmlns wildcarded, but no xmlns_attr set on ' . $self->name . q[ property '] . $meta_att->att_name . q[']) 0 30 30
151 or $context->exception(
152 "xmlns wildcarded, but no xmlns_attr set on "
153 .$self->name." property '"
154 .$meta_att->att_name."'",
155 );
156 95 };
157  
158 95 ▶ if ($meta_att) {
Branch: 5 branches
BranchTrueFalseTotal
if ($meta_att) { } 56 39 95
elsif ($meta_att = $attributes->{'*'}{$attr->localname}) { } 6 33 39
elsif ($meta_att = $attributes->{$xmlns}{'*'}) { } 6 27 33
elsif ($meta_att = $attributes->{'*'}{'*'}) { } 24 3 27
elsif ($xmlns =~ m[^\w+://\w+\.w3\.org/.*schema]i and $attr->localname =~ /schema/i) { } 0 3 3
159  
160 # sweet, it's ok
161 56 my $att_name = $meta_att->name;
162  
163 # check the type constraint
164 56 ▶ if ( my $tc = $meta_att->type_constraint
Branch: 1 branch
BranchTrueFalseTotal
if (my $tc = $meta_att->type_constraint and not $meta_att->xml_isa) 55 1 56
Condition66%2/3
my $tc = $meta_att->type_constraint and not $meta_att->xml_isa
!ll&&!rl&&r
1055
MC/DC50%1/2
my $tc = $meta_att->type_constraint and not $meta_att->xml_isa
my $tc = $meta_att->type_constraint not $meta_att->xml_isa
Truth table66%2/3
A and not(B)
A= my $tc = $meta_att->type_constraintB= not $meta_att->xml_isa
ABresult
0X0
100
111
165 and !$meta_att->xml_isa )
166 {
167 55 ▶ if ( !$tc->check($attr->value) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tc->check($attr->value)) 2 53 55
168 2 $context->exception(
169 "invalid value '" . $attr->value . "' of attribute ".$attr->nodeName,
170 $attr->parentNode,
171 );
172 }
173 }
174 54 add_to_list($rv{$att_name}, $attr->value);
175 }
176 elsif ( $meta_att = $attributes->{"*"}{$attr->localname} ) {
177  
178 # wildcard xmlns only; need to store the xmlns
179 # in another attribute. Also, multiple values
180 # may appear with different xml namespaces.
181 6 my $att_name = $meta_att->name;
182 6 $_xmlns_att_name->();
183 6 add_to_list($rv{$att_name}, $attr->value);
184 6 add_to_list($rv{$xmlns_att_name}, $xmlns);
185 }
186 elsif ( $meta_att = $attributes->{$xmlns}{"*"} ) {
187  
188 # wildcard attribute name. This attribute gets
189 # HashRef treatment.
190 6 $rv{$meta_att->name}{$attr->localname} = $attr->value;
191 }
192 elsif ( $meta_att = $attributes->{"*"}{"*"} ) {
193  
194 # wildcard attribute name and namespace. Both
195 # attributes gets the joy of HashRef[ArrayRef[Str]|Str]
196 24 my $att_name = $meta_att->name;
197 24 $_xmlns_att_name->();
198 add_to_list(
199 24 $rv{$att_name}{$attr->localname},
200 $attr->value,
201 );
202 add_to_list(
203 24 $rv{$xmlns_att_name}{$attr->localname},
204 $xmlns
205 );
206 }
207 ▶ elsif (
Condition33%1/3
$xmlns =~ m[^\w+://\w+\.w3\.org/.*schema]i and $attr->localname =~ /schema/i
!ll&&!rl&&r
300
MC/DC0%0/2
$xmlns =~ m[^\w+://\w+\.w3\.org/.*schema]i and $attr->localname =~ /schema/i
$xmlns =~ m[^\w+://\w+\.w3\.org/.*schema]i $attr->localname =~ /schema/i
Truth table33%1/3
A and B
A= $xmlns =~ m[^\w+://\w+\.w3\.org/.*schema]iB= $attr->localname =~ /schema/i
ABresult
0X0
100
111
208 $xmlns =~ m{^\w+://\w+\.w3\.org/.*schema}i
209 and
210 $attr->localname =~ m{schema}i
211 )
212 {
213  
214 # they said "schema" twice, they must be mad.
215 # ignore their craven input.
216 }
217 else {
218 # fail, unless we're in lax mode, in which case do nothing.
219 3 ▶ $context->exception("invalid attribute '".$attr->name."'")
Branch: 1 branch
BranchTrueFalseTotal
unless $lax 2 1 3
220 unless $lax;
221 }
222 }
223 239 (%rv);
224 }
225  
226 11 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
227  
228 sub accept_childnodes {
229 251 ▶ my $self = shift;
Subroutine accept_childnodes: called CC=24 SCAR=33.8
Pod documented
230
231 251 my ( $childNodes, $context, $lax ) = pos_validated_list(
232 \@_,
233 { isa => 'ArrayRef[XML::LibXML::Node]' },
234 { isa => 'PRANG::Graph::Context' },
235 { isa => 'Bool', optional => 1, default => 0 },
236 );
237
238 251 my $graph = $self->graph;
239  
240 251 my (%init_args, %init_arg_names, %init_arg_xmlns, %init_arg_nodes);
241 251 my @rv;
242 my @nodes = grep {
243 251 ▶ !( $_->isa("XML::LibXML::Text")
Condition100%3/3
$_->isa('XML::LibXML::Text') && $_->data =~ /\A\s*\Z/
!ll&&!rl&&r
3523016
MC/DC100%2/2
$_->isa('XML::LibXML::Text') && $_->data =~ /\A\s*\Z/
$_->isa('XML::LibXML::Text') $_->data =~ /\A\s*\Z/
Truth table100%3/3
A && B
A= $_->isa('XML::LibXML::Text')B= $_->data =~ /\A\s*\Z/
ABresult
0X0
100
111
244 and $_->data =~ /\A\s*\Z/
245 )
246 }
247 @$childNodes;
248 251 ▶ while ( my $input_node = shift @nodes ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $input_node = shift @nodes) 372 218 590
249 372 ▶ next if $input_node->nodeType == XML_COMMENT_NODE;
Branch: 1 branch
BranchTrueFalseTotal
if $input_node->nodeType == 8 0 372 372
250 372 my ($key, $value, $name, $xmlns) =
251 $graph->accept($input_node, $context, $lax);
252 345 ▶ if ( !$key ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($key) 5 340 345
253 5 ▶ next if $lax;
Branch: 1 branch
BranchTrueFalseTotal
if $lax 3 2 5
254
255 2 my (@what) = $graph->expected($context);
256 2 $context->exception(
257 "unexpected node: expecting @what",
258 $input_node,
259 );
260 }
261 340 my $meta_att = $self->find_attribute_by_name($key);
262 340 ▶ if ( !$meta_att->_item_tc->check($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($meta_att->_item_tc->check($value)) 4 336 340
263 4 $context = $context->next_ctx(
264 $input_node->namespaceURI,
265 $input_node->localname,
266 );
267 4 $context->exception(
268 "bad value '$value'",
269 $input_node
270 );
271 }
272 336 add_to_list($init_args{$key}, $value);
273 336 add_to_list($init_arg_nodes{$key}, $input_node);
274 336 ▶ if ( defined $name ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $name) 124 212 336
275 add_to_list(
276 124 $init_arg_names{$key},
277 $name,
278 );
279 }
280 336 ▶ if ( defined $xmlns ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $xmlns) 316 20 336
281 add_to_list(
282 316 $init_arg_xmlns{$key},
283 $xmlns,
284 );
285 }
286 }
287  
288 218 ▶ if ( !$graph->complete($context) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($graph->complete($context)) 6 212 218
289 6 my (@what) = $graph->expected($context);
290 6 $context->exception(
291 "Node incomplete; expecting: @what",
292 );
293 }
294  
295 # now, we have to take all the values we just got and
296 # collapse them to init args
297 212 for my $element ( @{ $self->xml_elements } ) {
298 736 my $key = $element->name;
299 736 ▶ next unless exists $init_args{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $init_args{$key} 490 246 736
300 246 my $expect;
301 246 ▶ if ( $element->has_xml_max and $element->xml_max == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($element->has_xml_max and $element->xml_max == 1) { } 185 61 246
Condition100%3/3
$element->has_xml_max and $element->xml_max == 1
!ll&&!rl&&r
538185
MC/DC100%2/2
$element->has_xml_max and $element->xml_max == 1
$element->has_xml_max $element->xml_max == 1
Truth table100%3/3
A and B
A= $element->has_xml_maxB= $element->xml_max == 1
ABresult
0X0
100
111
302 185 $expect = \&as_item;
303 }
304 else {
305 61 $expect = \&as_listref;
306 }
307 push @rv, eval {
308 246 my $val = $expect->(delete $init_args{$key});
309 246 ▶ if ( my $t_c = $element->type_constraint) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $t_c = $element->type_constraint) 246 0 246
310 246 ▶ if ( !$t_c->check($val) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($t_c->check($val)) 0 246 246
311 0 ▶ if ( ref $val ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val) 0 0 0
312 0 $val = encode_json $val;
313 }
314 0 die "value '$val' failed type check";
315 }
316 }
317 ( ( ( $element->has_xml_nodeName_attr
318 and
319 exists $init_arg_names{$key}
320 )
321 ? ( $element->xml_nodeName_attr =>
322 $expect->($init_arg_names{$key})
323 )
324 : ()
325 ),
326 ( ( $element->has_xmlns_attr
327 and
328 exists $init_arg_xmlns{$key}
329 )
330 ? ( $element->xmlns_attr =>
331 246 ▶ $expect->($init_arg_xmlns{$key})
Branch: 2 branches
BranchTrueFalseTotal
$element->has_xml_nodeName_attr && exists $init_arg_names{$key} ? : 18 228 246
$element->has_xmlns_attr && exists $init_arg_xmlns{$key} ? : 17 229 246
Condition66%4/6
$element->has_xml_nodeName_attr and exists $init_arg_names{$key}
!ll&&!rl&&r
228018
$element->has_xmlns_attr and exists $init_arg_xmlns{$key}
!ll&&!rl&&r
229017
MC/DC50%1/2
$element->has_xml_nodeName_attr and exists $init_arg_names{$key}
$element->has_xml_nodeName_attr exists $init_arg_names{$key}
MC/DC50%1/2
$element->has_xmlns_attr and exists $init_arg_xmlns{$key}
$element->has_xmlns_attr exists $init_arg_xmlns{$key}
Truth table66%2/3
A and B
A= $element->has_xml_nodeName_attrB= exists $init_arg_names{$key}
ABresult
0X0
100
111
Truth table66%2/3
A and B
A= $element->has_xmlns_attrB= exists $init_arg_xmlns{$key}
ABresult
0X0
100
111
332 )
333 : ()
334 ),
335 $key => $val,
336 );
337 246 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
push @rv, eval { do { my $val = &$expect(delete $init_args{$key}); if (my $t_c = $element->type_constraint) { unless ($t_c->check($val)) { if (ref $val) { $val = &encode_json($val); } ; die "value '${val}' failed type check"; } ; } ; $element->has_xml_nodeName_attr && exists $init_arg_names{$key} ? ($element->xml_nodeName_attr, &$expect($init_arg_names{$key})) : (), $element->has_xmlns_attr && exists $init_arg_xmlns{$key} ? ($element->xmlns_attr, &$expect($init_arg_xmlns{$key})) : (), $key, $val } } or do { my $err = $@; my $bad = $init_arg_nodes{$key}; if (ref $bad eq "ARRAY") { $bad = $bad->parentNode; } else { $context = $context->next_ctx($bad->namespaceURI, $bad->localname); } ; $context->exception("internal error: processing '${key}' attribute: $err", $bad) } 0 246 246
338 0 my $err = $@;
339 0 my $bad = $init_arg_nodes{$key};
340 0 ▶ if ( ref $bad eq "ARRAY" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $bad eq 'ARRAY') { } 0 0 0
341 0 $bad = $bad->parentNode;
342 }
343 else {
344 0 $context =
345 $context->next_ctx($bad->namespaceURI,
346 $bad->localname);
347 }
348 0 $context->exception(
349 "internal error: processing '$key' attribute: $err",
350 $bad,
351 );
352 };
353 }
354 212 ▶ if (my @leftovers = keys %init_args) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@leftovers) = keys %init_args) 0 212 212
355 0 $context->exception(
356 "internal error: ".@leftovers
357 ." init arg(s) left over (@leftovers)",
358 );
359 }
360 212 return @rv;
361 }
362  
363 sub marshall_in_element {
364 238 ▶ my $self = shift;
Subroutine marshall_in_element: called CC=7 SCAR=19.9
Pod documented
365
366 238 my ( $node, $ctx, $lax ) = pos_validated_list(
367 \@_,
368 { isa => 'XML::LibXML::Node' },
369 { isa => 'PRANG::Graph::Context' },
370 { isa => 'Bool', optional => 1, default => 0 },
371 );
372
373 238 my @node_attr = grep { $_->isa("XML::LibXML::Attr") }
374 $node->attributes;
375 238 my @ns_attr = $node->getNamespaces;
376  
377 238 ▶ if (@ns_attr) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ns_attr) 26 212 238
378 $ctx->add_xmlns($_->declaredPrefix//"" => $_->declaredURI)
379 26 ▶ for @ns_attr;
Condition100%2/2
$_->declaredPrefix // ""
l!l
561
MC/DC100%1/1
$_->declaredPrefix // ""
$_->declaredPrefix
Truth table100%2/2
A
A= $_->declaredPrefix
Aresult
11
00
380 }
381  
382 238 my $new_ctx = $ctx->next_ctx(
383 $node->namespaceURI,
384 $node->localname,
385 );
386  
387 238 my @init_args = $self->accept_attributes( \@node_attr, $new_ctx, $lax );
388  
389 # now process elements
390 my @childNodes = grep {
391 236 ▶ !( $_->isa("XML::LibXML::Comment")
Condition83%5/6
$_->isa('XML::LibXML::Comment') || $_->isa('XML::LibXML::Text') && $_->data =~ /\A\s+\Z/
l!l&&r!l&&!r
0386365
$_->isa("XML::LibXML::Text") and $_->data =~ /\A\s+\Z/
!ll&&!rl&&r
33530386
MC/DC66%2/3
$_->isa('XML::LibXML::Comment') || $_->isa('XML::LibXML::Text') && $_->data =~ /\A\s+\Z/
$_->isa('XML::LibXML::Comment') $_->isa("XML::LibXML::Text") $_->data =~ /\A\s+\Z/
Truth table75%3/4
A || B and C
A= $_->isa('XML::LibXML::Comment')B= $_->isa("XML::LibXML::Text")C= $_->data =~ /\A\s+\Z/
ABCresult
1XX1
0111
00X0
0100
392 or
393 $_->isa("XML::LibXML::Text") and $_->data =~ /\A\s+\Z/
394 )
395 } $node->childNodes;
396  
397 236 push @init_args, $self->accept_childnodes( \@childNodes, $new_ctx, $lax );
398  
399 206 my $value = eval { $self->name->new(@init_args) };
400 206 ▶ if ( !$value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $value) { } 0 206 206
401 0 my $error = $@;
402 0 $error =~ m|^(.+) at /|;
403 0 my $msg = $1;
404 0 $ctx->exception(
405 "Validation error from ".$self->name
406 ." constructor: $1",
407 $node,
408 );
409 }
410 else {
411 206 return $value;
412 }
413 }
414  
415 sub add_xml_attr {
416 104 ▶ my $self = shift;
Subroutine add_xml_attr: called CC=14 SCAR=26.4
Pod documented
417
418 104 my ( $item, $node, $ctx ) = pos_validated_list(
419 \@_,
420 { isa => 'Object' },
421 { isa => 'XML::LibXML::Element' },
422 { isa => 'PRANG::Graph::Context' },
423 );
424
425
426 104 my $attributes = $self->xml_attr;
427 104 ▶ while ( my ($xmlns, $att) = each %$attributes ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$attributes) 89 104 193
428 89 ▶ while ( my ($attName, $meta_att) = each %$att ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$att) 83 89 172
429 83 my $is_optional;
430 83 my $obj_att_name = $meta_att->name;
431 83 ▶ if ( $meta_att->has_xml_required ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($meta_att->has_xml_required) { } 48 35 83
elsif (not $meta_att->is_required) { } 33 2 35
432 48 $is_optional = !$meta_att->xml_required;
433 }
434 elsif ( !$meta_att->is_required ) {
435  
436 # it's optional
437 33 $is_optional = 1;
438 }
439  
440 # we /could/ use $meta_att->get_value($item)
441 # here, but I consider that to break
442 # encapsulation
443 83 my $value = $item->$obj_att_name;
444 83 my $xml_att_name = $attName;
445 83 ▶ if ( $meta_att->has_xml_name ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($meta_att->has_xml_name) 31 52 83
446 31 my $method = $meta_att->has_xmlns_attr;
447 31 $xml_att_name = $attName;
448 }
449 83 ▶ if ( $meta_att->has_xmlns_attr ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($meta_att->has_xmlns_attr) 14 69 83
450 14 my $method = $meta_att->xmlns_attr;
451 14 $xmlns = $item->$method;
452 }
453 83 ▶ if ( !defined $value ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 35 48 83
454 35 ▶ die "could not serialize $item; slot "
Branch: 1 branch
BranchTrueFalseTotal
unless $is_optional 0 35 35
455 .$meta_att->name." empty"
456 unless $is_optional;
457 35 next;
458 }
459  
460 my $emit_att = sub {
461 58 ▶ my ($xmlns, $name, $value) = @_;
Subroutine __ANON__: called CC=3 SCAR=11.0
462 58 my $prefix;
463 58 ▶ if ($xmlns) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xmlns) { } 10 48 58
464 10 $prefix = $ctx->get_prefix(
465 $xmlns, $item, $node,
466 );
467 10 ▶ if ( length $prefix ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $prefix) 10 0 10
468 10 $prefix .= ":";
469 }
470 }
471 else {
472 48 $prefix = "";
473 }
474 58 $node->setAttribute(
475 $prefix.$name, $value,
476 );
477 48 };
478  
479 my $do_array = sub {
480 3 ▶ my $att_name = shift;
Subroutine __ANON__: called CC=3 SCAR=11.0
481 3 my $array = shift;
482 3 my $xmlns = shift;
483 3 ▶ for ( my $i = 0; $i <= $#$array; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $array->$#*) 9 3 12
484 9 ▶ $emit_att->(
Condition66%2/3
$xmlns && $xmlns->[$i]
!ll&&!rl&&r
036
MC/DC50%1/2
$xmlns && $xmlns->[$i]
$xmlns $xmlns->[$i]
Truth table66%2/3
A && B
A= $xmlnsB= $xmlns->[$i]
ABresult
0X0
100
111
485 $xmlns&&$xmlns->[$i],
486 $att_name,
487 $array->[$i],
488 );
489 }
490 48 };
491  
492 48 ▶ if ( ref $value eq "HASH" ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $value eq 'HASH') { } 7 41 48
elsif (ref $value eq 'ARRAY') { } 1 40 41
493  
494 # wildcarded attribute name case
495 7 ▶ while ( my ($att, $val) = each %$value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$value) 11 7 18
496 11 my $att_xmlns;
497 11 ▶ if ($xmlns) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xmlns) 8 3 11
498 8 $att_xmlns = $xmlns->{$att};
499 }
500  
501 # now, we can *still* have arrays here..
502 11 ▶ if ( ref $val eq "ARRAY" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val eq 'ARRAY') { } 2 9 11
503 2 $do_array->(
504 $att, $val,
505 $att_xmlns,
506 );
507 }
508 else {
509 9 $emit_att->(
510 $att_xmlns,
511 $att, $val,
512 );
513 }
514 }
515 }
516 elsif ( ref $value eq "ARRAY" ) {
517 1 $do_array->(
518 $xml_att_name,
519 $value,
520 $xmlns,
521 );
522 }
523 else {
524 40 $emit_att->( $xmlns, $xml_att_name, $value );
525 }
526 }
527 }
528 }
529  
530 sub to_libxml {
531 102 ▶ my $self = shift;
Subroutine to_libxml: called CC=1 SCAR=0.0
Pod documented
532
533 102 my ( $item, $node, $ctx ) = pos_validated_list(
534 \@_,
535 { isa => 'Object' },
536 { isa => 'XML::LibXML::Element' },
537 { isa => 'PRANG::Graph::Context' },
538 );
539
540
541 102 $self->add_xml_attr($item, $node, $ctx);
542 102 $self->graph->output($item, $node, $ctx);
543 }
544  
545 package Moose::Meta::Class::Custom::Trait::PRANG;
546 11 $Moose::Meta::Class::Custom::Trait::PRANG::VERSION = '0.19';
547 0 ▶sub register_implementation {"PRANG::Graph::Meta::Class"}
Subroutine register_implementation: not called CC=1 SCAR=6.9
548  
549 11 1;
550  
551 __END__
552  
553 =head1 NAME
554  
555 PRANG::Graph::Meta::Class - metaclass metarole for PRANG-enabled classes
556  
557 =head1 SYNOPSIS
558  
559 package MyClass;
560 use Moose;
561 use PRANG::Graph;
562  
563 # - or -
564 package MyClass;
565 use Moose -traits => ["PRANG"];
566  
567 # - or -
568 package MyClass;
569 use Moose;
570 PRANG::Graph::Meta::Class->meta->apply(__PACKAGE__->meta);
571  
572 =head1 DESCRIPTION
573  
574 This role defines class properties and methods for PRANG classes' meta
575 objects. ie, the methods it defines are all to be found in
576 C<YourClass-E<gt>meta>, not C<YourClass>.
577  
578 The L<PRANG::Graph::Meta::Class> object is the centre of the state
579 machine which defines the parsing and emitting rules for your classes.
580 In other words, the I<XML Graph> (see L<PRANG>). Each one corresponds
581 to an XML element (though not all XML elements will require a full
582 object class), and via these objects can be found the lists of
583 elements and attributes which define the XML structure.
584  
585 =head1 ATTRIBUTES
586  
587 =over
588  
589 =item B<HashRef[HashRef[PRANG::Graph::Meta::Attr]] xml_attr>
590  
591 This read-only property maps from XML namespace and localname to a
592 L<PRANG::Graph::Meta::Attr> object, defining the type of that
593 attribute and other things described on its perldoc.
594  
595 The first time it is accessed, it is built - so be sure to carry out
596 any run-time meta magic before parsing or emitting objects of that
597 type.
598  
599 =item B<ArrayRef[PRANG::Graph::Meta::Element] xml_elements>
600  
601 This contains an ordered list of all of the XML elements which exist
602 in this class. See L<PRANG::Graph::Meta::Element>.
603  
604 Like C<xml_attr>, the first time it is accessed it is built. There
605 are currently some problems with ordering and role composition; as the
606 ordering of elements is returned from a moose accessor, but when
607 composing roles into classes, they are applied in any order.
608  
609 =item B<PRANG::Graph::Node graph>
610  
611 The C<graph> property is the acceptor and emitter for the child nodes
612 of this class. See L<PRANG::Graph::Node> for the low-down. This is
613 constructed by a transform on the B<xml_elements> property.
614  
615 =back
616  
617 =head1 METHODS
618  
619 =head2 B<accept_attributes(\@node_attr, $ctx)>
620  
621 =head2 B<accept_childnodes(\@childNodes, $ctx)>
622  
623 =head2 B<marshall_in_element($node, $ctx)>
624  
625 These methods are the parsing machinery, their API is quite subject to
626 change; the methods provided by the L<PRANG::Graph> role are what you
627 should be using, unless you are writing a PRANG extension.
628  
629 =head2 B<add_xml_attr($item, $node, $ctx)>
630  
631 =head2 B<to_libxml($item, $node, $ctx)>
632  
633 Similarly, these are the emitting methods.
634  
635 =head1 SEE ALSO
636  
637 L<PRANG::Graph::Meta::Attr>, L<PRANG::Graph::Meta::Element>,
638 L<PRANG::Graph::Node>
639  
640 =head1 AUTHOR AND LICENCE
641  
642 Development commissioned by NZ Registry Services, and carried out by
643 Catalyst IT - L<http://www.catalyst.net.nz/>
644  
645 Copyright 2009, 2010, NZ Registry Services. This module is licensed
646 under the Artistic License v2.0, which permits relicensing under other
647 Free Software licenses.
648  
649 =cut
650  
« PRANG/Marshaller.pm ↑ Summary PRANG/XMLSchema/Types.pm »