blib/lib/XML/XPath/Simple.pm

filter: Statementstmt 94.3% 67 / 71 Branchbran 83.3% 15 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 10 / 10 Podpod 75.0% 3 / 4 totaltotal 92.2% 95 / 103 CC 10 SCAR 10.0
CC
10
Cov
92%
_convert
5.0
new
3.2
find
2.0
CRAP 2.7
↑ Summary
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 <whytwolf@spots.ab.ca>
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 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 1 ▶use XML::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
35
36 1 @ISA = qw(Exporter);
37 1 @EXPORT = qw();
38 1 @EXPORT_OK = qw();
39
40 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 1 ▶use vars qw($VERSION @ISA @EXPORT @EXPORT_OK $errmsg);
Subroutine BEGIN: called CC=1 SCAR=0.0
42
43 1 $VERSION = '0.05';
44 1 $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 1 ▶sub new {
Subroutine new: called CC=3 SCAR=11.6
Pod undocumented
55 1 my $class = shift;
56 1 my %params = @_;
57 1 my %xmlopts = ( keeproot => '1',
58 keyattr => [],
59 forcecontent => '1',
60 forcearray => '1',
61 contentkey => 'text',
62 );
63
64 1 my $self = {};
65
66 1 ▶ if($params{xml}){
Branch: 1 branch
BranchTrueFalseTotal
if ($params{'xml'}) { } 1 0 1
67 1 $$self{xml} = $params{xml};
68 1 ▶ if($params{context}){
Branch: 1 branch
BranchTrueFalseTotal
if ($params{'context'}) { } 1 0 1
69 1 $$self{context} = $params{context};
70 } else {
71 0 $$self{context} = '/';
72 }
73 1 $$self{ref} = XMLin($$self{xml}, %xmlopts);
74
75 1 bless $self, $class;
76 1 return $self;
77 }else{
78 0 $errmsg = "XML::XPath::Simple -- No XML to parse: This module requires an XML string.";
79 0 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 4 ▶sub context {
Subroutine context: called CC=2 SCAR=6.9
Pod documented
91 4 my $self = shift;
92 4 ▶ if(scalar(@_) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ == 0) { } 2 2 4
93 2 return $$self{context};
94 } else {
95 2 my $path = shift;
96 2 $$self{context} = $path;
97 }
98 2 return undef;
99 }
100
101 #############################################################
102 # find
103 #
104 # Returns true if the node specified by the path exists
105 # false otherwise.
106
107 1 ▶sub find {
Subroutine find: called CC=2 SCAR=7.1
Pod documented
108 1 my $self = shift;
109 1 my $path = shift;
110 1 my $ref = $$self{ref};
111 1 $path = $self->_convert($path);
112
113 1 ▶ if (eval($path)) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval $path) { } 1 0 1
114 1 return 'true';
115 } else {
116 0 return 'false';
117 }
118 }
119
120 #############################################################
121 # valueof
122 #
123 # Returns the value of the node at the path specified
124
125 7 ▶sub valueof {
Subroutine valueof: called CC=2 SCAR=6.9
Pod documented
126 7 my $self = shift;
127 7 my $path = shift;
128 7 my $ref = $$self{ref};
129 7 $path = $self->_convert($path);
130 7 ▶ if (ref(eval($path))){
Branch: 1 branch
BranchTrueFalseTotal
if (ref eval $path) 3 4 7
131 3 $path .= '->{text}';
132 }
133
134 7 return eval($path);
135 }
136
137 #############################################################
138 # _convert
139 #
140 # An internal subroutine that converts XPaths into
141 # XML::Simple hash references.
142
143 8 ▶sub _convert {
Subroutine _convert: called CC=5 SCAR=16.1
144
145 8 my $self = shift;
146 8 my $path = shift;
147 8 ▶ if(substr($path, 0, 1) ne '/'){
Branch: 1 branch
BranchTrueFalseTotal
if (substr($path, 0, 1) ne "/") 4 4 8
148 4 ▶ if(substr($path, 0, 2) eq '..'){
Branch: 3 branches
BranchTrueFalseTotal
if (substr($path, 0, 2) eq '..') { } 1 3 4
elsif ($path eq '.') { } 1 2 3
elsif (substr($path, 0, 1) eq '@') { } 1 1 2
149 1 my $context = $$self{context};
150 1 $context =~ s/\/(\w*|@\w*|\w*\[\w*\])$//igs;
151 1 $path =~ s/^..//is;
152 1 $path = $context . $path;
153 } elsif($path eq '.'){
154 1 $path = $$self{context};
155 } elsif(substr($path, 0, 1) eq '@'){
156 1 $path = $$self{context} . $path;
157 } else {
158 1 $path = $$self{context} . '/' . $path;
159 }
160 }
161
162 8 $path =~ s/\/(\w*)\[(\d*)\]/->{$1}[($2 - 1)]/igs;
163 8 $path =~ s/\/(\w*)/->{$1}[0]/igs;
164 8 $path =~ s/@(\w*)/->{$1}/igs;
165 8 $path = '$ref' . $path;
166
167 8 return $path;
168
169 }
170
171 1 1;
172 __END__
↑ Summary