lib/XML/XQL.pm

filter: Statementstmt 17.1% 249 / 1453 Branchbran 6.4% 41 / 634 Conditioncond 8.0% 8 / 99 MC/DCmcdc 0.0% 0 / 56 Subroutinesub 19.2% 55 / 286 Podpod 22.0% 13 / 59 totaltotal 14.1% 366 / 2587 CC 420 SCAR 44.0
CC
420
Cov
14%
Lexer
812.0
generateFunction
336.1
findFunctionOrMethod
272.0
CRAP 81.6
« lib/XML/Checker.pm ↑ Summary lib/XML/XQL/Plus.pm »
1 ############################################################################
2 # Copyright (c) 1998,1999 Enno Derksen
3 # All rights reserved.
4 # This program is free software; you can redistribute it and/or modify it
5 # under the same terms as Perl itself.
6 ############################################################################
7 #
8 # To do (in no particular order):
9 #
10 # - Element tag names that are the same as a XQL keyword (e.g. "or", "not", ..)
11 # are currently not supported. The parser and lexer needs to be smarter and
12 # know what context they are in.
13 # - output using xql:result etc.
14 # - xml:space=preserve still needs to be adhered to in text() etc.
15 # - I already added xql_preserveSpace. Still need to use it in (raw)text() etc.
16 # - XQL functions (like value()) should probably work on input lists > 1 node
17 # (The code was changed, but it needs to be tested. ancestor() wasn't fixed)
18 # - verify implementation of xql_namespace
19 # - verify implementation of end, index
20 # - check passing of context to the solve() methods
21 # - functions/methods may be wrong. They receive the entire LHS set,
22 # so count() is right, but the rest may be wrong!
23 # - may need to use different comment delimiters, '#' may be used in future XQL
24 # definition (according to Joe Lapp, one of the XQL spec authors)
25 # - caching of Node xql_values (?)
26 # - finish the Date class
27 # - discuss which classes: Date, Time, and/or DateTime ?
28 # - conversion of Query result to Perl primitives, i.e. how do we return the
29 # result of a query.
30 # - add support for ordering/formatting the query results, see XML-QL
31 # - discuss typecasting mechanism
32 # - error reporting mechanism
33 # - yyerror handler doesn't seem to work
34 # - passing intermediate exceptions ($@) to the user
35 # - more debugging support
36 # - subst, map etc.
37 # - use rawText for Nodes?
38 # - recurse or not?
39 # - text/rawText default - recurse or not?
40 # - what should default value() implementation use?
41 # - check if all Syntactic Constraints in XQL spec are implemented
42 # - support all node types, i.e. Notation, Attlist etc.
43 # - sorting in 'document order' doesn't work yet for 'other' DOM nodes
44 # - generateFunction - support functions that return lists?
45 # - match() function - what should it return?
46 # - keeping track of reference nodes not always done right
47 # - also think about Perl builtin functions
48 # - conversion to Perl number throws warnings with -w (in comparisons etc.)
49 # - sorting
50 # - add sorting by attribute name (within same element)
51 # (or other criteria)
52 # - optional sorting in $union$ ?
53 # - could add a flag that says "don't worry about document order for $union$"
54 # - user defined sort?
55 # - OPTIMIZE!
56 # - Subscript operator
57 # - Filter operator
58 # - etc.
59  
60 package XML::XQL;
61 28 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
62  
63 28 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
64 28 ▶use XML::RegExp;
Subroutine BEGIN: called CC=1 SCAR=0.0
65  
66 28 ▶use vars qw( @EXPORT $VERSION
Subroutine BEGIN: called CC=1 SCAR=0.0
67 $ContextStart $ContextEnd $BoldOn $BoldOff
68 %Func %Method %FuncArgCount
69 %AllowedOutsideSubquery %ConstFunc %ExpandedType
70 $Restricted $Included $ReXQLName
71 %CompareOper $Token_q $Token_qq $LAST_SORT_KEY
72 28 );
73  
74 28 @EXPORT = qw( $VERSION $Restricted $Included );
75  
76 BEGIN
77 28 ▶{
Subroutine BEGIN: called CC=4 SCAR=13.9
78 28 $VERSION = '0.63';
79  
80 28 ▶ die "XML::XQL is already used/required" if defined $Included;
Branch: 1 branch
BranchTrueFalseTotal
if defined $XML::XQL::Included 0 28 28
81 28 $Included = 1;
82
83 # From XQL spec (The '-' was added to allow XPath style function names.)
84 28 $ReXQLName = "(?:[-a-zA-Z_]+\\w*)";
85
86 28 $Token_q = undef;
87 28 $Token_qq = undef;
88
89 28 ▶ $Restricted = 0 unless defined $Restricted;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $XML::XQL::Restricted 27 1 28
90  
91 28 ▶ if (not $Restricted)
Branch: 1 branch
BranchTrueFalseTotal
if (not $XML::XQL::Restricted) 27 1 28
92 {
93 # Allow names with Perl package prefixes
94 27 $ReXQLName = "(?:$ReXQLName(?:::$ReXQLName)*)";
95  
96 # Support q// and qq// strings
97 27 $Token_q = "q";
98 27 $Token_qq = "qq";
99 }
100 };
101  
102 # To save the user some typing for the simplest cases
103 sub solve
104 0 ▶{
Subroutine solve: not called CC=1 SCAR=6.9
Pod documented
105 0 my ($expr, @args) = @_;
106 0 my $query = new XML::XQL::Query (Expr => $expr);
107 0 my @result = $query->solve (@args);
108 0 $query->dispose;
109  
110 0 @result;
111 }
112  
113 #---------- Parser related stuff ----------------------------------------------
114  
115 # Find (nested) closing delimiter in q{} or qq{} strings
116 sub parse_q
117 0 ▶{
Subroutine parse_q: not called CC=3 SCAR=24.8
Pod undocumented
118 0 my ($qname, $q, $str, $d1, $d2) = @_;
119 0 my ($match) = "";
120 0 my ($found);
121  
122 0 ▶ while ($str =~ /^([^$d1$d2]*)($d1|($d2))(.*)/s)
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /^([^$d1$d2]*)($d1|($d2))(.*)/s) 0 0 0
123 {
124 0 ▶ defined ($3) and return ($4, $match . $1); # $d2 found
Branch: 1 branch
BranchTrueFalseTotal
defined $3 and return $4, $match . $1 0 0 0
125  
126 # match delimiters recursively
127 0 $match .= $1 . $2;
128  
129 0 ($str, $found) = parse_q ($qname, $q, $4, $d1, $d2);
130 0 $match .= $found . $d2;
131 }
132 0 XML::XQL::parseError ("no $qname// closing delimiter found near '$q$d1'");
133 }
134  
135 # To support nested delimiters in q{} and qq() strings
136 28 my %MatchingCloseDelim =
137 (
138 '{' => '}',
139 '(' => ')',
140 '<' => '>',
141 '[' => ']'
142 );
143  
144 sub Lexer
145 0 ▶{
Subroutine Lexer: not called CC=28 SCAR=67.0
Pod undocumented
146 0 my($parser)=shift;
147  
148 exists($parser->YYData->{LINE})
149 0 ▶ or $parser->YYData->{LINE} = 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $parser->YYData->{"LINE"} or $parser->YYData->{"LINE"} = 1 0 0 0
150  
151 $parser->YYData->{INPUT}
152 0 ▶ or return('', undef);
Branch: 1 branch
BranchTrueFalseTotal
$parser->YYData->{"INPUT"} or return "", undef 0 0 0
153  
154 print "Lexer input=[" . $parser->YYData->{INPUT} . "]\n"
155 0 ▶ if $parser->{yydebug};
Branch: 1 branch
BranchTrueFalseTotal
if $parser->{"yydebug"} 0 0 0
156  
157 0 ▶ if ($Restricted)
Branch: 1 branch
BranchTrueFalseTotal
if ($XML::XQL::Restricted) { } 0 0 0
158 {
159 # strip leading whitespace
160 0 $parser->YYData->{INPUT} =~ s/^\s*//;
161 }
162 else
163 {
164 # strip leading whitespace and comments
165 0 $parser->YYData->{INPUT} =~ s/^(\s|#.*)*//;
166 }
167  
168  
169 0 for ($parser->YYData->{INPUT})
170 {
171 0 ▶ s#^"([^"]*)"##o and return ('TEXT', $1);
Branch: 1 branch
BranchTrueFalseTotal
s/^"([^"]*)"//o and return "TEXT", $1 0 0 0
172 0 ▶ s#^'([^']*)'##o and return ('TEXT', $1);
Branch: 1 branch
BranchTrueFalseTotal
s/^'([^']*)'//o and return "TEXT", $1 0 0 0
173
174 0 ▶ if (not $Restricted)
Branch: 1 branch
BranchTrueFalseTotal
unless ($XML::XQL::Restricted) 0 0 0
175 {
176 # Support q// and qq// string delimiters
177 0 for my $qname ('q', 'qq')
178 {
179 0 my ($q) = $parser->{Query}->{$qname};
180 0 ▶ if (defined ($q) and s/^$q(\[\(\{\<#!=-\+|'":;\.,\?\/!@\%^\*)//)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $q and s/^$q(\[\(\{\<#!=-\+|'":;\.,\?\/!@\%^\*)//) 0 0 0
Condition0%0/3
defined $q and s/^$q(\[\(\{\<#!=-\+|'":;\.,\?\/!@\%^\*)//
!ll&&!rl&&r
000
MC/DC0%0/2
defined $q and s/^$q(\[\(\{\<#!=-\+|'":;\.,\?\/!@\%^\*)//
defined $q s/^$q(\[\(\{\<#!=-\+|'":;\.,\?\/!@\%^\*)//
Truth table0%0/3
A and B
A= defined $qB= s/^$q(\[\(\{\<#!=-\+|'":;\.,\?\/!@\%^\*)//
ABresult
0X0
100
111
181 {
182 0 my ($d1, $d2) = ($1, $MatchingCloseDelim{$1});
183 0 my ($str);
184 0 ▶ if (defined $d2)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $d2) { } 0 0 0
185 {
186 0 ($parser->YYData->{INPUT}, $str) = parse_q (
187 $qname, $q, $_, $d1, $d2);
188 }
189 else # close delim is same open delim
190 {
191 0 $d2 = $d1;
192 0 ▶ s/([^$d2])*$d2// or XML::XQL::parseError (
Branch: 1 branch
BranchTrueFalseTotal
s/([^$d2])*$d2// or parseError("no $qname// closing delimiter found near '$q${d1}'") 0 0 0
193 "no $qname// closing delimiter found near '$q$d1'");
194 0 $str = $1;
195 }
196 0 return ('TEXT', eval "$q$d1$str$d2");
197 }
198 }
199 }
200  
201 0 ▶ s/^(-?\d+\.(\d+)?)// and return ('NUMBER', $1);
Branch: 1 branch
BranchTrueFalseTotal
s/^(-?\d+\.(\d+)?)// and return "NUMBER", $1 0 0 0
202 0 ▶ s/^(-?\d+)// and return ('INTEGER', $1);
Branch: 1 branch
BranchTrueFalseTotal
s/^(-?\d+)// and return "INTEGER", $1 0 0 0
203  
204 0 ▶ s/^(\$|\b)(i?(eq|ne|lt|le|gt|ge))\1(?=\W)//i
Branch: 1 branch
BranchTrueFalseTotal
s/^(\$|\b)(i?(eq|ne|lt|le|gt|ge))\1(?=\W)//i and return "COMPARE", lc $2 0 0 0
205 and return ('COMPARE', "\L$2");
206  
207 0 ▶ s/^((\$|\b)(any|all|or|and|not|to|intersect)\2)(?=\W)//i
Branch: 1 branch
BranchTrueFalseTotal
s/^((\$|\b)(any|all|or|and|not|to|intersect)\2)(?=\W)//i and return lc $3, $1 0 0 0
208 and return ("\L$3", $1);
209  
210 0 ▶ s/^((\$|\b)union\2(?=\W)|\|)//i and return ('UnionOp', $1);
Branch: 1 branch
BranchTrueFalseTotal
s/^((\$|\b)union\2(?=\W)|\|)//i and return "UnionOp", $1 0 0 0
211  
212 0 ▶ s/^(;;?)// and return ('SeqOp', $1);
Branch: 1 branch
BranchTrueFalseTotal
s/^(;;?)// and return "SeqOp", $1 0 0 0
213  
214 0 ▶ if (not $Restricted)
Branch: 1 branch
BranchTrueFalseTotal
unless ($XML::XQL::Restricted) 0 0 0
215 {
216 0 ▶ s/^(=~|!~)// and return ('MATCH', $1);
Branch: 1 branch
BranchTrueFalseTotal
s/^(=~|!~)// and return "MATCH", $1 0 0 0
217 0 ▶ s/^\$((no_)?match)\$//i
Branch: 1 branch
BranchTrueFalseTotal
s/^\$((no_)?match)\$//i and return "MATCH", lc $1 0 0 0
218 and return ('MATCH', "\L$1");
219 0 ▶ s/^\$($ReXQLName)\$//o and return ('COMPARE', $1);
Branch: 1 branch
BranchTrueFalseTotal
s/^\$($XML::XQL::ReXQLName)\$//o and return "COMPARE", $1 0 0 0
220 }
221  
222 0 ▶ s/^(=|!=|<|<=|>|>=)// and return ('COMPARE', $1);
Branch: 1 branch
BranchTrueFalseTotal
s/^(=|!=|<|<=|>|>=)// and return "COMPARE", $1 0 0 0
223  
224 0 ▶ s!^(//|/|\(|\)|\.\.?|@|\!|\[|\]|\*|:|,)!!
Branch: 1 branch
BranchTrueFalseTotal
s<^(//|/|\(|\)|\.\.?|@|!|\[|\]|\*|:|,)>[] and return $1, $1 0 0 0
225 and return ($1, $1);
226  
227 0 ▶ s/^($ReXQLName)\s*\(//o
Branch: 1 branch
BranchTrueFalseTotal
s/^($XML::XQL::ReXQLName)\s*\(//o and return "XQLName_Paren", $1 0 0 0
228 and return ('XQLName_Paren', $1);
229  
230 0 ▶ s/^($XML::RegExp::Name)//o and return ('NCName', $1);
Branch: 1 branch
BranchTrueFalseTotal
s/^($XML::RegExp::Name)//o and return "NCName", $1 0 0 0
231 }
232 }
233  
234 #------ end Parser related stuff ----------------------------------------------
235  
236 # Converts result from a Disjunction to a 0 or 1.
237 # If it's a XML::XQL::Boolean, its value is returned.
238 # If it's an empty list it returns 0.
239 # If it's a node or a Text or Number, it returns 1.
240 # If it's a list with 1 or more elements, it returns 1 if at least one
241 # element evaluates to 1 (with toBoolean)
242 sub toBoolean # static method
243 0 ▶{
Subroutine toBoolean: not called CC=4 SCAR=30.0
Pod undocumented
244 0 my $arg = shift;
245  
246 0 my $type = ref ($arg);
247 0 ▶ if ($type eq "ARRAY")
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "ARRAY") 0 0 0
248 {
249 0 for my $n (@$arg)
250 {
251 0 ▶ return 1 if toBoolean ($n);
Branch: 1 branch
BranchTrueFalseTotal
if toBoolean($n) 0 0 0
252 }
253 0 return 0;
254 }
255 0 return $arg->xql_toBoolean;
256 }
257  
258 sub listContains
259 0 ▶{
Subroutine listContains: not called CC=8 SCAR=42.8
Pod undocumented
260 0 my ($list, $x) = @_;
261  
262 #?? $n should be a PrimitiveType or an XML Node
263 0 for my $y (@$list)
264 {
265 #?? return 1 if $x == $y;
266  
267 0 ▶ if (ref($x) eq ref($y)) # same object class
Branch: 1 branch
BranchTrueFalseTotal
if (ref $x eq ref $y) 0 0 0
268 {
269 0 my ($src1, $src2) = ($x->xql_sourceNode, $y->xql_sourceNode);
270 0 ▶ next if ((defined $src1 or defined $src2) and $src1 != $src2);
Branch: 1 branch
BranchTrueFalseTotal
if defined $src1 || defined $src2 and $src1 != $src2 0 0 0
Condition0%0/6
defined $src1 || defined $src2 and $src1 != $src2
!ll&&!rl&&r
000
defined $src1 or defined $src2
l!l&&r!l&&!r
000
MC/DC0%0/3
defined $src1 || defined $src2 and $src1 != $src2
defined $src1 defined $src2 $src1 != $src2
Truth table0%0/5
A or B and C
A= defined $src1B= defined $src2C= $src1 != $src2
ABCresult
00X0
1X00
0100
1X11
0111
271  
272 0 ▶ return ($x == $y) if (UNIVERSAL::isa ($x, 'XML::XQL::Node'));
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($x, "XML::XQL::Node") 0 0 0
273  
274 0 ▶ return 1 if $x->xql_eq ($y);
Branch: 1 branch
BranchTrueFalseTotal
if $x->xql_eq($y) 0 0 0
275 }
276 }
277 0 0;
278 }
279  
280 sub toList
281 0 ▶{
Subroutine toList: not called CC=2 SCAR=17.9
Pod undocumented
282 0 my $r = shift;
283 0 ▶ (ref ($r) eq "ARRAY") ? $r : [ $r ];
Branch: 1 branch
BranchTrueFalseTotal
ref $r eq 'ARRAY' ? : 0 0 0
284 }
285  
286 # Prepare right hand side for a comparison, i.e.
287 # turn it into a single value.
288 # If it is a list with 2 or more values, it croaks.
289 sub prepareRvalue
290 0 ▶{
Subroutine prepareRvalue: not called CC=5 SCAR=34.0
Pod undocumented
291 0 my $r = shift;
292  
293 0 ▶ if (ref ($r) eq "ARRAY")
Branch: 1 branch
BranchTrueFalseTotal
if (ref $r eq "ARRAY") 0 0 0
294 {
295 # more than 1 value gives a runtime error (as per Joe Lapp)
296 0 ▶ croak "bad rvalue $r" if @$r > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @$r > 1 0 0 0
297 0 $r = $r->[0];
298 }
299  
300 0 ▶ if (ref ($r) and $r->isa ('XML::XQL::Node'))
Branch: 1 branch
BranchTrueFalseTotal
if (ref $r and $r->isa("XML::XQL::Node")) 0 0 0
Condition0%0/3
ref $r and $r->isa("XML::XQL::Node")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $r and $r->isa("XML::XQL::Node")
ref $r $r->isa("XML::XQL::Node")
Truth table0%0/3
A and B
A= ref $rB= $r->isa("XML::XQL::Node")
ABresult
0X0
100
111
301 {
302 0 $r = $r->xql_value;
303 }
304 0 $r;
305 }
306  
307 sub trimSpace
308 0 ▶{
Subroutine trimSpace: not called CC=1 SCAR=6.9
Pod undocumented
309 0 $_[0] =~ s/^\s+//;
310 0 $_[0] =~ s/\s+$//;
311 0 $_[0];
312 }
313  
314 # Assumption: max. 32768 (2**15 = 2**($BITS-1)) children (or attributes) per node
315 # Use setMaxChildren() to support larger offspring.
316 28 my $BITS = 16;
317 28 $LAST_SORT_KEY = (2 ** $BITS) - 1;
318  
319 # Call with values: $max = 128 * (256**N), where N=0, 1, 2, ...
320 sub setMaxChildren
321 0 ▶{
Subroutine setMaxChildren: not called CC=2 SCAR=17.9
Pod undocumented
322 0 my $max = shift;
323 0 my $m = 128;
324 0 $BITS = 8;
325 0 ▶ while ($max > $m)
Branch: 1 branch
BranchTrueFalseTotal
if ($max > $m) 0 0 0
326 {
327 0 $m = $m * 256;
328 0 $BITS += 8;
329 }
330 0 $LAST_SORT_KEY = (2 ** $BITS) - 1;
331 }
332  
333 sub createSortKey
334 0 ▶{
Subroutine createSortKey: not called CC=2 SCAR=17.9
Pod undocumented
335 # $_[0] = parent sort key, $_[1] = child index,
336 # $_[2] = 0 for attribute nodes, 1 for other node types
337 0 my $vec = "";
338 0 vec ($vec, 0, $BITS) = $_[1];
339 0 ▶ vec ($vec, 7, 1) = $_[2] if $_[2]; # set leftmost bit (for non-attributes)
Branch: 1 branch
BranchTrueFalseTotal
if $_[2] 0 0 0
340 0 $_[0] . $vec;
341 }
342  
343 #--------------- Sorting source nodes ----------------------------------------
344  
345 # Sort the list by 'document order' (as per the XQL spec.)
346 # Values with an associated source node are sorted by the position of their
347 # source node in the XML document.
348 # Values without a source node are placed at the end of the resulting list.
349 # The source node of an Attribute node, is its (parent) Element node
350 # (per definition.) The source node of the other types of XML nodes, is itself.
351 # The order for values with the same source node is undefined.
352  
353 sub sortDocOrder
354 0 ▶{
Subroutine sortDocOrder: not called CC=1 SCAR=6.9
Pod undocumented
355 #?? or should I just use: sort { $a->xql_sortKey cmp $b->xql_sortKey }
356  
357 0 my $list = shift;
358  
359 #print "before---\n";
360 #for (@$list)
361 #{
362 # print "key=" . keyStr($_->xql_sortKey) . " node=" . $_->getTagName . " id=" . $_->getAttribute('id') . "\n";
363 #}
364  
365 0 @$list = map { $_->[1] } # 3) extract nodes
366 0 sort { $a->[0] cmp $b->[0] } # 2) sort by sortKey
367 0 map { [$_->xql_sortKey, $_] } # 1) make [sortKey,node] records
368 @$list;
369  
370 #print "after---\n";
371 #for (@$list)
372 #{
373 # print "key=" . keyStr($_->xql_sortKey) . " node=" . $_->getTagName . " id=" . $_->getAttribute('id') . "\n";
374 #}
375  
376 0 $list;
377 }
378  
379 # Converts sort key from createSortKey in human readable form
380 # For debugging only.
381 sub keyStr
382 0 ▶{
Subroutine keyStr: not called CC=5 SCAR=34.0
Pod undocumented
383 0 my $key = shift;
384 0 my $n = $BITS / 8;
385 0 my $bitn = 2 ** ($BITS - 1);
386 0 my $str;
387 0 ▶ for (my $i = 0; $i < length $key; $i += $n)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < length $key) 0 0 0
388 {
389 0 my $dig = substr ($key, $i, $n);
390 0 my $v = vec ($dig, 0, $BITS);
391 0 my $elem = 0;
392 0 ▶ if ($v >= $bitn)
Branch: 1 branch
BranchTrueFalseTotal
if ($v >= $bitn) 0 0 0
393 {
394 0 $v -= $bitn;
395 0 $elem = 1;
396 }
397 0 ▶ $str .= "/" if defined $str;
Branch: 1 branch
BranchTrueFalseTotal
if defined $str 0 0 0
398 0 ▶ $str .= "@" unless $elem;
Branch: 1 branch
BranchTrueFalseTotal
unless $elem 0 0 0
399 0 $str .= $v;
400 }
401 0 $str;
402 }
403  
404 sub isEmptyList
405 0 ▶{
Subroutine isEmptyList: not called CC=2 SCAR=17.9
Pod documented
406 0 my $list = shift;
407 0 ▶ (ref ($list) eq "ARRAY") && (@$list == 0);
Branch: 1 branch
BranchTrueFalseTotal
ref $list eq "ARRAY" and @$list == 0 0 0 0
408 }
409  
410 # Used by Element and Attribute nodes
411 sub buildNameSpaceExpr
412 0 ▶{
Subroutine buildNameSpaceExpr: not called CC=4 SCAR=30.0
Pod undocumented
413 0 my ($nameSpace, $name) = @_;
414 0 ▶ $name = ".*" if $name eq "*";
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "*" 0 0 0
415 0 ▶ if (defined $nameSpace)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $nameSpace) { } 0 0 0
416 {
417 0 ▶ $nameSpace = ".*" if $nameSpace eq "*";
Branch: 1 branch
BranchTrueFalseTotal
if $nameSpace eq "*" 0 0 0
418 0 "^$nameSpace:$name\$";
419 }
420 else
421 {
422 0 "^$name\$";
423 }
424 }
425  
426 sub prepareForCompare
427 0 ▶{
Subroutine prepareForCompare: not called CC=3 SCAR=24.8
Pod undocumented
428 0 my ($left, $right) = @_;
429 0 my $leftType = $left->xql_primType;
430 0 ▶ if ($leftType == 0) # Node
Branch: 1 branch
BranchTrueFalseTotal
if ($leftType == 0) 0 0 0
431 {
432 0 $left = $left->xql_value;
433 0 $leftType = $left->xql_primType;
434 }
435 0 my $rightType = $right->xql_primType;
436 0 ▶ if ($rightType == 0) # Node
Branch: 1 branch
BranchTrueFalseTotal
if ($rightType == 0) 0 0 0
437 {
438 0 $right = $right->xql_value;
439 0 $rightType = $right->xql_primType;
440 }
441 # Note: reverse the order if $leftType < $rightType
442 0 ($leftType < $rightType, $left, $right);
443 }
444  
445 sub xql_eq
446 0 ▶{
Subroutine xql_eq: not called CC=2 SCAR=17.9
Pod undocumented
447 0 my ($left, $right, $ignoreCase) = @_;
448 0 my $reverse;
449 0 ($reverse, $left, $right) = prepareForCompare ($left, $right);
450 0 ▶ $reverse ? $right->xql_eq ($left, $ignoreCase)
Branch: 1 branch
BranchTrueFalseTotal
$reverse ? : 0 0 0
451 : $left->xql_eq ($right, $ignoreCase);
452 }
453  
454 sub xql_ne
455 0 ▶{
Subroutine xql_ne: not called CC=2 SCAR=17.9
Pod undocumented
456 0 my ($left, $right, $ignoreCase) = @_;
457 0 my $reverse;
458 0 ($reverse, $left, $right) = prepareForCompare ($left, $right);
459 0 ▶ $reverse ? $right->xql_ne ($left, $ignoreCase)
Branch: 1 branch
BranchTrueFalseTotal
$reverse ? : 0 0 0
460 : $left->xql_ne ($right, $ignoreCase);
461 }
462  
463 sub xql_lt
464 0 ▶{
Subroutine xql_lt: not called CC=2 SCAR=17.9
Pod undocumented
465 0 my ($left, $right, $ignoreCase) = @_;
466 0 my $reverse;
467 0 ($reverse, $left, $right) = prepareForCompare ($left, $right);
468 0 ▶ $reverse ? $right->xql_ge ($left, $ignoreCase)
Branch: 1 branch
BranchTrueFalseTotal
$reverse ? : 0 0 0
469 : $left->xql_lt ($right, $ignoreCase);
470 }
471  
472 sub xql_le
473 0 ▶{
Subroutine xql_le: not called CC=2 SCAR=17.9
Pod undocumented
474 0 my ($left, $right, $ignoreCase) = @_;
475 0 my $reverse;
476 0 ($reverse, $left, $right) = prepareForCompare ($left, $right);
477 0 ▶ $reverse ? $right->xql_gt ($left, $ignoreCase)
Branch: 1 branch
BranchTrueFalseTotal
$reverse ? : 0 0 0
478 : $left->xql_le ($right, $ignoreCase);
479 }
480  
481 sub xql_gt
482 0 ▶{
Subroutine xql_gt: not called CC=2 SCAR=17.9
Pod undocumented
483 0 my ($left, $right, $ignoreCase) = @_;
484 0 my $reverse;
485 0 ($reverse, $left, $right) = prepareForCompare ($left, $right);
486 0 ▶ $reverse ? $right->xql_le ($left, $ignoreCase)
Branch: 1 branch
BranchTrueFalseTotal
$reverse ? : 0 0 0
487 : $left->xql_gt ($right, $ignoreCase);
488 }
489  
490 sub xql_ge
491 0 ▶{
Subroutine xql_ge: not called CC=2 SCAR=17.9
Pod undocumented
492 0 my ($left, $right, $ignoreCase) = @_;
493 0 my $reverse;
494 0 ($reverse, $left, $right) = prepareForCompare ($left, $right);
495 0 ▶ $reverse ? $right->xql_lt ($left, $ignoreCase)
Branch: 1 branch
BranchTrueFalseTotal
$reverse ? : 0 0 0
496 : $left->xql_ge ($right, $ignoreCase);
497 }
498  
499 0 ▶sub xql_ieq { xql_eq (@_, 1); }
Subroutine xql_ieq: not called CC=1 SCAR=6.9
Pod undocumented
500 0 ▶sub xql_ine { xql_ne (@_, 1); }
Subroutine xql_ine: not called CC=1 SCAR=6.9
Pod undocumented
501 0 ▶sub xql_ilt { xql_lt (@_, 1); }
Subroutine xql_ilt: not called CC=1 SCAR=6.9
Pod undocumented
502 0 ▶sub xql_igt { xql_gt (@_, 1); }
Subroutine xql_igt: not called CC=1 SCAR=6.9
Pod undocumented
503 0 ▶sub xql_ige { xql_ge (@_, 1); }
Subroutine xql_ige: not called CC=1 SCAR=6.9
Pod undocumented
504 0 ▶sub xql_ile { xql_le (@_, 1); }
Subroutine xql_ile: not called CC=1 SCAR=6.9
Pod undocumented
505  
506 sub tput
507 140 ▶{
Subroutine tput: called CC=2 SCAR=7.6
Pod undocumented
508 # Let me know if I need to add other systems for which 'tput' is not
509 # available.
510 140 ▶ if ($^O =~ /Win|MacOS/)
Branch: 1 branch
BranchTrueFalseTotal
if ($^O =~ /Win|MacOS/) { } 0 140 140
511 {
512 0 return undef;
513 }
514 else
515 {
516 140 my $c = shift;
517  
518 # tput is only available on Unix systems.
519 # Calling `tput ...` on Windows generates warning messages
520 # that can not be suppressed.
521 140 return `tput $c`;
522 }
523 }
524  
525 # Underline the query subexpression that fails (if tput exists)
526 28 ▶$ContextStart = tput ('smul') || ">>"; # smul: underline on
Condition50%1/2
tput('smul') || '>>'
l!l
280
MC/DC0%0/1
tput('smul') || '>>'
tput('smul')
Truth table50%1/2
A
A= tput('smul')
Aresult
11
00
527 28 ▶$ContextEnd = tput ('rmul') || "<<"; # rmul: underline off
Condition50%1/2
tput('rmul') || '<<'
l!l
280
MC/DC0%0/1
tput('rmul') || '<<'
tput('rmul')
Truth table50%1/2
A
A= tput('rmul')
Aresult
11
00
528 # Used for making the significant keyword of a subexpression bold, e.g. "$and$"
529 28 ▶$BoldOn = tput ('bold') || "";
Condition50%1/2
tput('bold') || ''
l!l
280
MC/DC0%0/1
tput('bold') || ''
tput('bold')
Truth table50%1/2
A
A= tput('bold')
Aresult
11
00
530 28 ▶$BoldOff = tput ('rmul') . tput ('smul') || "";
Condition50%1/2
tput('rmul') . tput('smul') || ''
l!l
280
MC/DC0%0/1
tput('rmul') . tput('smul') || ''
tput('rmul') . tput('smul')
Truth table50%1/2
A
A= tput('rmul') . tput('smul')
Aresult
11
00
531 # rmul reverts the string back to normal text, smul makes it underlined again,
532 # so the rest of the subexpresion will be underlined.
533  
534 sub setErrorContextDelimiters
535 0 ▶{
Subroutine setErrorContextDelimiters: not called CC=1 SCAR=6.9
Pod documented
536 0 ($ContextStart, $ContextEnd, $BoldOn, $BoldOff) = @_;
537 }
538  
539 sub delim
540 0 ▶{
Subroutine delim: not called CC=2 SCAR=17.9
Pod undocumented
541 0 my ($str, $node, $contextNode) = @_;
542 0 ▶ if ($node == $contextNode)
Branch: 1 branch
BranchTrueFalseTotal
if ($node == $contextNode) { } 0 0 0
543 {
544 0 $str =~ s/\016([^\017]*)\017/$BoldOn$1$BoldOff/g;
545 0 "$ContextStart$str$ContextEnd";
546 }
547 else
548 {
549 0 $str =~ s/\016([^\017]*)\017/$1/g;
550 0 $str;
551 }
552 }
553  
554 sub bold
555 0 ▶{
Subroutine bold: not called CC=1 SCAR=6.9
Pod undocumented
556 0 my $x = shift;
557 0 "\016$x\017"; # arbitrary ASCII codes
558 }
559  
560 sub parseError
561 0 ▶{
Subroutine parseError: not called CC=1 SCAR=6.9
Pod undocumented
562 0 my ($msg) = @_;
563 0 print STDERR $msg . "\n";
564 0 croak $msg;
565 }
566  
567 # Builtin XQL functions (may not appear after Bang "!")
568 28 %Func =
569 (
570 ancestor => \&XML::XQL::Func::ancestor,
571 attribute => \&XML::XQL::Func::attribute,
572 comment => \&XML::XQL::Func::comment,
573 element => \&XML::XQL::Func::element,
574 id => \&XML::XQL::Func::id,
575 node => \&XML::XQL::Func::node,
576 pi => \&XML::XQL::Func::pi,
577 textNode => \&XML::XQL::Func::textNode,
578 true => \&XML::XQL::Func::true,
579 false => \&XML::XQL::Func::false,
580  
581 # NOTE: date() is added with: use XML::XQL::Date;
582 );
583  
584 # Builtin XQL methods (may appear after Bang "!")
585 28 %Method =
586 (
587 baseName => \&XML::XQL::Func::baseName,
588 count => \&XML::XQL::Func::count,
589 end => \&XML::XQL::Func::end,
590 'index' => \&XML::XQL::Func::xql_index,
591 namespace => \&XML::XQL::Func::namespace,
592 nodeName => \&XML::XQL::Func::nodeName,
593 nodeType => \&XML::XQL::Func::nodeType,
594 nodeTypeString => \&XML::XQL::Func::nodeTypeString,
595 prefix => \&XML::XQL::Func::prefix,
596 text => \&XML::XQL::Func::text,
597 rawText => \&XML::XQL::Func::rawText,
598 value => \&XML::XQL::Func::value,
599 );
600  
601 # Number of arguments for builtin XQL functions:
602 # Value is either an integer or a range. Value is 0 if not specified.
603 # Range syntax:
604 #
605 # range ::= '[' start ',' end [ ',' start ',' end ]* ']'
606 # start ::= INTEGER
607 # end ::= INTEGER | '-1' ('-1' means: "or more")
608 #
609 # Example: [2, 4, 7, 7, 10, -1] means (2,3,4,7,10,11,...)
610  
611 28 %FuncArgCount =
612 (
613 ancestor => 1,
614 attribute => [0,1],
615 count => [0,1],
616 # date => 1,
617 element => [0,1],
618 id => 1,
619 text => [0,1],
620 rawText => [0,1],
621 );
622  
623 28 %AllowedOutsideSubquery =
624 (
625 ancestor => 1,
626 attribute => 1,
627 comment => 1,
628 element => 1,
629 id => 1,
630 node => 1,
631 pi => 1,
632 textNode => 1,
633  
634 #?? what about subst etc.
635 );
636  
637 # Functions that always return the same thing if their arguments are constant
638 28 %ConstFunc =
639 (
640 true => 1,
641 false => 1,
642 # date => 1,
643 );
644  
645 28 %ExpandedType =
646 (
647 "boolean" => "XML::XQL::Boolean",
648 "text" => "XML::XQL::Text",
649 "number" => "XML::XQL::Number",
650 "date" => "XML::XQL::Date",
651 "node" => "XML::XQL::Node",
652 );
653  
654 sub expandType
655 351 ▶{
Subroutine expandType: called CC=2 SCAR=6.9
Pod documented
656 351 my ($type) = @_;
657 # Expand "number" to "XML::XQL::Number" etc.
658 351 my $expanded = $ExpandedType{"\L$type"};
659 351 ▶ defined $expanded ? $expanded : $type;
Branch: 1 branch
BranchTrueFalseTotal
defined $expanded ? : 297 54 351
660 }
661  
662 sub defineExpandedTypes
663 0 ▶{
Subroutine defineExpandedTypes: not called CC=2 SCAR=17.9
Pod documented
664 0 my (%args) = @_;
665 0 ▶ while (my ($key, $val) = each %args)
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %args) 0 0 0
666 {
667 # Convert keys to lowercase
668 0 $ExpandedType{"\L$key"} = $val;
669 }
670 }
671  
672 sub generateFunction
673 351 ▶{
Subroutine generateFunction: called CC=24 SCAR=58.2
Pod documented
674 351 my ($name, $funcName, $returnType, $argCount, $allowedOutsideSubquery,
675 $const, $queryArg) = @_;
676 351 ▶ $argCount = 0 unless defined $argCount;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $argCount 0 351 351
677 351 ▶ $allowedOutsideSubquery = 1 unless defined $allowedOutsideSubquery;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $allowedOutsideSubquery 0 351 351
678 351 ▶ $const = 0 unless defined $const;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $const 297 54 351
679 351 ▶ $queryArg = 0 unless defined $queryArg;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $queryArg 297 54 351
680  
681 351 $returnType = expandType ($returnType);
682 351 my $wrapperName = "xql_wrap_$name";
683 351 $wrapperName =~ s/\W/_/g; # replace colons etc.
684  
685 351 my $func;
686 351 my $code = <<END_CODE;
687 sub $wrapperName {
688 my (\$context, \$list, \@arg) = \@_;
689 for my \$i (0 .. \$#arg)
690 {
691 if (\$i == $queryArg)
692 {
693 \$arg[\$i] = XML::XQL::toList (\$arg[\$i]->solve (\$context, \$list));
694 }
695 else
696 {
697 \$arg[\$i] = XML::XQL::prepareRvalue (\$arg[\$i]->solve (\$context, \$list));
698 return [] if XML::XQL::isEmptyList (\$arg[\$i]);
699 \$arg[\$i] = \$arg[\$i]->xql_toString;
700 }
701 }
702 END_CODE
703  
704 351 ▶ if (ref ($argCount) eq "ARRAY" && @$argCount == 2 &&
Branch: 1 branch
BranchTrueFalseTotal
if (ref $argCount eq "ARRAY" and @$argCount == 2 and $argCount->[0] == $argCount->[1]) 0 351 351
Condition66%4/6
ref $argCount eq "ARRAY" and @$argCount == 2 and $argCount->[0] == $argCount->[1]
!ll&&!rl&&r
270810
ref $argCount eq "ARRAY" and @$argCount == 2
!ll&&!rl&&r
270081
MC/DC0%0/3
ref $argCount eq "ARRAY" and @$argCount == 2 and $argCount->[0] == $argCount->[1]
ref $argCount eq "ARRAY" @$argCount == 2 $argCount->[0] == $argCount->[1]
Truth table50%2/4
A and B and C
A= ref $argCount eq "ARRAY"B= @$argCount == 2C= $argCount->[0] == $argCount->[1]
ABCresult
0XX0
10X0
1100
1111
705 $argCount->[0] == $argCount->[1])
706 {
707 0 $argCount = $argCount->[0];
708 }
709  
710 351 ▶ if ($queryArg != -1)
Branch: 1 branch
BranchTrueFalseTotal
if ($queryArg != -1) 351 0 351
711 {
712 351 $code .=<<END_CODE;
713 my \@result = ();
714 my \@qp = \@{\$arg[$queryArg]};
715 for (my \$k = 0; \$k < \@qp; \$k++)
716 {
717 \$arg[$queryArg] = \$qp[\$k]->xql_toString;
718 END_CODE
719 }
720  
721 351 ▶ if (ref ($argCount) ne "ARRAY")
Branch: 2 branches
BranchTrueFalseTotal
if (ref $argCount ne 'ARRAY') { } 270 81 351
elsif (@$argCount == 2) { } 81 0 81
722 {
723 270 $code .= " my \$result = $funcName (";
724 270 for my $i (0 .. $argCount-1)
725 {
726 459 ▶ $code .= ", " if $i;
Branch: 1 branch
BranchTrueFalseTotal
if $i 189 270 459
727 459 $code .= "\$arg[$i]";
728 }
729 270 $code .= ");\n";
730 }
731 elsif (@$argCount == 2)
732 {
733 81 my ($start, $end) = ($argCount->[0], $argCount->[1]);
734 81 ▶ if ($end == -1)
Branch: 1 branch
BranchTrueFalseTotal
if ($end == -1) { } 54 27 81
735 {
736 54 $code .= " my \$result = $funcName (";
737 54 for my $i (0 .. ($start - 1))
738 {
739 81 ▶ $code .= ", " if $i;
Branch: 1 branch
BranchTrueFalseTotal
if $i 27 54 81
740 81 $code .= "\$arg[$i]";
741 }
742 54 $code .= ", \@arg[" . $start . " .. \$#arg]);\n";
743 }
744 else
745 {
746 27 $code .= " my \$n = \@arg;\n my \$result;\n ";
747 27 for my $j ($argCount->[0] .. $argCount->[1])
748 {
749 54 ▶ $code .= " els" unless $j == $argCount->[0];
Branch: 1 branch
BranchTrueFalseTotal
unless $j == $argCount->[0] 27 27 54
750 54 ▶ $code .= ($j == $argCount->[1] ? "e\n" :
Branch: 1 branch
BranchTrueFalseTotal
$j == $argCount->[1] ? : 27 27 54
751 "if (\$n == $j)\n");
752 54 $code .= " {\n \$result = $funcName (";
753 54 for my $i (0 .. $j-1)
754 {
755 135 ▶ $code .= ", " if $i;
Branch: 1 branch
BranchTrueFalseTotal
if $i 81 54 135
756 135 $code .= "\$arg[$i]";
757 }
758 54 $code .= ");\n }\n";
759 }
760 }
761 }
762 else #?? what now...
763 {
764 0 $code .= " my \$result = $funcName (\@arg);\n";
765 }
766  
767 351 ▶ if ($returnType eq "*") # return result as is
Branch: 1 branch
BranchTrueFalseTotal
if ($returnType eq '*') { } 54 297 351
768 {
769 54 $code .= " \$result = [] unless defined \$result;\n";
770 }
771 else
772 {
773 297 $code .= " \$result = defined \$result ? new $returnType (\$result) : [];\n";
774 }
775  
776 351 ▶ if ($queryArg == -1)
Branch: 1 branch
BranchTrueFalseTotal
if ($queryArg == -1) { } 0 351 351
777 {
778 0 $code .= " \$result;\n}\n";
779 }
780 else
781 {
782 351 $code .= " push \@result, \$result;\n }\n \\\@result;\n}\n";
783 }
784 351 $code .= "\$func = \\\&$wrapperName;";
785  
786 #print "CODE=$code\n";
787  
788 0 ▶ eval "$code";
Subroutine xql_wrap_ceiling: not called CC=6 SCAR=37.3
Subroutine xql_wrap_concat: not called CC=6 SCAR=37.3
Subroutine xql_wrap_contains: not called CC=6 SCAR=37.3
Subroutine xql_wrap_document: not called CC=6 SCAR=37.3
Subroutine xql_wrap_ends_with: not called CC=6 SCAR=37.3
Subroutine xql_wrap_floor: not called CC=6 SCAR=37.3
Subroutine xql_wrap_new: not called CC=6 SCAR=37.3
Subroutine xql_wrap_round: not called CC=6 SCAR=37.3
Subroutine xql_wrap_starts_with: not called CC=6 SCAR=37.3
Subroutine xql_wrap_substring: not called CC=7 SCAR=40.2
Subroutine xql_wrap_substring_after: not called CC=6 SCAR=37.3
Subroutine xql_wrap_substring_before: not called CC=6 SCAR=37.3
Subroutine xql_wrap_translate: not called CC=6 SCAR=37.3
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Branch: 53 branches
BranchTrueFalseTotal
if ($i == 0) { } 0 0 0
if isEmptyList($arg[$i]) 0 0 0
if ($k < @qp) 0 0 0
defined $result ? : 0 0 0
if ($i == 0) { } 0 0 0
if isEmptyList($arg[$i]) 0 0 0
if ($k < @qp) 0 0 0
defined $result ? : 0 0 0
if ($i == 0) { } 0 0 0
if isEmptyList($arg[$i]) 0 0 0
if ($k < @qp) 0 0 0
defined $result ? : 0 0 0
if ($i == 0) { } 0 0 0
if isEmptyList($arg[$i]) 0 0 0
if ($k < @qp) 0 0 0
unless defined $result 0 0 0
if ($i == 0) { } 0 0 0
if isEmptyList($arg[$i]) 0 0 0
if ($k < @qp) 0 0 0
defined $result ? : 0 0 0
if ($i == 0) { } 0 0 0
if isEmptyList($arg[$i]) 0 0 0
if ($k < @qp) 0 0 0
defined $result ? : 0 0 0
if ($i == 1) { } 0 0 0
if isEmptyList($arg[$i]) 0 0 0
if ($k < @qp) 0 0 0
unless defined $result 0 0 0
if ($i == 0) { } 0 0 0
if isEmptyList($arg[$i]) 0 0 0
if ($k < @qp) 0 0 0
defined $result ? : 0 0 0
if ($i == 0) { } 0 0 0
if isEmptyList($arg[$i]) 0 0 0
if ($k < @qp) 0 0 0
defined $result ? : 0 0 0
if ($i == 0) { } 0 0 0
if isEmptyList($arg[$i]) 0 0 0
if ($k < @qp) 0 0 0
if ($n == 2) { } 0 0 0
defined $result ? : 0 0 0
if ($i == 0) { } 0 0 0
if isEmptyList($arg[$i]) 0 0 0
if ($k < @qp) 0 0 0
defined $result ? : 0 0 0
if ($i == 0) { } 0 0 0
if isEmptyList($arg[$i]) 0 0 0
if ($k < @qp) 0 0 0
defined $result ? : 0 0 0
if ($i == 0) { } 0 0 0
if isEmptyList($arg[$i]) 0 0 0
if ($k < @qp) 0 0 0
defined $result ? : 0 0 0
789 0 ▶ if ($@) { croak "generateFunction failed for $funcName: $@\n"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 351 351
790  
791 351 defineFunction ($name, $func, $argCount,
792 $allowedOutsideSubquery, $const);
793 }
794  
795 sub defineFunction
796 594 ▶{
Subroutine defineFunction: called CC=2 SCAR=6.9
Pod documented
797 594 my ($name, $func, $argCount, $allowedOutside, $const) = @_;
798 594 $Func{$name} = $func;
799 594 $FuncArgCount{$name} = $argCount;
800 594 ▶ $AllowedOutsideSubquery{$name} = 1 if $allowedOutside;
Branch: 1 branch
BranchTrueFalseTotal
if $allowedOutside 513 81 594
801 594 $ConstFunc{$name} = $const;
802 }
803  
804 sub defineMethod
805 81 ▶{
Subroutine defineMethod: called CC=2 SCAR=6.9
Pod documented
806 81 my ($name, $func, $argCount, $allowedOutside) = @_;
807 81 $Method{$name} = $func;
808 81 $FuncArgCount{$name} = $argCount;
809 81 ▶ $AllowedOutsideSubquery{$name} = 1 if $allowedOutside;
Branch: 1 branch
BranchTrueFalseTotal
if $allowedOutside 54 27 81
810 }
811  
812 28 %CompareOper =
813 (
814 'eq' => \&XML::XQL::xql_eq,
815 'ne' => \&XML::XQL::xql_ne,
816 'le' => \&XML::XQL::xql_le,
817 'ge' => \&XML::XQL::xql_ge,
818 'gt' => \&XML::XQL::xql_gt,
819 'lt' => \&XML::XQL::xql_lt,
820  
821 'ieq' => \&XML::XQL::xql_ieq,
822 'ine' => \&XML::XQL::xql_ine,
823 'ile' => \&XML::XQL::xql_ile,
824 'ige' => \&XML::XQL::xql_ige,
825 'igt' => \&XML::XQL::xql_igt,
826 'ilt' => \&XML::XQL::xql_ilt,
827  
828 '=' => \&XML::XQL::xql_eq,
829 '!=' => \&XML::XQL::xql_ne,
830 '>' => \&XML::XQL::xql_gt,
831 '>=' => \&XML::XQL::xql_ge,
832 '<' => \&XML::XQL::xql_lt,
833 '<=' => \&XML::XQL::xql_le,
834 );
835  
836 sub defineComparisonOperators
837 27 ▶{
Subroutine defineComparisonOperators: called CC=1 SCAR=0.0
Pod documented
838 27 my (%args) = @_;
839 27 %CompareOper = (%CompareOper, %args);
840 }
841  
842 sub defineTokenQ
843 0 ▶{
Subroutine defineTokenQ: not called CC=1 SCAR=6.9
Pod documented
844 0 $Token_q = $_[0];
845 }
846  
847 sub defineTokenQQ
848 0 ▶{
Subroutine defineTokenQQ: not called CC=1 SCAR=6.9
Pod documented
849 0 $Token_qq = $_[0];
850 }
851  
852 28 my %ElementValueType = ();
853 28 my $ElementValueTypeCount = 0;
854  
855 sub elementValue
856 0 ▶{
Subroutine elementValue: not called CC=3 SCAR=24.8
Pod undocumented
857 0 my ($elem) = @_;
858  
859 #?? raw text/recursive ?
860  
861 0 ▶ return new XML::XQL::Text ($elem->xql_text, $elem)
Branch: 1 branch
BranchTrueFalseTotal
if $ElementValueTypeCount == 0 0 0 0
862 if $ElementValueTypeCount == 0; # user hasn't defined any types
863  
864 0 my $tagName = $elem->xql_nodeName;
865 0 my $func = $ElementValueType{$tagName};
866 0 ▶ return new XML::XQL::Text ($elem->xql_text, $elem) unless defined $func;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $func 0 0 0
867  
868 0 &$func ($elem, $tagName);
869 }
870  
871 sub defineElementValueConvertor
872 0 ▶{
Subroutine defineElementValueConvertor: not called CC=3 SCAR=24.8
Pod documented
873 0 my ($elemTagName, $func) = @_;
874 0 my $prev = defined $ElementValueType{$elemTagName};
875 0 $ElementValueType{$elemTagName} = $func;
876 0 ▶ if (defined $func != $prev)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $func != $prev) 0 0 0
877 {
878 0 ▶ defined $func ? $ElementValueTypeCount++ : $ElementValueTypeCount--;
Branch: 1 branch
BranchTrueFalseTotal
defined $func ? : 0 0 0
879 }
880 }
881  
882 28 my %AttrValueType = ();
883 28 my $AttrValueTypeCount = 0;
884  
885 sub attrValue
886 0 ▶{
Subroutine attrValue: not called CC=4 SCAR=30.0
Pod undocumented
887 0 my ($attr) = @_;
888  
889 #?? raw text/recursive ?
890 0 ▶ return new XML::XQL::Text ($attr->xql_text, $attr)
Branch: 1 branch
BranchTrueFalseTotal
if $AttrValueTypeCount == 0 0 0 0
891 if $AttrValueTypeCount == 0; # user hasn't defined any types
892  
893 0 my $elem = $attr->xql_parent->xql_nodeName;
894 0 my $attrName = $attr->xql_nodeName;
895 0 my $func = $AttrValueType{"$elem $attrName"};
896
897 0 ▶ if (not defined $func)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $func) 0 0 0
898 {
899 0 $elem = "*";
900 0 $func = $AttrValueType{"$elem $attrName"};
901 }
902 0 ▶ return new XML::XQL::Text ($attr->xql_text, $attr) unless defined $func;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $func 0 0 0
903  
904 0 &$func ($attr, $attrName, $elem);
905 }
906  
907 sub defineAttrValueConvertor
908 0 ▶{
Subroutine defineAttrValueConvertor: not called CC=3 SCAR=24.8
Pod documented
909 0 my ($elemTagName, $attrName, $type) = @_;
910 0 my $both = "$elemTagName $attrName";
911  
912 0 my $prev = defined $AttrValueType{$both};
913 0 $AttrValueType{$both} = $type;
914 0 ▶ if (defined $type != $prev)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $type != $prev) 0 0 0
915 {
916 0 ▶ defined $type ? $AttrValueTypeCount++ : $AttrValueTypeCount--;
Branch: 1 branch
BranchTrueFalseTotal
defined $type ? : 0 0 0
917 }
918 }
919  
920 #=== debug
921  
922 sub exception
923 0 ▶{
Subroutine exception: not called CC=2 SCAR=17.9
Pod undocumented
924 0 my ($ex) = @_;
925 0 ▶ print "Exception: $ex\n" if $ex;
Branch: 1 branch
BranchTrueFalseTotal
if $ex 0 0 0
926 0 $ex;
927 }
928  
929 sub d
930 0 ▶{
Subroutine d: not called CC=10 SCAR=47.0
Pod undocumented
931 0 my $n = shift;
932 0 my $type = ref $n;
933  
934 0 ▶ if ($type eq "ARRAY")
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'ARRAY') { } 0 0 0
elsif ($type eq 'HASH') { } 0 0 0
elsif ($type) { } 0 0 0
935 {
936 0 my $str = "";
937 0 for my $i (@$n)
938 {
939 0 ▶ $str .= ", " unless $str eq "";
Branch: 1 branch
BranchTrueFalseTotal
unless $str eq "" 0 0 0
940 0 $str .= d ($i);
941 }
942 0 return "[$str]";
943 }
944 elsif ($type eq "HASH")
945 {
946 0 my $str = "";
947 0 ▶ while (my ($key, $val) = %$n)
Branch: 1 branch
BranchTrueFalseTotal
if (() = %$n) 0 0 0
948 {
949 0 ▶ $str .= ", " unless $str eq "";
Branch: 1 branch
BranchTrueFalseTotal
unless $str eq "" 0 0 0
950 0 $str .= $key . " => " . d ($val);
951 }
952 0 return "{$str}";
953 }
954 elsif ($type)
955 {
956 0 ▶ return $n->xql_contextString if ($n->isa ('XML::XQL::Operator'));
Branch: 1 branch
BranchTrueFalseTotal
if $n->isa("XML::XQL::Operator") 0 0 0
957 0 ▶ return "${type}\[" . $n->xql_toString . "]" if $n->isa ('XML::XQL::PrimitiveType');
Branch: 1 branch
BranchTrueFalseTotal
if $n->isa("XML::XQL::PrimitiveType") 0 0 0
958 # return "${type}\[" . $n->toString . "]" if $n->isa ('XML::DOM::Element');
959 }
960 0 $n;
961 }
962  
963  
964 package XML::XQL::Query;
965  
966 28 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
967 28 ▶use XML::XQL::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
968  
969 28 ▶use vars qw( %Func %FuncArgCount );
Subroutine BEGIN: called CC=1 SCAR=0.0
970  
971 28 my $parser = new XML::XQL::Parser;
972  
973 # This is passed as 'yyerror' to YYParse
974 sub Error
975 0 ▶{
Subroutine Error: not called CC=1 SCAR=6.9
976 0 my($parser) = shift;
977  
978 0 print STDERR "Error in Query Expression near: " . $parser->YYData->{INPUT} . "\n";
979 }
980  
981 sub defineFunction
982 0 ▶{
Subroutine defineFunction: not called CC=2 SCAR=17.9
983 0 my ($self, $name, $func, $argCount, $allowedOutside, $const) = @_;
984 0 $self->{Func}->{$name} = $func;
985 0 $self->{FuncArgCount}->{$name} = $argCount;
986 0 ▶ $self->{AllowedOutsideSubquery}->{$name} = 1 if $allowedOutside;
Branch: 1 branch
BranchTrueFalseTotal
if $allowedOutside 0 0 0
987 0 $self->{ConstFunc} = $const;
988 }
989  
990 sub defineMethod
991 0 ▶{
Subroutine defineMethod: not called CC=2 SCAR=17.9
992 0 my ($self, $name, $func, $argCount, $allowedOutside) = @_;
993 0 $self->{Method}->{$name} = $func;
994 0 $self->{FuncArgCount}->{$name} = $argCount;
995 0 ▶ $self->{AllowedOutsideSubquery}->{$name} = 1 if $allowedOutside;
Branch: 1 branch
BranchTrueFalseTotal
if $allowedOutside 0 0 0
996 }
997  
998 sub defineComparisonOperators
999 0 ▶{
Subroutine defineComparisonOperators: not called CC=1 SCAR=6.9
1000 0 my ($self, %args) = @_;
1001 0 $self->{CompareOper} = \%args;
1002 }
1003  
1004 sub defineTokenQ
1005 0 ▶{
Subroutine defineTokenQ: not called CC=1 SCAR=6.9
1006 0 $_[0]->{'q'} = $_[1];
1007 }
1008  
1009 sub defineTokenQQ
1010 0 ▶{
Subroutine defineTokenQQ: not called CC=1 SCAR=6.9
1011 0 $_[0]->{'qq'} = $_[1];
1012 }
1013  
1014 sub new
1015 0 ▶{
Subroutine new: not called CC=9 SCAR=45.0
1016 0 my ($class, %args) = @_;
1017  
1018 0 ▶ croak "no Expr specified" unless defined $args{Expr};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"Expr"} 0 0 0
1019  
1020 0 my $self = bless \%args, $class;
1021  
1022 0 ▶ my $error = $self->{'Error'} || \&XML::XQL::Query::Error;
Condition0%0/2
$self->{'Error'} || \&XML::XQL::Query::Error
l!l
00
MC/DC0%0/1
$self->{'Error'} || \&XML::XQL::Query::Error
$self->{'Error'}
Truth table0%0/2
A
A= $self->{'Error'}
Aresult
11
00
1023 0 ▶ my $debug = defined ($self->{Debug}) ? $self->{Debug} : 0; # 0x17;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'Debug'} ? : 0 0 0
1024  
1025 0 ▶ $self->{'q'} = $XML::XQL::Token_q unless exists $self->{'q'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"q"} 0 0 0
1026 0 ▶ $self->{'qq'} = $XML::XQL::Token_qq unless exists $self->{'qq'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"qq"} 0 0 0
1027  
1028 # Invoke the query expression parser
1029 0 $parser->YYData->{INPUT} = $self->{Expr};
1030 0 $parser->{Query} = $self;
1031 0 $self->{Tree} = $parser->YYParse (yylex => \&XML::XQL::Lexer,
1032 yyerror => $error,
1033 yydebug => $debug);
1034  
1035 # Nothing but whitespace should be left over
1036 0 ▶ if ($parser->YYData->{INPUT} !~ /^\s*$/)
Branch: 1 branch
BranchTrueFalseTotal
unless ($parser->YYData->{"INPUT"} =~ /^\s*$/) 0 0 0
1037 {
1038 0 XML::XQL::parseError ("Error when parsing expression. Unexpected characters at end of expression [" . $parser->YYData->{INPUT} . "]")
1039 }
1040  
1041 XML::XQL::parseError ("Error when parsing expression")
1042 0 ▶ unless defined $self->{Tree};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"Tree"} 0 0 0
1043  
1044 0 $self->{Tree}->{Query} = $self;
1045 0 $self->{Tree}->xql_check (0, 0); # inSubQuery=0, inParam=0
1046  
1047 0 ▶ print "Expression parsed successfully\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
1048  
1049 0 $self;
1050 }
1051  
1052 sub dispose
1053 0 ▶{
Subroutine dispose: not called CC=1 SCAR=6.9
1054 0 my $self = shift;
1055  
1056 0 undef $self->{Tree}->{Query};
1057  
1058 0 $self->{Tree}->dispose;
1059 0 delete $self->{Tree};
1060 }
1061  
1062 sub isNodeQuery
1063 0 ▶{
Subroutine isNodeQuery: not called CC=1 SCAR=6.9
1064 0 $_[0]->{NodeQuery};
1065 }
1066  
1067 sub solve
1068 0 ▶{
Subroutine solve: not called CC=2 SCAR=17.9
1069 0 my ($self, @list) = @_;
1070 0 my $context = undef;
1071  
1072 # clear cached "once" values
1073 0 $self->{Tree}->xql_prepCache;
1074 0 my $result = $self->{Tree}->solve ($context, \@list);
1075 0 ▶ ref ($result) eq "ARRAY" ? @$result : ($result);
Branch: 1 branch
BranchTrueFalseTotal
ref $result eq 'ARRAY' ? : 0 0 0
1076 }
1077  
1078 sub toString
1079 0 ▶{
Subroutine toString: not called CC=1 SCAR=6.9
1080 0 $_[0]->{Expr};
1081 }
1082  
1083 sub toDOM
1084 0 ▶{
Subroutine toDOM: not called CC=1 SCAR=6.9
1085 0 my ($self, $doc) = @_;
1086 0 my $root = $doc->createElement ("XQL");
1087 0 $doc->appendChild ($root);
1088 0 $root->appendChild ($self->{Tree}->xql_toDOM ($doc));
1089 0 $doc;
1090 }
1091  
1092 sub findComparisonOperator
1093 0 ▶{
Subroutine findComparisonOperator: not called CC=3 SCAR=24.8
1094 0 my ($self, $name) = @_;
1095 0 my $cmp;
1096 0 ▶ if (exists $self->{CompareOper}->{$name})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'CompareOper'}{$name}) { } 0 0 0
1097 {
1098 0 $cmp = $self->{CompareOper}->{$name};
1099 }
1100 else
1101 {
1102 0 $cmp = $XML::XQL::CompareOper{$name};
1103 }
1104 0 ▶ if (not defined $cmp)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cmp) 0 0 0
1105 {
1106 0 XML::XQL::parseError ("undefined comparison operator '$name'");
1107 }
1108 0 $cmp;
1109 }
1110  
1111 # Return function pointer. Croak if wrong number of arguments.
1112 sub findFunctionOrMethod
1113 0 ▶{
Subroutine findFunctionOrMethod: not called CC=16 SCAR=56.1
1114 0 my ($self, $name, $args) = @_;
1115  
1116 0 my $func;
1117 0 my $type = "function";
1118 0 ▶ if (exists $self->{Func}->{$name})
Branch: 5 branches
BranchTrueFalseTotal
if (exists $self->{'Func'}{$name}) { } 0 0 0
elsif (exists $self->{'Method'}{$name}) { } 0 0 0
elsif (defined $XML::XQL::Func{$name}) { } 0 0 0
elsif (defined $XML::XQL::Method{$name}) { } 0 0 0
elsif (not $XML::XQL::Restricted) { } 0 0 0
1119 {
1120 0 $func = $self->{Func}->{$name};
1121 }
1122 elsif (exists $self->{Method}->{$name})
1123 {
1124 0 $func = $self->{Method}->{$name};
1125 0 $type = "method";
1126 }
1127 elsif (defined $XML::XQL::Func{$name})
1128 {
1129 0 $func = $XML::XQL::Func{$name};
1130 }
1131 elsif (defined $XML::XQL::Method{$name})
1132 {
1133 0 $func = $XML::XQL::Method{$name};
1134 0 $type = "method";
1135 }
1136 elsif (not $XML::XQL::Restricted)
1137 {
1138 0 $func = XML::XQL::generatePerlWrapper ($name);
1139 }
1140  
1141 0 ▶ XML::XQL::parseError ("undefined function/method '$name' in query '" .
Branch: 1 branch
BranchTrueFalseTotal
unless defined &$func 0 0 0
1142 $self->toString . "'")
1143 unless defined &$func;
1144  
1145 my $funcArgCount = $self->{FuncArgCount}->{$name}
1146 0 ▶ || $XML::XQL::FuncArgCount{$name} || 0;
Condition0%0/5
$self->{'FuncArgCount'}{$name} || $XML::XQL::FuncArgCount{$name} || 0
l!l
00
$self->{"FuncArgCount"}{$name} or $XML::XQL::FuncArgCount{$name}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'FuncArgCount'}{$name} || $XML::XQL::FuncArgCount{$name} || 0
$self->{"FuncArgCount"}{$name} $XML::XQL::FuncArgCount{$name}
Truth table0%0/3
A or B
A= $self->{"FuncArgCount"}{$name}B= $XML::XQL::FuncArgCount{$name}
ABresult
1X1
011
000
1147  
1148 # Check number of args
1149 0 my $nargs = @$args;
1150  
1151 #print "$args " . XML::XQL::d($args) . "\n";
1152  
1153 0 my $ok = 0;
1154 0 ▶ if (ref ($funcArgCount) eq "ARRAY")
Branch: 1 branch
BranchTrueFalseTotal
if (ref $funcArgCount eq 'ARRAY') { } 0 0 0
1155 {
1156 0 my $i = 0;
1157 0 my $n = @$funcArgCount;
1158 0 ▶ while ($i < $n)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 0 0 0
1159 {
1160 0 my $s = $funcArgCount->[$i++];
1161 0 ▶ my $e = $funcArgCount->[$i++] || $s; # same as $s if odd #args
Condition0%0/3
$$funcArgCount[$i++] || $s
l!l&&r!l&&!r
000
MC/DC0%0/2
$$funcArgCount[$i++] || $s
$$funcArgCount[$i++] $s
Truth table0%0/3
A || B
A= $$funcArgCount[$i++]B= $s
ABresult
1X1
011
000
1162 0 ▶ if ($nargs >= $s && ($e == -1 || $nargs <= $e))
Branch: 1 branch
BranchTrueFalseTotal
if ($nargs >= $s and $e == -1 || $nargs <= $e) 0 0 0
Condition0%0/6
$nargs >= $s and $e == -1 || $nargs <= $e
!ll&&!rl&&r
000
$e == -1 or $nargs <= $e
l!l&&r!l&&!r
000
MC/DC0%0/3
$nargs >= $s and $e == -1 || $nargs <= $e
$nargs >= $s $e == -1 $nargs <= $e
Truth table0%0/4
A and B or C
A= $nargs >= $sB= $e == -1C= $nargs <= $e
ABCresult
0XX0
1000
11X1
1011
1163 {
1164 0 $ok = 1; # found it
1165 0 last;
1166 }
1167 }
1168 }
1169 else
1170 {
1171 0 $ok = ($nargs eq $funcArgCount);
1172 }
1173  
1174 0 ▶ XML::XQL::parseError ("wrong number of args ($nargs) for $type $name in query '" .
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 0 0
1175 $self->toString . "', it should be " . XML::XQL::d($funcArgCount))
1176 if not $ok;
1177  
1178 0 return ($func, $type);
1179 }
1180  
1181 sub isAllowedOutsideSubquery
1182 0 ▶{
Subroutine isAllowedOutsideSubquery: not called CC=2 SCAR=17.9
1183 0 my ($self, $funcName) = @_;
1184 0 my ($ok) = $self->{AllowedOutsideSubquery}->{$funcName};
1185 0 ▶ return $ok if defined $ok;
Branch: 1 branch
BranchTrueFalseTotal
if defined $ok 0 0 0
1186 0 $XML::XQL::AllowedOutsideSubquery{$funcName};
1187 }
1188  
1189 package XML::XQL::Operator;
1190 28 ▶use fields qw{ Left Right Parent };
Subroutine BEGIN: called CC=1 SCAR=0.0
1191  
1192 sub new
1193 0 ▶{
Subroutine new: not called CC=3 SCAR=24.8
1194 0 my ($class, %attr) = @_;
1195 0 my $self = bless \%attr, $class;
1196  
1197 0 ▶ $self->{Left}->setParent ($self) if defined $self->{Left};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"Left"} 0 0 0
1198 0 ▶ $self->{Right}->setParent ($self) if defined $self->{Right};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"Right"} 0 0 0
1199  
1200 0 $self;
1201 }
1202  
1203 sub dispose
1204 0 ▶{
Subroutine dispose: not called CC=3 SCAR=24.8
1205 0 my $self = shift;
1206 0 ▶ if (defined ($self->{Left}))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"Left"}) 0 0 0
1207 {
1208 0 $self->{Left}->dispose;
1209 0 undef $self->{Left};
1210 }
1211 0 ▶ if (defined ($self->{Right}))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"Right"}) 0 0 0
1212 {
1213 0 $self->{Right}->dispose;
1214 0 undef $self->{Right};
1215 }
1216  
1217 0 undef $self->{Parent};
1218 }
1219  
1220 sub xql_check
1221 0 ▶{
Subroutine xql_check: not called CC=2 SCAR=17.9
1222 0 my ($self, $inSubQuery, $inParam) = @_;
1223 0 $self->{Left}->xql_check ($inSubQuery, $inParam);
1224 0 ▶ $self->{Right}->xql_check ($inSubQuery, $inParam) if defined $self->{Right};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"Right"} 0 0 0
1225 }
1226  
1227 sub xql_prepCache
1228 0 ▶{
Subroutine xql_prepCache: not called CC=2 SCAR=17.9
1229 0 my ($self) = @_;
1230 0 $self->{Left}->xql_prepCache;
1231 0 ▶ $self->{Right}->xql_prepCache if defined $self->{Right};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"Right"} 0 0 0
1232 }
1233  
1234 sub xql_toDOM
1235 0 ▶{
Subroutine xql_toDOM: not called CC=3 SCAR=24.8
1236 0 my ($self, $doc) = @_;
1237 0 my $name = ref $self;
1238 0 $name =~ s/.*:://;
1239 0 my $elem = $doc->createElement ($name);
1240 0 ▶ if (defined $self->{Left})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"Left"}) 0 0 0
1241 {
1242 0 my $left = $doc->createElement ("left");
1243 0 $elem->appendChild ($left);
1244 0 $left->appendChild ($self->{Left}->xql_toDOM ($doc));
1245 }
1246 0 ▶ if (defined $self->{Right})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"Right"}) 0 0 0
1247 {
1248 0 my $right = $doc->createElement ("right");
1249 0 $elem->appendChild ($right);
1250 0 $right->appendChild ($self->{Right}->xql_toDOM ($doc));
1251 }
1252 0 $elem;
1253 }
1254  
1255 sub isConstant
1256 0 ▶{
Subroutine isConstant: not called CC=1 SCAR=6.9
1257 0 0;
1258 }
1259  
1260 # Overriden by Union and Path operators
1261 sub mustSort
1262 0 ▶{
Subroutine mustSort: not called CC=1 SCAR=6.9
1263 0 0;
1264 }
1265  
1266 sub setParent
1267 0 ▶{
Subroutine setParent: not called CC=1 SCAR=6.9
1268 0 $_[0]->{Parent} = $_[1];
1269 }
1270  
1271 sub warning
1272 0 ▶{
Subroutine warning: not called CC=1 SCAR=6.9
1273 0 my ($self, $msg) = @_;
1274 0 print STDERR "WARNING: $msg";
1275 0 print STDERR " Context: " . $self->toContextString . "\n";
1276 }
1277  
1278 sub root
1279 0 ▶{
Subroutine root: not called CC=2 SCAR=17.9
1280 0 my ($self) = @_;
1281 0 my $top = $self;
1282  
1283 0 ▶ while (defined ($top->{Parent}))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $top->{"Parent"}) 0 0 0
1284 {
1285 0 $top = $top->{Parent};
1286 }
1287 0 $top;
1288 }
1289  
1290 sub query
1291 0 ▶{
Subroutine query: not called CC=1 SCAR=6.9
1292 0 $_[0]->root->{Query};
1293 }
1294  
1295 sub toContextString
1296 0 ▶{
Subroutine toContextString: not called CC=1 SCAR=6.9
1297 0 my ($self) = @_;
1298 0 $self->root->xql_contextString ($self);
1299 }
1300  
1301 sub debugString
1302 0 ▶{
Subroutine debugString: not called CC=2 SCAR=17.9
1303 0 my ($self) = @_;
1304 0 my $str = "[" . ref($self);
1305 0 ▶ while (my ($key, $val) = each %$self)
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$self) 0 0 0
1306 {
1307 0 $str .= "$key=>" . XML::XQL::d($val);
1308 }
1309 0 $str . "]";
1310 }
1311  
1312 sub verbose
1313 0 ▶{
Subroutine verbose: not called CC=1 SCAR=6.9
1314 0 my ($self, $str, $list) = @_;
1315 # print STDERR "$self - $str: " . XML::XQL::d($list) . "\n";
1316 0 $list;
1317 }
1318  
1319 package XML::XQL::Root; # "/" at start of XQL expression
1320 28 ▶use base 'XML::XQL::Operator'; # L -> L
Subroutine BEGIN: called CC=1 SCAR=0.0
1321  
1322 sub solve
1323 0 ▶{
Subroutine solve: not called CC=2 SCAR=17.9
1324 0 my ($self, $context, $list) = @_;
1325 0 ▶ return [] if (@$list < 1);
Branch: 1 branch
BranchTrueFalseTotal
if @$list < 1 0 0 0
1326  
1327 #?? what if first value is not a XML::XQL::Node? should we try the second one?
1328 0 [$list->[0]->xql_document];
1329 }
1330 #?? add isOnce here?
1331  
1332 sub xql_check
1333 0 ▶{
Subroutine xql_check: not called CC=1 SCAR=0.0
1334 }
1335  
1336 sub xql_prepCache
1337 0 ▶{
Subroutine xql_prepCache: not called CC=1 SCAR=0.0
1338 }
1339  
1340 sub xql_contextString
1341 0 ▶{
Subroutine xql_contextString: not called CC=1 SCAR=6.9
1342 0 XML::XQL::delim ("/", @_);
1343 }
1344  
1345 package XML::XQL::Path;
1346 28 ▶use base 'XML::XQL::Operator'; # L -> L
Subroutine BEGIN: called CC=1 SCAR=0.0
1347 28 ▶use fields qw{ PathOp };
Subroutine BEGIN: called CC=1 SCAR=0.0
1348  
1349 sub new
1350 0 ▶{
Subroutine new: not called CC=2 SCAR=17.9
1351 0 my ($class, %arg) = @_;
1352 0 my $self = bless \%arg, $class;
1353  
1354 0 ▶ $self->{Left} ||= new XML::XQL::Root;
Condition0%0/3
$self->{"Left"} ||= "XML::XQL::Root"->new
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"Left"} ||= "XML::XQL::Root"->new
$self->{"Left"} "XML::XQL::Root"->new
Truth table0%0/3
A ||= B
A= $self->{"Left"}B= "XML::XQL::Root"->new
ABresult
1X1
011
000
1355  
1356 0 $self->{Left}->setParent ($self);
1357 0 $self->{Right}->setParent ($self);
1358  
1359 0 $self;
1360 }
1361  
1362 sub solve
1363 0 ▶{
Subroutine solve: not called CC=11 SCAR=48.8
1364 0 my ($self, $context, $list) = @_;
1365 0 $list = $self->{Left}->solve ($context, $list);
1366 0 $self->verbose ("left", $list);
1367  
1368 0 ▶ return $list if @$list < 1;
Branch: 1 branch
BranchTrueFalseTotal
if @$list < 1 0 0 0
1369  
1370 0 ▶ if ($self->{PathOp} eq '/')
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'PathOp'} eq '/') { } 0 0 0
1371 {
1372 0 $self->verbose ("result", $self->{Right}->solve ($context, $list));
1373 }
1374 else # recurse "//"
1375 {
1376 0 my $new_list = [];
1377 0 my $n = @$list;
1378 0 ▶ NODE: for (my $i = 0; $i < $n; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 0 0 0
1379 {
1380 0 my $node = $list->[$i];
1381 # Node must be an Element or must be allowed to contain Elements
1382 # i.e. must be an Element or a Document
1383 # (DocumentFragment is not expected here)
1384 0 my $nodeType = $node->xql_nodeType;
1385 0 ▶ next NODE unless ($nodeType == 1 || $nodeType == 9);
Branch: 1 branch
BranchTrueFalseTotal
unless $nodeType == 1 or $nodeType == 9 0 0 0
Condition0%0/3
$nodeType == 1 or $nodeType == 9
l!l&&r!l&&!r
000
MC/DC0%0/2
$nodeType == 1 or $nodeType == 9
$nodeType == 1 $nodeType == 9
Truth table0%0/3
A or B
A= $nodeType == 1B= $nodeType == 9
ABresult
1X1
011
000
1386
1387 # Skip the node if one of its ancestors is part of the input $list
1388 # (and therefore already processed)
1389 0 my $parent = $node->xql_parent;
1390 0 ▶ while (defined $parent)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $parent) 0 0 0
1391 {
1392 0 ▶ for (my $j = $i - 1; $j >= 0; $j--)
Branch: 1 branch
BranchTrueFalseTotal
if ($j >= 0) 0 0 0
1393 {
1394 0 ▶ next NODE if ($parent == $list->[$j]);
Branch: 1 branch
BranchTrueFalseTotal
if $parent == $list->[$j] 0 0 0
1395 }
1396 0 $parent = $parent->xql_parent;
1397 }
1398 0 recurse ($node, $new_list);
1399 }
1400
1401 0 my $results = $self->{Right}->solve ($context, $new_list);
1402  
1403 # Sort the result list unless the parent Operator will sort
1404 0 my $parent = $self->{Parent};
1405 0 ▶ XML::XQL::sortDocOrder ($results)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $parent and $parent->mustSort 0 0 0
Condition0%0/3
defined $parent and $parent->mustSort
!ll&&!rl&&r
000
MC/DC0%0/2
defined $parent and $parent->mustSort
defined $parent $parent->mustSort
Truth table0%0/3
A and B
A= defined $parentB= $parent->mustSort
ABresult
0X0
100
111
1406 unless defined ($parent) and $parent->mustSort;
1407  
1408 0 $self->verbose ("result //", $results);
1409 }
1410 }
1411  
1412 sub mustSort
1413 0 ▶{
Subroutine mustSort: not called CC=1 SCAR=6.9
1414 0 $_[0]->{PathOp} eq '//';
1415 }
1416  
1417 sub recurse
1418 0 ▶{
Subroutine recurse: not called CC=2 SCAR=17.9
1419 0 my ($node, $list) = @_;
1420 0 push @$list, $node;
1421 0 for my $kid (@{$node->xql_element})
1422 {
1423 0 recurse ($kid, $list);
1424 }
1425 }
1426  
1427 sub xql_contextString
1428 0 ▶{
Subroutine xql_contextString: not called CC=2 SCAR=17.9
1429 0 my $self = shift;
1430  
1431 my $str = $self->{Left}->isa ('XML::XQL::Root') ?
1432 0 ▶ "" : $self->{Left}->xql_contextString (@_);
Branch: 1 branch
BranchTrueFalseTotal
$self->{'Left'}->isa('XML::XQL::Root') ? : 0 0 0
1433  
1434 XML::XQL::delim ($str . XML::XQL::bold($self->{PathOp}) .
1435 0 $self->{Right}->xql_contextString (@_), $self, @_);
1436 }
1437  
1438 sub xql_toDOM
1439 0 ▶{
Subroutine xql_toDOM: not called CC=1 SCAR=6.9
1440 0 my ($self, $doc) = @_;
1441 0 my $elem = $self->SUPER::xql_toDOM ($doc);
1442 0 $elem->setAttribute ("pathOp", $self->{PathOp});
1443 0 $elem;
1444 }
1445  
1446 package XML::XQL::Sequence; # "elem;elem" or "elem;;elem"
1447 28 ▶use base 'XML::XQL::Operator'; # L -> L
Subroutine BEGIN: called CC=1 SCAR=0.0
1448 28 ▶use fields qw{ Oper };
Subroutine BEGIN: called CC=1 SCAR=0.0
1449  
1450 # See "The Design of XQL" by Jonathan Robie
1451 # <URL:http://www.texcel.no/whitepapers/xql-design.html>
1452 # for definition of Sequence operators.
1453  
1454 # Note that the "naive" implementation slows things down quite a bit here...
1455 sub solve
1456 0 ▶{
Subroutine solve: not called CC=15 SCAR=54.8
1457 0 my ($self, $context, $list) = @_;
1458 0 my $left = $self->{Left}->solve ($context, $list);
1459 0 $self->verbose ("left", $left);
1460 0 ▶ return [] unless @$left;
Branch: 1 branch
BranchTrueFalseTotal
unless @$left 0 0 0
1461  
1462 0 my $right = $self->{Right}->solve ($context, $list);
1463 0 $self->verbose ("right", $right);
1464 0 ▶ return [] unless @$right;
Branch: 1 branch
BranchTrueFalseTotal
unless @$right 0 0 0
1465  
1466 0 my @result;
1467 0 ▶ if ($self->{Oper} eq ';') # immediately precedes
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'Oper'} eq ';') { } 0 0 0
1468 {
1469 0 my %hleft; @hleft{@$left} = (); # initialize all values to undef
1470 0 my %pushed;
1471  
1472 0 for my $r (@$right)
1473 {
1474 # Find previous sibling that is not a text node that has only
1475 # whitespace that can be ignored (because xml:space=preserve)
1476 0 my $prev = $r->xql_prevNonWS;
1477 # $prev must be defined and must exist in $left
1478 0 ▶ next unless $prev and exists $hleft{$prev};
Branch: 1 branch
BranchTrueFalseTotal
unless $prev and exists $hleft{$prev} 0 0 0
Condition0%0/3
$prev and exists $hleft{$prev}
!ll&&!rl&&r
000
MC/DC0%0/2
$prev and exists $hleft{$prev}
$prev exists $hleft{$prev}
Truth table0%0/3
A and B
A= $prevB= exists $hleft{$prev}
ABresult
0X0
100
111
1479  
1480 # Filter duplicates (no need to sort afterwards)
1481 0 ▶ push @result, $prev unless $pushed{$prev}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $pushed{$prev}++ 0 0 0
1482 0 ▶ push @result, $r unless $pushed{$r}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $pushed{$r}++ 0 0 0
1483 }
1484 }
1485 else # oper eq ';;' (i.e. precedes)
1486 {
1487 0 my %pushed;
1488  
1489 0 for my $r (@$right)
1490 {
1491 0 for my $l (@$left)
1492 {
1493 # If left node precedes right node, add them
1494 0 ▶ if ($l->xql_sortKey lt $r->xql_sortKey)
Branch: 1 branch
BranchTrueFalseTotal
if ($l->xql_sortKey lt $r->xql_sortKey) 0 0 0
1495 {
1496 # Filter duplicates
1497 0 ▶ push @result, $l unless $pushed{$l}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $pushed{$l}++ 0 0 0
1498 0 ▶ push @result, $r unless $pushed{$r}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $pushed{$r}++ 0 0 0
1499 }
1500 }
1501  
1502 #?? optimize - left & right are already sorted...
1503 # sort in document order
1504 0 ▶ XML::XQL::sortDocOrder (\@result) if @result;
Branch: 1 branch
BranchTrueFalseTotal
if @result 0 0 0
1505 }
1506 }
1507 0 \@result;
1508 }
1509  
1510 sub xql_contextString
1511 0 ▶{
Subroutine xql_contextString: not called CC=1 SCAR=6.9
1512 0 my $self = shift;
1513 XML::XQL::delim ($self->{Left}->xql_contextString (@_) .
1514 XML::XQL::bold($self->{Oper}) .
1515 0 $self->{Right}->xql_contextString (@_), $self, @_);
1516 }
1517  
1518 package XML::XQL::Current; # "."
1519 28 ▶use base 'XML::XQL::Operator'; # L -> L
Subroutine BEGIN: called CC=1 SCAR=0.0
1520  
1521 sub xql_check
1522 0 ▶{
Subroutine xql_check: not called CC=1 SCAR=0.0
1523 }
1524  
1525 sub xql_prepCache
1526 0 ▶{
Subroutine xql_prepCache: not called CC=1 SCAR=0.0
1527 }
1528  
1529 sub solve
1530 0 ▶{
Subroutine solve: not called CC=1 SCAR=6.9
1531 0 my ($self, $context, $list) = @_;
1532 0 $list;
1533 }
1534  
1535 sub xql_contextString
1536 0 ▶{
Subroutine xql_contextString: not called CC=1 SCAR=6.9
1537 0 XML::XQL::delim (".", @_);
1538 }
1539  
1540 package XML::XQL::Parent; # ".."
1541 28 ▶use base 'XML::XQL::Operator';
Subroutine BEGIN: called CC=1 SCAR=0.0
1542  
1543 sub xql_check
1544 0 ▶{
Subroutine xql_check: not called CC=1 SCAR=0.0
1545 }
1546  
1547 sub xql_prepCache
1548 0 ▶{
Subroutine xql_prepCache: not called CC=1 SCAR=0.0
1549 }
1550  
1551 sub solve
1552 0 ▶{
Subroutine solve: not called CC=2 SCAR=17.9
1553 0 my ($self, $context, $list) = @_;
1554 0 my @result = ();
1555 0 for my $node (@$list)
1556 {
1557 0 push @result, $node->xql_parent;
1558 }
1559 0 \@result;
1560 }
1561  
1562 sub xql_contextString
1563 0 ▶{
Subroutine xql_contextString: not called CC=1 SCAR=6.9
1564 0 XML::XQL::delim ("..", @_);
1565 }
1566  
1567 package XML::XQL::Element; # "elem"
1568 28 ▶use base 'XML::XQL::Operator'; # L -> L
Subroutine BEGIN: called CC=1 SCAR=0.0
1569 28 ▶use fields qw{ Name NameSpace Expr };
Subroutine BEGIN: called CC=1 SCAR=0.0
1570  
1571 sub new
1572 0 ▶{
Subroutine new: not called CC=3 SCAR=24.8
1573 0 my ($class, %args) = @_;
1574 0 ▶ if (not defined ($args{NameSpace}))
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"NameSpace"}) 0 0 0
1575 {
1576 0 ▶ if ($args{Name} eq "*")
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'Name'} eq '*') { } 0 0 0
1577 {
1578 0 return bless \%args, 'XML::XQL::AllElements';
1579 }
1580 else
1581 {
1582 0 return bless \%args, 'XML::XQL::SimpleElement';
1583 }
1584 }
1585  
1586 $args{Expr} = XML::XQL::buildNameSpaceExpr ($args{NameSpace},
1587 0 $args{Name});
1588 0 bless \%args, $class;
1589 }
1590  
1591 sub xql_check
1592 0 ▶{
Subroutine xql_check: not called CC=1 SCAR=0.0
1593 }
1594  
1595 sub xql_prepCache
1596 0 ▶{
Subroutine xql_prepCache: not called CC=1 SCAR=0.0
1597 }
1598  
1599 sub solve
1600 0 ▶{
Subroutine solve: not called CC=4 SCAR=30.0
1601 0 my ($self, $context, $list) = @_;
1602 0 my @result = ();
1603  
1604 0 my $expr = $self->{Expr};
1605 0 for my $node (@$list)
1606 {
1607 0 for my $kid (@{$node->xql_element})
1608 {
1609 0 ▶ push @result, $kid if $kid->xql_nodeName =~ /$expr/;
Branch: 1 branch
BranchTrueFalseTotal
if $kid->xql_nodeName =~ /$expr/ 0 0 0
1610 }
1611 }
1612 0 \@result;
1613 }
1614  
1615 sub xql_contextString
1616 0 ▶{
Subroutine xql_contextString: not called CC=2 SCAR=17.9
1617 0 my $self = shift;
1618 0 my $name = $self->{Name};
1619 0 my $space = $self->{NameSpace};
1620  
1621 0 ▶ my $str = defined($space) ? "$space:$name" : $name;
Branch: 1 branch
BranchTrueFalseTotal
defined $space ? : 0 0 0
1622  
1623 0 XML::XQL::delim ($str, $self, @_);
1624 }
1625  
1626 sub xql_toDOM
1627 0 ▶{
Subroutine xql_toDOM: not called CC=2 SCAR=17.9
1628 0 my ($self, $doc) = @_;
1629 0 my $elem = $self->SUPER::xql_toDOM ($doc);
1630  
1631 0 my $name = $self->{Name};
1632 0 my $space = $self->{NameSpace};
1633 0 ▶ my $str = defined($space) ? "$space:$name" : $name;
Branch: 1 branch
BranchTrueFalseTotal
defined $space ? : 0 0 0
1634  
1635 0 $elem->setAttribute ("name", $str);
1636 0 $elem;
1637 }
1638  
1639 package XML::XQL::SimpleElement; # "elem"
1640 28 ▶use base 'XML::XQL::Element'; # L -> L
Subroutine BEGIN: called CC=1 SCAR=0.0
1641  
1642 sub solve
1643 0 ▶{
Subroutine solve: not called CC=2 SCAR=17.9
1644 0 my ($self, $context, $list) = @_;
1645 0 my @result = ();
1646 0 my $name = $self->{Name};
1647  
1648 0 for my $node (@$list)
1649 {
1650 0 push @result, @{ $node->xql_element ($name) };
1651 }
1652 0 \@result;
1653 }
1654  
1655 package XML::XQL::AllElements; # "*"
1656 28 ▶use base 'XML::XQL::Element'; # L -> L
Subroutine BEGIN: called CC=1 SCAR=0.0
1657  
1658 sub solve
1659 0 ▶{
Subroutine solve: not called CC=2 SCAR=17.9
1660 0 my ($self, $context, $list) = @_;
1661 0 my @result = ();
1662  
1663 0 for my $node (@$list)
1664 {
1665 0 push @result, @{$node->xql_element};
1666 }
1667 0 \@result;
1668 }
1669  
1670 package XML::XQL::Attribute; # "@attr"
1671 28 ▶use base 'XML::XQL::Operator'; # L -> L of Attributes
Subroutine BEGIN: called CC=1 SCAR=0.0
1672 28 ▶use fields qw{ Name NameSpace Expr };
Subroutine BEGIN: called CC=1 SCAR=0.0
1673  
1674 sub new
1675 0 ▶{
Subroutine new: not called CC=3 SCAR=24.8
1676 0 my ($class, %args) = @_;
1677  
1678 0 ▶ if (not defined ($args{NameSpace}))
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"NameSpace"}) 0 0 0
1679 {
1680 0 ▶ if ($args{Name} eq "*")
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'Name'} eq '*') { } 0 0 0
1681 {
1682 0 return bless \%args, 'XML::XQL::AllAttr';
1683 }
1684 else
1685 {
1686 0 return bless \%args, 'XML::XQL::SimpleAttr';
1687 }
1688 }
1689  
1690 $args{Expr} = XML::XQL::buildNameSpaceExpr ($args{NameSpace},
1691 0 $args{Name});
1692 0 bless \%args, $class;
1693 }
1694  
1695 sub xql_check
1696 0 ▶{
Subroutine xql_check: not called CC=1 SCAR=0.0
1697 }
1698  
1699 sub xql_prepCache
1700 0 ▶{
Subroutine xql_prepCache: not called CC=1 SCAR=0.0
1701 }
1702  
1703 sub solve
1704 0 ▶{
Subroutine solve: not called CC=4 SCAR=30.0
1705 0 my ($self, $context, $list) = @_;
1706 0 my @result = ();
1707  
1708 0 my $expr = $self->{Expr};
1709 0 for my $node (@$list)
1710 {
1711 0 for my $kid (@{$node->xql_attribute})
1712 {
1713 0 ▶ push @result, $kid if $kid->xql_nodeName =~ /$expr/;
Branch: 1 branch
BranchTrueFalseTotal
if $kid->xql_nodeName =~ /$expr/ 0 0 0
1714 }
1715 }
1716 }
1717  
1718 sub xql_contextString
1719 0 ▶{
Subroutine xql_contextString: not called CC=2 SCAR=17.9
1720 0 my $self = shift;
1721 0 my $name = $self->{Name};
1722 0 my $space = $self->{NameSpace};
1723  
1724 0 ▶ my $str = defined($space) ? "\@$space:$name" : ('@' . $name);
Branch: 1 branch
BranchTrueFalseTotal
defined $space ? : 0 0 0
1725  
1726 0 XML::XQL::delim ($str, $self, @_);
1727 }
1728  
1729 package XML::XQL::SimpleAttr; # "@attr"
1730 28 ▶use base 'XML::XQL::Attribute'; # L -> L
Subroutine BEGIN: called CC=1 SCAR=0.0
1731  
1732 sub solve
1733 0 ▶{
Subroutine solve: not called CC=2 SCAR=17.9
1734 0 my ($self, $context, $list) = @_;
1735 0 my @result = ();
1736 0 my $name = $self->{Name};
1737  
1738 0 for my $node (@$list)
1739 {
1740 0 push @result, @{ $node->xql_attribute ($name) };
1741 }
1742 0 \@result;
1743 }
1744  
1745 package XML::XQL::AllAttr; # "@*"
1746 28 ▶use base 'XML::XQL::Attribute'; # L -> L
Subroutine BEGIN: called CC=1 SCAR=0.0
1747  
1748 sub solve
1749 0 ▶{
Subroutine solve: not called CC=2 SCAR=17.9
1750 0 my ($self, $context, $list) = @_;
1751 0 my @result = ();
1752  
1753 0 for my $node (@$list)
1754 {
1755 0 push @result, @{$node->xql_attribute};
1756 }
1757 0 \@result;
1758 }
1759  
1760 package XML::XQL::Subscript; # "[3, 5 $to$ 7, -1]"
1761 28 ▶use base 'XML::XQL::Operator'; # L -> L
Subroutine BEGIN: called CC=1 SCAR=0.0
1762 28 ▶use fields qw{ IndexList };
Subroutine BEGIN: called CC=1 SCAR=0.0
1763  
1764 #?? optimize for simple subscripts
1765 sub solve
1766 0 ▶{
Subroutine solve: not called CC=14 SCAR=53.5
1767 0 my ($self, $context, $inlist) = @_;
1768 0 my @result = ();
1769  
1770 0 for my $node (@$inlist)
1771 {
1772  
1773 0 my $list = $self->{Left}->solve ($context, [$node]);
1774 0 $self->verbose("Left", $list);
1775  
1776 0 my $n = int (@$list);
1777 0 ▶ next if ($n == 0);
Branch: 1 branch
BranchTrueFalseTotal
if $n == 0 0 0 0
1778  
1779 # build ordered index list
1780 0 my @indexFlags = ();
1781 0 $#indexFlags = $n - 1;
1782
1783 0 my $index = $self->{IndexList};
1784 0 my $len = @$index;
1785  
1786 #?? this is done a lot - optimize....
1787 0 my $i = 0;
1788 0 ▶ while ($i < $len)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $len) 0 0 0
1789 {
1790 0 my $start = $index->[$i++];
1791 0 ▶ $start += $n if ($start < 0);
Branch: 1 branch
BranchTrueFalseTotal
if $start < 0 0 0 0
1792 0 my $end = $index->[$i++];
1793 0 ▶ $end += $n if ($end < 0);
Branch: 1 branch
BranchTrueFalseTotal
if $end < 0 0 0 0
1794
1795 0 ▶ next unless $start <= $end && $end >=0 && $start < $n;
Branch: 1 branch
BranchTrueFalseTotal
unless $start <= $end and $end >= 0 and $start < $n 0 0 0
Condition0%0/6
$start <= $end and $end >= 0 and $start < $n
!ll&&!rl&&r
000
$start <= $end and $end >= 0
!ll&&!rl&&r
000
MC/DC0%0/3
$start <= $end and $end >= 0 and $start < $n
$start <= $end $end >= 0 $start < $n
Truth table0%0/4
A and B and C
A= $start <= $endB= $end >= 0C= $start < $n
ABCresult
0XX0
10X0
1100
1111
1796 0 ▶ $start = 0 if ($start < 0);
Branch: 1 branch
BranchTrueFalseTotal
if $start < 0 0 0 0
1797 0 ▶ $end = $n-1 if ($end >= $n);
Branch: 1 branch
BranchTrueFalseTotal
if $end >= $n 0 0 0
1798
1799 0 for my $j ($start .. $end)
1800 {
1801 0 $indexFlags[$j] = 1;
1802 }
1803 }
1804 0 for $i (0 .. $n-1)
1805 {
1806 0 ▶ push @result, $list->[$i] if $indexFlags[$i];
Branch: 1 branch
BranchTrueFalseTotal
if $indexFlags[$i] 0 0 0
1807 }
1808 }
1809 0 \@result;
1810 }
1811  
1812 sub xql_contextString
1813 0 ▶{
Subroutine xql_contextString: not called CC=4 SCAR=30.0
1814 0 my $self = shift;
1815  
1816 0 my $index = $self->{IndexList};
1817 0 my $str = XML::XQL::bold("[");
1818 0 ▶ for (my $i = 0; $i < @$index; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$index) 0 0 0
1819 {
1820 0 ▶ $str .= ", " if $i > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $i > 0 0 0 0
1821  
1822 0 my $s = $index->[$i++];
1823 0 my $e = $index->[$i];
1824 0 ▶ $str = ($s == $e) ? $s : "$s \$to\$ $e";
Branch: 1 branch
BranchTrueFalseTotal
$s == $e ? : 0 0 0
1825 }
1826 0 $str .= XML::XQL::bold("]");
1827  
1828 0 XML::XQL::delim ($self->{Left}->xql_contextString (@_) . $str, $self, @_);
1829 }
1830  
1831 sub xql_toDOM
1832 0 ▶{
Subroutine xql_toDOM: not called CC=4 SCAR=30.0
1833 0 my ($self, $doc) = @_;
1834 0 my $elem = $self->SUPER::xql_toDOM ($doc);
1835  
1836 0 my $index = $self->{IndexList};
1837 0 my $str = "";
1838 0 ▶ for (my $i = 0; $i < @$index; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$index) 0 0 0
1839 {
1840 0 ▶ $str .= ", " if $i > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $i > 0 0 0 0
1841  
1842 0 my $s = $index->[$i++];
1843 0 my $e = $index->[$i];
1844 0 ▶ $str .= ($s == $e) ? $s : "$s \$to\$ $e";
Branch: 1 branch
BranchTrueFalseTotal
$s == $e ? : 0 0 0
1845 }
1846  
1847 0 my $ie = $doc->createElement ("index");
1848 0 $ie->setAttribute ("list", $str);
1849 0 $elem->appendChild ($ie);
1850 0 $elem;
1851 }
1852  
1853 package XML::XQL::Union; # "book $union$ magazine", also "|"
1854 28 ▶use base 'XML::XQL::Operator'; # L x L -> L
Subroutine BEGIN: called CC=1 SCAR=0.0
1855  
1856 sub solve
1857 0 ▶{
Subroutine solve: not called CC=7 SCAR=40.3
1858 0 my ($self, $context, $list) = @_;
1859 0 my $left = XML::XQL::toList ($self->{Left}->solve ($context, $list));
1860 0 my $right = XML::XQL::toList ($self->{Right}->solve ($context, $list));
1861  
1862 0 ▶ return $right if (@$left < 1);
Branch: 1 branch
BranchTrueFalseTotal
if @$left < 1 0 0 0
1863 0 ▶ return $left if (@$right < 1);
Branch: 1 branch
BranchTrueFalseTotal
if @$right < 1 0 0 0
1864  
1865 0 my @result = @$left;
1866 0 for my $node (@$right)
1867 {
1868 0 ▶ push @result, $node unless XML::XQL::listContains ($left, $node);
Branch: 1 branch
BranchTrueFalseTotal
unless XML::XQL::listContains($left, $node) 0 0 0
1869 }
1870  
1871 0 my $parent = $self->{Parent};
1872  
1873 # Don't sort if parent is a Union or //, because the parent will do the sort
1874 0 ▶ unless (defined $parent and $parent->mustSort)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $parent and $parent->mustSort) 0 0 0
Condition0%0/3
defined $parent and $parent->mustSort
!ll&&!rl&&r
000
MC/DC0%0/2
defined $parent and $parent->mustSort
defined $parent $parent->mustSort
Truth table0%0/3
A and B
A= defined $parentB= $parent->mustSort
ABresult
0X0
100
111
1875 {
1876 0 XML::XQL::sortDocOrder (\@result)
1877 }
1878 # $self->verbose ("Union result", \@result);
1879  
1880 0 \@result;
1881 }
1882  
1883 sub mustSort
1884 0 ▶{
Subroutine mustSort: not called CC=1 SCAR=6.9
1885 0 1;
1886 }
1887  
1888 sub xql_contextString
1889 0 ▶{
Subroutine xql_contextString: not called CC=1 SCAR=6.9
1890 0 my $self = shift;
1891 XML::XQL::delim ($self->{Left}->xql_contextString (@_) .
1892 XML::XQL::bold (" \$union\$ ") .
1893 0 $self->{Right}->xql_contextString (@_), $self, @_);
1894 }
1895  
1896 package XML::XQL::Intersect; # "book $intersect$ magazine"
1897 28 ▶use base 'XML::XQL::Operator'; # L x L -> L
Subroutine BEGIN: called CC=1 SCAR=0.0
1898  
1899 sub solve
1900 0 ▶{
Subroutine solve: not called CC=5 SCAR=34.0
1901 0 my ($self, $context, $list) = @_;
1902 0 my $left = XML::XQL::toList ($self->{Left}->solve ($context, $list));
1903 0 ▶ return [] if @$left < 1;
Branch: 1 branch
BranchTrueFalseTotal
if @$left < 1 0 0 0
1904  
1905 0 my $right = XML::XQL::toList ($self->{Right}->solve ($context, $list));
1906 0 ▶ return [] if @$right < 1;
Branch: 1 branch
BranchTrueFalseTotal
if @$right < 1 0 0 0
1907  
1908 # Assumption: $left and $right don't have duplicates themselves
1909 0 my @result = ();
1910 0 for my $node (@$left)
1911 {
1912 #? reimplement with hash - faster!
1913 0 ▶ push @result, $node if XML::XQL::listContains ($right, $node);
Branch: 1 branch
BranchTrueFalseTotal
if XML::XQL::listContains($right, $node) 0 0 0
1914 }
1915 0 \@result;
1916 }
1917  
1918 sub xql_contextString
1919 0 ▶{
Subroutine xql_contextString: not called CC=1 SCAR=6.9
1920 0 my $self = shift;
1921 XML::XQL::delim ($self->{Left}->xql_contextString (@_) .
1922 XML::XQL::bold (" \$intersect\$ ") .
1923 0 $self->{Right}->xql_contextString (@_), $self, @_);
1924 }
1925  
1926 package XML::XQL::Filter; # "elem[expr]"
1927 28 ▶use base 'XML::XQL::Operator'; # L -> L
Subroutine BEGIN: called CC=1 SCAR=0.0
1928  
1929 sub solve
1930 0 ▶{
Subroutine solve: not called CC=5 SCAR=34.0
1931 0 my ($self, $context, $inlist) = @_;
1932 0 my @result = ();
1933  
1934 0 for my $node (@$inlist)
1935 {
1936  
1937 0 my $list = $self->{Left}->solve ($context, [$node]);
1938 0 ▶ next if @$list == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @$list == 0 0 0 0
1939
1940 0 my $subQuery = $self->{Right};
1941
1942 0 $context = [0, scalar (@$list)];
1943 0 for my $node (@$list)
1944 {
1945 #?? optimize? only need the first one to succeed
1946 0 my $r = $subQuery->solve ($context, [ $node ]);
1947 0 ▶ push @result, $node if XML::XQL::toBoolean ($r);
Branch: 1 branch
BranchTrueFalseTotal
if XML::XQL::toBoolean($r) 0 0 0
1948 0 $context->[0]++; # increase the index for the index() method
1949 }
1950 }
1951 0 \@result;
1952 }
1953  
1954 sub xql_check
1955 0 ▶{
Subroutine xql_check: not called CC=1 SCAR=6.9
1956 0 my ($self, $inSubQuery, $inParam) = @_;
1957 0 $self->{Left}->xql_check ($inSubQuery, $inParam);
1958 0 $self->{Right}->xql_check (1, $inParam);
1959 }
1960  
1961 sub xql_contextString
1962 0 ▶{
Subroutine xql_contextString: not called CC=1 SCAR=6.9
1963 0 my $self = shift;
1964 XML::XQL::delim ($self->{Left}->xql_contextString (@_) .
1965 XML::XQL::bold ("[") .
1966 0 $self->{Right}->xql_contextString (@_) .
1967 XML::XQL::bold ("]"), $self, @_);
1968 }
1969  
1970 package XML::XQL::BooleanOp;
1971 28 ▶use base 'XML::XQL::Operator';
Subroutine BEGIN: called CC=1 SCAR=0.0
1972  
1973 package XML::XQL::Or;
1974 28 ▶use base 'XML::XQL::BooleanOp';
Subroutine BEGIN: called CC=1 SCAR=0.0
1975  
1976 sub solve
1977 0 ▶{
Subroutine solve: not called CC=2 SCAR=17.9
1978 0 my ($self, $context, $list) = @_;
1979 0 my $left = $self->{Left}->solve ($context, $list);
1980 0 ▶ return $XML::XQL::Boolean::TRUE if XML::XQL::toBoolean ($left);
Branch: 1 branch
BranchTrueFalseTotal
if XML::XQL::toBoolean($left) 0 0 0
1981 0 return $self->{Right}->solve ($context, $list);
1982 }
1983  
1984 sub xql_contextString
1985 0 ▶{
Subroutine xql_contextString: not called CC=1 SCAR=6.9
1986 0 my $self = shift;
1987 XML::XQL::delim ($self->{Left}->xql_contextString (@_) .
1988 XML::XQL::bold (" \$or\$ ") .
1989 0 $self->{Right}->xql_contextString (@_), $self, @_);
1990 }
1991  
1992 package XML::XQL::And;
1993 28 ▶use base 'XML::XQL::BooleanOp';
Subroutine BEGIN: called CC=1 SCAR=0.0
1994  
1995 sub solve
1996 0 ▶{
Subroutine solve: not called CC=2 SCAR=17.9
1997 0 my ($self, $context, $list) = @_;
1998 0 my $left = $self->{Left}->solve ($context, $list);
1999 0 ▶ return $XML::XQL::Boolean::FALSE unless XML::XQL::toBoolean ($left);
Branch: 1 branch
BranchTrueFalseTotal
unless XML::XQL::toBoolean($left) 0 0 0
2000 0 return $self->{Right}->solve ($context, $list);
2001 }
2002  
2003 sub xql_contextString
2004 0 ▶{
Subroutine xql_contextString: not called CC=1 SCAR=6.9
2005 0 my $self = shift;
2006 XML::XQL::delim ($self->{Left}->xql_contextString (@_) .
2007 XML::XQL::bold (" \$and\$ ") .
2008 0 $self->{Right}->xql_contextString (@_), $self, @_);
2009 }
2010  
2011 package XML::XQL::Not;
2012 28 ▶use base 'XML::XQL::BooleanOp';
Subroutine BEGIN: called CC=1 SCAR=0.0
2013  
2014 sub solve
2015 0 ▶{
Subroutine solve: not called CC=2 SCAR=17.9
2016 0 my ($self, $context, $list) = @_;
2017 0 my $left = $self->{Left}->solve ($context, $list);
2018 0 ▶ return XML::XQL::toBoolean ($left) ? $XML::XQL::Boolean::FALSE : $XML::XQL::Boolean::TRUE;
Branch: 1 branch
BranchTrueFalseTotal
XML::XQL::toBoolean($left) ? : 0 0 0
2019 }
2020  
2021 sub xql_contextString
2022 0 ▶{
Subroutine xql_contextString: not called CC=1 SCAR=6.9
2023 0 my $self = shift;
2024 XML::XQL::delim (XML::XQL::bold ("\$not\$ ") .
2025 0 $self->{Left}->xql_contextString (@_), $self, @_);
2026 }
2027  
2028 package XML::XQL::Compare;
2029 28 ▶use base 'XML::XQL::Operator';
Subroutine BEGIN: called CC=1 SCAR=0.0
2030 28 ▶use fields qw{ Func All };
Subroutine BEGIN: called CC=1 SCAR=0.0
2031  
2032 28 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
2033  
2034 sub solve
2035 0 ▶{
Subroutine solve: not called CC=10 SCAR=47.0
2036 0 my ($self, $context, $list) = @_;
2037
2038 0 my $type;
2039 0 my $cmpFunc = $self->{Func};
2040  
2041 0 my $left = $self->verbose ("left", XML::XQL::toList ($self->{Left}->solve ($context, $list)));
2042 0 ▶ return [] if @$left < 1;
Branch: 1 branch
BranchTrueFalseTotal
if @$left < 1 0 0 0
2043  
2044 0 my $right;
2045 0 eval {
2046 0 $right = $self->verbose ("right", XML::XQL::prepareRvalue ($self->{Right}->solve ($context, $list)));
2047 };
2048 0 ▶ return [] if XML::XQL::exception ($@);
Branch: 1 branch
BranchTrueFalseTotal
if XML::XQL::exception($@) 0 0 0
2049  
2050 0 ▶ if ($self->{All})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'All'}) { } 0 0 0
2051 {
2052 0 for my $node (@$left)
2053 {
2054 0 eval {
2055 # Stop if any of the comparisons fails
2056 0 ▶ return [] unless &$cmpFunc ($node, $right);
Branch: 1 branch
BranchTrueFalseTotal
unless &$cmpFunc($node, $right) 0 0 0
2057 };
2058 0 ▶ return [] if XML::XQL::exception ($@);
Branch: 1 branch
BranchTrueFalseTotal
if XML::XQL::exception($@) 0 0 0
2059 }
2060 0 return $left;
2061 }
2062 else # $any$
2063 {
2064 0 my @result = ();
2065 0 for my $node (@$left)
2066 {
2067 0 eval {
2068 0 ▶ push (@result, $node)
Branch: 1 branch
BranchTrueFalseTotal
if &$cmpFunc($node, $right) 0 0 0
2069 if &$cmpFunc ($node, $right);
2070 };
2071 0 ▶ return [] if XML::XQL::exception ($@);
Branch: 1 branch
BranchTrueFalseTotal
if XML::XQL::exception($@) 0 0 0
2072 }
2073 0 return \@result;
2074 }
2075 }
2076  
2077 sub xql_contextString
2078 0 ▶{
Subroutine xql_contextString: not called CC=2 SCAR=17.9
2079 0 my $self = shift;
2080 0 ▶ my $all = $self->{All} ? "\$all\$ " : "";
Branch: 1 branch
BranchTrueFalseTotal
$self->{'All'} ? : 0 0 0
2081  
2082 XML::XQL::delim ($all . $self->{Left}->xql_contextString (@_) . " " .
2083 XML::XQL::bold ($self->{Oper}) . " " .
2084 0 $self->{Right}->xql_contextString (@_), $self, @_);
2085 }
2086  
2087 package XML::XQL::Func;
2088  
2089 28 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
2090  
2091 sub count
2092 0 ▶{
Subroutine count: not called CC=2 SCAR=17.9
2093 0 my ($context, $list, $expr) = @_;
2094  
2095 0 my $cnt;
2096 0 ▶ if (defined $expr)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $expr) { } 0 0 0
2097 {
2098 0 $list = XML::XQL::toList ($expr->solve ($context, $list));
2099 0 $cnt = @$list;
2100 }
2101 else
2102 {
2103 0 $cnt = $context->[1];
2104 }
2105 #?? ref node?
2106 0 new XML::XQL::Number ($cnt);
2107 }
2108  
2109 sub id
2110 0 ▶{
Subroutine id: not called CC=2 SCAR=17.9
2111 0 my ($context, $list, $query) = @_;
2112  
2113 0 ▶ return [] if @$list == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @$list == 0 0 0 0
2114  
2115 0 my $id = XML::XQL::prepareRvalue ($query->solve ($context, $list));
2116 #?? check result?
2117  
2118 #?? if [0] is not a Node, I should probably try the next one
2119 0 my $doc = $list->[0]->xql_document;
2120
2121 0 _findId ($doc->xql_element->[0], $id);
2122 }
2123  
2124 sub _findId # static method
2125 0 ▶{
Subroutine _findId: not called CC=5 SCAR=34.0
2126 0 my ($elem, $id) = @_;
2127 0 my $attr = $elem->xql_attribute ("id");
2128 0 ▶ return [$elem] if (@$attr == 1 && $attr->[0]->xql_nodeName eq $id);
Branch: 1 branch
BranchTrueFalseTotal
if @$attr == 1 and $attr->[0]->xql_nodeName eq $id 0 0 0
Condition0%0/3
@$attr == 1 and $attr->[0]->xql_nodeName eq $id
!ll&&!rl&&r
000
MC/DC0%0/2
@$attr == 1 and $attr->[0]->xql_nodeName eq $id
@$attr == 1 $attr->[0]->xql_nodeName eq $id
Truth table0%0/3
A and B
A= @$attr == 1B= $attr->[0]->xql_nodeName eq $id
ABresult
0X0
100
111
2129  
2130 0 for my $kid (@{$elem->xql_element})
2131 {
2132 0 $attr = _findId ($kid);
2133 0 ▶ return $attr if @$attr;
Branch: 1 branch
BranchTrueFalseTotal
if @$attr 0 0 0
2134 }
2135 0 return [];
2136 }
2137  
2138 sub end
2139 0 ▶{
Subroutine end: not called CC=2 SCAR=17.9
2140 0 my ($context, $list) = @_;
2141  
2142 0 ▶ return [] if @$list == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @$list == 0 0 0 0
2143 0 new XML::XQL::Boolean ($context->[0] == $context->[1] - 1);
2144 }
2145  
2146 sub xql_index
2147 0 ▶{
Subroutine xql_index: not called CC=2 SCAR=17.9
2148 0 my ($context, $list) = @_;
2149  
2150 # print "index: " . XML::XQL::d($context) . "\n";
2151 #?? wrong!
2152 0 ▶ return [] if @$list == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @$list == 0 0 0 0
2153 0 new XML::XQL::Number ($context->[0]);
2154 }
2155  
2156 sub ancestor
2157 0 ▶{
Subroutine ancestor: not called CC=6 SCAR=37.4
2158 0 my ($context, $list, $query) = @_;
2159  
2160 0 ▶ return [] if @$list == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @$list == 0 0 0 0
2161
2162 0 my @anc = ();
2163 #?? fix for @$list > 1
2164 0 my $parent = $list->[0]->xql_parent;
2165  
2166 0 ▶ while (defined $parent)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $parent) 0 0 0
2167 {
2168 # keep list of ancestors so far
2169 0 unshift @anc, $parent;
2170  
2171 # solve the query for the ancestor
2172 0 my $result = $query->solve ($context, [$parent]);
2173 0 for my $node (@{$result})
2174 {
2175 0 for my $anc (@anc)
2176 {
2177 0 ▶ return [$node] if $node == $anc;
Branch: 1 branch
BranchTrueFalseTotal
if $node == $anc 0 0 0
2178 }
2179 }
2180 0 $parent = $parent->xql_parent;
2181 }
2182 0 return [];
2183 }
2184  
2185 sub node
2186 0 ▶{
Subroutine node: not called CC=4 SCAR=30.0
2187 0 my ($context, $list) = @_;
2188  
2189 0 ▶ return [] if @$list == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @$list == 0 0 0 0
2190 0 ▶ return $list->[0]->xql_node if @$list == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @$list == 1 0 0 0
2191  
2192 0 my @result;
2193 0 for my $node (@$list)
2194 {
2195 0 push @result, @{ $node->xql_node };
2196 }
2197 0 XML::XQL::sortDocOrder (\@result);
2198 }
2199  
2200 sub _nodesByType
2201 0 ▶{
Subroutine _nodesByType: not called CC=6 SCAR=37.4
2202 0 my ($list, $type) = @_;
2203  
2204 0 ▶ return [] if @$list == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @$list == 0 0 0 0
2205  
2206 0 my @result;
2207 0 for my $node (@$list)
2208 {
2209 0 for my $kid (@{ $node->xql_node })
2210 {
2211 0 ▶ push @result, $kid if $kid->xql_nodeType == $type;
Branch: 1 branch
BranchTrueFalseTotal
if $kid->xql_nodeType == $type 0 0 0
2212 }
2213 }
2214 0 ▶ @$list > 1 ? XML::XQL::sortDocOrder (\@result) : \@result;
Branch: 1 branch
BranchTrueFalseTotal
@$list > 1 ? : 0 0 0
2215 }
2216  
2217 sub pi
2218 0 ▶{
Subroutine pi: not called CC=8 SCAR=42.8
2219 0 my ($context, $list, $pi_name) = @_;
2220 0 ▶ if (defined $pi_name)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pi_name) 0 0 0
2221 {
2222 0 ▶ return [] if @$list == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @$list == 0 0 0 0
2223  
2224 0 $pi_name = $pi_name->solve ($context, $list)->xql_toString;
2225  
2226 0 my @result;
2227 0 for my $node (@$list)
2228 {
2229 0 for my $kid (@{ $node->xql_node })
2230 {
2231 0 ▶ push @result, $kid
Branch: 1 branch
BranchTrueFalseTotal
if $kid->xql_nodeType == 7 and $kid->getTarget eq $pi_name 0 0 0
Condition0%0/3
$kid->xql_nodeType == 7 and $kid->getTarget eq $pi_name
!ll&&!rl&&r
000
MC/DC0%0/2
$kid->xql_nodeType == 7 and $kid->getTarget eq $pi_name
$kid->xql_nodeType == 7 $kid->getTarget eq $pi_name
Truth table0%0/3
A and B
A= $kid->xql_nodeType == 7B= $kid->getTarget eq $pi_name
ABresult
0X0
100
111
2232 if $kid->xql_nodeType == 7 && $kid->getTarget eq $pi_name;
2233 }
2234 }
2235 0 ▶ return @$list > 1 ? XML::XQL::sortDocOrder (\@result) : \@result;
Branch: 1 branch
BranchTrueFalseTotal
@$list > 1 ? : 0 0 0
2236 }
2237  
2238 0 return _nodesByType ($_[1], 7);
2239 }
2240  
2241 sub comment
2242 0 ▶{
Subroutine comment: not called CC=1 SCAR=6.9
2243 0 _nodesByType ($_[1], 8);
2244 }
2245  
2246 sub textNode
2247 0 ▶{
Subroutine textNode: not called CC=1 SCAR=6.9
2248 0 _nodesByType ($_[1], 3);
2249 }
2250  
2251 sub nodeName
2252 0 ▶{
Subroutine nodeName: not called CC=3 SCAR=24.8
2253 0 my ($context, $list) = @_;
2254  
2255 0 ▶ return [] if @$list == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @$list == 0 0 0 0
2256  
2257 0 my @result;
2258 0 for my $node (@$list)
2259 {
2260 0 push @result, new XML::XQL::Text ($node->xql_nodeName, $node);
2261 }
2262 0 \@result;
2263 }
2264  
2265 sub namespace
2266 0 ▶{
Subroutine namespace: not called CC=4 SCAR=30.0
2267 0 my ($context, $list) = @_;
2268  
2269 0 ▶ return [] if @$list == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @$list == 0 0 0 0
2270  
2271 0 my @result;
2272 0 for my $node (@$list)
2273 {
2274 0 my $namespace = $node->xql_namespace;
2275 0 ▶ next unless defined $namespace;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $namespace 0 0 0
2276 0 push @result, new XML::XQL::Text ($namespace, $node);
2277 }
2278 0 \@result;
2279 }
2280  
2281 sub prefix
2282 0 ▶{
Subroutine prefix: not called CC=4 SCAR=30.0
2283 0 my ($context, $list) = @_;
2284  
2285 0 ▶ return [] if @$list == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @$list == 0 0 0 0
2286  
2287 0 my @result;
2288 0 for my $node (@$list)
2289 {
2290 0 my $prefix = $node->xql_prefix;
2291 0 ▶ next unless defined $prefix;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $prefix 0 0 0
2292 0 push @result, new XML::XQL::Text ($prefix, $node);
2293 }
2294 0 \@result;
2295 }
2296  
2297 sub baseName
2298 0 ▶{
Subroutine baseName: not called CC=4 SCAR=30.0
2299 0 my ($context, $list) = @_;
2300  
2301 0 ▶ return [] if @$list == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @$list == 0 0 0 0
2302  
2303 0 my @result;
2304 0 for my $node (@$list)
2305 {
2306 0 my $basename = $node->xql_baseName;
2307 0 ▶ next unless defined $basename;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $basename 0 0 0
2308 0 push @result, new XML::XQL::Text ($basename, $node);
2309 }
2310 0 \@result;
2311 }
2312  
2313 sub nodeType
2314 0 ▶{
Subroutine nodeType: not called CC=3 SCAR=24.8
2315 0 my ($context, $list) = @_;
2316  
2317 0 ▶ return [] if @$list == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @$list == 0 0 0 0
2318  
2319 0 my @result;
2320 0 for my $node (@$list)
2321 {
2322 0 push @result, new XML::XQL::Number ($node->xql_nodeType, $node);
2323 }
2324 0 \@result;
2325 }
2326  
2327 sub nodeTypeString
2328 0 ▶{
Subroutine nodeTypeString: not called CC=3 SCAR=24.8
2329 0 my ($context, $list) = @_;
2330  
2331 0 ▶ return [] if @$list == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @$list == 0 0 0 0
2332  
2333 0 my @result;
2334 0 for my $node (@$list)
2335 {
2336 0 push @result, new XML::XQL::Text ($node->xql_nodeTypeString, $node);
2337 }
2338 0 @result;
2339 }
2340  
2341 sub value
2342 0 ▶{
Subroutine value: not called CC=3 SCAR=24.8
2343 0 my ($context, $list) = @_;
2344  
2345 0 ▶ return [] if @$list == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @$list == 0 0 0 0
2346  
2347 0 my @result;
2348 0 for my $node (@$list)
2349 {
2350 0 push @result, $node->xql_value; # value always returns an object
2351 }
2352 0 \@result;
2353 }
2354  
2355 sub text
2356 0 ▶{
Subroutine text: not called CC=5 SCAR=34.0
2357 0 my ($context, $list, $recurse) = @_;
2358  
2359 0 ▶ return [] if @$list == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @$list == 0 0 0 0
2360  
2361 0 ▶ if (defined $recurse)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $recurse) { } 0 0 0
2362 {
2363 0 $recurse = $recurse->solve ($context, $list)->xql_toString;
2364 }
2365 else
2366 {
2367 0 $recurse = 1; # default
2368 }
2369  
2370 0 my @result;
2371 0 for my $node (@$list)
2372 {
2373 0 my $text = $node->xql_text ($recurse);
2374 0 ▶ next unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 0 0
2375
2376 0 push @result, new XML::XQL::Text ($text, $node);
2377 }
2378 0 \@result;
2379 }
2380  
2381 sub rawText
2382 0 ▶{
Subroutine rawText: not called CC=5 SCAR=34.0
2383 0 my ($context, $list, $recurse) = @_;
2384  
2385 0 ▶ return [] if @$list == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @$list == 0 0 0 0
2386  
2387 0 ▶ if (defined $recurse)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $recurse) { } 0 0 0
2388 {
2389 0 $recurse = $recurse->solve ($context, $list)->xql_toString;
2390 }
2391 else
2392 {
2393 0 $recurse = 1; # default
2394 }
2395  
2396 0 my @result;
2397 0 for my $node (@$list)
2398 {
2399 0 my $text = $node->xql_rawText ($recurse);
2400 0 ▶ next unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 0 0
2401
2402 0 push @result, new XML::XQL::Text ($text, $node);
2403 }
2404 0 \@result;
2405 }
2406  
2407 sub true
2408 0 ▶{
Subroutine true: not called CC=1 SCAR=6.9
2409 0 return $XML::XQL::Boolean::TRUE;
2410 }
2411  
2412 sub false
2413 0 ▶{
Subroutine false: not called CC=1 SCAR=6.9
2414 0 return $XML::XQL::Boolean::FALSE;
2415 }
2416  
2417 #sub date() is in XQL::XML::Date
2418  
2419 sub element
2420 0 ▶{
Subroutine element: not called CC=6 SCAR=37.4
2421 0 my ($context, $list, $text) = @_;
2422  
2423 0 ▶ return [] if @$list == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @$list == 0 0 0 0
2424  
2425 0 my @result;
2426 0 ▶ if (defined $text)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $text) { } 0 0 0
2427 {
2428 0 $text = XML::XQL::prepareRvalue ($text->solve ($context, $list))->xql_toString;
2429 0 for my $node (@$list)
2430 {
2431 0 push @result, @{$node->xql_element ($text)};
2432 }
2433 }
2434 else
2435 {
2436 0 for my $node (@$list)
2437 {
2438 0 push @result, @{$node->xql_element};
2439 }
2440 }
2441 0 ▶ @$list > 1 ? XML::XQL::sortDocOrder (\@result) : \@result;
Branch: 1 branch
BranchTrueFalseTotal
@$list > 1 ? : 0 0 0
2442 }
2443  
2444 sub attribute
2445 0 ▶{
Subroutine attribute: not called CC=5 SCAR=34.0
2446 0 my ($context, $list, $text) = @_;
2447  
2448 0 ▶ return [] if @$list == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @$list == 0 0 0 0
2449  
2450 0 my @result;
2451 0 ▶ if (defined $text)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $text) { } 0 0 0
2452 {
2453 0 $text = XML::XQL::prepareRvalue ($text->solve ($context, $list))->xql_toString;
2454 0 for my $node (@$list)
2455 {
2456 0 push @result, @{ $node->xql_attribute ($text) };
2457 }
2458 }
2459 else
2460 {
2461 0 for my $node (@$list)
2462 {
2463 0 push @result, @{ $node->xql_attribute };
2464 }
2465 }
2466 0 \@result;
2467 }
2468  
2469 package XML::XQL::Bang;
2470 28 ▶use base 'XML::XQL::Operator';
Subroutine BEGIN: called CC=1 SCAR=0.0
2471  
2472 sub solve
2473 0 ▶{
Subroutine solve: not called CC=1 SCAR=6.9
2474 0 my ($self, $context, $list) = @_;
2475 0 $list = $self->{Left}->solve ($context, $list);
2476 0 $self->{Right}->solve ($context, $list);
2477 }
2478  
2479 sub xql_contextString
2480 0 ▶{
Subroutine xql_contextString: not called CC=1 SCAR=6.9
2481 0 my $self = shift;
2482 XML::XQL::delim ($self->{Left}->xql_contextString (@_) .
2483 XML::XQL::bold ("!") .
2484 0 $self->{Right}->xql_contextString (@_), $self, @_);
2485 }
2486  
2487 package XML::XQL::Invocation;
2488 28 ▶use base 'XML::XQL::Operator';
Subroutine BEGIN: called CC=1 SCAR=0.0
2489 28 ▶use fields qw{ Args Name Type Once ConstVal };
Subroutine BEGIN: called CC=1 SCAR=0.0
2490  
2491 28 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
2492  
2493 sub new
2494 0 ▶{
Subroutine new: not called CC=2 SCAR=17.9
2495 0 my ($class, %args) = @_;
2496  
2497 0 my $self = bless \%args, $class;
2498 0 for my $par (@{$self->{Args}})
2499 {
2500 0 $par->setParent ($self);
2501 }
2502 0 $self;
2503 }
2504  
2505 sub dispose
2506 0 ▶{
Subroutine dispose: not called CC=2 SCAR=17.9
2507 0 my $self = shift;
2508 0 for (@{ $self->{Args} })
2509 {
2510 0 $_->dispose;
2511 }
2512 0 undef $self->{Args};
2513  
2514 0 undef $self->{Parent};
2515 }
2516  
2517 sub isConstant
2518 0 ▶{
Subroutine isConstant: not called CC=5 SCAR=34.0
2519 0 my ($self) = @_;
2520
2521 # An Invocation is constant, if all it's arguments are constant
2522 # and it's a "constant" function
2523 0 my $name = $self->{Name};
2524 0 my $cf = $self->query->{ConstFunc};
2525 my $const = exists ($cf->{$name}) ?
2526 0 ▶ $cf->{name} : $XML::XQL::ConstFunc{$name};
Branch: 1 branch
BranchTrueFalseTotal
exists $cf->{$name} ? : 0 0 0
2527 0 ▶ return 0 unless $const;
Branch: 1 branch
BranchTrueFalseTotal
unless $const 0 0 0
2528  
2529 0 for my $par (@{$self->{Args}})
2530 {
2531 0 ▶ return 0 unless $par->isConstant;
Branch: 1 branch
BranchTrueFalseTotal
unless $par->isConstant 0 0 0
2532 }
2533 0 1;
2534 }
2535  
2536 sub xql_check
2537 0 ▶{
Subroutine xql_check: not called CC=7 SCAR=40.3
2538 0 my ($self, $inSubQuery, $inParam) = @_;
2539  
2540 # Syntactic Constraint 7:
2541 # In a node query this function or method is only valid inside an instance
2542 # of Subquery, unless it appears within an instance of Param.
2543 # Functions and methods are valid anywhere in a full query.
2544  
2545 0 my $query;
2546 0 ▶ if (not ($inSubQuery or $inParam) and ($query = $self->query)->isNodeQuery)
Branch: 1 branch
BranchTrueFalseTotal
if (not $inSubQuery || $inParam and ($query = $self->query)->isNodeQuery) 0 0 0
Condition0%0/6
not $inSubQuery || $inParam and ($query = $self->query)->isNodeQuery
!ll&&!rl&&r
000
$inSubQuery || $inParam
l!l&&r!l&&!r
000
MC/DC0%0/3
not $inSubQuery || $inParam and ($query = $self->query)->isNodeQuery
$inSubQuery $inParam ($query = $self->query)->isNodeQuery
Truth table0%0/4
not(A || B) and C
A= $inSubQueryB= $inParamC= ($query = $self->query)->isNodeQuery
ABCresult
1XX0
01X0
0000
0011
2547 {
2548 0 ▶ unless ($query->isAllowedOutsideSubquery ($self->{Name}))
Branch: 1 branch
BranchTrueFalseTotal
unless ($query->isAllowedOutsideSubquery($self->{"Name"})) 0 0 0
2549 {
2550 XML::XQL::parseError $self->{Type} . " " . $self->{Name} .
2551 0 " is only allowed inside a Subquery or Param for 'Node Queries'." .
2552 " Context: " . $self->toContextString;
2553 }
2554 }
2555 0 for my $par (@{$self->{Args}})
2556 {
2557 0 $par->xql_check ($inSubQuery, 1); # these are Params
2558 }
2559 # once() should only be evaluated once per query
2560 # "constant" functions should only be evaluated once *ever*
2561 0 ▶ $self->{Once} = $self->isOnce || $self->isConstant;
Condition0%0/3
$self->isOnce || $self->isConstant
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->isOnce || $self->isConstant
$self->isOnce $self->isConstant
Truth table0%0/3
A || B
A= $self->isOnceB= $self->isConstant
ABresult
1X1
011
000
2562 }
2563  
2564 sub xql_prepCache
2565 0 ▶{
Subroutine xql_prepCache: not called CC=3 SCAR=24.8
2566 0 my ($self) = @_;
2567 # once() should only be evaluated once per query
2568 # "constant" functions should only be evaluated once *ever*
2569 0 ▶ delete $self->{ConstVal} if $self->isOnce;
Branch: 1 branch
BranchTrueFalseTotal
if $self->isOnce 0 0 0
2570  
2571 0 for my $par (@{$self->{Args}})
2572 {
2573 0 $par->xql_prepCache;
2574 }
2575 }
2576  
2577 sub isOnce
2578 0 ▶{
Subroutine isOnce: not called CC=1 SCAR=6.9
2579 0 $_[0]->{Name} eq "once";
2580 }
2581  
2582 sub isMethod
2583 0 ▶{
Subroutine isMethod: not called CC=1 SCAR=6.9
2584 0 $_[0]->{Type} eq "method";
2585 }
2586  
2587 sub solve
2588 0 ▶{
Subroutine solve: not called CC=5 SCAR=34.0
2589 0 my ($self, $context, $list) = @_;
2590  
2591 # Use the cached value if it's a "constant" function
2592 0 ▶ return $self->{ConstVal} if (exists $self->{ConstVal});
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"ConstVal"} 0 0 0
2593  
2594 0 my $func = $self->{Func};
2595  
2596 0 my $result;
2597 0 eval {
2598 0 $result = &$func ($context, $list, @{$self->{Args}});
2599 0 ▶ $self->{ConstVal} = $result if $self->{Once};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"Once"} 0 0 0
2600 };
2601 0 ▶ if ($@)
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
2602 {
2603 #?? or croak
2604 0 $self->warning ("invocation of '" . $self->{Name} . "' failed:\n\t$@");
2605 0 ▶ $self->{ConstVal} = [] if $self->{Once};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"Once"} 0 0 0
2606 0 return [];
2607 }
2608 0 $result;
2609 }
2610  
2611 sub xql_contextString
2612 0 ▶{
Subroutine xql_contextString: not called CC=3 SCAR=24.8
2613 0 my $self = shift;
2614
2615 0 my $str = XML::XQL::bold ($self->{Name}) . "(";
2616 0 ▶ for (my $i = 0; $i < @{$self->{Args}}; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$self->{"Args"};}) 0 0 0
2617 {
2618 0 ▶ $str .= ", " if $i > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $i > 0 0 0 0
2619 0 $str .= $self->{Args}->[$i]->xql_contextString (@_);
2620 }
2621 0 $str .= ")";
2622  
2623 0 XML::XQL::delim ($str, $self, @_);
2624 }
2625  
2626 # Base class shared by Node and PrimitiveType
2627 package XML::XQL::PrimitiveTypeBase;
2628  
2629 sub dispose
2630 0 ▶{
Subroutine dispose: not called CC=1 SCAR=0.0
2631 }
2632  
2633 sub xql_check
2634 0 ▶{
Subroutine xql_check: not called CC=1 SCAR=0.0
2635 }
2636  
2637 sub xql_prepCache
2638 0 ▶{
Subroutine xql_prepCache: not called CC=1 SCAR=0.0
2639 }
2640  
2641 sub xql_prevSibling
2642 0 ▶{
Subroutine xql_prevSibling: not called CC=1 SCAR=6.9
2643 0 undef;
2644 }
2645  
2646 # This method returns an integer that determines how values should be casted
2647 # for comparisons. If the left value (LHS) has a higher xql_primType, the
2648 # right value (RHS) is cast to the type of the LHS (otherwise, the LHS is casted
2649 # to the type of the LHS)
2650 #
2651 # Values for certain types:
2652 # Node 0 (always cast a node to a Text string first)
2653 # Text 1
2654 # Number 2
2655 # Boolean 3
2656 # Date 4 (other classes automatically use 4 by default)
2657  
2658 sub xql_primType
2659 0 ▶{
Subroutine xql_primType: not called CC=1 SCAR=6.9
2660 0 4; # default for all classes other then Node, Text, Number, Boolean
2661 }
2662  
2663 sub xql_toBoolean
2664 0 ▶{
Subroutine xql_toBoolean: not called CC=1 SCAR=6.9
2665 0 1; # it is true if it exists
2666 }
2667  
2668 sub xql_namespace
2669 0 ▶{
Subroutine xql_namespace: not called CC=1 SCAR=6.9
2670 0 undef;
2671 }
2672  
2673 sub xql_baseName
2674 0 ▶{
Subroutine xql_baseName: not called CC=1 SCAR=6.9
2675 0 undef;
2676 }
2677  
2678 sub xql_prefix
2679 0 ▶{
Subroutine xql_prefix: not called CC=1 SCAR=6.9
2680 0 undef;
2681 }
2682  
2683 sub xql_sortKey
2684 0 ▶{
Subroutine xql_sortKey: not called CC=2 SCAR=17.9
2685 0 my $src = $_[0]->xql_sourceNode;
2686 0 ▶ $src ? $src->xql_sortKey : $XML::XQL::LAST_SORT_KEY;
Branch: 1 branch
BranchTrueFalseTotal
$src ? : 0 0 0
2687 }
2688  
2689 sub xql_toDOM
2690 0 ▶{
Subroutine xql_toDOM: not called CC=1 SCAR=6.9
2691 0 my ($self, $doc) = @_;
2692 0 my $name = ref $self;
2693 0 $name =~ s/.*:://;
2694 0 my $elem = $doc->createElement ($name);
2695 0 $elem->setAttribute ("value", $self->xql_toString);
2696 0 $elem;
2697 }
2698  
2699 package XML::XQL::PrimitiveType;
2700 28 ▶use vars qw( @ISA );
Subroutine BEGIN: called CC=1 SCAR=0.0
2701 28 @ISA = qw( XML::XQL::PrimitiveTypeBase );
2702  
2703 sub new
2704 56 ▶{
Subroutine new: called CC=1 SCAR=0.0
2705 56 my ($class, $val, $srcNode) = @_;
2706 56 bless [$val, $srcNode], $class;
2707 }
2708  
2709 sub isConstant
2710 0 ▶{
Subroutine isConstant: not called CC=1 SCAR=6.9
2711 0 1;
2712 }
2713  
2714 sub setParent
2715 0 ▶{
Subroutine setParent: not called CC=1 SCAR=0.0
2716 # not defined
2717 }
2718  
2719 sub solve
2720 0 ▶{
Subroutine solve: not called CC=1 SCAR=6.9
2721 0 $_[0]; # evaluates to itself
2722 }
2723  
2724 #
2725 # Derived classes should not override this method.
2726 # Override xql_toString instead.
2727 #
2728 sub xql_contextString
2729 0 ▶{
Subroutine xql_contextString: not called CC=1 SCAR=6.9
2730 0 my $self = shift;
2731
2732 0 XML::XQL::delim ($self->xql_toString, $self, @_);
2733 }
2734  
2735 #
2736 # Return the value of the Object as a primitive Perl value, i.e. an integer,
2737 # a float, or a string.
2738 #
2739 sub xql_toString
2740 0 ▶{
Subroutine xql_toString: not called CC=1 SCAR=6.9
2741 0 $_[0]->[0];
2742 }
2743  
2744 sub xql_sourceNode
2745 0 ▶{
Subroutine xql_sourceNode: not called CC=1 SCAR=6.9
2746 0 $_[0]->[1];
2747 }
2748  
2749 sub xql_setSourceNode
2750 0 ▶{
Subroutine xql_setSourceNode: not called CC=1 SCAR=6.9
2751 0 $_[0]->[1] = $_[1];
2752 }
2753  
2754 sub xql_setValue
2755 0 ▶{
Subroutine xql_setValue: not called CC=1 SCAR=6.9
2756 # This could potentially change the value of a constant in the XQL
2757 # query expression.
2758 0 $_[0]->[0] = $_[1];
2759 }
2760  
2761 sub xql_nodeType
2762 0 ▶{
Subroutine xql_nodeType: not called CC=1 SCAR=6.9
2763 0 0; # it's not a Node
2764 }
2765  
2766 sub xql_compare
2767 0 ▶{
Subroutine xql_compare: not called CC=1 SCAR=6.9
2768 # Temporarily switch off $WARNING flag, to disable messages a la:
2769 # Argument "1993-02-14" isn't numeric in ncmp
2770 0 local $^W = 0;
2771 0 $_[0]->[0] <=> $_[1]->xql_toString;
2772 }
2773  
2774 0 ▶sub xql_eq { my $self = shift; $self->xql_compare (@_) == 0; }
Subroutine xql_eq: not called CC=1 SCAR=6.9
2775 0 ▶sub xql_ne { my $self = shift; $self->xql_compare (@_) != 0; }
Subroutine xql_ne: not called CC=1 SCAR=6.9
2776 0 ▶sub xql_lt { my $self = shift; $self->xql_compare (@_) < 0; }
Subroutine xql_lt: not called CC=1 SCAR=6.9
2777 0 ▶sub xql_le { my $self = shift; $self->xql_compare (@_) <= 0; }
Subroutine xql_le: not called CC=1 SCAR=6.9
2778 0 ▶sub xql_gt { my $self = shift; $self->xql_compare (@_) > 0; }
Subroutine xql_gt: not called CC=1 SCAR=6.9
2779 0 ▶sub xql_ge { my $self = shift; $self->xql_compare (@_) >= 0; }
Subroutine xql_ge: not called CC=1 SCAR=6.9
2780  
2781 package XML::XQL::Boolean;
2782 28 ▶use vars qw( @ISA @EXPORT $TRUE $FALSE );
Subroutine BEGIN: called CC=1 SCAR=0.0
2783  
2784 28 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
2785  
2786 28 @ISA = qw( XML::XQL::PrimitiveType );
2787 28 @EXPORT = qw( $TRUE $FALSE );
2788  
2789 28 $TRUE = new XML::XQL::Boolean (1);
2790 28 $FALSE = new XML::XQL::Boolean (0);
2791  
2792 sub xql_primType
2793 0 ▶{
Subroutine xql_primType: not called CC=1 SCAR=6.9
2794 0 3;
2795 }
2796  
2797 sub xql_toBoolean
2798 0 ▶{
Subroutine xql_toBoolean: not called CC=1 SCAR=6.9
2799 0 $_[0]->[0]; # evaluate it to its value
2800 }
2801  
2802 sub xql_negate
2803 0 ▶{
Subroutine xql_negate: not called CC=2 SCAR=17.9
2804 #?? do we need to keep track of a source node here?
2805 0 ▶ $_[0]->[0] ? $FALSE : $TRUE;
Branch: 1 branch
BranchTrueFalseTotal
$_[0][0] ? : 0 0 0
2806 }
2807  
2808 sub xql_compare
2809 0 ▶{
Subroutine xql_compare: not called CC=2 SCAR=17.9
2810 #?? how do we convert string to boolean value
2811 0 ▶ $_[0]->[0] <=> ($_[1]->xql_toString ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$_[1]->xql_toString ? : 0 0 0
2812 }
2813  
2814 0 ▶sub xql_lt { badComparisonError (@_); }
Subroutine xql_lt: not called CC=1 SCAR=6.9
2815 0 ▶sub xql_gt { badComparisonError (@_); }
Subroutine xql_gt: not called CC=1 SCAR=6.9
2816 0 ▶sub xql_le { badComparisonError (@_); }
Subroutine xql_le: not called CC=1 SCAR=6.9
2817 0 ▶sub xql_ge { badComparisonError (@_); }
Subroutine xql_ge: not called CC=1 SCAR=6.9
2818  
2819 sub badComparisonError
2820 0 ▶{
Subroutine badComparisonError: not called CC=1 SCAR=6.9
2821 0 croak 'comparison operator (other than =, !=, $ieq$, $ine$) not defined for type Boolean';
2822 }
2823  
2824 package XML::XQL::Number;
2825 28 ▶use vars qw( @ISA );
Subroutine BEGIN: called CC=1 SCAR=0.0
2826 28 @ISA = qw( XML::XQL::PrimitiveType );
2827  
2828 #use overload
2829 # 'fallback' => 1, # use default operators, if not specified
2830 # '""' => \&debug;
2831  
2832 sub debug
2833 0 ▶{
Subroutine debug: not called CC=1 SCAR=6.9
2834 0 "Number[" . $_[0]->[0] . "]";
2835 }
2836  
2837 sub xql_primType
2838 0 ▶{
Subroutine xql_primType: not called CC=1 SCAR=6.9
2839 0 2;
2840 }
2841  
2842 package XML::XQL::Text;
2843 28 ▶use vars qw( @ISA );
Subroutine BEGIN: called CC=1 SCAR=0.0
2844 28 @ISA = qw( XML::XQL::PrimitiveType );
2845  
2846 #use overload
2847 # 'fallback' => 1, # use default operators, if not specified
2848 # '""' => \&debug;
2849  
2850 sub debug
2851 0 ▶{
Subroutine debug: not called CC=1 SCAR=6.9
2852 0 "Text[" . $_[0]->[0] . "]";
2853 }
2854  
2855 sub xql_primType
2856 0 ▶{
Subroutine xql_primType: not called CC=1 SCAR=6.9
2857 0 1;
2858 }
2859  
2860 sub xql_compare
2861 0 ▶{
Subroutine xql_compare: not called CC=2 SCAR=17.9
2862 0 my ($self, $other, $ignoreCase) = @_;
2863 0 ▶ if ($ignoreCase)
Branch: 1 branch
BranchTrueFalseTotal
if ($ignoreCase) { } 0 0 0
2864 {
2865 0 my $lhs = $self->[0];
2866 0 my $rhs = $other->xql_toString;
2867 0 "\U$lhs" cmp "\U$rhs";
2868 }
2869 else
2870 {
2871 0 $self->[0] cmp $other->xql_toString;
2872 }
2873 }
2874  
2875 # Declare package XML::XQL::Node so that XML implementations can say
2876 # that their nodes derive from it:
2877 #
2878 # This worked for me when I added XQL support for XML::DOM:
2879 #
2880 # BEGIN
2881 # {
2882 # push @XML::DOM::Node::ISA, 'XML::XQL::Node';
2883 # }
2884 #
2885  
2886 package XML::XQL::Node;
2887  
2888 28 ▶use vars qw( @ISA );
Subroutine BEGIN: called CC=1 SCAR=0.0
2889 28 @ISA = qw( XML::XQL::PrimitiveTypeBase );
2890  
2891 28 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
2892  
2893 sub xql_primType
2894 0 ▶{
Subroutine xql_primType: not called CC=1 SCAR=6.9
2895 0 0;
2896 }
2897  
2898 sub xql_toBoolean
2899 0 ▶{
Subroutine xql_toBoolean: not called CC=1 SCAR=6.9
2900 0 1; # it is true if it exists
2901 }
2902  
2903 sub xql_attribute
2904 0 ▶{
Subroutine xql_attribute: not called CC=1 SCAR=6.9
2905 0 [];
2906 }
2907  
2908 sub xql_sourceNode
2909 0 ▶{
Subroutine xql_sourceNode: not called CC=1 SCAR=6.9
2910 0 $_[0];
2911 }
2912  
2913 # Default implementation - override this for speed
2914 sub xql_element
2915 0 ▶{
Subroutine xql_element: not called CC=7 SCAR=40.3
2916 0 my ($node, $elem) = @_;
2917  
2918 0 my @list = ();
2919 0 ▶ if (defined $elem)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $elem) { } 0 0 0
2920 {
2921 0 for my $kid (@{$_[0]->xql_node})
2922 {
2923 # 1: element
2924 0 ▶ push @list, $kid
Branch: 1 branch
BranchTrueFalseTotal
if $kid->xql_nodeType == 1 and $kid->xql_nodeName eq $elem 0 0 0
Condition0%0/3
$kid->xql_nodeType == 1 and $kid->xql_nodeName eq $elem
!ll&&!rl&&r
000
MC/DC0%0/2
$kid->xql_nodeType == 1 and $kid->xql_nodeName eq $elem
$kid->xql_nodeType == 1 $kid->xql_nodeName eq $elem
Truth table0%0/3
A and B
A= $kid->xql_nodeType == 1B= $kid->xql_nodeName eq $elem
ABresult
0X0
100
111
2925 if $kid->xql_nodeType == 1 && $kid->xql_nodeName eq $elem;
2926 }
2927 }
2928 else
2929 {
2930 0 for my $kid (@{$_[0]->xql_node})
2931 {
2932 0 ▶ push @list, $kid if $kid->xql_nodeType == 1; # 1: element
Branch: 1 branch
BranchTrueFalseTotal
if $kid->xql_nodeType == 1 0 0 0
2933 }
2934 }
2935 0 \@list;
2936 }
2937  
2938 sub xql_text
2939 0 ▶{
Subroutine xql_text: not called CC=1 SCAR=6.9
2940 0 undef;
2941 }
2942  
2943 sub xql_rawText
2944 0 ▶{
Subroutine xql_rawText: not called CC=1 SCAR=6.9
2945 0 undef;
2946 }
2947  
2948 sub xql_rawTextBlocks
2949 0 ▶{
Subroutine xql_rawTextBlocks: not called CC=1 SCAR=6.9
2950 0 undef;
2951 }
2952  
2953 sub xql_value
2954 0 ▶{
Subroutine xql_value: not called CC=1 SCAR=6.9
2955 0 new XML::XQL::Text ($_[0]->xql_text ($_[1]), $_[0]);
2956 }
2957  
2958 # Convert xql_value to Perl string (or undef if xql_value is undefined)
2959 sub xql_toString
2960 0 ▶{
Subroutine xql_toString: not called CC=2 SCAR=17.9
2961 0 my $val = $_[0]->xql_value;
2962 0 ▶ return undef if XML::XQL::isEmptyList ($val);
Branch: 1 branch
BranchTrueFalseTotal
if XML::XQL::isEmptyList($val) 0 0 0
2963  
2964 0 $val->xql_toString;
2965 }
2966  
2967 sub xql_setValue
2968 0 ▶{
Subroutine xql_setValue: not called CC=1 SCAR=0.0
2969 # Not implemented for most node types
2970 }
2971  
2972 sub xql_data
2973 0 ▶{
Subroutine xql_data: not called CC=1 SCAR=6.9
2974 0 "";
2975 }
2976  
2977 sub xql_nodeType
2978 0 ▶{
Subroutine xql_nodeType: not called CC=1 SCAR=6.9
2979 0 0;
2980 }
2981  
2982 sub xql_nodeName
2983 0 ▶{
Subroutine xql_nodeName: not called CC=1 SCAR=6.9
2984 0 [];
2985 }
2986  
2987 # Java code from "XML:: Namespaces in 20 lines" by James Clark:
2988 # see: http://www.oasis-open.org/cover/clarkNS-980804.html
2989 #
2990 # String expandName(String name, Element element, boolean isAttribute) {
2991 # // The index of the colon character in the name.
2992 # int colonIndex = name.indexOf(':');
2993 # // The name of the attribute that declares the namespace prefix.
2994 # String declAttName;
2995 # if (colonIndex == -1) {
2996 # // Default namespace applies only to element type names.
2997 # if (isAttribute)
2998 # return name;
2999 # declAttName = "xmlns";
3000 # }
3001 # else {
3002 # String prefix = name.substring(0, colonIndex);
3003 # // "xml:" is special
3004 # if (prefix.equals("xml"))
3005 # return name;
3006 # declAttName = "xmlns:" + prefix;
3007 # }
3008 # for (; element != null; element = element.getParent()) {
3009 # String ns = element.getAttributeValue(declAttName);
3010 # if (ns != null) {
3011 # // Handle special meaning of xmlns=""
3012 # if (ns.length() == 0 && colonIndex == -1)
3013 # return name;
3014 # return ns + '+' + name.substring(colonIndex + 1);
3015 # }
3016 # }
3017 # return null;
3018 # }
3019  
3020 # From "Namespaces in XML"
3021 # at http://www.w3.org/TR/1998/WD-xml-names-19980916
3022 #
3023 # The prefix xml is by definition bound to the namespace name
3024 # urn:Connolly:input:required. The prefix xmlns is used only for
3025 # namespace bindings and is not itself bound to any namespace name.
3026  
3027 28 my $DEFAULT_NAMESPACE = undef;
3028 28 my $XML_NAMESPACE = "urn:Connolly:input:required";
3029 #?? default namespace
3030  
3031 sub xql_namespace
3032 0 ▶{
Subroutine xql_namespace: not called CC=9 SCAR=45.0
3033 0 my ($self) = @_;
3034 0 my $nodeType = $self->xql_nodeType;
3035 0 my $element = $self;
3036  
3037 0 ▶ if ($nodeType == 2) # 2: Attr
Branch: 2 branches
BranchTrueFalseTotal
if ($nodeType == 2) { } 0 0 0
elsif ($nodeType != 1) { } 0 0 0
3038 {
3039 0 $element = $self->xql_parent;
3040 }
3041 elsif ($nodeType != 1) # 1: Element
3042 {
3043 0 return undef;
3044 }
3045 0 my $name = $self->xql_nodeName;
3046 0 my $declAttName;
3047  
3048 0 ▶ if ($name =~ /([^:]+):([^:]+)/)
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /([^:]+):([^:]+)/) { } 0 0 0
3049 {
3050 0 my ($prefix, $basename) = ($1, $2);
3051  
3052 # "xml:" is special
3053 0 ▶ return $XML_NAMESPACE if $prefix eq "xml";
Branch: 1 branch
BranchTrueFalseTotal
if $prefix eq "xml" 0 0 0
3054  
3055 0 $declAttName = "xmlns:$prefix";
3056 }
3057 else
3058 {
3059 # Default namespace applies only to element type names.
3060 0 ▶ return $DEFAULT_NAMESPACE if $nodeType == 2; # 2: Attr
Branch: 1 branch
BranchTrueFalseTotal
if $nodeType == 2 0 0 0
3061 #?? default namespace?
3062 0 $declAttName = "xmlns";
3063 }
3064  
3065 do
3066 0 ▶ {
Branch: 1 branch
BranchTrueFalseTotal
defined $element and $element->xql_nodeType == 1 and do { do { my $ns = $element->xql_attribute($declAttName); next unless defined $ns; return $ns->xql_rawText; $element = $element->xql_parent } } 0 0 0
Condition0%0/3
defined $element and $element->xql_nodeType == 1
!ll&&!rl&&r
000
MC/DC0%0/2
defined $element and $element->xql_nodeType == 1
defined $element $element->xql_nodeType == 1
Truth table0%0/3
A and B
A= defined $elementB= $element->xql_nodeType == 1
ABresult
0X0
100
111
3067 0 my $ns = $element->xql_attribute ($declAttName);
3068 0 ▶ next unless defined $ns;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ns 0 0 0
3069 0 return $ns->xql_rawText;
3070  
3071 0 $element = $element->xql_parent;
3072 }
3073 while (defined ($element) and $element->xql_nodeType == 1);
3074  
3075 # namespace not found
3076 0 undef;
3077 }
3078  
3079 sub xql_basename
3080 0 ▶{
Subroutine xql_basename: not called CC=3 SCAR=24.8
3081 0 my ($self) = @_;
3082 0 my $nodeType = $self->xql_nodeType;
3083 0 ▶ return undef unless $nodeType == 1 || $nodeType == 2;
Branch: 1 branch
BranchTrueFalseTotal
unless $nodeType == 1 or $nodeType == 2 0 0 0
Condition0%0/3
$nodeType == 1 or $nodeType == 2
l!l&&r!l&&!r
000
MC/DC0%0/2
$nodeType == 1 or $nodeType == 2
$nodeType == 1 $nodeType == 2
Truth table0%0/3
A or B
A= $nodeType == 1B= $nodeType == 2
ABresult
1X1
011
000
3084  
3085 0 my $name = $self->xql_nodeName;
3086 0 $name =~ s/^[^:]://; # strip prefix
3087 0 $name;
3088 }
3089  
3090 sub xql_prefix
3091 0 ▶{
Subroutine xql_prefix: not called CC=3 SCAR=24.8
3092 0 my ($self) = @_;
3093 0 my $nodeType = $self->xql_nodeType;
3094 0 ▶ return undef unless $nodeType == 1 || $nodeType == 2;
Branch: 1 branch
BranchTrueFalseTotal
unless $nodeType == 1 or $nodeType == 2 0 0 0
Condition0%0/3
$nodeType == 1 or $nodeType == 2
l!l&&r!l&&!r
000
MC/DC0%0/2
$nodeType == 1 or $nodeType == 2
$nodeType == 1 $nodeType == 2
Truth table0%0/3
A or B
A= $nodeType == 1B= $nodeType == 2
ABresult
1X1
011
000
3095  
3096 0 $self->xql_nodeName =~ /^([^:]+):/;
3097 0 $1;
3098 }
3099  
3100 # Used by ancestor()
3101 sub xql_parent
3102 0 ▶{
Subroutine xql_parent: not called CC=1 SCAR=6.9
3103 0 undef;
3104 }
3105  
3106 28 my @NodeTypeString =
3107 (
3108 "", "element", "attribute", "text", "", "", "", "processing_instruction",
3109 "comment", "document"
3110 );
3111  
3112 sub xql_nodeTypeString
3113 0 ▶{
Subroutine xql_nodeTypeString: not called CC=5 SCAR=34.0
3114 0 my $i = $_[0]->xql_nodeType;
3115 0 ▶ return $NodeTypeString[$i] if ($i >= 1 && $i <= 3 || $i >= 7 && $i <= 9);
Branch: 1 branch
BranchTrueFalseTotal
if $i >= 1 and $i <= 3 or $i >= 7 and $i <= 9 0 0 0
Condition0%0/9
$i >= 1 and $i <= 3 or $i >= 7 and $i <= 9
l!l&&r!l&&!r
000
$i >= 1 and $i <= 3
!ll&&!rl&&r
000
$i >= 7 and $i <= 9
!ll&&!rl&&r
000
MC/DC0%0/4
$i >= 1 and $i <= 3 or $i >= 7 and $i <= 9
$i >= 1 $i <= 3 $i >= 7 $i <= 9
Truth table0%0/7
A and B or C and D
A= $i >= 1B= $i <= 3C= $i >= 7D= $i <= 9
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
3116  
3117 #?? what should this return?
3118 0 "<unknown xql_nodeType $i>";
3119 }
3120  
3121 28 ▶if (not $XML::XQL::Restricted)
Branch: 1 branch
BranchTrueFalseTotal
unless ($XML::XQL::Restricted) 27 1 28
3122 {
3123 27 require XML::XQL::Plus;
3124 }
3125  
3126 # All nodes should implement:
3127  
3128 #?? this section must be updated!!
3129  
3130 # - xql_document
3131 # - xql_node: return an unblessed list reference with childNodes (not
3132 # attributes)
3133 # - xql_nodeType (default implementation for XML::XQL::Node returns 0):
3134 # Element: 1
3135 # Element Attribute: 2
3136 # Markup-Delimited Region of Text (Text and CDATASection): 3
3137 # Processing Instruction: 7
3138 # Comment: 8
3139 # Document (Entity): 9
3140 # - xql_text
3141 # - xql_value (default implementation is xql_text)
3142 # - xql_parent: return parent node or undef (Document, DocumentFragment)
3143 #
3144 # Element should define/override the following:
3145 # - xql_nodeName: return the element name
3146 # - xql_attribute("attributeName"): return an unblessed reference to a list
3147 # with the attribute, or [] if no such attribute
3148 # - xql_attribute(): return an unblessed reference to a list with
3149 # all attribute nodes
3150 # - xql_baseName, xql_prefix
3151 #
3152 # Attribute:
3153 # - xql_nodeName: return the attribute name
3154 # - xql_baseName, xql_prefix
3155 #
3156 # EntityReference:
3157 # - xql_data: return expanded text value
3158 #
3159 # Text, CDATASection:
3160 # - xql_data: return expanded text value
3161 #
3162 # -xql_element could be overriden to speed up performance
3163 #
3164  
3165 28 1;
3166  
3167 __END__
« lib/XML/Checker.pm ↑ Summary lib/XML/XQL/Plus.pm »