File Coverage

blib/lib/XML/XPath/Simple.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
1             package XML::XPath::Simple;
2             #############################################################
3             # XML::XPath::Simple
4             # Whyte.Wolf Simple XPath Module
5             # Version 0.05
6             #
7             # Copyright (c) 2002 by S.D. Campbell
8             #
9             # Created 26 March 2002; Revised 30 March 2002 by SDC
10             #
11             # Description:
12             # A perl module which can be used for simple parsing of
13             # XPath expressions.
14             #
15             #############################################################
16             #
17             # This program is free software; you can redistribute it and/or
18             # modify it under the terms of the GNU General Public License
19             # as published by the Free Software Foundation; either version 2
20             # of the License, or (at your option) any later version.
21             #
22             # This program is distributed in the hope that it will be useful,
23             # but WITHOUT ANY WARRANTY; without even the implied warranty of
24             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25             # GNU General Public License for more details.
26             #
27             # You should have received a copy of the GNU General Public License
28             # along with this program; if not, write to the Free Software
29             # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30             #############################################################
31            
32 1     1   20070 use Exporter;
  1         2  
  1         41  
33 1     1   4 use Carp;
  1         2  
  1         54  
34 1     1   341 use XML::Simple;
  0            
  0            
35            
36             @ISA = qw(Exporter);
37             @EXPORT = qw();
38             @EXPORT_OK = qw();
39            
40             use strict;
41             use vars qw($VERSION @ISA @EXPORT @EXPORT_OK $errmsg);
42            
43             $VERSION = '0.05';
44             $errmsg = "";
45            
46            
47             #############################################################
48             # new
49             #
50             # The constructor for the class. Requires a string containing
51             # XML. Returns a reference to the new object or undef
52             # on error. Error can be retrieved from $XML::XPath::Simple::errmsg
53            
54             sub new {
55             my $class = shift;
56             my %params = @_;
57             my %xmlopts = ( keeproot => '1',
58             keyattr => [],
59             forcecontent => '1',
60             forcearray => '1',
61             contentkey => 'text',
62             );
63            
64             my $self = {};
65            
66             if($params{xml}){
67             $$self{xml} = $params{xml};
68             if($params{context}){
69             $$self{context} = $params{context};
70             } else {
71             $$self{context} = '/';
72             }
73             $$self{ref} = XMLin($$self{xml}, %xmlopts);
74            
75             bless $self, $class;
76             return $self;
77             }else{
78             $errmsg = "XML::XPath::Simple -- No XML to parse: This module requires an XML string.";
79             return undef;
80             }
81             }
82            
83             #############################################################
84             # context
85             #
86             # Allows the user to get/set the context. Returns
87             # current default context, or undef if passed a new
88             # context to set.
89            
90             sub context {
91             my $self = shift;
92             if(scalar(@_) == 0) {
93             return $$self{context};
94             } else {
95             my $path = shift;
96             $$self{context} = $path;
97             }
98             return undef;
99             }
100            
101             #############################################################
102             # find
103             #
104             # Returns true if the node specified by the path exists
105             # false otherwise.
106            
107             sub find {
108             my $self = shift;
109             my $path = shift;
110             my $ref = $$self{ref};
111             $path = $self->_convert($path);
112            
113             if (eval($path)) {
114             return 'true';
115             } else {
116             return 'false';
117             }
118             }
119            
120             #############################################################
121             # valueof
122             #
123             # Returns the value of the node at the path specified
124            
125             sub valueof {
126             my $self = shift;
127             my $path = shift;
128             my $ref = $$self{ref};
129             $path = $self->_convert($path);
130             if (ref(eval($path))){
131             $path .= '->{text}';
132             }
133            
134             return eval($path);
135             }
136            
137             #############################################################
138             # _convert
139             #
140             # An internal subroutine that converts XPaths into
141             # XML::Simple hash references.
142            
143             sub _convert {
144            
145             my $self = shift;
146             my $path = shift;
147             if(substr($path, 0, 1) ne '/'){
148             if(substr($path, 0, 2) eq '..'){
149             my $context = $$self{context};
150             $context =~ s/\/(\w*|@\w*|\w*\[\w*\])$//igs;
151             $path =~ s/^..//is;
152             $path = $context . $path;
153             } elsif($path eq '.'){
154             $path = $$self{context};
155             } elsif(substr($path, 0, 1) eq '@'){
156             $path = $$self{context} . $path;
157             } else {
158             $path = $$self{context} . '/' . $path;
159             }
160             }
161            
162             $path =~ s/\/(\w*)\[(\d*)\]/->{$1}[($2 - 1)]/igs;
163             $path =~ s/\/(\w*)/->{$1}[0]/igs;
164             $path =~ s/@(\w*)/->{$1}/igs;
165             $path = '$ref' . $path;
166            
167             return $path;
168            
169             }
170            
171             1;
172             __END__