blib/lib/XML/XPath/Step.pm untested

filter: Statementstmt 0.0% 0 / 256 Branchbran 0.0% 0 / 142 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 0.0% 0 / 37 Podpod 0.0% 0 / 32 totaltotal 0.0% 0 / 501 CC 72 SCAR 48.8
CC
72
Cov
0%
node_test
506.0
test_attribute
110.0
as_string
72.0
CRAP 131.1
1 package XML::XPath::Step;
2  
3 use XML::XPath::Parser;
4 use XML::XPath::Node;
5 use strict;
6 use warnings;
7  
8 our $VERSION = '1.49';
9  
10 # the beginnings of using XS for this file...
11 # require DynaLoader;
12 # use vars qw/$VERSION @ISA/;
13 # $VERSION = '1.49';
14 # @ISA = qw(DynaLoader);
15 #
16 # bootstrap XML::XPath::Step $VERSION;
17  
18 sub test_qname () { 0; } # Full name
19 sub test_ncwild () { 1; } # NCName:*
20 sub test_any () { 2; } # *
21  
22 sub test_attr_qname () { 3; } # @ns:attrib
23 sub test_attr_ncwild () { 4; } # @nc:*
24 sub test_attr_any () { 5; } # @*
25  
26 sub test_nt_comment () { 6; } # comment()
27 sub test_nt_text () { 7; } # text()
28 sub test_nt_pi () { 8; } # processing-instruction()
29 sub test_nt_node () { 9; } # node()
30  
31 sub new {
32 my $class = shift;
33 my ($pp, $axis, $test, $literal) = @_;
34 my $axis_method = "axis_$axis";
35 $axis_method =~ tr/-/_/;
36 my $self = {
37 pp => $pp, # the XML::XPath::Parser class
38 axis => $axis,
39 axis_method => $axis_method,
40 test => $test,
41 literal => $literal,
42 predicates => [],
43 };
44 bless $self, $class;
45 }
46  
47 sub as_string {
48 my $self = shift;
49 my $string = $self->{axis} . "::";
50  
51 my $test = $self->{test};
52  
53 if ($test == test_nt_pi) {
54 $string .= 'processing-instruction(';
55 if ($self->{literal}->value) {
56 $string .= $self->{literal}->as_string;
57 }
58 $string .= ")";
59 }
60 elsif ($test == test_nt_comment) {
61 $string .= 'comment()';
62 }
63 elsif ($test == test_nt_text) {
64 $string .= 'text()';
65 }
66 elsif ($test == test_nt_node) {
67 $string .= 'node()';
68 }
69 elsif ($test == test_ncwild || $test == test_attr_ncwild) {
70 $string .= $self->{literal} . ':*';
71 }
72 else {
73 $string .= $self->{literal};
74 }
75  
76 foreach (@{$self->{predicates}}) {
77 next unless defined $_;
78 $string .= "[" . $_->as_string . "]";
79 }
80 return $string;
81 }
82  
83 sub as_xml {
84 my $self = shift;
85 my $string = "<Step>\n";
86 $string .= "<Axis>" . $self->{axis} . "</Axis>\n";
87 my $test = $self->{test};
88  
89 $string .= "<Test>";
90  
91 if ($test == test_nt_pi) {
92 $string .= '<processing-instruction';
93 if ($self->{literal}->value) {
94 $string .= '>';
95 $string .= $self->{literal}->as_string;
96 $string .= '</processing-instruction>';
97 }
98 else {
99 $string .= '/>';
100 }
101 }
102 elsif ($test == test_nt_comment) {
103 $string .= '<comment/>';
104 }
105 elsif ($test == test_nt_text) {
106 $string .= '<text/>';
107 }
108 elsif ($test == test_nt_node) {
109 $string .= '<node/>';
110 }
111 elsif ($test == test_ncwild || $test == test_attr_ncwild) {
112 $string .= '<namespace-prefix>' . $self->{literal} . '</namespace-prefix>';
113 }
114 else {
115 $string .= '<nametest>' . $self->{literal} . '</nametest>';
116 }
117  
118 $string .= "</Test>\n";
119  
120 foreach (@{$self->{predicates}}) {
121 next unless defined $_;
122 $string .= "<Predicate>\n" . $_->as_xml() . "</Predicate>\n";
123 }
124  
125 $string .= "</Step>\n";
126  
127 return $string;
128 }
129  
130 sub evaluate {
131 my $self = shift;
132 my $from = shift; # context nodeset
133  
134 # warn "Step::evaluate called with ", $from->size, " length nodeset\n";
135 my $saved_context = $self->{pp}->get_context_set;
136 my $saved_pos = $self->{pp}->get_context_pos;
137  
138 $self->{pp}->set_context_set($from);
139  
140 my $initial_nodeset = XML::XPath::NodeSet->new();
141  
142 # See spec section 2.1, paragraphs 3,4,5:
143 # The node-set selected by the location step is the node-set
144 # that results from generating an initial node set from the
145 # axis and node-test, and then filtering that node-set by
146 # each of the predicates in turn.
147  
148 # Make each node in the nodeset be the context node, one by one
149 for(my $i = 1; $i <= $from->size; $i++) {
150 $self->{pp}->set_context_pos($i);
151 $initial_nodeset->append($self->evaluate_node($from->get_node($i)));
152 }
153  
154 # warn "Step::evaluate initial nodeset size: ", $initial_nodeset->size, "\n";
155  
156 $self->{pp}->set_context_set($saved_context);
157 $self->{pp}->set_context_pos($saved_pos);
158  
159 $initial_nodeset->sort;
160  
161 return $initial_nodeset;
162 }
163  
164 # Evaluate the step against a particular node
165 sub evaluate_node {
166 my $self = shift;
167 my $context = shift;
168  
169 # warn "Evaluate node: $self->{axis}\n";
170  
171 # warn "Node: ", $context->[node_name], "\n";
172  
173 my $method = $self->{axis_method};
174  
175 my $results = XML::XPath::NodeSet->new();
176 no strict 'refs';
177 eval {
178 $method->($self, $context, $results);
179 };
180 if ($@) {
181 die "axis $method not implemented [$@]\n";
182 }
183  
184 # warn("results: ", join('><', map {$_->string_value} @$results), "\n");
185 # filter initial nodeset by each predicate
186 foreach my $predicate (@{$self->{predicates}}) {
187 $results = $self->filter_by_predicate($results, $predicate);
188 }
189  
190 return $results;
191 }
192  
193 sub axis_ancestor {
194 my $self = shift;
195 my ($context, $results) = @_;
196  
197 my $parent = $context->getParentNode;
198  
199 START:
200 return $results unless $parent;
201 if (node_test($self, $parent)) {
202 $results->push($parent);
203 }
204 $parent = $parent->getParentNode;
205 goto START;
206 }
207  
208 sub axis_ancestor_or_self {
209 my $self = shift;
210 my ($context, $results) = @_;
211  
212 START:
213 return $results unless $context;
214 if (node_test($self, $context)) {
215 $results->push($context);
216 }
217 $context = $context->getParentNode;
218 goto START;
219 }
220  
221 sub axis_attribute {
222 my $self = shift;
223 my ($context, $results) = @_;
224  
225 foreach my $attrib (@{$context->getAttributes}) {
226 if ($self->test_attribute($attrib)) {
227 $results->push($attrib);
228 }
229 }
230 }
231  
232 sub axis_child {
233 my $self = shift;
234 my ($context, $results) = @_;
235  
236 foreach my $node (@{$context->getChildNodes}) {
237 if (node_test($self, $node)) {
238 $results->push($node);
239 }
240 }
241 }
242  
243 sub axis_descendant {
244 my $self = shift;
245 my ($context, $results) = @_;
246  
247 my @stack = $context->getChildNodes;
248  
249 while (@stack) {
250 my $node = pop @stack;
251 if (node_test($self, $node)) {
252 $results->unshift($node);
253 }
254 push @stack, $node->getChildNodes;
255 }
256 }
257  
258 sub axis_descendant_or_self {
259 my $self = shift;
260 my ($context, $results) = @_;
261  
262 my @stack = ($context);
263  
264 while (@stack) {
265 my $node = pop @stack;
266 if (node_test($self, $node)) {
267 $results->unshift($node);
268 }
269 push @stack, $node->getChildNodes;
270 }
271 }
272  
273 sub axis_following {
274 my $self = shift;
275 my ($context, $results) = @_;
276  
277 START:
278  
279 my $parent = $context->getParentNode;
280 return $results unless $parent;
281  
282 while ($context = $context->getNextSibling) {
283 axis_descendant_or_self($self, $context, $results);
284 }
285  
286 $context = $parent;
287 goto START;
288 }
289  
290 sub axis_following_sibling {
291 my $self = shift;
292 my ($context, $results) = @_;
293  
294 while ($context = $context->getNextSibling) {
295 if (node_test($self, $context)) {
296 $results->push($context);
297 }
298 }
299 }
300  
301 sub axis_namespace {
302 my $self = shift;
303 my ($context, $results) = @_;
304  
305 return $results unless $context->isElementNode;
306 foreach my $ns (@{$context->getNamespaces}) {
307 if ($self->test_namespace($ns)) {
308 $results->push($ns);
309 }
310 }
311 }
312  
313 sub axis_parent {
314 my $self = shift;
315 my ($context, $results) = @_;
316  
317 my $parent = $context->getParentNode;
318 return $results unless $parent;
319 if (node_test($self, $parent)) {
320 $results->push($parent);
321 }
322 }
323  
324 sub axis_preceding {
325 my $self = shift;
326 my ($context, $results) = @_;
327  
328 # all preceding nodes in document order, except ancestors
329  
330 START:
331  
332 my $parent = $context->getParentNode;
333 return $results unless $parent;
334  
335 while ($context = $context->getPreviousSibling) {
336 axis_descendant_or_self($self, $context, $results);
337 }
338  
339 $context = $parent;
340 goto START;
341 }
342  
343 sub axis_preceding_sibling {
344 my $self = shift;
345 my ($context, $results) = @_;
346  
347 while ($context = $context->getPreviousSibling) {
348 if (node_test($self, $context)) {
349 $results->push($context);
350 }
351 }
352 }
353  
354 sub axis_self {
355 my $self = shift;
356 my ($context, $results) = @_;
357  
358 if (node_test($self, $context)) {
359 $results->push($context);
360 }
361 }
362  
363 sub node_test {
364 my $self = shift;
365 my $node = shift;
366  
367 # if node passes test, return true
368  
369 my $test = $self->{test};
370  
371 return 1 if $test == test_nt_node;
372  
373 if ($test == test_any) {
374 return 1 if $node->isElementNode && defined $node->getName;
375 }
376  
377 local $^W;
378  
379 if ($test == test_ncwild) {
380 return unless $node->isElementNode;
381 my $match_ns = $self->{pp}->get_namespace($self->{literal}, $node);
382 if (my $node_nsnode = $node->getNamespace()) {
383 return 1 if $match_ns eq $node_nsnode->getValue;
384 }
385 }
386 elsif ($test == test_qname) {
387 return unless $node->isElementNode;
388 if ($self->{literal} =~ /:/) {
389 my ($prefix, $name) = split(':', $self->{literal}, 2);
390 my $match_ns = $self->{pp}->get_namespace($prefix, $node);
391 if (my $node_nsnode = $node->getNamespace()) {
392 # warn "match: '$self->{literal}' match NS: '$match_ns' got NS: '", $node_nsnode->getValue, "'\n";
393 return 1 if defined $match_ns && ($match_ns eq $node_nsnode->getValue) &&
394 ($name eq $node->getLocalName);
395 }
396 }
397 else {
398 # warn "Node test: ", $node->getName, "\n";
399 return 1 if $node->getName eq $self->{literal};
400 }
401 }
402 elsif ($test == test_nt_text) {
403 return 1 if $node->isTextNode;
404 }
405 elsif ($test == test_nt_comment) {
406 return 1 if $node->isCommentNode;
407 }
408 # elsif ($test == test_nt_pi && !$self->{literal}) {
409 # warn "Unreachable code???";
410 # return 1 if $node->isPINode;
411 # }
412 elsif ($test == test_nt_pi) {
413 return unless $node->isPINode;
414 if (my $val = $self->{literal}->value) {
415 return 1 if $node->getTarget eq $val;
416 }
417 else {
418 return 1;
419 }
420 }
421  
422 return; # fallthrough returns false
423 }
424  
425 sub test_attribute {
426 my $self = shift;
427 my $node = shift;
428  
429 # warn "test_attrib: '$self->{test}' against: ", $node->getName, "\n";
430 # warn "node type: $node->[node_type]\n";
431  
432 my $test = $self->{test};
433  
434 return 1 if ($test == test_attr_any) || ($test == test_nt_node);
435  
436 if ($test == test_attr_ncwild) {
437 my $match_ns = $self->{pp}->get_namespace($self->{literal}, $node);
438 if (my $node_nsnode = $node->getNamespace()) {
439 return 1 if $match_ns eq $node_nsnode->getValue;
440 }
441 }
442 elsif ($test == test_attr_qname) {
443 if ($self->{literal} =~ /:/) {
444 my ($prefix, $name) = split(':', $self->{literal}, 2);
445 my $match_ns = $self->{pp}->get_namespace($prefix, $node);
446 if (my $node_nsnode = $node->getNamespace()) {
447 return 1 if ($match_ns eq $node_nsnode->getValue) &&
448 ($name eq $node->getLocalName);
449 }
450 }
451 else {
452 return 1 if $node->getName eq $self->{literal};
453 }
454 }
455  
456 return; # fallthrough returns false
457 }
458  
459 sub test_namespace {
460 my $self = shift;
461 my $node = shift;
462  
463 # Not sure if this is correct. The spec seems very unclear on what
464 # constitutes a namespace test... bah!
465  
466 my $test = $self->{test};
467  
468 return 1 if $test == test_any; # True for all nodes of principal type
469  
470 if ($test == test_any) {
471 return 1;
472 }
473 elsif ($self->{literal} eq $node->getExpanded) {
474 return 1;
475 }
476  
477 return;
478 }
479  
480 sub filter_by_predicate {
481 my $self = shift;
482 my ($nodeset, $predicate) = @_;
483  
484 # See spec section 2.4, paragraphs 2 & 3:
485 # For each node in the node-set to be filtered, the predicate Expr
486 # is evaluated with that node as the context node, with the number
487 # of nodes in the node set as the context size, and with the
488 # proximity position of the node in the node set with respect to
489 # the axis as the context position.
490  
491 if (!ref($nodeset)) { # use ref because nodeset has a bool context
492 die "No nodeset!!!";
493 }
494  
495 # warn "Filter by predicate: $predicate\n";
496  
497 my $newset = XML::XPath::NodeSet->new();
498  
499 for(my $i = 1; $i <= $nodeset->size; $i++) {
500 # set context set each time 'cos a loc-path in the expr could change it
501 $self->{pp}->set_context_set($nodeset);
502 $self->{pp}->set_context_pos($i);
503 my $result = $predicate->evaluate($nodeset->get_node($i));
504 if ($result->isa('XML::XPath::Boolean')) {
505 if ($result->value) {
506 $newset->push($nodeset->get_node($i));
507 }
508 }
509 elsif ($result->isa('XML::XPath::Number')) {
510 if ($result->value == $i) {
511 $newset->push($nodeset->get_node($i));
512 }
513 }
514 else {
515 if ($result->to_boolean->value) {
516 $newset->push($nodeset->get_node($i));
517 }
518 }
519 }
520  
521 return $newset;
522 }
523  
524 1;