XPath/Axis.pm

filter: Statementstmt 99.3% 161 / 162 Branchbran 88.5% 62 / 70 Conditioncond 77.7% 7 / 9 MC/DCmcdc 66.6% 4 / 6 Subroutinesub 100% 21 / 21 Podpod 0.0% 0 / 4 totaltotal 93.7% 255 / 272 CC 55 SCAR 17.9
CC
55
Cov
95%
filter
14.0
_axis_attribute
6.2
_axis_following_sibling
6.0
CRAP 6.0
« XPath/Result.pm ↑ Summary XPath/Eval.pm »
1 package XML::Parser::Lite::Tree::XPath::Axis;
2  
3 33 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 33 ▶use XML::Parser::Lite::Tree::XPath::Result;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 33 ▶use vars qw( $axis );
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 280 ▶sub instance {
Subroutine instance: called CC=2 SCAR=6.9
Pod undocumented
9 280 ▶ return $axis if $axis;
Branch: 1 branch
BranchTrueFalseTotal
if $XML::Parser::Lite::Tree::XPath::Axis::axis 255 25 280
10 25 $axis = __PACKAGE__->new;
11 }
12  
13 25 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
14 25 return bless {}, $_[0];
15 }
16  
17 280 ▶sub filter {
Subroutine filter: called CC=14 SCAR=26.4
Pod undocumented
18 280 my ($self, $token, $context) = @_;
19  
20 280 $self->{token} = $token;
21 280 ▶ $self->{axis} = defined($token->{axis}) ? $token->{axis} : 'child';
Branch: 1 branch
BranchTrueFalseTotal
defined $token->{'axis'} ? : 119 161 280
22  
23 280 ▶ return $self->_axis_child($context) if $self->{axis} eq 'child';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"axis"} eq "child" 165 115 280
24 115 ▶ return $self->_axis_descendant($context, 0) if $self->{axis} eq 'descendant';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"axis"} eq "descendant" 6 109 115
25 109 ▶ return $self->_axis_descendant($context, 1) if $self->{axis} eq 'descendant-or-self';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"axis"} eq "descendant-or-self" 60 49 109
26 49 ▶ return $self->_axis_parent($context) if $self->{axis} eq 'parent';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"axis"} eq "parent" 2 47 49
27 47 ▶ return $self->_axis_ancestor($context, 0) if $self->{axis} eq 'ancestor';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"axis"} eq "ancestor" 4 43 47
28 43 ▶ return $self->_axis_ancestor($context, 1) if $self->{axis} eq 'ancestor-or-self';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"axis"} eq "ancestor-or-self" 2 41 43
29 41 ▶ return $self->_axis_following_sibling($context) if $self->{axis} eq 'following-sibling';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"axis"} eq "following-sibling" 2 39 41
30 39 ▶ return $self->_axis_preceding_sibling($context) if $self->{axis} eq 'preceding-sibling';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"axis"} eq "preceding-sibling" 2 37 39
31 37 ▶ return $self->_axis_following($context) if $self->{axis} eq 'following';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"axis"} eq "following" 4 33 37
32 33 ▶ return $self->_axis_preceding($context) if $self->{axis} eq 'preceding';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"axis"} eq "preceding" 4 29 33
33 29 ▶ return $self->_axis_attribute($context) if $self->{axis} eq 'attribute';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"axis"} eq "attribute" 26 3 29
34  
35 3 ▶ return $context if $self->{axis} eq 'self';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"axis"} eq "self" 2 1 3
36  
37 1 return $self->ret('Error', "Unknown axis '$self->{axis}'");
38 }
39  
40 278 ▶sub ret {
Subroutine ret: called CC=1 SCAR=0.0
Pod undocumented
41 278 my ($self, $type, $value) = @_;
42 278 return XML::Parser::Lite::Tree::XPath::Result->new($type, $value);
43 }
44  
45 165 ▶sub _axis_child {
Subroutine _axis_child: called CC=3 SCAR=11.0
46 165 my ($self, $in) = @_;
47  
48 165 my $out = $self->ret('nodeset', []);
49  
50 165 for my $tag(@{$in->{value}}){
51 745 for my $child(@{$tag->{children}}){
52 757 push @{$out->{value}}, $child;
53 }
54 }
55  
56 165 return $out;
57 }
58  
59 66 ▶sub _axis_descendant {
Subroutine _axis_descendant: called CC=2 SCAR=6.9
60 66 my ($self, $in, $me) = @_;
61  
62 66 my $out = $self->ret('nodeset', []);
63  
64 66 for my $tag(@{$in->{value}}){
65  
66 map{
67 70 push @{$out->{value}}, $_;
68  
69 }$self->_axis_descendant_single($tag, $me);
70 }
71  
72 66 return $out;
73 }
74  
75 690 ▶sub _axis_descendant_single {
Subroutine _axis_descendant_single: called CC=4 SCAR=13.9
76 690 my ($self, $tag, $me) = @_;
77  
78 690 my @out;
79  
80 690 ▶ push @out, $tag if $me;
Branch: 1 branch
BranchTrueFalseTotal
if $me 682 8 690
81  
82 690 for my $child(@{$tag->{children}}){
83  
84 622 ▶ if ($child->{type} eq 'element'){
Branch: 1 branch
BranchTrueFalseTotal
if ($child->{"type"} eq "element") 620 2 622
85  
86 map{
87 620 push @out, $_;
88 }$self->_axis_descendant_single($child, 1);
89 }
90 }
91  
92 690 return @out;
93 }
94  
95 26 ▶sub _axis_attribute {
Subroutine _axis_attribute: called CC=6 SCAR=18.3
96 26 my ($self, $input) = @_;
97  
98 26 my $out = $self->ret('nodeset', []);
99 26 my $nodes = [];
100  
101 26 ▶ if ($input->{type} eq 'nodeset'){
Branch: 1 branch
BranchTrueFalseTotal
if ($input->{"type"} eq "nodeset") 26 0 26
102 26 $nodes = $input->{value};
103 }
104  
105 26 ▶ if ($input->{type} eq 'node'){
Branch: 1 branch
BranchTrueFalseTotal
if ($input->{"type"} eq "node") 0 26 26
106 0 $nodes = [$input->{value}];
107 }
108  
109 26 ▶ return $self->ret('Error', "attribute axis can only filter nodes and nodesets (not a $input->{type})") unless defined $nodes;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $nodes 0 26 26
110  
111 26 my $i = 0;
112  
113 26 for my $node(@{$nodes}){
114 31 for my $key(keys %{$node->{attributes}}){
115 30 push @{$out->{value}}, {
116 'name' => $key,
117 'value' => $node->{attributes}->{$key},
118 'type' => 'attribute',
119 30 'order' => ($node->{order} * 10000000) + $i++,
120 };
121 }
122 }
123  
124 26 return $out;
125 }
126  
127 2 ▶sub _axis_parent {
Subroutine _axis_parent: called CC=3 SCAR=11.0
128 2 my ($self, $in) = @_;
129  
130 2 my $out = $self->ret('nodeset', []);
131  
132 2 for my $tag(@{$in->{value}}){
133 6 ▶ push @{$out->{value}}, $tag->{parent} if defined $tag->{parent};
Branch: 1 branch
BranchTrueFalseTotal
if defined $tag->{"parent"} 6 0 6
134 }
135  
136 2 return $out;
137 }
138  
139 6 ▶sub _axis_ancestor {
Subroutine _axis_ancestor: called CC=2 SCAR=6.9
140 6 my ($self, $in, $me) = @_;
141  
142 6 my $out = $self->ret('nodeset', []);
143  
144 6 for my $tag(@{$in->{value}}){
145  
146 map{
147 6 push @{$out->{value}}, $_;
148  
149 }$self->_axis_ancestor_single($tag, $me);
150 }
151  
152 6 return $out;
153 }
154  
155 36 ▶sub _axis_ancestor_single {
Subroutine _axis_ancestor_single: called CC=3 SCAR=11.0
156 36 my ($self, $tag, $me) = @_;
157  
158 36 my @out;
159  
160 36 ▶ push @out, $tag if $me;
Branch: 1 branch
BranchTrueFalseTotal
if $me 32 4 36
161  
162 36 ▶ if (defined $tag->{parent}){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tag->{"parent"}) 30 6 36
163  
164 map{
165 91 push @out, $_;
166 30 }$self->_axis_ancestor_single($tag->{parent}, 1);
167 }
168  
169 36 return @out;
170 }
171  
172 2 ▶sub _axis_following_sibling {
Subroutine _axis_following_sibling: called CC=6 SCAR=17.9
173 2 my ($self, $in) = @_;
174  
175 2 my $out = $self->ret('nodeset', []);
176  
177 2 for my $tag(@{$in->{value}}){
178 4 ▶ if (defined $tag->{parent}){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tag->{"parent"}) 4 0 4
179 4 my $parent = $tag->{parent};
180 4 my $found = 0;
181 4 for my $child(@{$parent->{children}}){
182 13 ▶ push @{$out->{value}}, $child if $found;
Branch: 1 branch
BranchTrueFalseTotal
if $found 5 8 13
183 13 ▶ $found = 1 if $child->{order} == $tag->{order};
Branch: 1 branch
BranchTrueFalseTotal
if $child->{"order"} == $tag->{"order"} 4 9 13
184 }
185 }
186 }
187  
188 2 return $out;
189 }
190  
191 2 ▶sub _axis_preceding_sibling {
Subroutine _axis_preceding_sibling: called CC=6 SCAR=17.9
192 2 my ($self, $in) = @_;
193  
194 2 my $out = $self->ret('nodeset', []);
195  
196 2 for my $tag(@{$in->{value}}){
197 4 ▶ if (defined $tag->{parent}){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tag->{"parent"}) 4 0 4
198 4 my $parent = $tag->{parent};
199 4 my $found = 0;
200 4 for my $child(@{$parent->{children}}){
201 13 ▶ $found = 1 if $child->{order} == $tag->{order};
Branch: 1 branch
BranchTrueFalseTotal
if $child->{"order"} == $tag->{"order"} 4 9 13
202 13 ▶ push @{$out->{value}}, $child unless $found;
Branch: 1 branch
BranchTrueFalseTotal
unless $found 5 8 13
203 }
204 }
205 }
206  
207 2 return $out;
208 }
209  
210 4 ▶sub _axis_following {
Subroutine _axis_following: called CC=3 SCAR=11.0
211 4 my ($self, $in) = @_;
212  
213 4 my $min_order = 1 + $self->{token}->{max_order};
214 4 for my $tag(@{$in->{value}}){
215 4 ▶ $min_order = $tag->{order} if $tag->{order} < $min_order;
Branch: 1 branch
BranchTrueFalseTotal
if $tag->{"order"} < $min_order 4 0 4
216 }
217  
218 # recurse the whole tree, adding after we find $min_order (but don't descend into it!)
219  
220 4 my @tags = $self->_axis_following_recurse( $self->{token}->{root}->{value}->[0], $min_order );
221  
222 4 return $self->ret('nodeset', \@tags);
223 }
224  
225 54 ▶sub _axis_following_recurse {
Subroutine _axis_following_recurse: called CC=5 SCAR=16.1
226 54 my ($self, $tag, $min) = @_;
227  
228 54 my @out;
229  
230 54 ▶ push @out, $tag if $tag->{order} > $min;
Branch: 1 branch
BranchTrueFalseTotal
if $tag->{"order"} > $min 20 34 54
231  
232 54 for my $child(@{$tag->{children}}){
233  
234 54 ▶ if (($child->{order}) != $min && ($child->{type} eq 'element')){
Branch: 1 branch
BranchTrueFalseTotal
if ($child->{"order"} != $min and $child->{"type"} eq "element") 50 4 54
Condition66%2/3
$child->{"order"} != $min and $child->{"type"} eq "element"
!ll&&!rl&&r
4050
MC/DC50%1/2
$child->{"order"} != $min and $child->{"type"} eq "element"
$child->{"order"} != $min $child->{"type"} eq "element"
Truth table66%2/3
A and B
A= $child->{"order"} != $minB= $child->{"type"} eq "element"
ABresult
0X0
100
111
235  
236 map{
237 50 push @out, $_;
238 }$self->_axis_following_recurse($child, $min);
239 }
240 }
241  
242 54 return @out;
243 }
244  
245 4 ▶sub _axis_preceding {
Subroutine _axis_preceding: called CC=3 SCAR=11.0
246 4 my ($self, $in) = @_;
247  
248 4 my $max_order = -1;
249 4 my $parents;
250 4 for my $tag(@{$in->{value}}){
251 4 ▶ if ($tag->{order} > $max_order){
Branch: 1 branch
BranchTrueFalseTotal
if ($tag->{"order"} > $max_order) 4 0 4
252 4 $max_order = $tag->{order};
253 4 $parents = $self->_get_parent_orders($tag);
254 }
255 }
256  
257 # recurse the whole tree, adding until we find $max_order (but don't descend into it!)
258  
259 4 my @tags = $self->_axis_preceding_recurse( $self->{token}->{root}->{value}->[0], $parents, $max_order );
260  
261 4 return $self->ret('nodeset', \@tags);
262 }
263  
264 49 ▶sub _axis_preceding_recurse {
Subroutine _axis_preceding_recurse: called CC=6 SCAR=17.9
265 49 my ($self, $tag, $parents, $max) = @_;
266  
267 49 my @out;
268  
269 49 ▶ push @out, $tag if $tag->{order} < $max && !$parents->{$tag->{order}};
Branch: 1 branch
BranchTrueFalseTotal
if $tag->{"order"} < $max and not $$parents{$tag->{"order"}} 22 27 49
Condition100%3/3
$tag->{"order"} < $max and not $$parents{$tag->{"order"}}
!ll&&!rl&&r
101722
MC/DC100%2/2
$tag->{"order"} < $max and not $$parents{$tag->{"order"}}
$tag->{"order"} < $max not $$parents{$tag->{"order"}}
Truth table100%3/3
A and not(B)
A= $tag->{"order"} < $maxB= not $$parents{$tag->{"order"}}
ABresult
0X0
100
111
270  
271 49 for my $child(@{$tag->{children}}){
272  
273 49 ▶ if (($child->{order}) != $max && ($child->{type} eq 'element')){
Branch: 1 branch
BranchTrueFalseTotal
if ($child->{"order"} != $max and $child->{"type"} eq "element") 45 4 49
Condition66%2/3
$child->{"order"} != $max and $child->{"type"} eq "element"
!ll&&!rl&&r
4045
MC/DC50%1/2
$child->{"order"} != $max and $child->{"type"} eq "element"
$child->{"order"} != $max $child->{"type"} eq "element"
Truth table66%2/3
A and B
A= $child->{"order"} != $maxB= $child->{"type"} eq "element"
ABresult
0X0
100
111
274  
275 map{
276 45 push @out, $_;
277 }$self->_axis_preceding_recurse($child, $parents, $max);
278 }
279 }
280  
281 49 return @out;
282 }
283  
284 4 ▶sub _get_parent_orders {
Subroutine _get_parent_orders: called CC=2 SCAR=6.9
285 4 my ($self, $tag) = @_;
286 4 my $parents;
287  
288 4 ▶ while(defined $tag->{parent}){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tag->{"parent"}) 17 4 21
289 17 $tag = $tag->{parent};
290 17 $parents->{$tag->{order}} = 1;
291 }
292  
293 4 return $parents;
294 }
295  
296 33 1;
« XPath/Result.pm ↑ Summary XPath/Eval.pm »