| 1 | package XML::XPath::Step; | ||
| 2 | |||
| 3 | $VERSION = '1.45'; | ||
| 4 | |||
| 5 | use XML::XPath::Parser; | ||
| 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.45'; | ||
| 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 | my $saved_context = $self->{pp}->get_context_set; | ||
| 135 | my $saved_pos = $self->{pp}->get_context_pos; | ||
| 136 | |||
| 137 | $self->{pp}->set_context_set($from); | ||
| 138 | |||
| 139 | my $initial_nodeset = XML::XPath::NodeSet->new(); | ||
| 140 | |||
| 141 | # See spec section 2.1, paragraphs 3,4,5: | ||
| 142 | # The node-set selected by the location step is the node-set | ||
| 143 | # that results from generating an initial node set from the | ||
| 144 | # axis and node-test, and then filtering that node-set by | ||
| 145 | # each of the predicates in turn. | ||
| 146 | |||
| 147 | # Make each node in the nodeset be the context node, one by one | ||
| 148 | for(my $i = 1; $i <= $from->size; $i++) { | ||
| 149 | $self->{pp}->set_context_pos($i); | ||
| 150 | $initial_nodeset->append($self->evaluate_node($from->get_node($i))); | ||
| 151 | } | ||
| 152 | |||
| 153 | # warn "Step::evaluate initial nodeset size: ", $initial_nodeset->size, "\n"; | ||
| 154 | |||
| 155 | $self->{pp}->set_context_set($saved_context); | ||
| 156 | $self->{pp}->set_context_pos($saved_pos); | ||
| 157 | |||
| 158 | $initial_nodeset->sort; | ||
| 159 | |||
| 160 | return $initial_nodeset; | ||
| 161 | } | ||
| 162 | |||
| 163 | # Evaluate the step against a particular node | ||
| 164 | sub evaluate_node { | ||
| 165 | my $self = shift; | ||
| 166 | my $context = shift; | ||
| 167 | |||
| 168 | # warn "Evaluate node: $self->{axis}\n"; | ||
| 169 | |||
| 170 | # warn "Node: ", $context->[node_name], "\n"; | ||
| 171 | |||
| 172 | my $method = $self->{axis_method}; | ||
| 173 | |||
| 174 | my $results = XML::XPath::NodeSet->new(); | ||
| 175 | no strict 'refs'; | ||
| 176 | eval { | ||
| 177 | $method->($self, $context, $results); | ||
| 178 | }; | ||
| 179 | if ($@) { | ||
| 180 | die "axis $method not implemented [$@]\n"; | ||
| 181 | } | ||
| 182 | |||
| 183 | # warn("results: ", join('><', map {$_->string_value} @$results), "\n"); | ||
| 184 | # filter initial nodeset by each predicate | ||
| 185 | foreach my $predicate (@{$self->{predicates}}) { | ||
| 186 | $results = $self->filter_by_predicate($results, $predicate); | ||
| 187 | } | ||
| 188 | |||
| 189 | return $results; | ||
| 190 | } | ||
| 191 | |||
| 192 | sub axis_ancestor { | ||
| 193 | my $self = shift; | ||
| 194 | my ($context, $results) = @_; | ||
| 195 | |||
| 196 | my $parent = $context->getParentNode; | ||
| 197 | |||
| 198 | START: | ||
| 199 | return $results unless $parent; | ||
| 200 | if (node_test($self, $parent)) { | ||
| 201 | $results->push($parent); | ||
| 202 | } | ||
| 203 | $parent = $parent->getParentNode; | ||
| 204 | goto START; | ||
| 205 | } | ||
| 206 | |||
| 207 | sub axis_ancestor_or_self { | ||
| 208 | my $self = shift; | ||
| 209 | my ($context, $results) = @_; | ||
| 210 | |||
| 211 | START: | ||
| 212 | return $results unless $context; | ||
| 213 | if (node_test($self, $context)) { | ||
| 214 | $results->push($context); | ||
| 215 | } | ||
| 216 | $context = $context->getParentNode; | ||
| 217 | goto START; | ||
| 218 | } | ||
| 219 | |||
| 220 | sub axis_attribute { | ||
| 221 | my $self = shift; | ||
| 222 | my ($context, $results) = @_; | ||
| 223 | |||
| 224 | foreach my $attrib (@{$context->getAttributes}) { | ||
| 225 | if ($self->test_attribute($attrib)) { | ||
| 226 | $results->push($attrib); | ||
| 227 | } | ||
| 228 | } | ||
| 229 | } | ||
| 230 | |||
| 231 | sub axis_child { | ||
| 232 | my $self = shift; | ||
| 233 | my ($context, $results) = @_; | ||
| 234 | |||
| 235 | foreach my $node (@{$context->getChildNodes}) { | ||
| 236 | if (node_test($self, $node)) { | ||
| 237 | $results->push($node); | ||
| 238 | } | ||
| 239 | } | ||
| 240 | } | ||
| 241 | |||
| 242 | sub axis_descendant { | ||
| 243 | my $self = shift; | ||
| 244 | my ($context, $results) = @_; | ||
| 245 | |||
| 246 | my @stack = $context->getChildNodes; | ||
| 247 | |||
| 248 | while (@stack) { | ||
| 249 | my $node = pop @stack; | ||
| 250 | if (node_test($self, $node)) { | ||
| 251 | $results->unshift($node); | ||
| 252 | } | ||
| 253 | push @stack, $node->getChildNodes; | ||
| 254 | } | ||
| 255 | } | ||
| 256 | |||
| 257 | sub axis_descendant_or_self { | ||
| 258 | my $self = shift; | ||
| 259 | my ($context, $results) = @_; | ||
| 260 | |||
| 261 | my @stack = ($context); | ||
| 262 | |||
| 263 | while (@stack) { | ||
| 264 | my $node = pop @stack; | ||
| 265 | if (node_test($self, $node)) { | ||
| 266 | $results->unshift($node); | ||
| 267 | } | ||
| 268 | push @stack, $node->getChildNodes; | ||
| 269 | } | ||
| 270 | } | ||
| 271 | |||
| 272 | sub axis_following { | ||
| 273 | my $self = shift; | ||
| 274 | my ($context, $results) = @_; | ||
| 275 | |||
| 276 | START: | ||
| 277 | |||
| 278 | my $parent = $context->getParentNode; | ||
| 279 | return $results unless $parent; | ||
| 280 | |||
| 281 | while ($context = $context->getNextSibling) { | ||
| 282 | axis_descendant_or_self($self, $context, $results); | ||
| 283 | } | ||
| 284 | |||
| 285 | $context = $parent; | ||
| 286 | goto START; | ||
| 287 | } | ||
| 288 | |||
| 289 | sub axis_following_sibling { | ||
| 290 | my $self = shift; | ||
| 291 | my ($context, $results) = @_; | ||
| 292 | |||
| 293 | while ($context = $context->getNextSibling) { | ||
| 294 | if (node_test($self, $context)) { | ||
| 295 | $results->push($context); | ||
| 296 | } | ||
| 297 | } | ||
| 298 | } | ||
| 299 | |||
| 300 | sub axis_namespace { | ||
| 301 | my $self = shift; | ||
| 302 | my ($context, $results) = @_; | ||
| 303 | |||
| 304 | return $results unless $context->isElementNode; | ||
| 305 | foreach my $ns (@{$context->getNamespaces}) { | ||
| 306 | if ($self->test_namespace($ns)) { | ||
| 307 | $results->push($ns); | ||
| 308 | } | ||
| 309 | } | ||
| 310 | } | ||
| 311 | |||
| 312 | sub axis_parent { | ||
| 313 | my $self = shift; | ||
| 314 | my ($context, $results) = @_; | ||
| 315 | |||
| 316 | my $parent = $context->getParentNode; | ||
| 317 | return $results unless $parent; | ||
| 318 | if (node_test($self, $parent)) { | ||
| 319 | $results->push($parent); | ||
| 320 | } | ||
| 321 | } | ||
| 322 | |||
| 323 | sub axis_preceding { | ||
| 324 | my $self = shift; | ||
| 325 | my ($context, $results) = @_; | ||
| 326 | |||
| 327 | # all preceding nodes in document order, except ancestors | ||
| 328 | |||
| 329 | START: | ||
| 330 | |||
| 331 | my $parent = $context->getParentNode; | ||
| 332 | return $results unless $parent; | ||
| 333 | |||
| 334 | while ($context = $context->getPreviousSibling) { | ||
| 335 | axis_descendant_or_self($self, $context, $results); | ||
| 336 | } | ||
| 337 | |||
| 338 | $context = $parent; | ||
| 339 | goto START; | ||
| 340 | } | ||
| 341 | |||
| 342 | sub axis_preceding_sibling { | ||
| 343 | my $self = shift; | ||
| 344 | my ($context, $results) = @_; | ||
| 345 | |||
| 346 | while ($context = $context->getPreviousSibling) { | ||
| 347 | if (node_test($self, $context)) { | ||
| 348 | $results->push($context); | ||
| 349 | } | ||
| 350 | } | ||
| 351 | } | ||
| 352 | |||
| 353 | sub axis_self { | ||
| 354 | my $self = shift; | ||
| 355 | my ($context, $results) = @_; | ||
| 356 | |||
| 357 | if (node_test($self, $context)) { | ||
| 358 | $results->push($context); | ||
| 359 | } | ||
| 360 | } | ||
| 361 | |||
| 362 | sub node_test { | ||
| 363 | my $self = shift; | ||
| 364 | my $node = shift; | ||
| 365 | |||
| 366 | # if node passes test, return true | ||
| 367 | |||
| 368 | my $test = $self->{test}; | ||
| 369 | |||
| 370 | return 1 if $test == test_nt_node; | ||
| 371 | |||
| 372 | if ($test == test_any) { | ||
| 373 | return 1 if $node->isElementNode && defined $node->getName; | ||
| 374 | } | ||
| 375 | |||
| 376 | local $^W; | ||
| 377 | |||
| 378 | if ($test == test_ncwild) { | ||
| 379 | return unless $node->isElementNode; | ||
| 380 | my $match_ns = $self->{pp}->get_namespace($self->{literal}, $node); | ||
| 381 | if (my $node_nsnode = $node->getNamespace()) { | ||
| 382 | return 1 if $match_ns eq $node_nsnode->getValue; | ||
| 383 | } | ||
| 384 | } | ||
| 385 | elsif ($test == test_qname) { | ||
| 386 | return unless $node->isElementNode; | ||
| 387 | if ($self->{literal} =~ /:/) { | ||
| 388 | my ($prefix, $name) = split(':', $self->{literal}, 2); | ||
| 389 | my $match_ns = $self->{pp}->get_namespace($prefix, $node); | ||
| 390 | if (my $node_nsnode = $node->getNamespace()) { | ||
| 391 | # warn "match: '$self->{literal}' match NS: '$match_ns' got NS: '", $node_nsnode->getValue, "'\n"; | ||
| 392 | return 1 if defined $match_ns && ($match_ns eq $node_nsnode->getValue) && | ||
| 393 | ($name eq $node->getLocalName); | ||
| 394 | } | ||
| 395 | } | ||
| 396 | else { | ||
| 397 | # warn "Node test: ", $node->getName, "\n"; | ||
| 398 | return 1 if $node->getName eq $self->{literal}; | ||
| 399 | } | ||
| 400 | } | ||
| 401 | elsif ($test == test_nt_text) { | ||
| 402 | return 1 if $node->isTextNode; | ||
| 403 | } | ||
| 404 | elsif ($test == test_nt_comment) { | ||
| 405 | return 1 if $node->isCommentNode; | ||
| 406 | } | ||
| 407 | # elsif ($test == test_nt_pi && !$self->{literal}) { | ||
| 408 | # warn "Unreachable code???"; | ||
| 409 | # return 1 if $node->isPINode; | ||
| 410 | # } | ||
| 411 | elsif ($test == test_nt_pi) { | ||
| 412 | return unless $node->isPINode; | ||
| 413 | if (my $val = $self->{literal}->value) { | ||
| 414 | return 1 if $node->getTarget eq $val; | ||
| 415 | } | ||
| 416 | else { | ||
| 417 | return 1; | ||
| 418 | } | ||
| 419 | } | ||
| 420 | |||
| 421 | return; # fallthrough returns false | ||
| 422 | } | ||
| 423 | |||
| 424 | sub test_attribute { | ||
| 425 | my $self = shift; | ||
| 426 | my $node = shift; | ||
| 427 | |||
| 428 | # warn "test_attrib: '$self->{test}' against: ", $node->getName, "\n"; | ||
| 429 | # warn "node type: $node->[node_type]\n"; | ||
| 430 | |||
| 431 | my $test = $self->{test}; | ||
| 432 | |||
| 433 | return 1 if ($test == test_attr_any) || ($test == test_nt_node); | ||
| 434 | |||
| 435 | if ($test == test_attr_ncwild) { | ||
| 436 | my $match_ns = $self->{pp}->get_namespace($self->{literal}, $node); | ||
| 437 | if (my $node_nsnode = $node->getNamespace()) { | ||
| 438 | return 1 if $match_ns eq $node_nsnode->getValue; | ||
| 439 | } | ||
| 440 | } | ||
| 441 | elsif ($test == test_attr_qname) { | ||
| 442 | if ($self->{literal} =~ /:/) { | ||
| 443 | my ($prefix, $name) = split(':', $self->{literal}, 2); | ||
| 444 | my $match_ns = $self->{pp}->get_namespace($prefix, $node); | ||
| 445 | if (my $node_nsnode = $node->getNamespace()) { | ||
| 446 | return 1 if ($match_ns eq $node_nsnode->getValue) && | ||
| 447 | ($name eq $node->getLocalName); | ||
| 448 | } | ||
| 449 | } | ||
| 450 | else { | ||
| 451 | return 1 if $node->getName eq $self->{literal}; | ||
| 452 | } | ||
| 453 | } | ||
| 454 | |||
| 455 | return; # fallthrough returns false | ||
| 456 | } | ||
| 457 | |||
| 458 | sub test_namespace { | ||
| 459 | my $self = shift; | ||
| 460 | my $node = shift; | ||
| 461 | |||
| 462 | # Not sure if this is correct. The spec seems very unclear on what | ||
| 463 | # constitutes a namespace test... bah! | ||
| 464 | |||
| 465 | my $test = $self->{test}; | ||
| 466 | |||
| 467 | return 1 if $test == test_any; # True for all nodes of principal type | ||
| 468 | |||
| 469 | if ($test == test_any) { | ||
| 470 | return 1; | ||
| 471 | } | ||
| 472 | elsif ($self->{literal} eq $node->getExpanded) { | ||
| 473 | return 1; | ||
| 474 | } | ||
| 475 | |||
| 476 | return; | ||
| 477 | } | ||
| 478 | |||
| 479 | sub filter_by_predicate { | ||
| 480 | my $self = shift; | ||
| 481 | my ($nodeset, $predicate) = @_; | ||
| 482 | |||
| 483 | # See spec section 2.4, paragraphs 2 & 3: | ||
| 484 | # For each node in the node-set to be filtered, the predicate Expr | ||
| 485 | # is evaluated with that node as the context node, with the number | ||
| 486 | # of nodes in the node set as the context size, and with the | ||
| 487 | # proximity position of the node in the node set with respect to | ||
| 488 | # the axis as the context position. | ||
| 489 | |||
| 490 | if (!ref($nodeset)) { # use ref because nodeset has a bool context | ||
| 491 | die "No nodeset!!!"; | ||
| 492 | } | ||
| 493 | |||
| 494 | # warn "Filter by predicate: $predicate\n"; | ||
| 495 | |||
| 496 | my $newset = XML::XPath::NodeSet->new(); | ||
| 497 | |||
| 498 | for(my $i = 1; $i <= $nodeset->size; $i++) { | ||
| 499 | # set context set each time 'cos a loc-path in the expr could change it | ||
| 500 | $self->{pp}->set_context_set($nodeset); | ||
| 501 | $self->{pp}->set_context_pos($i); | ||
| 502 | my $result = $predicate->evaluate($nodeset->get_node($i)); | ||
| 503 | if ($result->isa('XML::XPath::Boolean')) { | ||
| 504 | if ($result->value) { | ||
| 505 | $newset->push($nodeset->get_node($i)); | ||
| 506 | } | ||
| 507 | } | ||
| 508 | elsif ($result->isa('XML::XPath::Number')) { | ||
| 509 | if ($result->value == $i) { | ||
| 510 | $newset->push($nodeset->get_node($i)); | ||
| 511 | } | ||
| 512 | } | ||
| 513 | else { | ||
| 514 | if ($result->to_boolean->value) { | ||
| 515 | $newset->push($nodeset->get_node($i)); | ||
| 516 | } | ||
| 517 | } | ||
| 518 | } | ||
| 519 | |||
| 520 | return $newset; | ||
| 521 | } | ||
| 522 | |||
| 523 | 1; |