blib/lib/XML/Stream/XPath/Query.pm

filter: Statementstmt 84.4% 157 / 186 Branchbran 79.0% 68 / 86 Conditioncond 55.5% 5 / 9 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 91.6% 11 / 12 Podpod 0.0% 0 / 7 totaltotal 79.0% 242 / 306 SCAR 41.5
CC
49
Cov
82%
getOp
58.2
new
3.2
getNextToken
3.0
CRAP 63.4
« blib/lib/XML/Stream/XPath/Op.pm ↑ Summary blib/lib/XML/Stream/Tools.pm »
1 ##############################################################################
2 #
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Library General Public
5 # License as published by the Free Software Foundation; either
6 # version 2 of the License, or (at your option) any later version.
7 #
8 # This library is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 # Library General Public License for more details.
12 #
13 # You should have received a copy of the GNU Library General Public
14 # License along with this library; if not, write to the
15 # Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 # Boston, MA 02111-1307, USA.
17 #
18 # Jabber
19 # Copyright (C) 1998-2004 Jabber Software Foundation http://jabber.org/
20 #
21 ##############################################################################
22  
23 package XML::Stream::XPath::Query;
24  
25 12 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 12 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 12 ▶use vars qw( $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 12 $VERSION = "1.23_07";
32  
33 sub new
34 {
35 105 ▶ my $proto = shift;
Subroutine new: called CC=3 SCAR=11.7
Pod undocumented
36 105 my $self = { };
37  
38 105 bless($self,$proto);
39  
40 105 $self->{TOKENS} = [ '/','[',']','@','"',"'",'=','!','(',')',':',' ',','];
41 105 $self->{QUERY} = shift;
42
43 105 ▶ if (!defined($self->{QUERY}) || ($self->{QUERY} eq ""))
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"QUERY"} or $self->{"QUERY"} eq "") 0 105 105
Condition33%1/3
not defined $self->{"QUERY"} or $self->{"QUERY"} eq ""
l!l&&r!l&&!r
00105
MC/DC0%0/2
not defined $self->{"QUERY"} or $self->{"QUERY"} eq ""
not defined $self->{"QUERY"} $self->{"QUERY"} eq ""
Truth table33%1/3
not(A) or B
A= not defined $self->{"QUERY"}B= $self->{"QUERY"} eq ""
ABresult
1X1
011
000
44 {
45 0 confess("No query string specified");
46 }
47
48 105 $self->parseQuery();
49
50 105 return $self;
51 }
52  
53  
54 sub getNextToken
55 {
56 1218 ▶ my $self = shift;
Subroutine getNextToken: called CC=3 SCAR=11.0
Pod undocumented
57 1218 my $pos = shift;
58  
59 1218 my @toks = grep{ $_ eq substr($self->{QUERY},$$pos,1)} @{$self->{TOKENS}};
60 1218 ▶ while( $#toks == -1 )
Branch: 1 branch
BranchTrueFalseTotal
if ($#toks == -1) 1492 1200 2692
61 {
62 1492 $$pos++;
63 1492 ▶ if ($$pos > length($self->{QUERY}))
Branch: 1 branch
BranchTrueFalseTotal
if ($$pos > length $self->{"QUERY"}) 18 1474 1492
64 {
65 18 $$pos = length($self->{QUERY});
66 18 return 0;
67 }
68 1474 @toks = grep{ $_ eq substr($self->{QUERY},$$pos,1)} @{$self->{TOKENS}};
69 }
70  
71 1200 return $toks[0];
72 }
73  
74  
75 sub getNextIdentifier
76 {
77 321 ▶ my $self = shift;
Subroutine getNextIdentifier: called CC=1 SCAR=0.0
Pod undocumented
78 321 my $pos = shift;
79 321 my $sp = $$pos;
80 321 $self->getNextToken($pos);
81 321 return substr($self->{QUERY},$sp,$$pos-$sp);
82 }
83  
84  
85 sub getOp
86 {
87 493 ▶ my $self = shift;
Subroutine getOp: called CC=41 SCAR=40.6
Pod undocumented
88 493 my $pos = shift;
89 493 my $in_context = shift;
90 493 ▶ $in_context = 0 unless defined($in_context);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $in_context 323 170 493
91  
92 493 my $ret_op;
93  
94 493 my $loop = 1;
95 493 ▶ while( $loop )
Branch: 1 branch
BranchTrueFalseTotal
if ($loop) 813 493 1306
96 {
97 813 my $pos_start = $$pos;
98  
99 813 my $token = $self->getNextToken($pos);
100 813 ▶ if (($token eq "0") && $in_context)
Branch: 1 branch
BranchTrueFalseTotal
if ($token eq 0 and $in_context) 0 813 813
Condition66%2/3
$token eq 0 and $in_context
!ll&&!rl&&r
81210
MC/DC0%0/2
$token eq 0 and $in_context
$token eq 0 $in_context
Truth table66%2/3
A and B
A= $token eq 0B= $in_context
ABresult
0X0
100
111
101 {
102 0 return;
103 }
104  
105 813 my $token_start = ++$$pos;
106 813 my $ident;
107
108 813 ▶ if (defined($token))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $token) { } 813 0 813
109 {
110  
111 813 ▶ if ($pos_start != ($token_start-1))
Branch: 12 branches
BranchTrueFalseTotal
if ($pos_start != $token_start - 1) { } 43 770 813
elsif ($token eq '/') { } 46 724 770
elsif ($token eq '@') { } 156 568 724
elsif ($token eq ']') { } 84 484 568
elsif ($token eq '"' or $token eq q[']) { } 148 336 484
elsif ($token eq ' ') { } 76 260 336
elsif ($token eq '[') { } 84 176 260
elsif ($token eq '(') { } 14 162 176
elsif ($token eq ')') { } 14 148 162
elsif ($token eq ',') { } 2 146 148
elsif ($token eq '=') { } 144 2 146
elsif ($token eq '!') { } 2 0 2
112 {
113 43 $$pos = $pos_start;
114 43 my $temp_ident = $self->getNextIdentifier($pos);
115 43 $ret_op = XML::Stream::XPath::NodeOp->new($temp_ident,"0");
116 }
117 elsif ($token eq "/")
118 {
119 46 ▶ if (substr($self->{QUERY},$token_start,1) eq "/")
Branch: 1 branch
BranchTrueFalseTotal
if (substr($self->{'QUERY'}, $token_start, 1) eq '/') { } 14 32 46
120 {
121 14 $$pos++;
122 14 my $temp_ident = $self->getNextIdentifier($pos);
123 14 $ret_op = XML::Stream::XPath::AllOp->new($temp_ident);
124 }
125 else
126 {
127 32 my $temp_ident = $self->getNextIdentifier($pos);
128 32 ▶ if ($temp_ident ne "")
Branch: 1 branch
BranchTrueFalseTotal
if ($temp_ident ne "") 26 6 32
129 {
130 26 ▶ $ret_op = XML::Stream::XPath::NodeOp->new($temp_ident,($pos_start == 0 ? "1" : "0"));
Branch: 1 branch
BranchTrueFalseTotal
$pos_start == 0 ? : 0 26 26
131 }
132 }
133 }
134 elsif ($token eq "\@")
135 {
136 156 $ret_op = XML::Stream::XPath::AttributeOp->new($self->getNextIdentifier($pos));
137 }
138 elsif ($token eq "]")
139 {
140 84 ▶ if ($in_context eq "[")
Branch: 1 branch
BranchTrueFalseTotal
if ($in_context eq '[') { } 84 0 84
141 {
142 84 $ret_op = pop(@{$self->{OPS}});
143 84 $in_context = 0;
144 }
145 else
146 {
147 0 confess("Found ']' but not in context");
148 0 return;
149 }
150 }
151 ▶ elsif (($token eq "\"") || ($token eq "\'"))
Condition66%2/3
$token eq "\"" or $token eq "'"
l!l&&r!l&&!r
1480336
MC/DC50%1/2
$token eq "\"" or $token eq "'"
$token eq "\"" $token eq "'"
Truth table66%2/3
A or B
A= $token eq "\""B= $token eq "'"
ABresult
1X1
011
000
152 {
153 148 $$pos = index($self->{QUERY},$token,$token_start);
154 148 $ret_op = XML::Stream::XPath::Op->new("LITERAL",substr($self->{QUERY},$token_start,$$pos-$token_start));
155 148 $$pos++;
156 }
157 elsif ($token eq " ")
158 {
159 76 $ident = $self->getNextIdentifier($pos);
160 76 ▶ if ($ident eq "and")
Branch: 2 branches
BranchTrueFalseTotal
if ($ident eq 'and') { } 4 72 76
elsif ($ident eq 'or') { } 68 4 72
161 {
162 4 $$pos++;
163 4 my $tmp_op = $self->getOp($pos,$in_context);
164 4 ▶ if (!defined($tmp_op))
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $tmp_op) 0 4 4
165 {
166 0 confess("Invalid 'and' operation");
167 0 return;
168 }
169 4 $ret_op = XML::Stream::XPath::AndOp->new($self->{OPS}->[$#{$self->{OPS}}],$tmp_op);
170 4 $in_context = 0;
171 4 pop(@{$self->{OPS}});
172 }
173 elsif ($ident eq "or")
174 {
175 68 $$pos++;
176 68 my $tmp_op = $self->getOp($pos,$in_context);
177 68 ▶ if (!defined($tmp_op))
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $tmp_op) 0 68 68
178 {
179 0 confess("Invalid 'or' operation");
180 0 return;
181 }
182 68 $ret_op = XML::Stream::XPath::OrOp->new($self->{OPS}->[$#{$self->{OPS}}],$tmp_op);
183 68 $in_context = 0;
184 68 pop(@{$self->{OPS}});
185 }
186 }
187 elsif ($token eq "[")
188 {
189 84 ▶ if ($self->getNextToken($pos) eq "]")
Branch: 1 branch
BranchTrueFalseTotal
if ($self->getNextToken($pos) eq ']') { } 0 84 84
190 {
191 0 ▶ if ($$pos == $token_start)
Branch: 1 branch
BranchTrueFalseTotal
if ($$pos == $token_start) 0 0 0
192 {
193 0 confess("Nothing in the []");
194 0 return;
195 }
196
197 0 $$pos = $token_start;
198 0 my $val = $self->getNextIdentifier($pos);
199 0 ▶ if ($val =~ /^\d+$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ /^\d+$/) { } 0 0 0
200 {
201 0 $ret_op = XML::Stream::XPath::PositionOp->new($val);
202 0 $$pos++;
203 }
204 else
205 {
206 0 $$pos = $pos_start + 1;
207 0 $ret_op = XML::Stream::XPath::ContextOp->new($self->getOp($pos,$token));
208 }
209 }
210 else
211 {
212 84 $$pos = $pos_start + 1;
213 84 $ret_op = XML::Stream::XPath::ContextOp->new($self->getOp($pos,$token));
214 }
215 }
216 elsif ($token eq "(")
217 {
218 #-------------------------------------------------------------
219 # The function name would have been mistaken for a NodeOp.
220 # Pop it off the back and get the function name.
221 #-------------------------------------------------------------
222 14 my $op = pop(@{$self->{OPS}});
223 14 ▶ if ($op->getType() ne "NODE")
Branch: 1 branch
BranchTrueFalseTotal
if ($op->getType ne "NODE") 0 14 14
224 {
225 0 confess("No function name specified.");
226 }
227 14 my $function = $op->getValue();
228 14 ▶ if (!exists($XML::Stream::XPath::FUNCTIONS{$function}))
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $XML::Stream::XPath::FUNCTIONS{$function}) 0 14 14
229 {
230 0 confess("Undefined function \"$function\"");
231 }
232 14 $ret_op = XML::Stream::XPath::FunctionOp->new($function);
233  
234 14 my $op_pos = $#{$self->{OPS}} + 1;
235  
236 14 $self->getOp($pos,$token);
237
238 14 foreach my $arg ($op_pos..$#{$self->{OPS}})
239 {
240 6 $ret_op->addArg($self->{OPS}->[$arg]);
241 }
242  
243 14 splice(@{$self->{OPS}},$op_pos);
244
245 }
246 elsif ($token eq ")")
247 {
248 14 ▶ if ($in_context eq "(")
Branch: 1 branch
BranchTrueFalseTotal
if ($in_context eq '(') { } 14 0 14
249 {
250 14 $ret_op = undef;
251 14 $in_context = 0;
252 }
253 else
254 {
255 0 confess("Found ')' but not in context");
256 }
257 }
258 elsif ($token eq ",")
259 {
260 2 ▶ if ($in_context ne "(")
Branch: 1 branch
BranchTrueFalseTotal
if ($in_context ne "(") 0 2 2
261 {
262 0 confess("Found ',' but not in a function");
263 }
264
265 }
266 elsif ($token eq "=")
267 {
268 144 my $tmp_op;
269 144 ▶ while(!defined($tmp_op))
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $tmp_op) 144 144 288
270 {
271 144 $tmp_op = $self->getOp($pos);
272 }
273 144 $ret_op = XML::Stream::XPath::EqualOp->new($self->{OPS}->[$#{$self->{OPS}}],$tmp_op);
274 144 pop(@{$self->{OPS}});
275 }
276 elsif ($token eq "!")
277 {
278 2 ▶ if (substr($self->{QUERY},$token_start,1) ne "=")
Branch: 1 branch
BranchTrueFalseTotal
if (substr($self->{"QUERY"}, $token_start, 1) ne "=") 0 2 2
279 {
280 0 confess("Badly formed !=");
281 }
282 2 $$pos++;
283
284 2 my $tmp_op;
285 2 ▶ while(!defined($tmp_op))
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $tmp_op) 4 2 6
286 {
287 4 $tmp_op = $self->getOp($pos);
288 }
289 2 $ret_op = XML::Stream::XPath::NotEqualOp->new($self->{OPS}->[$#{$self->{OPS}}],$tmp_op);
290 2 pop(@{$self->{OPS}});
291 }
292 else
293 {
294 0 confess("Unhandled \"$token\"");
295 }
296  
297 813 ▶ if ($in_context)
Branch: 1 branch
BranchTrueFalseTotal
if ($in_context) 320 493 813
298 {
299 320 ▶ if (defined($ret_op))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ret_op) 316 4 320
300 {
301 316 push(@{$self->{OPS}},$ret_op);
302 }
303 320 $ret_op = undef;
304 }
305 }
306 else
307 {
308 0 confess("Token undefined");
309 }
310
311 813 ▶ $loop = 0 unless $in_context;
Branch: 1 branch
BranchTrueFalseTotal
unless $in_context 493 320 813
312 }
313  
314 493 return $ret_op;
315 }
316  
317  
318 sub parseQuery
319 {
320 105 ▶ my $self = shift;
Subroutine parseQuery: called CC=3 SCAR=11.0
Pod undocumented
321 105 my $query = shift;
322  
323 105 my $op;
324 105 my $pos = 0;
325 105 ▶ while($pos < length($self->{QUERY}))
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < length $self->{"QUERY"}) 175 105 280
326 {
327 175 $op = $self->getOp(\$pos);
328 175 ▶ if (defined($op))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $op) 169 6 175
329 {
330 169 push(@{$self->{OPS}},$op);
331 }
332 }
333  
334 #foreach my $op (@{$self->{OPS}})
335 #{
336 # $op->display();
337 #}
338  
339 105 return 1;
340 }
341  
342  
343 sub execute
344 {
345 105 ▶ my $self = shift;
Subroutine execute: called CC=3 SCAR=11.0
Pod undocumented
346 105 my $root = shift;
347  
348 105 my $ctxt = XML::Stream::XPath::Value->new($root);
349  
350 105 foreach my $op (@{$self->{OPS}})
351 {
352 163 ▶ if (!$op->isValid(\$ctxt))
Branch: 1 branch
BranchTrueFalseTotal
unless ($op->isValid(\$ctxt)) 71 92 163
353 {
354 71 $ctxt->setValid(0);
355 71 return $ctxt;
356 }
357 }
358  
359 34 $ctxt->setValid(1);
360 34 return $ctxt;
361 }
362  
363  
364 sub check
365 {
366 0 ▶ my $self = shift;
Subroutine check: not called CC=1 SCAR=6.9
Pod undocumented
367 0 my $root = shift;
368  
369 0 my $ctxt = $self->execute($root);
370 0 return $ctxt->check();
371 }
372  
373  
374 12 1;
375  
« blib/lib/XML/Stream/XPath/Op.pm ↑ Summary blib/lib/XML/Stream/Tools.pm »