lib/XML/XQL/Plus.pm

filter: Statementstmt 17.9% 43 / 239 Branchbran 0.7% 1 / 126 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 14.7% 5 / 34 Podpod 14.2% 4 / 28 totaltotal 11.9% 53 / 442 CC 75 SCAR 43.7
CC
75
Cov
12%
subst
342.0
match
72.0
xql_map
56.0
CRAP 78.9
« lib/XML/XQL.pm ↑ Summary lib/XML/Handler/Composer.pm »
1 ############################################################################
2 # Copyright (c) 1998 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 # Extra functionality that is not part of the XQL spec
9 #
10  
11 package XML::XQL;
12 27 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 BEGIN
15 27 ▶{
Subroutine BEGIN: called CC=2 SCAR=7.6
16 27 ▶ die "don't use/require XML::XQL::Plus, either use/require XML::XQL or XML::XQL::Strict" unless $XML::XQL::Included;
Branch: 1 branch
BranchTrueFalseTotal
unless $XML::XQL::Included 0 27 27
17 };
18  
19 27 defineComparisonOperators
20 (
21 "=~" => \&XML::XQL::match_oper,
22 "!~" => \&XML::XQL::no_match_oper,
23 "match" => \&XML::XQL::match_oper,
24 "no_match" => \&XML::XQL::no_match_oper,
25 "isa" => \&XML::XQL::isa_oper,
26 "can" => \&XML::XQL::can_oper,
27 );
28  
29 sub match_oper
30 0 ▶{
Subroutine match_oper: not called CC=5 SCAR=34.0
Pod undocumented
31 0 my ($node, $expr) = @_;
32  
33 0 ▶ return [] if isEmptyList ($node);
Branch: 1 branch
BranchTrueFalseTotal
if isEmptyList($node) 0 0 0
34 #?? can this happen?
35  
36 0 my $str = $node->xql_toString;
37  
38 0 $expr = prepareRvalue ($expr->solve ([$node]));
39 0 ▶ return [] if isEmptyList ($expr);
Branch: 1 branch
BranchTrueFalseTotal
if isEmptyList($expr) 0 0 0
40 #?? can this happen?
41  
42 0 $expr = $expr->xql_toString;
43 0 ▶ croak "bad search pattern '$expr' for =~" unless $expr =~ m!^\s*[m/]!o;
Branch: 1 branch
BranchTrueFalseTotal
unless $expr =~ m[^\s*[m/]]o 0 0 0
44  
45 0 my $res = eval "\$str =~ $expr";
46 0 ▶ croak "bad search pattern '$expr' for =~ operator: $@" if ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
47 0 $res;
48 }
49  
50 sub no_match_oper
51 0 ▶{
Subroutine no_match_oper: not called CC=5 SCAR=34.0
Pod undocumented
52 0 my ($node, $expr) = @_;
53  
54 0 ▶ return [] if isEmptyList ($node);
Branch: 1 branch
BranchTrueFalseTotal
if isEmptyList($node) 0 0 0
55 #?? can this happen?
56  
57 0 my $str = $node->xql_toString;
58  
59 0 $expr = prepareRvalue ($expr->solve ([$node]));
60 0 ▶ return [] if isEmptyList ($expr);
Branch: 1 branch
BranchTrueFalseTotal
if isEmptyList($expr) 0 0 0
61 #?? can this happen?
62  
63 0 $expr = $expr->xql_toString;
64 0 ▶ croak "bad search pattern '$expr' for !~" unless $expr =~ m!^\s*[m/]!o;
Branch: 1 branch
BranchTrueFalseTotal
unless $expr =~ m[^\s*[m/]]o 0 0 0
65  
66 0 my $res = eval "\$str !~ $expr";
67 0 ▶ croak "bad search pattern '$expr' for !~ operator: $@" if ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
68 0 $res;
69 }
70  
71 sub isa_oper
72 0 ▶{
Subroutine isa_oper: not called CC=4 SCAR=30.0
Pod undocumented
73 0 my ($node, $expr) = @_;
74  
75 0 ▶ return [] if isEmptyList ($node);
Branch: 1 branch
BranchTrueFalseTotal
if isEmptyList($node) 0 0 0
76 #?? can this happen?
77  
78 0 $expr = prepareRvalue ($expr->solve ([$node]));
79 0 ▶ return [] if isEmptyList ($expr);
Branch: 1 branch
BranchTrueFalseTotal
if isEmptyList($expr) 0 0 0
80 #?? can this happen?
81  
82 0 $expr = $expr->xql_toString;
83  
84 # Expand "number" to "XML::XQL::Number" etc.
85 0 $expr = expandType ($expr);
86  
87 #?? I don't think empty lists are possible here. If so, add "[]" as expr
88  
89 0 ▶ ref($node) and $node->isa ($expr);
Branch: 1 branch
BranchTrueFalseTotal
ref $node and $node->isa($expr) 0 0 0
90 }
91  
92 #
93 # Not sure how useful this is, unless it supports XQL functions/methods...
94 #
95 sub can_oper
96 0 ▶{
Subroutine can_oper: not called CC=4 SCAR=30.0
Pod undocumented
97 0 my ($node, $expr) = @_;
98  
99 0 ▶ return [] if isEmptyList ($node);
Branch: 1 branch
BranchTrueFalseTotal
if isEmptyList($node) 0 0 0
100 #?? can this happen?
101  
102 0 $expr = prepareRvalue ($expr->solve ([$node]));
103 0 ▶ return [] if isEmptyList ($expr);
Branch: 1 branch
BranchTrueFalseTotal
if isEmptyList($expr) 0 0 0
104 #?? can this happen?
105  
106 0 $expr = $expr->xql_toString;
107  
108 0 ▶ ref ($node) and $node->can ($expr);
Branch: 1 branch
BranchTrueFalseTotal
ref $node and $node->can($expr) 0 0 0
109 }
110  
111 sub once
112 0 ▶{
Subroutine once: not called CC=1 SCAR=6.9
Pod documented
113 0 my ($context, $list, $expr) = @_;
114 0 $expr->solve ($context, $list);
115 }
116  
117 sub xql_eval
118 0 ▶{
Subroutine xql_eval: not called CC=6 SCAR=37.4
Pod undocumented
119 0 my ($context, $list, $query, $type) = @_;
120  
121 # return [] if @$list == 0;
122  
123 0 $query = toList ($query->solve ($context, $list));
124 0 ▶ return [] unless @$query;
Branch: 1 branch
BranchTrueFalseTotal
unless @$query 0 0 0
125  
126 0 ▶ if (defined $type)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $type) { } 0 0 0
127 {
128 0 $type = prepareRvalue ($type->solve ($context, $list));
129 0 ▶ $type = isEmptyList ($type) ? "Text" : $type->xql_toString;
Branch: 1 branch
BranchTrueFalseTotal
isEmptyList($type) ? : 0 0 0
130  
131 # Expand "number" to "XML::XQL::Number" etc.
132 0 $type = expandType ($type);
133 }
134 else
135 {
136 0 $type = "XML::XQL::Text";
137 }
138  
139 0 my @result = ();
140 0 for my $val (@$query)
141 {
142 0 $val = $val->xql_toString;
143 0 $val = eval $val;
144  
145 #print "eval result=$val\n";
146 #?? check result?
147 0 ▶ push @result, eval "new $type (\$val)" if defined $val;
Branch: 1 branch
BranchTrueFalseTotal
if defined $val 0 0 0
148 }
149 0 \@result;
150 }
151  
152 sub subst
153 0 ▶{
Subroutine subst: not called CC=18 SCAR=58.3
Pod documented
154 0 my ($context, $list, $query, $expr, $repl, $mod, $mode) = @_;
155  
156 #?? not sure?
157 0 ▶ return [] if @$list == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @$list == 0 0 0 0
158  
159 0 $expr = prepareRvalue ($expr->solve ($context, $list));
160 0 ▶ return [] if isEmptyList ($expr);
Branch: 1 branch
BranchTrueFalseTotal
if isEmptyList($expr) 0 0 0
161 0 $expr = $expr->xql_toString;
162
163 0 $repl = prepareRvalue ($repl->solve ($context, $list));
164 0 ▶ return [] if isEmptyList ($repl);
Branch: 1 branch
BranchTrueFalseTotal
if isEmptyList($repl) 0 0 0
165 0 $repl = $repl->xql_toString;
166  
167 0 ▶ if (defined $mod)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $mod) 0 0 0
168 {
169 0 $mod = prepareRvalue ($mod->solve ($context, $list));
170 0 ▶ $mod = isEmptyList ($mod) ? "" : $mod->xql_toString;
Branch: 1 branch
BranchTrueFalseTotal
isEmptyList($mod) ? : 0 0 0
171 }
172  
173 0 ▶ if (defined $mode)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $mode) { } 0 0 0
174 {
175 0 $mode = prepareRvalue ($mode->solve ($context, $list));
176 0 ▶ $mode = isEmptyList ($mode) ? 0 : $mode->xql_toString;
Branch: 1 branch
BranchTrueFalseTotal
isEmptyList($mode) ? : 0 0 0
177 }
178 else
179 {
180 0 $mode = 0; # default mode: use textBlocks for Elements
181 }
182  
183 0 my @result = ();
184 0 my $nodes = toList ($query->solve ($context, $list));
185  
186 0 for my $node (@$nodes)
187 {
188 0 ▶ if ($mode == 0 && $node->xql_nodeType == 1) # 1: Element node
Branch: 1 branch
BranchTrueFalseTotal
if ($mode == 0 and $node->xql_nodeType == 1) { } 0 0 0
Condition0%0/3
$mode == 0 and $node->xql_nodeType == 1
!ll&&!rl&&r
000
MC/DC0%0/2
$mode == 0 and $node->xql_nodeType == 1
$mode == 0 $node->xql_nodeType == 1
Truth table0%0/3
A and B
A= $mode == 0B= $node->xql_nodeType == 1
ABresult
0X0
100
111
189 {
190 # For Element nodes, replace text in consecutive text blocks
191 # Note that xql_rawtextBlocks, returns the blocks in reverse order,
192 # so that the indices of nodes within previous blocks don't need
193 # to be adjusted when a replacement occurs.
194 0 my $block_matched = 0;
195 0 BLOCK: for my $block ($node->xql_rawTextBlocks)
196 {
197 0 my $str = $block->[2];
198 0 my $result = eval "\$str =~ s/\$expr/\$repl/$mod";
199 0 ▶ croak "bad subst expression s/$expr/$repl/$mod: $@" if ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
200 0 ▶ next BLOCK unless $result;
Branch: 1 branch
BranchTrueFalseTotal
unless $result 0 0 0
201  
202 0 $block_matched++;
203 0 $node->xql_replaceBlockWithText ($block->[0], $block->[1], $str);
204 }
205 # Return the input parameter only if a substitution occurred
206 0 ▶ push @result, $node if $block_matched;
Branch: 1 branch
BranchTrueFalseTotal
if $block_matched 0 0 0
207 }
208 else
209 {
210 0 my $str = $node->xql_toString;
211 0 ▶ next unless defined $str;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str 0 0 0
212
213 0 my $result = eval "\$str =~ s/\$expr/\$repl/$mod";
214 0 ▶ croak "bad subst expression s/$expr/$repl/$mod: $@" if ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
215 0 ▶ next unless $result;
Branch: 1 branch
BranchTrueFalseTotal
unless $result 0 0 0
216 #print "result=$result for str[$str] =~ s/$expr/$repl/$mod\n";
217  
218 # Return the input parameter only if a substitution occurred
219 0 $node->xql_setValue ($str);
220 0 push @result, $node;
221 }
222 # xql_setValue will actually change the value of the node for an Attr,
223 # Text, CDataSection, EntityRef or Element
224 }
225 0 \@result;
226 }
227  
228 #?? redo match - what should it return?
229 sub match
230 0 ▶{
Subroutine match: not called CC=8 SCAR=42.8
Pod undocumented
231 0 my ($context, $list, $query, $repl, $mod) = @_;
232  
233 0 ▶ return [] if @$list == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @$list == 0 0 0 0
234  
235 0 $query = prepareRvalue ($query->solve ($context, $list));
236 0 ▶ return [] if isEmptyList ($query);
Branch: 1 branch
BranchTrueFalseTotal
if isEmptyList($query) 0 0 0
237 0 $query = $query->xql_toString;
238
239 0 ▶ if (defined $mod)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $mod) 0 0 0
240 {
241 0 $mod = prepareRvalue ($mod->solve ($context, $list));
242 0 ▶ $mod = isEmptyList ($mod) ? "" : $mod->xql_toString;
Branch: 1 branch
BranchTrueFalseTotal
isEmptyList($mod) ? : 0 0 0
243 }
244  
245 0 my $str = $list->[0]->xql_toString;
246 0 ▶ return [] unless defined $str;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str 0 0 0
247  
248 0 my (@matches) = ();
249 0 eval "\@matches = (\$str =~ /\$query/$mod)";
250 0 ▶ croak "bad match expression m/$query/$mod" if ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
251  
252 #?? or should I map undef to XML::XQL::Text("") ?
253 0 ▶ @matches = map { defined($_) ? new XML::XQL::Text ($_) : [] } @matches;
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
254 0 \@matches;
255 }
256  
257 sub xql_map
258 0 ▶{
Subroutine xql_map: not called CC=7 SCAR=40.3
Pod undocumented
259 0 my ($context, $list, $query, $code) = @_;
260  
261 #?? not sure?
262 0 ▶ return [] if @$list == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @$list == 0 0 0 0
263  
264 0 $code = prepareRvalue ($code->solve ($context, $list));
265 0 ▶ return [] if isEmptyList ($code);
Branch: 1 branch
BranchTrueFalseTotal
if isEmptyList($code) 0 0 0
266 0 $code = $code->xql_toString;
267
268 0 my @result = ();
269 0 my $nodes = toList ($query->solve ($context, $list));
270  
271 0 for my $node (@$nodes)
272 {
273 0 my $str = $node->xql_toString;
274 0 ▶ next unless defined $str;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str 0 0 0
275  
276 0 my (@mapresult) = ($str);
277  
278 #?? NOTE: the $code should
279 0 eval "\@mapresult = map { $code } (\$str)";
280 0 ▶ croak "bad map expression '$code' ($@)" if ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
281  
282 # Return the input parameter only if a change occurred
283 0 ▶ next unless $mapresult[0] eq $str;
Branch: 1 branch
BranchTrueFalseTotal
unless $mapresult[0] eq $str 0 0 0
284  
285 # xql_setValue will actually change the value of the node for an Attr,
286 # Text, CDataSection, EntityRef or Element
287 0 $node->xql_setValue ($str);
288 0 push @result, $node;
289 }
290 0 \@result;
291 }
292  
293 sub xql_new
294 0 ▶{
Subroutine xql_new: not called CC=2 SCAR=17.9
Pod undocumented
295 0 my ($type, @arg) = @_;
296  
297 # Expand "number" to "XML::XQL::Number" etc.
298 0 $type = expandType ($type);
299  
300 0 my $obj = eval "new $type (\@arg)";
301 0 ▶ $@ ? [] : $obj; # return empty list on exception
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 0 0 0
302 }
303  
304 27 my $DOM_PARSER; # used by xql_document (below)
305 sub setDocParser
306 0 ▶{
Subroutine setDocParser: not called CC=1 SCAR=6.9
Pod documented
307 0 $DOM_PARSER = shift;
308 }
309  
310 sub xql_document
311 0 ▶{
Subroutine xql_document: not called CC=4 SCAR=30.0
Pod undocumented
312 0 my ($docname) = @_;
313 0 ▶ my $parser = $DOM_PARSER ||= new XML::DOM::Parser;
Condition0%0/3
$DOM_PARSER ||= "XML::DOM::Parser"->new
l!l&&r!l&&!r
000
MC/DC0%0/2
$DOM_PARSER ||= "XML::DOM::Parser"->new
$DOM_PARSER "XML::DOM::Parser"->new
Truth table0%0/3
A ||= B
A= $DOM_PARSERB= "XML::DOM::Parser"->new
ABresult
1X1
011
000
314 0 my $doc;
315 eval
316 0 {
317 0 $doc = $parser->parsefile ($docname);
318 };
319 0 ▶ if ($@)
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
320 {
321 0 warn "xql_document: could not read XML file [$docname]: $@";
322 }
323 0 ▶ return defined $doc ? $doc : [];
Branch: 1 branch
BranchTrueFalseTotal
defined $doc ? : 0 0 0
324 }
325  
326 #----------- XQL+ methods --------------------------------------------
327  
328  
329 sub DOM_nodeType
330 0 ▶{
Subroutine DOM_nodeType: not called CC=2 SCAR=17.9
Pod documented
331 0 my ($context, $list) = @_;
332  
333 0 ▶ return [] if @$list == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @$list == 0 0 0 0
334  
335 0 new XML::XQL::Number ($list->[0]->xql_DOM_nodeType, $list->[0]);
336 }
337  
338 #----------- Perl Builtin Functions ----------------------------------
339  
340 # Note that certain functions (like mkdir) are not considered "constant"
341 # because we don't want their invocation values cached. (We want the
342 # function to be called every time the Invocation is solved/evaluated.)
343 27 my %PerlFunc =
344 (
345 # Format:
346 # "funcName", => [ARGCOUNT, RETURN_TYPE [, CONSTANT = 0, [QUERY_ARG = 0]]]
347  
348 #-------- Arithmetic Functions
349  
350 "abs" => [1, "Number", 1],
351 "atan2" => [2, "Number", 1, -1],
352 "cos" => [1, "Number", 1],
353 "exp" => [1, "Number", 1],
354 "int" => [1, "Number", 1],
355 "log" => [1, "Number", 1],
356 "rand" => [[0, 1], "Number", 0, -1],
357 "sin" => [1, "Number", 1],
358 "sqrt" => [1, "Number", 1],
359 "srand" => [[0, 1], "Number", 0, -1],
360 "time" => [0, "Number", 0, -1],
361  
362 #-------- Conversion Functions
363  
364 "chr" => [1, "Text", 1],
365 # "gmtime" => [1, "List of Number", 1],
366 "hex" => [1, "Number", 1],
367 # "localtime" => [1, "List of Number", 1],
368 "oct" => [1, "Number", 1],
369 "ord" => [1, "Text", 1],
370 "vec" => [3, "Number", 1],
371 "pack" => [[1, -1], "Text", 1, -1], #?? how should this work??
372 # "unpack" => [2, "List of ?", 1],
373  
374 #-------- String Functions
375  
376 "chomp" => [1, "Text", 1],
377 "chop" => [1, "Text", 1],
378 "crypt" => [2, "Text", 1],
379 "lindex" => [[2, 3], "Number", 1], # "index" is already taken by XQL
380 "length" => [1, "Number", 1],
381 "lc" => [1, "Text", 1],
382 "lcfirst" => [1, "Text", 1],
383 "quotemeta" => [1, "Text", 1],
384 "rindex" => [[2, 3], "Number", 1],
385 "substr" => [[2, 3], "Text", 1],
386 "uc" => [1, "Text", 1],
387 "ucfirst" => [1, "Text", 1],
388 "reverse" => [1, "Text", 1],
389 "sprintf" => [[1, -1], "Text", 1, -1],
390  
391 #-------- Array Functions
392  
393 "join" => [[1, -1], "Text", 1],
394 # "split" => [[2, 3], "List of Text", 1],
395  
396 #-------- File Functions
397  
398 "chmod" => [2, "Boolean", 0, 1],
399 "chown" => [3, "Boolean", 0, 2],
400 "link" => [2, "Number", 0, -1], #?? no return value
401 # "lstat" => [1, "List of Number"],
402 "mkdir" => [2, "Boolean"], #?? or is 1 arg also allowed?
403 "readlink" => [1, "Text"],
404 "rename" => [2, "Boolean", 0, -1],
405 "rmdir" => [1, "Boolean"],
406 # "stat" => [1, "List of Number"],
407 "symlink" => [2, "Boolean", 0, -1],
408 "unlink" => [1, "Boolean"],
409 "utime" => [3, "Boolean", 0, 2],
410 "truncate" => [2, "Number"], #?? no return value
411  
412 #-------- System Interaction
413  
414 "exit" => [[0, 1], "Number"],
415 # "glob" => [1, "List of Text"],
416 "system" => [[1, -1], "Number", 0, -1],
417 # "times" => [0, "List of Number"],
418  
419 #-------- Miscellaneous
420  
421 "defined" => [1, "Boolean"], # is this useful??
422 "dump" => [[0, 1], "Number", 0, -1],
423 "ref" => [1, "Text"],
424 );
425 #?? die, warn, croak (etc.),
426 #?? file test (-X), tr// (same as y//)
427 #?? array functions, sort
428  
429 # Generate wrapper for Perl builtin function on the fly
430 sub generatePerlWrapper
431 0 ▶{
Subroutine generatePerlWrapper: not called CC=3 SCAR=24.8
Pod undocumented
432 0 my ($name) = @_;
433 0 my $args = $PerlFunc{$name};
434 0 ▶ return undef unless defined $args; # not found
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args 0 0 0
435  
436 0 my ($argCount, $returnType, $const, $queryArg) = @$args;
437 0 my $funcName = $name;
438 0 ▶ if ($name eq "lindex") # "index" is already taken
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "lindex") 0 0 0
439 {
440 0 $funcName = "index";
441 }
442 0 generateFunction ($name, $funcName, $returnType, $argCount, 0, $const,
443 $queryArg);
444 0 $Func{$name};
445 }
446  
447 #?? Inline functions, do they make sense? E.g. 'elem!sub("code", "arg1")'
448 #?? Normally, user should use defineFunction, but if most of them have
449 #?? a lot of common code, I could provide the pre- and post-code.
450 #?? After processing the user-supplied code block, how should I convert the
451 #?? user's result back to an Invocation result. E.g. do I get a single value
452 #?? or a list back?
453  
454 27 defineFunction ("eval", \&XML::XQL::xql_eval, [1, 2]);
455 27 defineFunction ("subst", \&XML::XQL::subst, [3, 5], 1);
456 27 defineFunction ("s", \&XML::XQL::subst, [3, 5], 1);
457 27 defineFunction ("match", \&XML::XQL::match, [1, 2]);
458 27 defineFunction ("m", \&XML::XQL::match, [1, 2]);
459 27 defineFunction ("map", \&XML::XQL::xql_map, 2, 1);
460 27 defineFunction ("once", \&XML::XQL::once, 1, 1);
461  
462 27 defineMethod ("DOM_nodeType", \&XML::XQL::DOM_nodeType, 0, 0);
463  
464 27 generateFunction ("new", "XML::XQL::xql_new", "*", [1, -1], 1, 0, 1);
465 27 generateFunction ("document", "XML::XQL::xql_document", "*", 1, 1, 0, 0);
466  
467 # doc() is an alias for document()
468 27 defineFunction ("doc", \&XML::XQL::xql_wrap_document, 1, 1);
469  
470 #------------------------------------------------------------------------------
471 # The following functions were found in the XPath spec.
472  
473 # Found in XPath but not (yet) implemented in XML::XQL:
474 # - type casting (string, number, boolean) - Not sure if needed...
475 # Note that string() converts booleans to 'true' and 'false', but our
476 # internal type casting converts it to perl values '0' and '1'...
477 # - math (+,-,*,mod,div) - Use eval() for now
478 # - last(), position() - Similar to end() and index() except they're 1-based
479 # - local-name(node-set?), namespace-uri(node-set?)
480 # - name(node-set?) - Can we pass a node-set in XQL?
481 # - lang(string)
482  
483 0 ▶sub xpath_concat { join ("", @_) }
Subroutine xpath_concat: not called CC=1 SCAR=6.9
Pod undocumented
484 0 ▶sub xpath_starts_with { $_[0] =~ /^\Q$_[1]\E/ }
Subroutine xpath_starts_with: not called CC=1 SCAR=6.9
Pod undocumented
485 # ends-with is not part of XPath
486 0 ▶sub xpath_ends_with { $_[0] =~ /\Q$_[1]\E$/ }
Subroutine xpath_ends_with: not called CC=1 SCAR=6.9
Pod undocumented
487 0 ▶sub xpath_contains { $_[0] =~ /\Q$_[1]\E/ }
Subroutine xpath_contains: not called CC=1 SCAR=6.9
Pod undocumented
488  
489 # The following methods don't know about NaN, +/-Infinity or -0.
490 0 ▶sub xpath_floor { use POSIX; POSIX::floor ($_[0]) }
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine xpath_floor: not called CC=1 SCAR=0.2
Pod undocumented
491 0 ▶sub xpath_ceiling { use POSIX; POSIX::ceil ($_[0]) }
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine xpath_ceiling: not called CC=1 SCAR=0.2
Pod undocumented
492 0 ▶sub xpath_round { use POSIX; POSIX::floor ($_[0] + 0.5) }
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine xpath_round: not called CC=1 SCAR=0.2
Pod undocumented
493  
494 # Note that the start-index is 1-based in XPath
495 sub xpath_substring
496 0 ▶{
Subroutine xpath_substring: not called CC=2 SCAR=17.9
Pod undocumented
497 0 ▶ defined $_[2] ? substr ($_[0], $_[1] - 1, $_[2])
Branch: 1 branch
BranchTrueFalseTotal
defined $_[2] ? : 0 0 0
498 : substr ($_[0], $_[1] - 1)
499 }
500  
501 sub xpath_substring_before
502 0 ▶{
Subroutine xpath_substring_before: not called CC=2 SCAR=17.9
Pod undocumented
503 0 my $i = index ($_[0], $_[1]);
504 0 ▶ $i == -1 ? undef : substr ($_[0], 0, $i)
Branch: 1 branch
BranchTrueFalseTotal
$i == -1 ? : 0 0 0
505 }
506  
507 sub xpath_substring_after
508 0 ▶{
Subroutine xpath_substring_after: not called CC=2 SCAR=17.9
Pod undocumented
509 0 my $i = index ($_[0], $_[1]);
510 0 ▶ $i == -1 ? undef : substr ($_[0], $i + length($_[1]))
Branch: 1 branch
BranchTrueFalseTotal
$i == -1 ? : 0 0 0
511 }
512  
513 # Note that d,c,s are tr/// modifiers. Also can't use open delimiters i.e. {[(<
514 27 my @TR_DELIMITERS = split //, "/!%^&*)-_=+|~]}'\";:,.>/?abefghijklmnopqrtuvwxyz";
515  
516 sub xpath_translate
517 0 ▶{
Subroutine xpath_translate: not called CC=5 SCAR=34.0
Pod undocumented
518 0 my ($str, $from, $to) = @_;
519  
520 0 my $delim;
521 0 for my $d (@TR_DELIMITERS)
522 {
523 0 ▶ if (index ($from, $d) == -1 && index ($to, $d) == -1)
Branch: 1 branch
BranchTrueFalseTotal
if (index($from, $d) == -1 and index($to, $d) == -1) 0 0 0
Condition0%0/3
index($from, $d) == -1 and index($to, $d) == -1
!ll&&!rl&&r
000
MC/DC0%0/2
index($from, $d) == -1 and index($to, $d) == -1
index($from, $d) == -1 index($to, $d) == -1
Truth table0%0/3
A and B
A= index($from, $d) == -1B= index($to, $d) == -1
ABresult
0X0
100
111
524 {
525 0 $delim = $d;
526 0 last;
527 }
528 }
529 0 ▶ die "(xpath_)translate: can't find suitable 'tr' delimiter"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $delim 0 0 0
530 unless defined $delim;
531  
532 # XPath defines that if length($from) > length($to), characters in $from
533 # for which there is no match in $to, should be deleted.
534 # (So we must use the 's' modifier.)
535 0 eval "\$str =~ tr$delim$from$delim$to${delim}d";
536 0 $str;
537 }
538  
539 sub xpath_string_length
540 0 ▶{
Subroutine xpath_string_length: not called CC=5 SCAR=34.0
Pod undocumented
541 0 my ($context, $list, $text) = @_;
542  
543 0 ▶ if (defined $text)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $text) { } 0 0 0
544 {
545 0 $text = XML::XQL::prepareRvalue ($text->solve ($context, $list));
546 0 ▶ return [] unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 0 0
547  
548 0 return new XML::XQL::Number (length $text->xql_toString,
549 $text->xql_sourceNode);
550 }
551 else
552 {
553 0 ▶ return [] if @$list == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @$list == 0 0 0 0
554  
555 0 my @result;
556 0 for my $node (@$list)
557 {
558 0 push @result, new XML::XQL::Number (length $node->xql_toString,
559 $node);
560 }
561 0 return \@result;
562 }
563 }
564  
565 sub _normalize
566 0 ▶{
Subroutine _normalize: not called CC=1 SCAR=6.9
567 0 $_[0] =~ s/\s+/ /g;
568 0 $_[0] =~ s/^\s+//;
569 0 $_[0] =~ s/\s+$//;
570 0 $_[0];
571 }
572  
573 sub xpath_normalize_space
574 0 ▶{
Subroutine xpath_normalize_space: not called CC=5 SCAR=34.0
Pod undocumented
575 0 my ($context, $list, $text) = @_;
576  
577 0 ▶ return [] if @$list == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @$list == 0 0 0 0
578  
579 0 ▶ if (defined $text)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $text) { } 0 0 0
580 {
581 0 $text = XML::XQL::prepareRvalue ($text->solve ($context, $list));
582 0 ▶ return [] unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 0 0
583  
584 0 return new XML::XQL::Text (_normalize ($text->xql_toString),
585 $text->xql_sourceNode);
586 }
587 else
588 {
589 0 my @result;
590 0 for my $node (@$list)
591 {
592 0 push @result, new XML::XQL::Text (_normalize ($node->xql_toString),
593 $node);
594 }
595 0 return \@result;
596 }
597 }
598  
599 sub xpath_sum
600 0 ▶{
Subroutine xpath_sum: not called CC=3 SCAR=24.8
Pod undocumented
601 0 my ($context, $list, $expr) = @_;
602  
603 0 ▶ return [] if @$list == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @$list == 0 0 0 0
604 #?? or return Number(0) ?
605  
606 0 my $sum = 0;
607 0 $expr = XML::XQL::toList ($expr->solve ($context, $list));
608 0 for my $r (@{ $expr })
609 {
610 0 $sum += $r->xql_toString;
611 }
612 0 return new XML::XQL::Number ($sum, undef);
613 }
614  
615 27 generateFunction ("round", "XML::XQL::xpath_round", "Number", 1, 1);
616 27 generateFunction ("floor", "XML::XQL::xpath_floor", "Number", 1, 1);
617 27 generateFunction ("ceiling", "XML::XQL::xpath_ceiling", "Number", 1, 1);
618  
619 27 generateFunction ("concat", "XML::XQL::xpath_concat", "Text", [2, -1], 1);
620 27 generateFunction ("starts-with", "XML::XQL::xpath_starts_with", "Boolean", 2, 1);
621 27 generateFunction ("ends-with", "XML::XQL::xpath_ends_with", "Boolean", 2, 1);
622 27 generateFunction ("contains", "XML::XQL::xpath_contains", "Boolean", 2, 1);
623 27 generateFunction ("substring-before", "XML::XQL::xpath_substring_before", "Text", 2, 1);
624 27 generateFunction ("substring-after", "XML::XQL::xpath_substring_after", "Text", 2, 1);
625 # Same as Perl substr() except index is 1-based
626 27 generateFunction ("substring", "XML::XQL::xpath_substring", "Text", [2, 3], 1);
627 27 generateFunction ("translate", "XML::XQL::xpath_translate", "Text", 3, 1);
628  
629 27 defineMethod ("string-length", \&XML::XQL::xpath_string_length, [0, 1], 1);
630 27 defineMethod ("normalize-space", \&XML::XQL::xpath_normalize_space, [0, 1], 1);
631  
632 27 defineFunction ("sum", \&XML::XQL::xpath_sum, 1, 1);
633  
634 27 1; # module return code
« lib/XML/XQL.pm ↑ Summary lib/XML/Handler/Composer.pm »