PRANG/Graph/Choice.pm

filter: Statementstmt 94.7% 109 / 115 Branchbran 84.6% 44 / 52 Conditioncond 82.1% 23 / 28 MC/DCmcdc 64.7% 11 / 17 Subroutinesub 91.6% 11 / 12 Podpod 0.0% 0 / 5 totaltotal 86.4% 198 / 229 SCAR 37.8
CC
42
Cov
90%
output
32.3
accept
9.0
expected
3.1
CRAP 43.6
« PRANG/XMLSchema/Types.pm ↑ Summary PRANG/Graph/Context.pm »
1  
2 package PRANG::Graph::Choice;
3 11 $PRANG::Graph::Choice::VERSION = '0.19';
4 11 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use Moose;
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 Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 11 ▶use Moose::Meta::TypeConstraint;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 BEGIN {
11 11 ▶ class_type('Moose::Meta::TypeConstraint');
Subroutine BEGIN: called CC=1 SCAR=0.0
12 }
13  
14 has 'choices' =>
15 is => "ro",
16 isa => "ArrayRef[PRANG::Graph::Node]",
17 0 ▶ default => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
18 11 ;
19  
20 11 has 'attrName' =>
21 is => "ro",
22 isa => "Str",
23 required => 1,
24 ;
25  
26 11 has 'type_map' =>
27 is => "ro",
28 isa => "HashRef[Str|Moose::Meta::TypeConstraint]",
29 predicate => "has_type_map",
30 ;
31  
32 11 has 'type_map_prefix' =>
33 is => "ro",
34 isa => "HashRef[Str]",
35 predicate => "has_type_map_prefix",
36 ;
37  
38 11 has 'xml_nodeName' =>
39 is => "ro",
40 isa => "Str",
41 predicate => "has_xml_nodeName",
42 ;
43  
44 11 has 'name_attr' =>
45 is => "ro",
46 isa => "Str",
47 predicate => "has_name_attr",
48 ;
49  
50 11 has 'xmlns_attr' =>
51 is => "ro",
52 isa => "Str",
53 predicate => "has_xmlns_attr",
54 ;
55  
56 11 has 'xmlns' =>
57 is => "ro",
58 isa => "Str",
59 predicate => "has_xmlns",
60 ;
61
62 sub node_ok {
63 153 ▶ my $self = shift;
Subroutine node_ok: called CC=3 SCAR=11.0
Pod undocumented
64 153 my ( $node, $ctx ) = pos_validated_list(
65 \@_,
66 { isa => 'XML::LibXML::Node' },
67 { isa => 'PRANG::Graph::Context' },
68 );
69
70 153 for my $choice ( @{ $self->choices } ) {
71 294 ▶ if ( defined $choice->node_ok($node, $ctx) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $choice->node_ok($node, $ctx)) 107 187 294
72 107 return 1;
73 }
74 }
75 46 return;
76 }
77  
78 sub accept {
79 121 ▶ my $self = shift;
Subroutine accept: called CC=9 SCAR=22.0
Pod undocumented
80 121 my ( $node, $ctx, $lax ) = pos_validated_list(
81 \@_,
82 { isa => 'XML::LibXML::Node' },
83 { isa => 'PRANG::Graph::Context' },
84 { isa => 'Bool', optional => 1 },
85 );
86  
87 121 ▶ if ($ctx->chosen) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ctx->chosen) 2 119 121
88  
89 # this is a safe exception; the only time this graph
90 # node will be called repeatedly is if it is the root
91 # node for an element, due to the structure of
92 # PRANG::Graph::Context
93 2 $ctx->exception(
94 "Single child node expected, multiple found",
95 $node,
96 );
97 }
98  
99 119 my $num;
100 119 ▶ my $name = $node->isa("XML::LibXML::Text")
Branch: 1 branch
BranchTrueFalseTotal
$node->isa('XML::LibXML::Text') ? : 26 93 119
101 ? ""
102 : $node->localname;
103 119 ▶ my $xmlns = length($name) && $node->namespaceURI;
Condition100%3/3
length $name && $node->namespaceURI
!ll&&!rl&&r
266726
MC/DC100%2/2
length $name && $node->namespaceURI
length $name $node->namespaceURI
Truth table100%3/3
A && B
A= length $nameB= $node->namespaceURI
ABresult
0X0
100
111
104 119 my ($key, $val, $x, $ns);
105 119 for my $choice ( @{ $self->choices } ) {
106 202 $num++;
107 202 ▶ if ( defined $choice->node_ok($node, $ctx) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $choice->node_ok($node, $ctx)) 119 83 202
108 119 ($key, $val, $x, $ns) = $choice->accept($node, $ctx, $lax);
109 }
110 196 ▶ if ($key) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key) 113 83 196
111 113 $ctx->chosen($num);
112 return (
113 113 ▶ $key, $val, $x||eval{$choice->nodeName}||"",
Condition100%5/5
$x || eval { || ''
l!l
8726
$x or eval {
l!l&&r!l&&!r
167126
MC/DC100%2/2
$x || eval { do { $choice->nodeName } } || ''
$x eval { do { $choice->nodeName } }
Truth table100%3/3
A or B
A= $xB= eval { do { $choice->nodeName } }
ABresult
1X1
011
000
114 $ns
115 );
116 }
117 }
118 0 return ();
119 }
120  
121 sub complete {
122 12 ▶ my $self = shift;
Subroutine complete: called CC=1 SCAR=0.0
Pod undocumented
123 12 my ( $ctx ) = pos_validated_list(
124 \@_,
125 { isa => 'PRANG::Graph::Context' },
126 );
127
128 12 $ctx->chosen;
129 }
130  
131 sub expected {
132 3 ▶ my $self = shift;
Subroutine expected: called CC=3 SCAR=11.2
Pod undocumented
133 3 my ( $ctx ) = pos_validated_list(
134 \@_,
135 { isa => 'PRANG::Graph::Context' },
136 );
137
138 3 ▶ if ( my $num = $ctx->chosen ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $num = $ctx->chosen) { } 0 3 3
139 0 return $self->choices->[$num-1]->expected($ctx);
140 }
141 else {
142 3 my @choices;
143 3 for my $choice ( @{$self->choices} ) {
144 6 push @choices, $choice->expected($ctx);
145 }
146 3 return @choices;
147 }
148 }
149  
150 11 our $REGISTRY =
151 Moose::Util::TypeConstraints::get_type_constraint_registry();
152  
153 sub output {
154 49 ▶ my $self = shift;
Subroutine output: called CC=30 SCAR=34.7
Pod undocumented
155
156 # First 3 args positional, rest are named
157 # Because we're making 2 validation calls, we have to use different cache keys
158 49 my ( $item, $node, $ctx ) = pos_validated_list(
159 [@_[0..2]],
160 { isa => 'Object' },
161 { isa => 'XML::LibXML::Element' },
162 { isa => 'PRANG::Graph::Context' },
163 MX_PARAMS_VALIDATE_CACHE_KEY => 'choice-output-positional',
164 );
165
166 49 my ( $value, $slot ) = validated_list(
167 [@_[3..$#_]],
168 value => { isa => 'Item', optional => 1 },
169 slot => { isa => 'Int', optional => 1 },
170 MX_PARAMS_VALIDATE_CACHE_KEY => 'choice-output-named',
171 );
172  
173 49 my $an = $self->attrName;
174 49 ▶ $value //= $item->$an;
Condition66%2/3
$value //= $item->$an
l!l&&r!l&&!r
4540
MC/DC0%0/2
$value //= $item->$an
$value $item->$an
Truth table66%2/3
A //= B
A= $valueB= $item->$an
ABresult
1X1
011
000
175 49 my ($name, $xmlns);
176 49 ▶ if ( $self->has_name_attr || $self->has_xmlns_attr ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->has_name_attr or $self->has_xmlns_attr) { } 13 36 49
elsif ($self->has_type_map) { } 36 0 36
Condition100%3/3
$self->has_name_attr or $self->has_xmlns_attr
l!l&&r!l&&!r
11236
MC/DC100%2/2
$self->has_name_attr or $self->has_xmlns_attr
$self->has_name_attr $self->has_xmlns_attr
Truth table100%3/3
A or B
A= $self->has_name_attrB= $self->has_xmlns_attr
ABresult
1X1
011
000
177 13 ▶ if ( $self->has_name_attr ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_name_attr) { } 11 2 13
178 11 my $x = $self->name_attr;
179 11 $name = $item->$x;
180 11 ▶ if ( defined $slot ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $slot) 9 2 11
181 9 $name = $name->[$slot];
182 }
183 }
184 else {
185 2 ▶ $name = $self->xml_nodeName // $an;
Condition33%1/3
$self->xml_nodeName // $an
l!l&&r!l&&!r
200
MC/DC0%0/2
$self->xml_nodeName // $an
$self->xml_nodeName $an
Truth table33%1/3
A // B
A= $self->xml_nodeNameB= $an
ABresult
1X1
011
000
186 }
187 13 ▶ if ( $self->has_xmlns_attr ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_xmlns_attr) { } 11 2 13
188 11 my $attr_getter = $self->xmlns_attr;
189 11 $xmlns = $item->$attr_getter;
190 11 ▶ if ( defined $slot ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $slot) 9 2 11
191 9 $xmlns = $xmlns->[$slot];
192 }
193 }
194 else {
195 2 ▶ $xmlns = $self->xmlns // "";
Condition50%1/2
$self->xmlns // ""
l!l
20
MC/DC0%0/1
$self->xmlns // ""
$self->xmlns
Truth table50%1/2
A
A= $self->xmlns
Aresult
11
00
196 }
197 }
198 elsif ( $self->has_type_map ) {
199 36 my $map = $self->type_map;
200 36 my $nodeCount = 0;
201 36 for my $element ( keys %$map ) {
202 100 my $type = $map->{$element};
203 100 ▶ if ( !ref $type ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $type) 12 88 100
204 12 $type = $map->{$element} =
205 $REGISTRY->get_type_constraint($type);
206 }
207
208 100 ▶ if ( $type->check($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type->check($value)) 36 64 100
209 36 $name = $element;
210 36 $nodeCount++;
211 }
212
213 100 ▶ if ($nodeCount > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nodeCount > 1) 0 100 100
214 0 $ctx->exception("More than 1 node type matched for value: '$value' " .
215 "You need a stricter type definition for your xml_nodeName map, or PRANG can't figure out which element name to emit");
216 }
217 }
218 }
219
220 49 ▶ if ( !defined $name ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $name) 0 49 49
221 0 $ctx->exception(
222 "don't know what to serialize $value to for slot "
223 .$self->attrName
224 );
225 }
226 49 ▶ if ( length $name ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $name) { } 37 12 49
227 37 ▶ if ( $self->has_type_map_prefix and $name =~ /(.*):(.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_type_map_prefix and $name =~ /(.*):(.*)/) 2 35 37
Condition66%2/3
$self->has_type_map_prefix and $name =~ /(.*):(.*)/
!ll&&!rl&&r
3502
MC/DC50%1/2
$self->has_type_map_prefix and $name =~ /(.*):(.*)/
$self->has_type_map_prefix $name =~ /(.*):(.*)/
Truth table66%2/3
A and B
A= $self->has_type_map_prefixB= $name =~ /(.*):(.*)/
ABresult
0X0
100
111
228 2 $name = $2;
229 2 $xmlns = $self->type_map_prefix->{$1};
230 }
231 37 my $found;
232 37 for my $choice ( @{ $self->choices } ) {
233 51 ▶ if ($xmlns) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xmlns) 9 42 51
234 9 ▶ next unless $choice->has_xmlns;
Branch: 1 branch
BranchTrueFalseTotal
unless $choice->has_xmlns 0 9 9
235 9 ▶ next unless $choice->xmlns eq $xmlns or
Branch: 1 branch
BranchTrueFalseTotal
unless $choice->xmlns eq $xmlns or $choice->xmlns eq "*" 2 7 9
Condition100%3/3
$choice->xmlns eq $xmlns or $choice->xmlns eq "*"
l!l&&r!l&&!r
432
MC/DC100%2/2
$choice->xmlns eq $xmlns or $choice->xmlns eq "*"
$choice->xmlns eq $xmlns $choice->xmlns eq "*"
Truth table100%3/3
A or B
A= $choice->xmlns eq $xmlnsB= $choice->xmlns eq "*"
ABresult
1X1
011
000
236 $choice->xmlns eq "*";
237 }
238 49 ▶ next unless $choice->nodeName eq $name or
Branch: 1 branch
BranchTrueFalseTotal
unless $choice->nodeName eq $name or $choice->nodeName eq "*" 12 37 49
Condition100%3/3
$choice->nodeName eq $name or $choice->nodeName eq "*"
l!l&&r!l&&!r
31612
MC/DC100%2/2
$choice->nodeName eq $name or $choice->nodeName eq "*"
$choice->nodeName eq $name $choice->nodeName eq "*"
Truth table100%3/3
A or B
A= $choice->nodeName eq $nameB= $choice->nodeName eq "*"
ABresult
1X1
011
000
239 $choice->nodeName eq "*";
240 37 $found++;
241 37 ▶ $choice->output(
Branch: 2 branches
BranchTrueFalseTotal
defined $slot ? : 25 12 37
defined $xmlns ? : 12 25 37
242 $item,$node,$ctx,
243 value => $value,
244 (defined $slot ? (slot => $slot) : ()),
245 name => $name,
246 (defined $xmlns ? (xmlns => $xmlns) : ()),
247 );
248 37 last;
249 }
250 37 ▶ if ( !$found ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $found) 0 37 37
251 0 ▶ $ctx->exception(
Branch: 1 branch
BranchTrueFalseTotal
$xmlns ? : 0 0 0
252 "don't know what to serialize $value to for slot "
253 .$self->attrName
254 ." (looked for '$name' node"
255 .(
256 $xmlns?" xmlns='$xmlns'":""
257 )
258 .")",
259 );
260 }
261 }
262 else {
263  
264 # textnode ... jfdi
265 12 my $tn = $self->createTextNode($node->ownerDocument, $value);
266 12 $node->appendChild($tn);
267 }
268 }
269  
270 11 with 'PRANG::Graph::Node';
271  
272 11 1;
273  
274 __END__
275  
276 =head1 NAME
277  
278 PRANG::Graph::Choice - accept multiple discrete node types
279  
280 =head1 SYNOPSIS
281  
282 See L<PRANG::Graph::Meta::Element> source and
283 L<PRANG::Graph::Node> for examples and information.
284  
285 =head1 DESCRIPTION
286  
287 This graph node specifies that the XML graph at this point may be one
288 of a list of text nodes or elements, depending on the type of entries
289 in the B<choices> property.
290  
291 If there is only one type of node allowed then the element does not
292 have one of these objects in their graph.
293  
294 =head1 ATTRIBUTES
295  
296 =over
297  
298 =item B<ArrayRef[PRANG::Graph::Node] choices>
299  
300 This property provides the next portion of the XML Graph. Depending
301 on the type of entry, it will accept and emit nodes of a particular
302 type.
303  
304 Entries must be one of L<PRANG::Graph::Element>, or
305 L<PRANG::Graph::Text>.
306  
307 =item B<HashRef[Str|Moose::Meta::TypeConstraint] type_map>
308  
309 This map is used for emitting. It maps from the localname of the XML
310 node to the type which that localname is appropriate for. This map
311 also needs to include the XML namespace, that it doesn't is currently a bug.
312  
313 =item B<Str name_attr>
314  
315 Used when emitting; avoid type-based selection and instead retrieve
316 the name of the XML node from this attribute.
317  
318 =item B<attrName>
319  
320 Used when emitting; specifies the method to call to retrieve the item
321 to be output.
322  
323 =back
324  
325 =head1 SEE ALSO
326  
327 L<PRANG::Graph::Meta::Class>, L<PRANG::Graph::Meta::Element>,
328 L<PRANG::Graph::Context>, L<PRANG::Graph::Node>
329  
330 Lower order L<PRANG::Graph::Node> types:
331  
332 L<PRANG::Graph::Element>, L<PRANG::Graph::Text>
333  
334 =head1 AUTHOR AND LICENCE
335  
336 Development commissioned by NZ Registry Services, and carried out by
337 Catalyst IT - L<http://www.catalyst.net.nz/>
338  
339 Copyright 2009, 2010, NZ Registry Services. This module is licensed
340 under the Artistic License v2.0, which permits relicensing under other
341 Free Software licenses.
342  
343 =cut
344  
« PRANG/XMLSchema/Types.pm ↑ Summary PRANG/Graph/Context.pm »