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 67     67   474 use strict;
  67         136  
  67         2047  
13 67     67   366 use warnings;
  67         159  
  67         2071  
14 67     67   385 use vars qw($VERSION @ISA $USE_LIBXML_DATA_TYPES);
  67         167  
  67         3813  
15              
16 67     67   408 use Carp;
  67         151  
  67         4277  
17 67     67   438 use XML::LibXML;
  67         193  
  67         2066  
18 67     67   395 use XML::LibXML::NodeList;
  67         153  
  67         52679  
19              
20             $VERSION = "2.0209"; # 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 4085 my ($self, $xpath, $node) = @_;
30              
31 61         143 my @nodes = $self->_guarded_find_call('_findnodes', $node, $xpath);
32              
33 59 100       155 if (wantarray) {
34 13         209 return @nodes;
35             }
36             else {
37 46         145 return XML::LibXML::NodeList->new(@nodes);
38             }
39             }
40              
41             sub find {
42 66     66 1 3704 my ($self, $xpath, $node) = @_;
43              
44 66         145 my ($type, @params) = $self->_guarded_find_call('_find', $node, $xpath,0);
45              
46 56 50       175 if ($type) {
47 56         306 return $type->new(@params);
48             }
49 0         0 return undef;
50             }
51              
52             sub exists {
53 3     3 1 11 my ($self, $xpath, $node) = @_;
54 3         8 my (undef, $value) = $self->_guarded_find_call('_find', $node, $xpath,1);
55 3         15 return $value;
56             }
57              
58             sub findvalue {
59 44     44 1 4385 my $self = shift;
60 44         111 return $self->find(@_)->to_literal->value;
61             }
62              
63             sub _guarded_find_call {
64 130     130   258 my ($self, $method, $node)=(shift,shift,shift);
65              
66 130         183 my $prev_node;
67 130 100       324 if (ref($node)) {
68 17         45 $prev_node = $self->getContextNode();
69 17         42 $self->setContextNode($node);
70             }
71 130         199 my @ret;
72 130         203 eval {
73 130         3456 @ret = $self->$method(@_);
74             };
75 130         815 $self->_free_node_pool;
76 130 100       306 $self->setContextNode($prev_node) if ref($node);
77              
78 130 100       278 if ($@) {
79 12         25 my $err = $@;
80 12         31 chomp $err;
81 12         1362 croak $err;
82             }
83              
84 118         962 return @ret;
85             }
86              
87             sub registerFunction {
88 10     10 1 1507 my ($self, $name, $sub) = @_;
89 10         83 $self->registerFunctionNS($name, undef, $sub);
90 10         26 return;
91             }
92              
93             sub unregisterNs {
94 3     3 1 9 my ($self, $prefix) = @_;
95 3         17 $self->registerNs($prefix, undef);
96 3         6 return;
97             }
98              
99             sub unregisterFunction {
100 2     2 1 7 my ($self, $name) = @_;
101 2         18 $self->registerFunctionNS($name, undef, undef);
102 2         5 return;
103             }
104              
105             sub unregisterFunctionNS {
106 1     1 1 4 my ($self, $name, $ns) = @_;
107 1         12 $self->registerFunctionNS($name, $ns, undef);
108 1         3 return;
109             }
110              
111             sub unregisterVarLookupFunc {
112 1     1 1 6 my ($self) = @_;
113 1         6 $self->registerVarLookupFunc(undef, undef);
114 1         2 return;
115             }
116              
117             # extension function perl dispatcher
118             # borrowed from XML::LibXSLT
119              
120             sub _perl_dispatcher {
121 32     32   83 my $func = shift;
122 32         220 my @params = @_;
123 32         46 my @perlParams;
124              
125 32         48 my $i = 0;
126 32         81 while (@params) {
127 23         40 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         16 my $val = shift(@params);
133 12 50       37 unshift(@perlParams, $USE_LIBXML_DATA_TYPES ? $type->new($val) : $val);
134             }
135             elsif ($type eq 'XML::LibXML::NodeList') {
136 11         17 my $node_count = shift(@params);
137 11         116 unshift(@perlParams, $type->new(splice(@params, 0, $node_count)));
138             }
139             }
140              
141 32 100 66     95 $func = "main::$func" unless ref($func) || $func =~ /(.+)::/;
142 67     67   544 no strict 'refs';
  67         201  
  67         5254  
143 32         82 my $res = $func->(@perlParams);
144 31         18844 return $res;
145             }
146              
147             1;