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

filter: Statementstmt 0.0% 0 / 303 Branchbran 0.0% 0 / 116 Conditioncond 0.0% 0 / 137 MC/DCmcdc 0.0% 0 / 91 Subroutinesub 0.0% 0 / 31 Podpod 0.0% 0 / 28 totaltotal 0.0% 0 / 706 CC 59 SCAR 44.4
CC
59
Cov
0%
op_equals
272.0
op_ge
110.0
op_gt
110.0
CRAP 84.9
1 package XML::XPath::Expr;
2  
3 $VERSION = '1.49';
4  
5 use strict; use warnings;
6  
7 sub new {
8 my $class = shift;
9 my ($pp) = @_;
10 bless { predicates => [], pp => $pp }, $class;
11 }
12  
13 sub as_string {
14 my $self = shift;
15 local $^W; # Use of uninitialized value! grrr
16 my $string = "(" ;
17 $string .= $self->{lhs}->as_string||'' if defined $self->{lhs};
18 $string .= " " . $self->{op} . " " if defined $self->{op};
19 $string .= $self->{rhs}->as_string if defined $self->{rhs};
20 $string .= ")";
21 foreach my $predicate (@{$self->{predicates}}) {
22 $string .= "[" . $predicate->as_string . "]";
23 }
24 return $string;
25 }
26  
27 sub as_xml {
28 my $self = shift;
29 local $^W; # Use of uninitialized value! grrr
30 my $string;
31 if (defined $self->{op}) {
32 $string .= $self->op_xml();
33 }
34 else {
35 $string .= $self->{lhs}->as_xml();
36 }
37 foreach my $predicate (@{$self->{predicates}}) {
38 $string .= "<Predicate>\n" . $predicate->as_xml() . "</Predicate>\n";
39 }
40 return $string;
41 }
42  
43 sub op_xml {
44 my $self = shift;
45 my $op = $self->{op};
46  
47 my $tag;
48 for ($op) {
49 /^or$/ && do {
50 $tag = "Or";
51 };
52 /^and$/ && do {
53 $tag = "And";
54 };
55 /^=$/ && do {
56 $tag = "Equals";
57 };
58 /^!=$/ && do {
59 $tag = "NotEquals";
60 };
61 /^<=$/ && do {
62 $tag = "LessThanOrEquals";
63 };
64 /^>=$/ && do {
65 $tag = "GreaterThanOrEquals";
66 };
67 /^>$/ && do {
68 $tag = "GreaterThan";
69 };
70 /^<$/ && do {
71 $tag = "LessThan";
72 };
73 /^\+$/ && do {
74 $tag = "Plus";
75 };
76 /^-$/ && do {
77 $tag = "Minus";
78 };
79 /^div$/ && do {
80 $tag = "Div";
81 };
82 /^mod$/ && do {
83 $tag = "Mod";
84 };
85 /^\*$/ && do {
86 $tag = "Multiply";
87 };
88 /^\|$/ && do {
89 $tag = "Union";
90 };
91 }
92  
93 return "<$tag>\n" . $self->{lhs}->as_xml() . $self->{rhs}->as_xml() . "</$tag>\n";
94 }
95  
96 sub set_lhs {
97 my $self = shift;
98 $self->{lhs} = $_[0];
99 }
100  
101 sub set_op {
102 my $self = shift;
103 $self->{op} = $_[0];
104 }
105  
106 sub set_rhs {
107 my $self = shift;
108 $self->{rhs} = $_[0];
109 }
110  
111 sub push_predicate {
112 my $self = shift;
113  
114 die "Only 1 predicate allowed on FilterExpr in W3C XPath 1.0"
115 if @{$self->{predicates}};
116  
117 push @{$self->{predicates}}, $_[0];
118 }
119  
120 sub get_lhs { $_[0]->{lhs}; }
121 sub get_rhs { $_[0]->{rhs}; }
122 sub get_op { $_[0]->{op}; }
123  
124 sub evaluate {
125 my $self = shift;
126 my $node = shift;
127  
128 # If there's an op, result is result of that op.
129 # If no op, just resolve Expr
130  
131 # warn "Evaluate Expr: ", $self->as_string, "\n";
132  
133 my $results;
134  
135 if ($self->{op}) {
136 die ("No RHS of ", $self->as_string) unless $self->{rhs};
137 $results = $self->op_eval($node);
138 }
139 else {
140 $results = $self->{lhs}->evaluate($node);
141 }
142  
143 if (my @predicates = @{$self->{predicates}}) {
144 if (!$results->isa('XML::XPath::NodeSet')) {
145 die "Can't have predicates execute on object type: " . ref($results);
146 }
147  
148 # filter initial nodeset by each predicate
149 foreach my $predicate (@{$self->{predicates}}) {
150 $results = $self->filter_by_predicate($results, $predicate);
151 }
152 }
153  
154 return $results;
155 }
156  
157 sub op_eval {
158 my $self = shift;
159 my $node = shift;
160  
161 my $op = $self->{op};
162  
163 for ($op) {
164 /^or$/ && do {
165 return op_or($node, $self->{lhs}, $self->{rhs});
166 };
167 /^and$/ && do {
168 return op_and($node, $self->{lhs}, $self->{rhs});
169 };
170 /^=$/ && do {
171 return op_equals($node, $self->{lhs}, $self->{rhs});
172 };
173 /^!=$/ && do {
174 return op_nequals($node, $self->{lhs}, $self->{rhs});
175 };
176 /^<=$/ && do {
177 return op_le($node, $self->{lhs}, $self->{rhs});
178 };
179 /^>=$/ && do {
180 return op_ge($node, $self->{lhs}, $self->{rhs});
181 };
182 /^>$/ && do {
183 return op_gt($node, $self->{lhs}, $self->{rhs});
184 };
185 /^<$/ && do {
186 return op_lt($node, $self->{lhs}, $self->{rhs});
187 };
188 /^\+$/ && do {
189 return op_plus($node, $self->{lhs}, $self->{rhs});
190 };
191 /^-$/ && do {
192 return op_minus($node, $self->{lhs}, $self->{rhs});
193 };
194 /^div$/ && do {
195 return op_div($node, $self->{lhs}, $self->{rhs});
196 };
197 /^mod$/ && do {
198 return op_mod($node, $self->{lhs}, $self->{rhs});
199 };
200 /^\*$/ && do {
201 return op_mult($node, $self->{lhs}, $self->{rhs});
202 };
203 /^\|$/ && do {
204 return op_union($node, $self->{lhs}, $self->{rhs});
205 };
206  
207 die "No such operator, or operator unimplemented in ", $self->as_string, "\n";
208 }
209 }
210  
211 # Operators
212  
213 use XML::XPath::Boolean;
214  
215 sub op_or {
216 my ($node, $lhs, $rhs) = @_;
217 if($lhs->evaluate($node)->to_boolean->value) {
218 return XML::XPath::Boolean->True;
219 }
220 else {
221 return $rhs->evaluate($node)->to_boolean;
222 }
223 }
224  
225 sub op_and {
226 my ($node, $lhs, $rhs) = @_;
227 if( ! $lhs->evaluate($node)->to_boolean->value ) {
228 return XML::XPath::Boolean->False;
229 }
230 else {
231 return $rhs->evaluate($node)->to_boolean;
232 }
233 }
234  
235 sub op_equals {
236 my ($node, $lhs, $rhs) = @_;
237  
238 my $lh_results = $lhs->evaluate($node);
239 my $rh_results = $rhs->evaluate($node);
240  
241 if ($lh_results->isa('XML::XPath::NodeSet') &&
242 $rh_results->isa('XML::XPath::NodeSet')) {
243 # True if and only if there is a node in the
244 # first set and a node in the second set such
245 # that the result of performing the comparison
246 # on the string-values of the two nodes is true.
247 foreach my $lhnode ($lh_results->get_nodelist) {
248 foreach my $rhnode ($rh_results->get_nodelist) {
249 if ($lhnode->string_value eq $rhnode->string_value) {
250 return XML::XPath::Boolean->True;
251 }
252 }
253 }
254 return XML::XPath::Boolean->False;
255 }
256 elsif (($lh_results->isa('XML::XPath::NodeSet') ||
257 $rh_results->isa('XML::XPath::NodeSet')) &&
258 (!$lh_results->isa('XML::XPath::NodeSet') ||
259 !$rh_results->isa('XML::XPath::NodeSet'))) {
260 # (that says: one is a nodeset, and one is not a nodeset)
261  
262 my ($nodeset, $other);
263 if ($lh_results->isa('XML::XPath::NodeSet')) {
264 $nodeset = $lh_results;
265 $other = $rh_results;
266 }
267 else {
268 $nodeset = $rh_results;
269 $other = $lh_results;
270 }
271  
272 # True if and only if there is a node in the
273 # nodeset such that the result of performing
274 # the comparison on <type>(string_value($node))
275 # is true.
276 if ($other->isa('XML::XPath::Number')) {
277 foreach my $node ($nodeset->get_nodelist) {
278 if ($node->string_value == $other->value) {
279 return XML::XPath::Boolean->True;
280 }
281 }
282 }
283 elsif ($other->isa('XML::XPath::Literal')) {
284 foreach my $node ($nodeset->get_nodelist) {
285 if ($node->string_value eq $other->value) {
286 return XML::XPath::Boolean->True;
287 }
288 }
289 }
290 elsif ($other->isa('XML::XPath::Boolean')) {
291 if ($nodeset->to_boolean->value == $other->value) {
292 return XML::XPath::Boolean->True;
293 }
294 }
295  
296 return XML::XPath::Boolean->False;
297 }
298 else { # Neither is a nodeset
299 if ($lh_results->isa('XML::XPath::Boolean') ||
300 $rh_results->isa('XML::XPath::Boolean')) {
301 # if either is a boolean
302 if ($lh_results->to_boolean->value == $rh_results->to_boolean->value) {
303 return XML::XPath::Boolean->True;
304 }
305 return XML::XPath::Boolean->False;
306 }
307 elsif ($lh_results->isa('XML::XPath::Number') ||
308 $rh_results->isa('XML::XPath::Number')) {
309 # if either is a number
310 local $^W; # 'number' might result in undef
311 if ($lh_results->to_number->value == $rh_results->to_number->value) {
312 return XML::XPath::Boolean->True;
313 }
314 return XML::XPath::Boolean->False;
315 }
316 else {
317 if ($lh_results->to_literal->value eq $rh_results->to_literal->value) {
318 return XML::XPath::Boolean->True;
319 }
320 return XML::XPath::Boolean->False;
321 }
322 }
323 }
324  
325 sub op_nequals {
326 my ($node, $lhs, $rhs) = @_;
327 if (op_equals($node, $lhs, $rhs)->value) {
328 return XML::XPath::Boolean->False;
329 }
330 return XML::XPath::Boolean->True;
331 }
332  
333 sub op_le {
334 my ($node, $lhs, $rhs) = @_;
335 op_ge($node, $rhs, $lhs);
336 }
337  
338 sub op_ge {
339 my ($node, $lhs, $rhs) = @_;
340  
341 my $lh_results = $lhs->evaluate($node);
342 my $rh_results = $rhs->evaluate($node);
343  
344 if ($lh_results->isa('XML::XPath::NodeSet') &&
345 $rh_results->isa('XML::XPath::NodeSet')) {
346  
347 foreach my $lhnode ($lh_results->get_nodelist) {
348 foreach my $rhnode ($rh_results->get_nodelist) {
349 my $lhNum = XML::XPath::Number->new($lhnode->string_value);
350 my $rhNum = XML::XPath::Number->new($rhnode->string_value);
351 if ($lhNum->value >= $rhNum->value) {
352 return XML::XPath::Boolean->True;
353 }
354 }
355 }
356 return XML::XPath::Boolean->False;
357 }
358 elsif (($lh_results->isa('XML::XPath::NodeSet') ||
359 $rh_results->isa('XML::XPath::NodeSet')) &&
360 (!$lh_results->isa('XML::XPath::NodeSet') ||
361 !$rh_results->isa('XML::XPath::NodeSet'))) {
362 # (that says: one is a nodeset, and one is not a nodeset)
363  
364 if ($lh_results->isa('XML::XPath::NodeSet')) {
365 foreach my $node ($lh_results->get_nodelist) {
366 if ($node->to_number->value >= $rh_results->to_number->value) {
367 return XML::XPath::Boolean->True;
368 }
369 }
370 }
371 else {
372 foreach my $node ($rh_results->get_nodelist) {
373 if ( $lh_results->to_number->value >= $node->to_number->value) {
374 return XML::XPath::Boolean->True;
375 }
376 }
377 }
378  
379 return XML::XPath::Boolean->False;
380 }
381 else { # Neither is a nodeset
382 if ($lh_results->isa('XML::XPath::Boolean') ||
383 $rh_results->isa('XML::XPath::Boolean')) {
384 # if either is a boolean
385 if ($lh_results->to_boolean->to_number->value
386 >= $rh_results->to_boolean->to_number->value) {
387 return XML::XPath::Boolean->True;
388 }
389 }
390 else {
391 if ($lh_results->to_number->value >= $rh_results->to_number->value) {
392 return XML::XPath::Boolean->True;
393 }
394 }
395 return XML::XPath::Boolean->False;
396 }
397 }
398  
399 sub op_gt {
400 my ($node, $lhs, $rhs) = @_;
401  
402 my $lh_results = $lhs->evaluate($node);
403 my $rh_results = $rhs->evaluate($node);
404  
405 if ($lh_results->isa('XML::XPath::NodeSet') &&
406 $rh_results->isa('XML::XPath::NodeSet')) {
407  
408 foreach my $lhnode ($lh_results->get_nodelist) {
409 foreach my $rhnode ($rh_results->get_nodelist) {
410 my $lhNum = XML::XPath::Number->new($lhnode->string_value);
411 my $rhNum = XML::XPath::Number->new($rhnode->string_value);
412 if ($lhNum->value > $rhNum->value) {
413 return XML::XPath::Boolean->True;
414 }
415 }
416 }
417 return XML::XPath::Boolean->False;
418 }
419 elsif (($lh_results->isa('XML::XPath::NodeSet') ||
420 $rh_results->isa('XML::XPath::NodeSet')) &&
421 (!$lh_results->isa('XML::XPath::NodeSet') ||
422 !$rh_results->isa('XML::XPath::NodeSet'))) {
423 # (that says: one is a nodeset, and one is not a nodeset)
424  
425 if ($lh_results->isa('XML::XPath::NodeSet')) {
426 foreach my $node ($lh_results->get_nodelist) {
427 if ($node->to_number->value > $rh_results->to_number->value) {
428 return XML::XPath::Boolean->True;
429 }
430 }
431 }
432 else {
433 foreach my $node ($rh_results->get_nodelist) {
434 if ( $lh_results->to_number->value > $node->to_number->value) {
435 return XML::XPath::Boolean->True;
436 }
437 }
438 }
439  
440 return XML::XPath::Boolean->False;
441 }
442 else { # Neither is a nodeset
443 if ($lh_results->isa('XML::XPath::Boolean') ||
444 $rh_results->isa('XML::XPath::Boolean')) {
445 # if either is a boolean
446 if ($lh_results->to_boolean->value > $rh_results->to_boolean->value) {
447 return XML::XPath::Boolean->True;
448 }
449 }
450 else {
451 if ($lh_results->to_number->value > $rh_results->to_number->value) {
452 return XML::XPath::Boolean->True;
453 }
454 }
455 return XML::XPath::Boolean->False;
456 }
457 }
458  
459 sub op_lt {
460 my ($node, $lhs, $rhs) = @_;
461 op_gt($node, $rhs, $lhs);
462 }
463  
464 sub op_plus {
465 my ($node, $lhs, $rhs) = @_;
466 my $lh_results = $lhs->evaluate($node);
467 my $rh_results = $rhs->evaluate($node);
468  
469 my $result =
470 $lh_results->to_number->value
471 +
472 $rh_results->to_number->value
473 ;
474 return XML::XPath::Number->new($result);
475 }
476  
477 sub op_minus {
478 my ($node, $lhs, $rhs) = @_;
479 my $lh_results = $lhs->evaluate($node);
480 my $rh_results = $rhs->evaluate($node);
481  
482 my $result =
483 $lh_results->to_number->value
484 -
485 $rh_results->to_number->value
486 ;
487 return XML::XPath::Number->new($result);
488 }
489  
490 sub op_div {
491 my ($node, $lhs, $rhs) = @_;
492 my $lh_results = $lhs->evaluate($node);
493 my $rh_results = $rhs->evaluate($node);
494  
495 # handle zero devided cases.
496 if ($rh_results->to_number->value == 0) {
497 my $lv = $lh_results->to_number->value;
498 if ($lv == 0) {
499 return XML::XPath::Literal->new('NaN');
500 } elsif ($lv > 0) {
501 return XML::XPath::Literal->new('Infinity');
502 } elsif ($lv < 0) {
503 return XML::XPath::Literal->new('-Infinity');
504 }
505 }
506  
507 my $result = eval {
508 $lh_results->to_number->value
509 /
510 $rh_results->to_number->value
511 ;
512 };
513 if ($@) {
514 # assume divide by zero
515 # This is probably a terrible way to handle this!
516 # Ah well... who wants to live forever...
517 return XML::XPath::Literal->new('Infinity');
518 }
519 return XML::XPath::Number->new($result);
520 }
521  
522 sub op_mod {
523 my ($node, $lhs, $rhs) = @_;
524 my $lh_results = $lhs->evaluate($node);
525 my $rh_results = $rhs->evaluate($node);
526  
527 my $result =
528 $lh_results->to_number->value
529 %
530 $rh_results->to_number->value
531 ;
532 return XML::XPath::Number->new($result);
533 }
534  
535 sub op_mult {
536 my ($node, $lhs, $rhs) = @_;
537 my $lh_results = $lhs->evaluate($node);
538 my $rh_results = $rhs->evaluate($node);
539  
540 my $result =
541 $lh_results->to_number->value
542 *
543 $rh_results->to_number->value
544 ;
545 return XML::XPath::Number->new($result);
546 }
547  
548 sub op_union {
549 my ($node, $lhs, $rhs) = @_;
550 my $lh_result = $lhs->evaluate($node);
551 my $rh_result = $rhs->evaluate($node);
552  
553 if ($lh_result->isa('XML::XPath::NodeSet') &&
554 $rh_result->isa('XML::XPath::NodeSet')) {
555 my %found;
556 my $results = XML::XPath::NodeSet->new;
557 foreach my $lhnode ($lh_result->get_nodelist) {
558 $found{"$lhnode"}++;
559 $results->push($lhnode);
560 }
561 foreach my $rhnode ($rh_result->get_nodelist) {
562 $results->push($rhnode)
563 unless exists $found{"$rhnode"};
564 }
565 $results->sort;
566 return $results;
567 }
568 die "Both sides of a union must be Node Sets\n";
569 }
570  
571 sub filter_by_predicate {
572 my $self = shift;
573 my ($nodeset, $predicate) = @_;
574  
575 # See spec section 2.4, paragraphs 2 & 3:
576 # For each node in the node-set to be filtered, the predicate Expr
577 # is evaluated with that node as the context node, with the number
578 # of nodes in the node set as the context size, and with the
579 # proximity position of the node in the node set with respect to
580 # the axis as the context position.
581  
582 if (!ref($nodeset)) { # use ref because nodeset has a bool context
583 die "No nodeset!!!";
584 }
585  
586 # warn "Filter by predicate: $predicate\n";
587  
588 my $newset = XML::XPath::NodeSet->new();
589  
590 for(my $i = 1; $i <= $nodeset->size; $i++) {
591 # set context set each time 'cos a loc-path in the expr could change it
592 $self->{pp}->set_context_set($nodeset);
593 $self->{pp}->set_context_pos($i);
594 my $result = $predicate->evaluate($nodeset->get_node($i));
595 if ($result->isa('XML::XPath::Boolean')) {
596 if ($result->value) {
597 $newset->push($nodeset->get_node($i));
598 }
599 }
600 elsif ($result->isa('XML::XPath::Number')) {
601 if ($result->value == $i) {
602 $newset->push($nodeset->get_node($i));
603 }
604 }
605 else {
606 if ($result->to_boolean->value) {
607 $newset->push($nodeset->get_node($i));
608 }
609 }
610 }
611  
612 return $newset;
613 }
614  
615 1;