File Coverage

blib/lib/XML/LibXML/XPathContext.pm
Criterion Covered Total %
statement 79 81 97.5
branch 15 18 83.3
condition 7 9 77.7
subroutine 18 19 94.7
pod 9 9 100.0
total 128 136 94.1


line stmt bran cond sub pod time code
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 66     66   472 use strict;
  66         140  
  66         2055  
13 66     66   339 use warnings;
  66         130  
  66         2164  
14 66     66   379 use vars qw($VERSION @ISA $USE_LIBXML_DATA_TYPES);
  66         145  
  66         4055  
15              
16 66     66   456 use Carp;
  66         152  
  66         4819  
17 66     66   479 use XML::LibXML;
  66         180  
  66         2263  
18 66     66   401 use XML::LibXML::NodeList;
  66         170  
  66         51690  
19              
20             $VERSION = "2.0207"; # 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 0     0   0 sub CLONE_SKIP { 1 }
27              
28             sub findnodes {
29 61     61 1 4263 my ($self, $xpath, $node) = @_;
30              
31 61         144 my @nodes = $self->_guarded_find_call('_findnodes', $node, $xpath);
32              
33 59 100       166 if (wantarray) {
34 13         212 return @nodes;
35             }
36             else {
37 46         162 return XML::LibXML::NodeList->new(@nodes);
38             }
39             }
40              
41             sub find {
42 66     66 1 3730 my ($self, $xpath, $node) = @_;
43              
44 66         163 my ($type, @params) = $self->_guarded_find_call('_find', $node, $xpath,0);
45              
46 56 50       171 if ($type) {
47 56         337 return $type->new(@params);
48             }
49 0         0 return undef;
50             }
51              
52             sub exists {
53 3     3 1 10 my ($self, $xpath, $node) = @_;
54 3         9 my (undef, $value) = $self->_guarded_find_call('_find', $node, $xpath,1);
55 3         19 return $value;
56             }
57              
58             sub findvalue {
59 44     44 1 4678 my $self = shift;
60 44         121 return $self->find(@_)->to_literal->value;
61             }
62              
63             sub _guarded_find_call {
64 130     130   279 my ($self, $method, $node)=(shift,shift,shift);
65              
66 130         183 my $prev_node;
67 130 100       330 if (ref($node)) {
68 17         94 $prev_node = $self->getContextNode();
69 17         59 $self->setContextNode($node);
70             }
71 130         243 my @ret;
72 130         195 eval {
73 130         3529 @ret = $self->$method(@_);
74             };
75 130         817 $self->_free_node_pool;
76 130 100       341 $self->setContextNode($prev_node) if ref($node);
77              
78 130 100       273 if ($@) {
79 12         25 my $err = $@;
80 12         32 chomp $err;
81 12         1690 croak $err;
82             }
83              
84 118         943 return @ret;
85             }
86              
87             sub registerFunction {
88 10     10 1 1585 my ($self, $name, $sub) = @_;
89 10         76 $self->registerFunctionNS($name, undef, $sub);
90 10         22 return;
91             }
92              
93             sub unregisterNs {
94 3     3 1 13 my ($self, $prefix) = @_;
95 3         21 $self->registerNs($prefix, undef);
96 3         8 return;
97             }
98              
99             sub unregisterFunction {
100 2     2 1 6 my ($self, $name) = @_;
101 2         20 $self->registerFunctionNS($name, undef, undef);
102 2         4 return;
103             }
104              
105             sub unregisterFunctionNS {
106 1     1 1 4 my ($self, $name, $ns) = @_;
107 1         13 $self->registerFunctionNS($name, $ns, undef);
108 1         3 return;
109             }
110              
111             sub unregisterVarLookupFunc {
112 1     1 1 3 my ($self) = @_;
113 1         6 $self->registerVarLookupFunc(undef, undef);
114 1         3 return;
115             }
116              
117             # extension function perl dispatcher
118             # borrowed from XML::LibXSLT
119              
120             sub _perl_dispatcher {
121 32     32   85 my $func = shift;
122 32         223 my @params = @_;
123 32         57 my @perlParams;
124              
125 32         50 my $i = 0;
126 32         77 while (@params) {
127 23         43 my $type = shift(@params);
128 23 100 100     115 if ($type eq 'XML::LibXML::Literal' or
    50 66        
129             $type eq 'XML::LibXML::Number' or
130             $type eq 'XML::LibXML::Boolean')
131             {
132 12         17 my $val = shift(@params);
133 12 50       40 unshift(@perlParams, $USE_LIBXML_DATA_TYPES ? $type->new($val) : $val);
134             }
135             elsif ($type eq 'XML::LibXML::NodeList') {
136 11         18 my $node_count = shift(@params);
137 11         157 unshift(@perlParams, $type->new(splice(@params, 0, $node_count)));
138             }
139             }
140              
141 32 100 66     93 $func = "main::$func" unless ref($func) || $func =~ /(.+)::/;
142 66     66   577 no strict 'refs';
  66         177  
  66         5147  
143 32         78 my $res = $func->(@perlParams);
144 31         18353 return $res;
145             }
146              
147             1;