PRANG/Graph/Context.pm

filter: Statementstmt 94.2% 131 / 139 Branchbran 80.4% 37 / 46 Conditioncond 58.6% 17 / 29 MC/DCmcdc 33.3% 6 / 18 Subroutinesub 92.0% 23 / 25 Podpod 60.0% 6 / 10 totaltotal 82.4% 220 / 267 SCAR 36.1
CC
34
Cov
86%
show_node
17.3
next_ctx
8.2
thing_xmlns
5.2
CRAP 36.9
« PRANG/Graph/Choice.pm ↑ Summary PRANG/Graph/Quantity.pm »
1  
2 package PRANG::Graph::Context;
3 11 $PRANG::Graph::Context::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  
9 BEGIN {
10 11 ▶ class_type "XML::LibXML::Element";
Subroutine BEGIN: called CC=1 SCAR=0.0
11 }
12  
13 has 'seq_pos' =>
14 is => "rw",
15 isa => "Int",
16 lazy => 1,
17 default => 1,
18 trigger => sub {
19 698 ▶ my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
20 698 $self->clear_quant;
21 698 $self->clear_chosen;
22 698 $self->clear_element_ok;
23 },
24 11 clearer => "clear_seq_pos",
25 ;
26  
27 sub reset {
28 0 ▶ my $self = shift;
Subroutine reset: not called CC=1 SCAR=6.9
Pod undocumented
29
30 0 $self->clear_seq_pos;
31 }
32  
33 has 'quant_found' =>
34 is => "rw",
35 isa => "Int",
36 lazy => 1,
37 default => 0,
38 clearer => 'clear_quant',
39 trigger => sub {
40 293 ▶ my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
41 293 $self->clear_chosen;
42 293 $self->clear_element_ok;
43 },
44 11 ;
45  
46 has 'chosen' =>
47 is => "rw",
48 isa => "Int",
49 clearer => "clear_chosen",
50 trigger => sub {
51 113 ▶ $_[0]->clear_element_ok;
Subroutine __ANON__: called CC=1 SCAR=0.0
52 }
53 11 ;
54  
55 11 has 'element_ok' =>
56 is => "rw",
57 isa => "Bool",
58 clearer => "clear_element_ok",
59 ;
60  
61 # For recursion, we need to know a couple of extra things.
62 11 has 'base' =>
63 is => "ro",
64 isa => 'PRANG::Marshaller',
65 ;
66  
67 11 has 'xpath' =>
68 is => "ro",
69 isa => "Str",
70 ;
71  
72 has 'xsi' =>
73 is => "rw",
74 isa => "HashRef",
75 0 ▶ default => sub { {} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
76 11 ;
77  
78 has 'old_xsi' =>
79 is => "rw",
80 isa => "HashRef",
81 506 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
82 11 ;
83  
84 has 'rxsi' =>
85 is => "rw",
86 isa => "HashRef",
87 lazy => 1,
88 default => sub {
89 99 ▶ my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
90 99 +{ reverse %{ $self->xsi } };
91 },
92 11 ;
93  
94 11 has 'xsi_virgin' =>
95 is => "rw",
96 isa => "Bool",
97 default => 1,
98 ;
99  
100 sub thing_xmlns {
101 20 ▶ my $thing = shift;
Subroutine thing_xmlns: called CC=4 SCAR=16.4
Pod undocumented
102 20 ▶ return unless blessed $thing;
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($thing) 0 20 20
103 20 my $xmlns = shift;
104 20 ▶ if ( $thing->can("preferred_prefix") ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($thing->can('preferred_prefix')) { } 0 20 20
elsif ($thing->can('xmlns_prefix')) { } 0 20 20
105 0 $thing->preferred_prefix($xmlns);
106 }
107 elsif ( $thing->can("xmlns_prefix") ) {
108 0 $thing->xmlns_prefix($xmlns);
109 }
110 }
111  
112 sub next_ctx {
113 394 ▶ my $self = shift;
Subroutine next_ctx: called CC=8 SCAR=21.0
Pod documented
114 394 my ( $xmlns, $newnode_name, $thing) = pos_validated_list(
115 \@_,
116 { isa => 'Maybe[Str]' },
117 { isa => 'Maybe[Str]' },
118 { optional => 1 },
119 );
120
121 394 my $prefix = $self->prefix;
122 394 my $new_prefix;
123 394 ▶ if ($xmlns) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xmlns) 55 339 394
124 55 ▶ if ( !exists $self->rxsi->{$xmlns} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $self->rxsi->{$xmlns}) { } 11 44 55
125 11 $new_prefix = 1;
126 11 ▶ $prefix = thing_xmlns($thing, $xmlns) //
Condition33%1/3
thing_xmlns($thing, $xmlns) // $self->base->generate_prefix($xmlns)
l!l&&r!l&&!r
0110
MC/DC0%0/2
thing_xmlns($thing, $xmlns) // $self->base->generate_prefix($xmlns)
thing_xmlns($thing, $xmlns) $self->base->generate_prefix($xmlns)
Truth table33%1/3
A // B
A= thing_xmlns($thing, $xmlns)B= $self->base->generate_prefix($xmlns)
ABresult
1X1
011
000
127 $self->base->generate_prefix($xmlns);
128 }
129 else {
130 44 $prefix = $self->get_prefix($xmlns);
131 }
132 }
133 394 ▶ my $nodename = (($newnode_name && $prefix) ? "$prefix:" : "") .
Branch: 1 branch
BranchTrueFalseTotal
$newnode_name && $prefix ? : 103 291 394
Condition60%3/5
$newnode_name and $prefix
!ll&&!rl&&r
0291103
$newnode_name || 'text()'
l!l
3940
MC/DC50%1/2
$newnode_name and $prefix
$newnode_name $prefix
MC/DC0%0/1
$newnode_name || 'text()'
$newnode_name
Truth table66%2/3
A and B
A= $newnode_nameB= $prefix
ABresult
0X0
100
111
Truth table50%1/2
A
A= $newnode_name
Aresult
11
00
134 ($newnode_name||"text()");
135  
136 394 my $clone = (ref $self)->new(
137 prefix => $prefix,
138 base => $self->base,
139 xpath => $self->xpath."/".$nodename,
140 xsi => $self->xsi,
141 rxsi => $self->rxsi,
142 );
143 394 ▶ if ($new_prefix) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_prefix) 11 383 394
144 11 $clone->add_xmlns($prefix, $xmlns);
145 }
146 394 $clone;
147 }
148  
149 sub prefix_new {
150 152 ▶ my $self = shift;
Subroutine prefix_new: called CC=2 SCAR=7.0
Pod documented
151 152 my ( $prefix) = pos_validated_list(
152 \@_,
153 { isa => 'Str' },
154 );
155
156 152 ▶ !$self->xsi_virgin and not exists $self->old_xsi->{$prefix};
Condition66%2/3
$self->xsi_virgin || exists $self->old_xsi->{$prefix}
l!l&&r!l&&!r
141011
MC/DC50%1/2
$self->xsi_virgin || exists $self->old_xsi->{$prefix}
$self->xsi_virgin exists $self->old_xsi->{$prefix}
Truth table66%2/3
A || B
A= $self->xsi_virginB= exists $self->old_xsi->{$prefix}
ABresult
1X1
011
000
157 }
158  
159 # this one is to know if the prefix was different to the parent type.
160 11 has 'prefix' =>
161 is => "ro",
162 isa => "Str",
163 ;
164  
165 11 ▶BEGIN { class_type "XML::LibXML::Node" }
Subroutine BEGIN: called CC=1 SCAR=0.0
166  
167 sub get_prefix {
168 54 ▶ my $self = shift;
Subroutine get_prefix: called CC=4 SCAR=14.1
Pod documented
169 54 my ( $xmlns, $thing, $victim ) = pos_validated_list(
170 \@_,
171 { isa => 'Str' },
172 { isa => 'Object', optional => 1 },
173 { isa => 'XML::LibXML::Element', optional => 1 },
174 );
175
176 54 ▶ if ( defined(my $prefix = $self->rxsi->{$xmlns}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $prefix = $self->rxsi->{$xmlns})) { } 45 9 54
177 45 $prefix;
178 }
179 else {
180 9 ▶ my $new_prefix = thing_xmlns($thing, $xmlns)
Condition33%1/3
thing_xmlns($thing, $xmlns) // $self->base->generate_prefix($xmlns)
l!l&&r!l&&!r
090
MC/DC0%0/2
thing_xmlns($thing, $xmlns) // $self->base->generate_prefix($xmlns)
thing_xmlns($thing, $xmlns) $self->base->generate_prefix($xmlns)
Truth table33%1/3
A // B
A= thing_xmlns($thing, $xmlns)B= $self->base->generate_prefix($xmlns)
ABresult
1X1
011
000
181 // $self->base->generate_prefix($xmlns);
182 9 $self->add_xmlns($new_prefix, $xmlns);
183 9 ▶ if ($victim) {
Branch: 1 branch
BranchTrueFalseTotal
if ($victim) 9 0 9
184 9 $victim->setAttribute(
185 "xmlns:".$new_prefix,
186 $xmlns,
187 );
188 }
189 9 $new_prefix;
190 }
191 }
192  
193 sub add_xmlns {
194 77 ▶ my $self = shift;
Subroutine add_xmlns: called CC=3 SCAR=11.0
Pod documented
195 77 my ( $prefix, $xmlns ) = pos_validated_list(
196 \@_,
197 { isa => 'Str' },
198 { isa => 'Str' },
199 );
200
201 77 ▶ if ( $self->xsi_virgin ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->xsi_virgin) { } 38 39 77
202 38 $self->xsi_virgin(0);
203 38 $self->old_xsi($self->xsi);
204 38 $self->xsi({ %{$self->xsi}, $prefix => $xmlns });
205 38 ▶ if ( $self->rxsi ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->rxsi) 38 0 38
206 38 $self->rxsi({ %{$self->rxsi}, $xmlns => $prefix });
207 }
208 }
209 else {
210 39 $self->xsi->{$prefix} = $xmlns;
211 39 $self->rxsi->{$xmlns} = $prefix;
212 }
213 }
214  
215 sub get_xmlns{
216 24 ▶ my $self = shift;
Subroutine get_xmlns: called CC=1 SCAR=0.0
Pod documented
217 24 my ( $prefix, ) = pos_validated_list(
218 \@_,
219 { isa => 'Str' },
220 );
221
222 24 $self->xsi->{$prefix};
223 }
224  
225 # this is a very convenient class to put a rich and useful exception
226 # method on; all important methods use it, and it has just the
227 # information to make the error message very useful.
228 sub exception {
229 26 ▶ my $self = shift;
Subroutine exception: called CC=3 SCAR=11.0
Pod documented
230 26 my ( $message, $node, $skip_ok ) = pos_validated_list(
231 \@_,
232 { isa => 'Str' },
233 { isa => 'XML::LibXML::Node', optional => 1 },
234 { isa => 'Bool', optional => 1 },
235 );
236
237
238 26 ▶ my $error = PRANG::Graph::Context::Error->new(
Branch: 2 branches
BranchTrueFalseTotal
$node ? : 17 9 26
$skip_ok ? : 0 26 26
239 ($node ? (node => $node) : ()),
240 message => $message,
241 xpath => $self->xpath,
242 ($skip_ok ? (skip_ok => 1) : ()),
243 );
244 26 die $error;
245 }
246  
247 package PRANG::Graph::Context::Error;
248 11 $PRANG::Graph::Context::Error::VERSION = '0.19';
249 11 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
250 11 ▶use MooseX::Params::Validate;
Subroutine BEGIN: called CC=1 SCAR=0.0
251  
252 11 has 'node' =>
253 is => "ro",
254 isa => "XML::LibXML::Node",
255 predicate => "has_node",
256 ;
257  
258 11 has 'message' =>
259 is => "ro",
260 isa => "Str",
261 ;
262  
263 11 has 'xpath' =>
264 is => "ro",
265 isa => "Str",
266 ;
267  
268 11 has 'skip_ok' =>
269 is => "ro",
270 isa => "Bool",
271 ;
272  
273 sub show_node {
274 37 ▶ my $self = shift;
Subroutine show_node: called CC=16 SCAR=28.5
Pod undocumented
275
276 37 ▶ return "" unless $self->has_node;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_node 15 22 37
277 22 my $extra = "";
278 22 my $node = $self->node;
279 22 ▶ if ( $node->isa("XML::LibXML::Element") ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($node->isa('XML::LibXML::Element')) { } 18 4 22
elsif ($node->isa('XML::LibXML::Text')) { } 4 0 4
elsif ($node) { } 0 0 0
280 18 $extra = " (parsing: <".$node->nodeName;
281 18 ▶ if ( $node->hasAttributes ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->hasAttributes) 4 14 18
282 $extra .= join(
283 " ", "",
284 map {
285 4 $_->name."='".$_->value."'"
286 } $node->attributes
287 );
288 }
289 my @nodes = grep {
290 18 ▶ !( $_->isa("XML::LibXML::Comment")
Condition50%3/6
$_->isa('XML::LibXML::Comment') || $_->isa('XML::LibXML::Text') && $_->data =~ /\A\s+\Z/
l!l&&r!l&&!r
0022
$_->isa("XML::LibXML::Text") and $_->data =~ /\A\s+\Z/
!ll&&!rl&&r
10120
MC/DC0%0/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 table50%2/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
291 or
292 $_->isa("XML::LibXML::Text") and $_->data =~ /\A\s+\Z/
293 )
294 } $node->childNodes;
295 18 ▶ if (@nodes > 1
Branch: 3 branches
BranchTrueFalseTotal
if (@nodes > 1 and grep {not $_->isa('XML::LibXML::Element');} @nodes) { } 2 16 18
elsif (@nodes and $nodes[0]->isa('XML::LibXML::Element')) { } 6 10 16
elsif (@nodes and $nodes[0]->isa('XML::LibXML::Text')) { } 6 8 14
Condition66%2/3
@nodes > 1 and grep {not $_->isa("XML::LibXML::Element");} @nodes
!ll&&!rl&&r
1602
MC/DC50%1/2
@nodes > 1 and grep {not $_->isa("XML::LibXML::Element");} @nodes
@nodes > 1 grep {not $_->isa("XML::LibXML::Element");} @nodes
Truth table66%2/3
A and B
A= @nodes > 1B= grep {not $_->isa("XML::LibXML::Element");} @nodes
ABresult
0X0
100
111
296 10 and grep { !$_->isa("XML::LibXML::Element") }
297 @nodes
298 )
299 2 { $extra .= ">(mixed content)";
300 }
301 ▶ elsif (@nodes and $nodes[0]->isa("XML::LibXML::Element")) {
Condition100%3/3
@nodes and $nodes[0]->isa("XML::LibXML::Element")
!ll&&!rl&&r
466
MC/DC100%2/2
@nodes and $nodes[0]->isa("XML::LibXML::Element")
@nodes $nodes[0]->isa("XML::LibXML::Element")
Truth table100%3/3
A and B
A= @nodesB= $nodes[0]->isa("XML::LibXML::Element")
ABresult
0X0
100
111
302 6 $extra .= "><!-- ".@nodes
303 ." child XML nodes -->";
304 }
305 ▶ elsif ( @nodes and $nodes[0]->isa("XML::LibXML::Text") ) {
Condition66%2/3
@nodes and $nodes[0]->isa("XML::LibXML::Text")
!ll&&!rl&&r
406
MC/DC50%1/2
@nodes and $nodes[0]->isa("XML::LibXML::Text")
@nodes $nodes[0]->isa("XML::LibXML::Text")
Truth table66%2/3
A and B
A= @nodesB= $nodes[0]->isa("XML::LibXML::Text")
ABresult
0X0
100
111
306 6 $extra .= ">(text content)";
307 }
308 18 ▶ if ( @nodes == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@nodes == 0) { } 4 14 18
309 4 $extra .= " />";
310 }
311 else {
312 14 $extra .= "</".$node->nodeName.">";
313 }
314 18 $extra .= ")";
315 }
316 elsif ( $node->isa("XML::LibXML::Text") ) {
317 4 my $val = $node->data;
318 4 ▶ if ( length($val) > 15 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $val > 15) 2 2 4
319 2 $val = substr($val, 0, 13);
320 2 $val .= "...";
321 }
322 4 $extra .= " (at text node: '$val')";
323 }
324 elsif ($node) {
325 0 my $type = ref $node;
326 0 $type =~ s{XML::LibXML::}{};
327 0 $extra .= " (bogon? $type node)";
328 }
329 22 $extra;
330 }
331  
332 sub build_error {
333 37 ▶ my $self = shift;
Subroutine build_error: called CC=1 SCAR=0.0
Pod undocumented
334 37 my $message = $self->message;
335 37 my $extra = $self->show_node;
336 37 return "$message at ".$self->xpath."$extra\n";
337 }
338  
339 use overload
340 11 '""' => \&build_error,
341 11 ▶ fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
342  
343 11 1;
344  
345 __END__
346  
347 =head1 NAME
348  
349 PRANG::Graph::Context - parse/emit state for Marshalling operations
350  
351 =head1 SYNOPSIS
352  
353 my $context = PRANG::Graph::Context->new(
354 base => PRANG::Marshaller->get($class),
355 xpath => "/nodename",
356 );
357  
358 =head1 DESCRIPTION
359  
360 This is a data class, it basically is like a loop counter for parsing
361 (or emitting). Except instead of walking over a list, it 'walks' over
362 a tree of a certain, bound shape.
363  
364 The shape of the XML Graph at each node is limited to:
365  
366 Seq -> Quant -> Choice -> Element -> ( Text | Null )
367  
368 (any of the above may be absent)
369  
370 There are assumptions that nodes only connect as above, and not just
371 in this class.
372  
373 These state in this object allows the code to remember where it is. A
374 new instance is created for each node which may have children for the
375 parsing efforts for that node.
376  
377 =head1 ATTRIBUTES
378  
379 =over
380  
381 =item B<seq_pos>
382  
383 =item B<quant_found>
384  
385 =item B<chosen>
386  
387 =item B<element_ok>
388  
389 The above four properties are state information for any
390 L<PRANG::Graph::Seq>, L<PRANG::Graph::Quant>, L<PRANG::Graph::Choice>
391 or L<PRANG::Graph::Element> objects which exist in the graph for a
392 given class. As the nodes always connect in a particular order,
393 setting one value will clear all of the values for the settings which
394 follow.
395  
396 =item B<xpath>
397  
398 The XML location of the current node. Used for helpful error messages.
399  
400 =item B<xsi>
401  
402 =item B<rxsi>
403  
404 These attributes contain mappings from XML prefixes to namespace URIs
405 and vice versa. They should not be modified, as they are
406 copy-on-write from the parent Context objects.
407  
408 =item B<old_xsi>
409  
410 The B<xsi> attribute from the parent object. Used for C<prefix_new>
411  
412 =item B<xsi_virgin>
413  
414 Unset the first time a prefix is defined.
415  
416 =back
417  
418 =head1 METHODS
419  
420 This API is probably subject to quite some change. It is mainly
421 provided for assisting understanding with internal code.
422  
423 =head2 B<$ctx-E<gt>exception("message", $node?, $skip_ok?)>
424  
425 Raise a context-sensitive exception via C<die>. The XPath that the
426 current node was constructed with is appended with the nodename of the
427 passed node to provide an XML path for the error.
428  
429 Where parsing or emitting errors happen with one of these objects
430 around, it should always be used for reporting the error. The error
431 is a structured object (of type C<PRANG::Graph::Context::Error>) which
432 knows how to stringify into a readable error message.
433  
434 =head2 B<next_ctx( Maybe[Str] $xmlns, Str $newnode_name, $thing? ) returns PRANG::Graph::Context>
435  
436 This returns a new C<PRANG::Graph::Context> object, for the next level
437 of parsing.
438  
439 =head2 B<get_xmlns( Str $prefix ) returns Str>
440  
441 Returns the XML namespace associated with the passed prefix.
442  
443 =head2 B<get_prefix( Str $xmlns, Object $thing?, XML::LibXML::Element $victim? ) returns Str>
444  
445 Used for emitting. This is an alternative to reading the C<rxsi> hash
446 attribute directly. It returns the prefix for the given namespace URI
447 (C<$xmlns>), and if it is not already defined it will figure out based
448 on the type of C<$thing> what prefix to use, and add XML namespace
449 nodes to the C<$victim> XML namespace node. If the C<$thing> does not
450 specify a default XML namespace prefix, then one is chosen for it.
451  
452 =head2 B<add_xmlns( Str $prefix, Str $xmlns )>
453  
454 Used for parsing. This associates the given prefix with the given XML
455 namespace URI.
456  
457 =head2 B<prefix_new( Str $prefix )>
458  
459 This tells you whether or not the passed prefix was declared with this
460 Context or not. Used for emitting.
461  
462 =head1 SEE ALSO
463  
464 L<PRANG::Graph::Meta::Class>, L<PRANG::Graph::Meta::Attr>,
465 L<PRANG::Graph::Meta::Element>, L<PRANG::Marshaller>,
466  
467 Implementations:
468  
469 L<PRANG::Graph::Seq>, L<PRANG::Graph::Quant>, L<PRANG::Graph::Choice>,
470 L<PRANG::Graph::Element>, L<PRANG::Graph::Text>
471  
472 =head1 AUTHOR AND LICENCE
473  
474 Development commissioned by NZ Registry Services, and carried out by
475 Catalyst IT - L<http://www.catalyst.net.nz/>
476  
477 Copyright 2009, 2010, NZ Registry Services. This module is licensed
478 under the Artistic License v2.0, which permits relicensing under other
479 Free Software licenses.
480  
481 =cut
« PRANG/Graph/Choice.pm ↑ Summary PRANG/Graph/Quantity.pm »