XPathEngine/Step.pm

filter: Statementstmt 90.6% 146 / 161 Branchbran 75.5% 65 / 86 Conditioncond 66.6% 8 / 12 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 95.4% 21 / 22 Podpod 100% 19 / 19 totaltotal 85.2% 261 / 306 CC 52 SCAR 19.1
CC
52
Cov
85%
as_string
30.0
node_test
12.4
filter_by_predicate
8.0
CRAP 6.7
« XPathEngine.pm ↑ Summary XPathEngine/Function.pm »
1 # $id$
2 # $Id: /tree-xpathengine/trunk/lib/Tree/XPathEngine/Step.pm 25 2006-02-15T15:34:11.453583Z mrodrigu $
3  
4 package Tree::XPathEngine::Step;
5 6 ▶use Tree::XPathEngine;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # constants used to describe the test part of a step
9 sub test_name () { 0; } # Full name
10 sub test_any () { 1; } # *
11 sub test_attr_name () { 2; } # @attrib
12 sub test_attr_any () { 3; } # @*
13 sub test_nt_text () { 4; } # text()
14 sub test_nt_node () { 5; } # node()
15  
16 275 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
17 275 my $class = shift;
18 275 my ($pp, $axis, $test, $literal) = @_;
19 275 my $axis_method = "axis_$axis";
20 275 $axis_method =~ tr/-/_/;
21 275 my $self = {
22 pp => $pp, # the Tree::XPathEngine class
23 axis => $axis,
24 axis_method => $axis_method,
25 test => $test,
26 literal => $literal,
27 predicates => [],
28 };
29 275 bless $self, $class;
30 }
31  
32 0 ▶sub as_string {
Subroutine as_string: not called CC=5 SCAR=34.0
Pod documented
33 0 my $self = shift;
34 0 my $string = $self->{axis} . "::";
35  
36 0 my $test = $self->{test};
37
38 0 ▶ if ($test == test_nt_text) {
Branch: 2 branches
BranchTrueFalseTotal
if ($test == 4) { } 0 0 0
elsif ($test == 5) { } 0 0 0
39 0 $string .= 'text()';
40 }
41 elsif ($test == test_nt_node) {
42 0 $string .= 'node()';
43 }
44 else {
45 0 $string .= $self->{literal};
46 }
47
48 0 foreach (@{$self->{predicates}}) {
49 0 ▶ next unless defined $_;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 0 0 0
50 0 $string .= "[" . $_->as_string . "]";
51 }
52 0 return $string;
53 }
54  
55 799 ▶sub evaluate {
Subroutine evaluate: called CC=2 SCAR=6.9
Pod documented
56 799 my $self = shift;
57 799 my $from = shift; # context nodeset
58
59 # warn "Step::evaluate called with ", $from->size, " length nodeset\n";
60
61 799 $self->{pp}->_set_context_set($from);
62
63 799 my $initial_nodeset = Tree::XPathEngine::NodeSet->new();
64
65 # See spec section 2.1, paragraphs 3,4,5:
66 # The node-set selected by the location step is the node-set
67 # that results from generating an initial node set from the
68 # axis and node-test, and then filtering that node-set by
69 # each of the predicates in turn.
70
71 # Make each node in the nodeset be the context node, one by one
72 799 ▶ for(my $i = 1; $i <= $from->size; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $from->size) 2108 797 2905
73 2108 $self->{pp}->_set_context_pos($i);
74 2108 $initial_nodeset->append($self->evaluate_node($from->get_node($i)));
75 }
76
77 # warn "Step::evaluate initial nodeset size: ", $initial_nodeset->size, "\n";
78
79 797 $self->{pp}->_set_context_set(undef);
80  
81 797 $initial_nodeset->sort;
82
83 797 return $initial_nodeset;
84 }
85  
86 # Evaluate the step against a particular node
87 2108 ▶sub evaluate_node {
Subroutine evaluate_node: called CC=3 SCAR=11.0
Pod documented
88 2108 my $self = shift;
89 2108 my $context = shift;
90
91 # warn "Evaluate node: $self->{axis}\n";
92
93 # warn "Node: ", $context->[node_name], "\n";
94
95 2108 my $method = $self->{axis_method};
96
97 2108 my $results = Tree::XPathEngine::NodeSet->new();
98 6 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
99 2108 eval {
100 #$method->($self, $context, $results);
101 2108 $self->$method( $context, $results);
102 };
103 2108 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 2108 2108
104 0 die "axis $method not implemented [$@]\n";
105 }
106
107 # warn("results: ", join('><', map {$_->xpath_string_value} @$results), "\n");
108 # filter initial nodeset by each predicate
109 2108 foreach my $predicate (@{$self->{predicates}}) {
110 1374 $results = $self->filter_by_predicate($results, $predicate);
111 }
112
113 2106 return $results;
114 }
115  
116 17 ▶sub axis_ancestor {
Subroutine axis_ancestor: called CC=4 SCAR=13.9
Pod documented
117 17 my $self = shift;
118 17 my ($context, $results) = @_;
119
120 17 my $parent = $context->xpath_get_parent_node;
121
122 17 ▶ while( $parent)
Branch: 1 branch
BranchTrueFalseTotal
if ($parent) 62 17 79
123 62 ▶ { $results->push($parent) if (node_test($self, $parent));
Branch: 1 branch
BranchTrueFalseTotal
if node_test($self, $parent) 22 40 62
124 62 $parent = $parent->xpath_get_parent_node;
125 }
126 17 ▶ return $results unless $parent;
Branch: 1 branch
BranchTrueFalseTotal
unless $parent 17 0 17
127 }
128  
129 16 ▶sub axis_ancestor_or_self {
Subroutine axis_ancestor_or_self: called CC=3 SCAR=11.0
Pod documented
130 16 my $self = shift;
131 16 my ($context, $results) = @_;
132
133 90 ▶ START:
Branch: 1 branch
BranchTrueFalseTotal
unless $context 16 74 90
134 return $results unless $context;
135 74 ▶ if (node_test($self, $context)) {
Branch: 1 branch
BranchTrueFalseTotal
if (node_test($self, $context)) 19 55 74
136 19 $results->push($context);
137 }
138 74 $context = $context->xpath_get_parent_node;
139 74 goto START;
140 }
141  
142 926 ▶sub axis_attribute {
Subroutine axis_attribute: called CC=3 SCAR=11.0
Pod documented
143 926 my $self = shift;
144 926 my ($context, $results) = @_;
145
146 926 foreach my $attrib ($context->xpath_get_attributes) {
147 2043 ▶ if ($self->test_attribute($attrib)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->test_attribute($attrib)) 822 1221 2043
148 822 $results->push($attrib);
149 }
150 }
151 }
152  
153 825 ▶sub axis_child {
Subroutine axis_child: called CC=3 SCAR=11.0
Pod documented
154 825 my $self = shift;
155 825 my ($context, $results) = @_;
156
157 825 foreach my $node ($context->xpath_get_child_nodes) {
158 846 ▶ if (node_test($self, $node)) {
Branch: 1 branch
BranchTrueFalseTotal
if (node_test($self, $node)) 482 364 846
159 482 $results->push($node);
160 }
161 }
162 }
163  
164 8 ▶sub axis_descendant {
Subroutine axis_descendant: called CC=3 SCAR=11.0
Pod documented
165 8 my $self = shift;
166 8 my ($context, $results) = @_;
167  
168 8 my @stack = $context->xpath_get_child_nodes;
169  
170 8 ▶ while (@stack) {
Branch: 1 branch
BranchTrueFalseTotal
if (@stack) 128 8 136
171 128 my $node = pop @stack;
172 128 ▶ if (node_test($self, $node)) {
Branch: 1 branch
BranchTrueFalseTotal
if (node_test($self, $node)) 27 101 128
173 27 $results->unshift($node);
174 }
175 128 push @stack, $node->xpath_get_child_nodes;
176 }
177 }
178  
179 91 ▶sub axis_descendant_or_self {
Subroutine axis_descendant_or_self: called CC=3 SCAR=11.0
Pod documented
180 91 my $self = shift;
181 91 my ($context, $results) = @_;
182
183 91 my @stack = ($context);
184
185 91 ▶ while (@stack) {
Branch: 1 branch
BranchTrueFalseTotal
if (@stack) 1348 91 1439
186 1348 my $node = pop @stack;
187 1348 ▶ if (node_test($self, $node)) {
Branch: 1 branch
BranchTrueFalseTotal
if (node_test($self, $node)) 1348 0 1348
188 1348 $results->unshift($node);
189 }
190 1348 push @stack, $node->xpath_get_child_nodes;
191 }
192 }
193  
194 1 ▶sub axis_following {
Subroutine axis_following: called CC=3 SCAR=11.0
Pod documented
195 1 my $self = shift;
196 1 my ($context, $results) = @_;
197
198 3 START:
199  
200 my $parent = $context->xpath_get_parent_node;
201 3 ▶ return $results unless $parent;
Branch: 1 branch
BranchTrueFalseTotal
unless $parent 1 2 3
202
203 2 ▶ while ($context = $context->xpath_get_next_sibling) {
Branch: 1 branch
BranchTrueFalseTotal
if ($context = $context->xpath_get_next_sibling) 1 2 3
204 1 axis_descendant_or_self($self, $context, $results);
205 }
206  
207 2 $context = $parent;
208 2 goto START;
209 }
210  
211 3 ▶sub axis_following_sibling {
Subroutine axis_following_sibling: called CC=3 SCAR=11.0
Pod documented
212 3 my $self = shift;
213 3 my ($context, $results) = @_;
214  
215 3 ▶ while ($context = $context->xpath_get_next_sibling) {
Branch: 1 branch
BranchTrueFalseTotal
if ($context = $context->xpath_get_next_sibling) 3 3 6
216 3 ▶ if (node_test($self, $context)) {
Branch: 1 branch
BranchTrueFalseTotal
if (node_test($self, $context)) 3 0 3
217 3 $results->push($context);
218 }
219 }
220 }
221  
222 12 ▶sub axis_parent {
Subroutine axis_parent: called CC=3 SCAR=11.2
Pod documented
223 12 my $self = shift;
224 12 my ($context, $results) = @_;
225
226 12 my $parent = $context->xpath_get_parent_node;
227 12 ▶ return $results unless $parent;
Branch: 1 branch
BranchTrueFalseTotal
unless $parent 0 12 12
228 12 ▶ if (node_test($self, $parent)) {
Branch: 1 branch
BranchTrueFalseTotal
if (node_test($self, $parent)) 12 0 12
229 12 $results->push($parent);
230 }
231 }
232  
233 1 ▶sub axis_preceding {
Subroutine axis_preceding: called CC=3 SCAR=11.0
Pod documented
234 1 my $self = shift;
235 1 my ($context, $results) = @_;
236
237 # all preceding nodes in document order, except ancestors
238
239 3 START:
240  
241 my $parent = $context->xpath_get_parent_node;
242 3 ▶ return $results unless $parent;
Branch: 1 branch
BranchTrueFalseTotal
unless $parent 1 2 3
243  
244 2 ▶ while ($context = $context->xpath_get_previous_sibling) {
Branch: 1 branch
BranchTrueFalseTotal
if ($context = $context->xpath_get_previous_sibling) 3 2 5
245 3 axis_descendant_or_self($self, $context, $results);
246 }
247
248 2 $context = $parent;
249 2 goto START;
250 }
251  
252 2 ▶sub axis_preceding_sibling {
Subroutine axis_preceding_sibling: called CC=3 SCAR=11.0
Pod documented
253 2 my $self = shift;
254 2 my ($context, $results) = @_;
255
256 2 ▶ while ($context = $context->xpath_get_previous_sibling) {
Branch: 1 branch
BranchTrueFalseTotal
if ($context = $context->xpath_get_previous_sibling) 6 2 8
257 6 ▶ if (node_test($self, $context)) {
Branch: 1 branch
BranchTrueFalseTotal
if (node_test($self, $context)) 6 0 6
258 6 $results->push($context);
259 }
260 }
261 }
262  
263 210 ▶sub axis_self {
Subroutine axis_self: called CC=2 SCAR=7.0
Pod documented
264 210 my $self = shift;
265 210 my ($context, $results) = @_;
266
267 210 ▶ if (node_test($self, $context)) {
Branch: 1 branch
BranchTrueFalseTotal
if (node_test($self, $context)) 210 0 210
268 210 $results->push($context);
269 }
270 }
271
272 2689 ▶sub node_test {
Subroutine node_test: called CC=10 SCAR=25.2
Pod documented
273 2689 my $self = shift;
274 2689 my $node = shift;
275
276 # if node passes test, return true
277
278 2689 my $test = $self->{test};
279  
280 2689 ▶ return 1 if $test == test_nt_node;
Branch: 1 branch
BranchTrueFalseTotal
if $test == 5 1558 1131 2689
281
282 1131 ▶ if ($test == test_any) {
Branch: 1 branch
BranchTrueFalseTotal
if ($test == 1) 288 843 1131
283 288 ▶ return 1 if( $node->xpath_is_element_node && defined $node->xpath_get_name);
Branch: 1 branch
BranchTrueFalseTotal
if $node->xpath_is_element_node and defined $node->xpath_get_name 288 0 288
Condition33%1/3
$node->xpath_is_element_node and defined $node->xpath_get_name
!ll&&!rl&&r
00288
MC/DC0%0/2
$node->xpath_is_element_node and defined $node->xpath_get_name
$node->xpath_is_element_node defined $node->xpath_get_name
Truth table33%1/3
A and B
A= $node->xpath_is_element_nodeB= defined $node->xpath_get_name
ABresult
0X0
100
111
284 }
285
286 843 local $^W;
287  
288 843 ▶ if ($test == test_name) {
Branch: 2 branches
BranchTrueFalseTotal
if ($test == 0) { } 843 0 843
elsif ($test == 4) { } 0 0 0
289 843 ▶ return unless $node->xpath_is_element_node;
Branch: 1 branch
BranchTrueFalseTotal
unless $node->xpath_is_element_node 15 828 843
290 828 ▶ return 1 if $node->xpath_get_name eq $self->{literal};
Branch: 1 branch
BranchTrueFalseTotal
if $node->xpath_get_name eq $self->{"literal"} 283 545 828
291 }
292 elsif ($test == test_nt_text) {
293 0 ▶ return 1 if $node->xpath_is_text_node;
Branch: 1 branch
BranchTrueFalseTotal
if $node->xpath_is_text_node 0 0 0
294 }
295 545 return; # fallthrough returns false
296 }
297  
298 2043 ▶sub test_attribute {
Subroutine test_attribute: called CC=5 SCAR=16.2
Pod documented
299 2043 my $self = shift;
300 2043 my $node = shift;
301
302 # warn "test_attrib: '$self->{test}' against: ", $node->xpath_get_name, "\n";
303 # warn "node type: $node->[node_type]\n";
304
305 2043 my $test = $self->{test};
306  
307 2043 ▶ if( ($test == test_attr_any) || ($test == test_nt_node)
Branch: 1 branch
BranchTrueFalseTotal
if ($test == 3 or $test == 5 or $test == 2 and $node->xpath_get_name eq $self->{'literal'}) { } 822 1221 2043
Condition77%7/9
$test == 3 or $test == 5 or $test == 2 and $node->xpath_get_name eq $self->{"literal"}
l!l&&r!l&&!r
1506721221
$test == 3 or $test == 5
l!l&&r!l&&!r
15001893
$test == 2 and $node->xpath_get_name eq $self->{"literal"}
!ll&&!rl&&r
01221672
MC/DC50%2/4
$test == 3 or $test == 5 or $test == 2 and $node->xpath_get_name eq $self->{"literal"}
$test == 3 $test == 5 $test == 2 $node->xpath_get_name eq $self->{"literal"}
Truth table60%3/5
A or B or C and D
A= $test == 3B= $test == 5C= $test == 2D= $node->xpath_get_name eq $self->{"literal"}
ABCDresult
1XXX1
01XX1
00111
000X0
00100
308 || ( ($test == test_attr_name) && ($node->xpath_get_name eq $self->{literal}) )
309 )
310 822 { return 1; }
311 else
312 1221 { return; }
313 }
314  
315  
316 1374 ▶sub filter_by_predicate {
Subroutine filter_by_predicate: called CC=8 SCAR=20.8
Pod documented
317 1374 my $self = shift;
318 1374 my ($nodeset, $predicate) = @_;
319
320 # See spec section 2.4, paragraphs 2 & 3:
321 # For each node in the node-set to be filtered, the predicate Expr
322 # is evaluated with that node as the context node, with the number
323 # of nodes in the node set as the context size, and with the
324 # proximity position of the node in the node set with respect to
325 # the axis as the context position.
326
327 1374 ▶ if (!ref($nodeset)) { # use ref because nodeset has a bool context
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $nodeset) 0 1374 1374
328 0 die "No nodeset!!!";
329 }
330
331 # warn "Filter by predicate: $predicate\n";
332
333 1374 my $newset = Tree::XPathEngine::NodeSet->new();
334
335 1374 ▶ for(my $i = 1; $i <= $nodeset->size; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $nodeset->size) 713 1372 2085
336 # set context set each time 'cos a loc-path in the expr could change it
337 713 $self->{pp}->_set_context_set($nodeset);
338 713 $self->{pp}->_set_context_pos($i);
339 713 my $result = $predicate->evaluate($nodeset->get_node($i));
340 711 ▶ if ($result->isa('Tree::XPathEngine::Boolean')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($result->isa('Tree::XPathEngine::Boolean')) { } 587 124 711
elsif ($result->isa('Tree::XPathEngine::Number')) { } 93 31 124
341 587 ▶ if ($result->value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->value) 159 428 587
342 159 $newset->push($nodeset->get_node($i));
343 }
344 }
345 elsif ($result->isa('Tree::XPathEngine::Number')) {
346 93 ▶ if ($result->value == $i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->value == $i) 22 71 93
347 22 $newset->push($nodeset->get_node($i));
348 }
349 }
350 else {
351 31 ▶ if ($result->xpath_to_boolean->value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->xpath_to_boolean->value) 17 14 31
352 17 $newset->push($nodeset->get_node($i));
353 }
354 }
355 }
356
357 1372 return $newset;
358 }
359  
360 6 1;
361  
362 __END__
« XPathEngine.pm ↑ Summary XPathEngine/Function.pm »