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