XPath/Step.pm

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