| 1 |
# $Id: XPathContext.pm 422 2002-11-08 17:10:30Z phish $ |
| 2 |
# |
| 3 |
# This is free software, you may use it and distribute it under the same terms as |
| 4 |
# Perl itself. |
| 5 |
# |
| 6 |
# Copyright 2001-2003 AxKit.com Ltd., 2002-2006 Christian Glahn, 2006-2009 Petr Pajas |
| 7 |
# |
| 8 |
# |
| 9 |
|
| 10 |
package XML::LibXML::XPathContext; |
| 11 |
|
| 12 |
use strict; |
| 13 |
use warnings; |
| 14 |
use vars qw($VERSION @ISA $USE_LIBXML_DATA_TYPES); |
| 15 |
|
| 16 |
use Carp; |
| 17 |
use XML::LibXML; |
| 18 |
use XML::LibXML::NodeList; |
| 19 |
|
| 20 |
$VERSION = "2.0134"; # VERSION TEMPLATE: DO NOT CHANGE |
| 21 |
|
| 22 |
# should LibXML XPath data types be used for simple objects |
| 23 |
# when passing parameters to extension functions (default: no) |
| 24 |
$USE_LIBXML_DATA_TYPES = 0; |
| 25 |
|
| 26 |
sub CLONE_SKIP { 1 } |
| 27 |
|
| 28 |
sub findnodes { |
| 29 |
my ($self, $xpath, $node) = @_; |
| 30 |
|
| 31 |
my @nodes = $self->_guarded_find_call('_findnodes', $node, $xpath); |
| 32 |
|
| 33 |
if (wantarray) { |
| 34 |
return @nodes; |
| 35 |
} |
| 36 |
else { |
| 37 |
return XML::LibXML::NodeList->new(@nodes); |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
sub find { |
| 42 |
my ($self, $xpath, $node) = @_; |
| 43 |
|
| 44 |
my ($type, @params) = $self->_guarded_find_call('_find', $node, $xpath,0); |
| 45 |
|
| 46 |
if ($type) { |
| 47 |
return $type->new(@params); |
| 48 |
} |
| 49 |
return undef; |
| 50 |
} |
| 51 |
|
| 52 |
sub exists { |
| 53 |
my ($self, $xpath, $node) = @_; |
| 54 |
my (undef, $value) = $self->_guarded_find_call('_find', $node, $xpath,1); |
| 55 |
return $value; |
| 56 |
} |
| 57 |
|
| 58 |
sub findvalue { |
| 59 |
my $self = shift; |
| 60 |
return $self->find(@_)->to_literal->value; |
| 61 |
} |
| 62 |
|
| 63 |
sub _guarded_find_call { |
| 64 |
my ($self, $method, $node)=(shift,shift,shift); |
| 65 |
|
| 66 |
my $prev_node; |
| 67 |
if (ref($node)) { |
| 68 |
$prev_node = $self->getContextNode(); |
| 69 |
$self->setContextNode($node); |
| 70 |
} |
| 71 |
my @ret; |
| 72 |
eval { |
| 73 |
@ret = $self->$method(@_); |
| 74 |
}; |
| 75 |
$self->_free_node_pool; |
| 76 |
$self->setContextNode($prev_node) if ref($node); |
| 77 |
|
| 78 |
if ($@) { |
| 79 |
my $err = $@; |
| 80 |
chomp $err; |
| 81 |
croak $err; |
| 82 |
} |
| 83 |
|
| 84 |
return @ret; |
| 85 |
} |
| 86 |
|
| 87 |
sub registerFunction { |
| 88 |
my ($self, $name, $sub) = @_; |
| 89 |
$self->registerFunctionNS($name, undef, $sub); |
| 90 |
return; |
| 91 |
} |
| 92 |
|
| 93 |
sub unregisterNs { |
| 94 |
my ($self, $prefix) = @_; |
| 95 |
$self->registerNs($prefix, undef); |
| 96 |
return; |
| 97 |
} |
| 98 |
|
| 99 |
sub unregisterFunction { |
| 100 |
my ($self, $name) = @_; |
| 101 |
$self->registerFunctionNS($name, undef, undef); |
| 102 |
return; |
| 103 |
} |
| 104 |
|
| 105 |
sub unregisterFunctionNS { |
| 106 |
my ($self, $name, $ns) = @_; |
| 107 |
$self->registerFunctionNS($name, $ns, undef); |
| 108 |
return; |
| 109 |
} |
| 110 |
|
| 111 |
sub unregisterVarLookupFunc { |
| 112 |
my ($self) = @_; |
| 113 |
$self->registerVarLookupFunc(undef, undef); |
| 114 |
return; |
| 115 |
} |
| 116 |
|
| 117 |
# extension function perl dispatcher |
| 118 |
# borrowed from XML::LibXSLT |
| 119 |
|
| 120 |
sub _perl_dispatcher { |
| 121 |
my $func = shift; |
| 122 |
my @params = @_; |
| 123 |
my @perlParams; |
| 124 |
|
| 125 |
my $i = 0; |
| 126 |
while (@params) { |
| 127 |
my $type = shift(@params); |
| 128 |
if ($type eq 'XML::LibXML::Literal' or |
| 129 |
$type eq 'XML::LibXML::Number' or |
| 130 |
$type eq 'XML::LibXML::Boolean') |
| 131 |
{ |
| 132 |
my $val = shift(@params); |
| 133 |
unshift(@perlParams, $USE_LIBXML_DATA_TYPES ? $type->new($val) : $val); |
| 134 |
} |
| 135 |
elsif ($type eq 'XML::LibXML::NodeList') { |
| 136 |
my $node_count = shift(@params); |
| 137 |
unshift(@perlParams, $type->new(splice(@params, 0, $node_count))); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
$func = "main::$func" unless ref($func) || $func =~ /(.+)::/; |
| 142 |
no strict 'refs'; |
| 143 |
my $res = $func->(@perlParams); |
| 144 |
return $res; |
| 145 |
} |
| 146 |
|
| 147 |
1; |