PRANG/Graph/Choice.pm

filter: Statementstmt 28.8% 32 / 111 Branchbran 0.0% 0 / 50 Conditioncond 0.0% 0 / 28 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 60.0% 9 / 15 Podpod n/a totaltotal 18.5% 41 / 221 CC 41 SCAR 61.3
CC
41
Cov
17%
__ANON__
846.0
__ANON__
83.1
__ANON__
9.8
CRAP 460.9
« PRANG/Graph/Element.pm ↑ Summary PRANG/Graph/Meta/Class.pm »
1  
2 package PRANG::Graph::Choice;
3  
4 9 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 ▶use MooseX::Method::Signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 has 'choices' =>
10 is => "ro",
11 isa => "ArrayRef[PRANG::Graph::Node]",
12 0 ▶ default => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
13 9 ;
14  
15 9 has 'attrName' =>
16 is => "ro",
17 isa => "Str",
18 required => 1,
19 ;
20  
21 9 has 'type_map' =>
22 is => "ro",
23 isa => "HashRef[Str|Moose::Meta::TypeConstraint]",
24 predicate => "has_type_map",
25 ;
26  
27 9 has 'type_map_prefix' =>
28 is => "ro",
29 isa => "HashRef[Str]",
30 predicate => "has_type_map_prefix",
31 ;
32  
33 9 has 'xml_nodeName' =>
34 is => "ro",
35 isa => "Str",
36 predicate => "has_xml_nodeName",
37 ;
38  
39 9 has 'name_attr' =>
40 is => "ro",
41 isa => "Str",
42 predicate => "has_name_attr",
43 ;
44  
45 9 has 'xmlns_attr' =>
46 is => "ro",
47 isa => "Str",
48 predicate => "has_xmlns_attr",
49 ;
50  
51 9 has 'xmlns' =>
52 is => "ro",
53 isa => "Str",
54 predicate => "has_xmlns",
55 ;
56  
57 9 ▶method node_ok( XML::LibXML::Node $node, PRANG::Graph::Context $ctx ) {
Subroutine BEGIN: called CC=1 SCAR=1.2
Subroutine __ANON__: not called CC=3 SCAR=22.3
58 0 for my $choice ( @{ $self->choices } ) {
59 0 ▶ if ( defined $choice->node_ok($node, $ctx) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $choice->node_ok($node, $ctx)) 0 0 0
60 0 return 1;
61 }
62 }
63 0 return;
64 9 }
65  
66 9 ▶method accept( XML::LibXML::Node $node, PRANG::Graph::Context $ctx ) {
Subroutine BEGIN: called CC=1 SCAR=1.2
Subroutine __ANON__: not called CC=9 SCAR=44.2
67  
68 0 ▶ if ($ctx->chosen) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ctx->chosen) 0 0 0
69 # this is a safe exception; the only time this graph
70 # node will be called repeatedly is if it is the root
71 # node for an element, due to the structure of
72 # PRANG::Graph::Context
73 0 $ctx->exception(
74 "Single child node expected, multiple found",
75 $node,
76 );
77 }
78  
79 0 my $num;
80 0 ▶ my $name = $node->isa("XML::LibXML::Text") ? ""
Branch: 1 branch
BranchTrueFalseTotal
$node->isa('XML::LibXML::Text') ? : 0 0 0
81 : $node->localname;
82 0 ▶ my $xmlns = length($name) && $node->namespaceURI;
Condition0%0/3
length $name && $node->namespaceURI
!ll&&!rl&&r
000
MC/DC0%0/2
length $name && $node->namespaceURI
length $name $node->namespaceURI
Truth table0%0/3
A && B
A= length $nameB= $node->namespaceURI
ABresult
0X0
100
111
83 0 my ($key, $val, $x, $ns);
84 0 for my $choice ( @{ $self->choices } ) {
85 0 $num++;
86 0 ▶ if ( defined $choice->node_ok($node, $ctx) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $choice->node_ok($node, $ctx)) 0 0 0
87 0 ($key, $val, $x, $ns) = $choice->accept($node, $ctx);
88 }
89 0 ▶ if ( $key ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key) 0 0 0
90 0 $ctx->chosen($num);
91 0 ▶ return ($key, $val, $x||eval{$choice->nodeName}||"",
Condition0%0/5
$x || eval { || ''
l!l
00
$x or eval {
l!l&&r!l&&!r
000
MC/DC0%0/2
$x || eval { do { $choice->nodeName } } || ''
$x eval { do { $choice->nodeName } }
Truth table0%0/3
A or B
A= $xB= eval { do { $choice->nodeName } }
ABresult
1X1
011
000
92 $ns);
93 }
94 }
95 0 return ();
96 9 }
97  
98 9 ▶method complete( PRANG::Graph::Context $ctx ) {
Subroutine BEGIN: called CC=1 SCAR=1.2
Subroutine __ANON__: not called CC=1 SCAR=2.6
99 0 $ctx->chosen;
100 9 }
101  
102 9 ▶method expected( PRANG::Graph::Context $ctx ) {
Subroutine BEGIN: called CC=1 SCAR=1.2
Subroutine __ANON__: not called CC=3 SCAR=22.8
103 0 ▶ if ( my $num = $ctx->chosen ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $num = $ctx->chosen) { } 0 0 0
104 0 return $self->choices->[$num-1]->expected($ctx);
105 }
106 else {
107 0 my @choices;
108 0 for my $choice ( @{$self->choices} ) {
109 0 push @choices, $choice->expected($ctx);
110 }
111 0 return @choices;
112 }
113 9 }
114  
115 9 our $REGISTRY = Moose::Util::TypeConstraints::get_type_constraint_registry();
116  
117 9 ▶method output ( Object $item, XML::LibXML::Element $node, PRANG::Graph::Context $ctx, Item :$value, Int :$slot ) {
Subroutine BEGIN: called CC=1 SCAR=1.2
Subroutine __ANON__: not called CC=29 SCAR=67.4
118  
119 0 my $an = $self->attrName;
120 0 ▶ $value //= $item->$an;
Condition0%0/3
$value //= $item->$an
l!l&&r!l&&!r
000
MC/DC0%0/2
$value //= $item->$an
$value $item->$an
Truth table0%0/3
A //= B
A= $valueB= $item->$an
ABresult
1X1
011
000
121 0 my ($name, $xmlns);
122 0 ▶ if ( $self->has_name_attr || $self->has_xmlns_attr ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->has_name_attr or $self->has_xmlns_attr) { } 0 0 0
elsif ($self->has_type_map) { } 0 0 0
Condition0%0/3
$self->has_name_attr or $self->has_xmlns_attr
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->has_name_attr or $self->has_xmlns_attr
$self->has_name_attr $self->has_xmlns_attr
Truth table0%0/3
A or B
A= $self->has_name_attrB= $self->has_xmlns_attr
ABresult
1X1
011
000
123 0 ▶ if ( $self->has_name_attr ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_name_attr) { } 0 0 0
124 0 my $x = $self->name_attr;
125 0 $name = $item->$x;
126 0 ▶ if ( defined $slot ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $slot) 0 0 0
127 0 $name = $name->[$slot];
128 }
129 }
130 else {
131 0 ▶ $name = $self->xml_nodeName // $an;
Condition0%0/3
$self->xml_nodeName // $an
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->xml_nodeName // $an
$self->xml_nodeName $an
Truth table0%0/3
A // B
A= $self->xml_nodeNameB= $an
ABresult
1X1
011
000
132 }
133 0 ▶ if ( $self->has_xmlns_attr ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_xmlns_attr) { } 0 0 0
134 0 my $attr_getter = $self->xmlns_attr;
135 0 $xmlns = $item->$attr_getter;
136 0 ▶ if ( defined $slot ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $slot) 0 0 0
137 0 $xmlns = $xmlns->[$slot];
138 }
139 }
140 else {
141 0 ▶ $xmlns = $self->xmlns // "";
Condition0%0/2
$self->xmlns // ""
l!l
00
MC/DC0%0/1
$self->xmlns // ""
$self->xmlns
Truth table0%0/2
A
A= $self->xmlns
Aresult
11
00
142 }
143 }
144 elsif ( $self->has_type_map ) {
145 0 my $map = $self->type_map;
146 0 for my $element ( keys %$map ) {
147 0 my $type = $map->{$element};
148 0 ▶ if ( ! ref $type ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $type) 0 0 0
149 0 $type = $map->{$element} =
150 $REGISTRY->get_type_constraint($type);
151 }
152 0 ▶ if ( $type->check($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type->check($value)) 0 0 0
153 0 $name = $element;
154 0 last;
155 }
156 }
157 }
158 0 ▶ if ( !defined $name ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $name) 0 0 0
159 0 $ctx->exception("don't know what to serialize $value to for slot ".$self->attrName);
160 }
161 0 ▶ if ( length $name ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $name) { } 0 0 0
162 0 ▶ if ( $self->has_type_map_prefix and $name =~ /(.*):(.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_type_map_prefix and $name =~ /(.*):(.*)/) 0 0 0
Condition0%0/3
$self->has_type_map_prefix and $name =~ /(.*):(.*)/
!ll&&!rl&&r
000
MC/DC0%0/2
$self->has_type_map_prefix and $name =~ /(.*):(.*)/
$self->has_type_map_prefix $name =~ /(.*):(.*)/
Truth table0%0/3
A and B
A= $self->has_type_map_prefixB= $name =~ /(.*):(.*)/
ABresult
0X0
100
111
163 0 $name = $2;
164 0 $xmlns = $self->type_map_prefix->{$1};
165 }
166 0 my $found;
167 0 for my $choice ( @{ $self->choices } ) {
168 0 ▶ if ( $xmlns ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xmlns) 0 0 0
169 0 ▶ next unless $choice->has_xmlns;
Branch: 1 branch
BranchTrueFalseTotal
unless $choice->has_xmlns 0 0 0
170 0 ▶ next unless $choice->xmlns eq $xmlns or
Branch: 1 branch
BranchTrueFalseTotal
unless $choice->xmlns eq $xmlns or $choice->xmlns eq "*" 0 0 0
Condition0%0/3
$choice->xmlns eq $xmlns or $choice->xmlns eq "*"
l!l&&r!l&&!r
000
MC/DC0%0/2
$choice->xmlns eq $xmlns or $choice->xmlns eq "*"
$choice->xmlns eq $xmlns $choice->xmlns eq "*"
Truth table0%0/3
A or B
A= $choice->xmlns eq $xmlnsB= $choice->xmlns eq "*"
ABresult
1X1
011
000
171 $choice->xmlns eq "*";
172 }
173 0 ▶ next unless $choice->nodeName eq $name or
Branch: 1 branch
BranchTrueFalseTotal
unless $choice->nodeName eq $name or $choice->nodeName eq "*" 0 0 0
Condition0%0/3
$choice->nodeName eq $name or $choice->nodeName eq "*"
l!l&&r!l&&!r
000
MC/DC0%0/2
$choice->nodeName eq $name or $choice->nodeName eq "*"
$choice->nodeName eq $name $choice->nodeName eq "*"
Truth table0%0/3
A or B
A= $choice->nodeName eq $nameB= $choice->nodeName eq "*"
ABresult
1X1
011
000
174 $choice->nodeName eq "*";
175 0 $found++;
176 0 ▶ $choice->output(
Branch: 2 branches
BranchTrueFalseTotal
defined $slot ? : 0 0 0
defined $xmlns ? : 0 0 0
177 $item,$node,$ctx,
178 value => $value,
179 (defined $slot ? (slot => $slot) : ()),
180 name => $name,
181 (defined $xmlns ? (xmlns => $xmlns) : ()),
182 );
183 0 last;
184 }
185 0 ▶ if ( !$found ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $found) 0 0 0
186 0 ▶ $ctx->exception(
Branch: 1 branch
BranchTrueFalseTotal
$xmlns ? : 0 0 0
187 "don't know what to serialize $value to for slot ".$self->attrName
188 ." (looked for '$name' node".($xmlns?" xmlns='$xmlns'":"").")",
189 );
190 }
191 }
192 else {
193 # textnode ... jfdi
194 0 my $tn = $node->ownerDocument->createTextNode($value);
195 0 $node->appendChild($tn);
196 }
197 9 }
198  
199 9 with 'PRANG::Graph::Node';
200  
201 9 1;
202  
203 __END__
204  
205 =head1 NAME
206  
207 PRANG::Graph::Choice - accept multiple discrete node types
208  
209 =head1 SYNOPSIS
210  
211 See L<PRANG::Graph::Meta::Element> source and
212 L<PRANG::Graph::Node> for examples and information.
213  
214 =head1 DESCRIPTION
215  
216 This graph node specifies that the XML graph at this point may be one
217 of a list of text nodes or elements, depending on the type of entries
218 in the B<choices> property.
219  
220 If there is only one type of node allowed then the element does not
221 have one of these objects in their graph.
222  
223 =head1 ATTRIBUTES
224  
225 =over
226  
227 =item B<ArrayRef[PRANG::Graph::Node] choices>
228  
229 This property provides the next portion of the XML Graph. Depending
230 on the type of entry, it will accept and emit nodes of a particular
231 type.
232  
233 Entries must be one of L<PRANG::Graph::Element>, or
234 L<PRANG::Graph::Text>.
235  
236 =item B<HashRef[Str|Moose::Meta::TypeConstraint] type_map>
237  
238 This map is used for emitting. It maps from the localname of the XML
239 node to the type which that localname is appropriate for. This map
240 also needs to include the XML namespace, that it doesn't is currently a bug.
241  
242 =item B<Str name_attr>
243  
244 Used when emitting; avoid type-based selection and instead retrieve
245 the name of the XML node from this attribute.
246  
247 =item B<attrName>
248  
249 Used when emitting; specifies the method to call to retrieve the item
250 to be output.
251  
252 =back
253  
254 =head1 SEE ALSO
255  
256 L<PRANG::Graph::Meta::Class>, L<PRANG::Graph::Meta::Element>,
257 L<PRANG::Graph::Context>, L<PRANG::Graph::Node>
258  
259 Lower order L<PRANG::Graph::Node> types:
260  
261 L<PRANG::Graph::Element>, L<PRANG::Graph::Text>
262  
263 =head1 AUTHOR AND LICENCE
264  
265 Development commissioned by NZ Registry Services, and carried out by
266 Catalyst IT - L<http://www.catalyst.net.nz/>
267  
268 Copyright 2009, 2010, NZ Registry Services. This module is licensed
269 under the Artistic License v2.0, which permits relicensing under other
270 Free Software licenses.
271  
272 =cut
273  
« PRANG/Graph/Element.pm ↑ Summary PRANG/Graph/Meta/Class.pm »