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