PRANG/Graph/Context.pm

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