1 require 'rexml/namespace'
2 require 'rexml/xmltokens'
6 # You don't want to use this class. Really. Use XPath, which is a wrapper
7 # for this class. Believe me. You don't want to poke around in here.
8 # There is strange, dark magic at work in this code. Beware. Go back! Go
9 # back while you still can!
12 LITERAL = /^'([^']*)'|^"([^"]*)"/u
14 def namespaces=( namespaces )
15 Functions::namespace_context = namespaces
16 @namespaces = namespaces
20 path.gsub!(/([\(\[])\s+/, '\1') # Strip ignorable spaces
21 path.gsub!( /\s+([\]\)])/, '\1' )
23 path = OrExpr(path, parsed)
29 Predicate( "[#{path}]", parsed )
33 def abbreviate( path )
34 path = path.kind_of?(String) ? parse( path ) : path
42 string << "/" if string.size > 0
45 string << "/" if string.size > 0
46 when :descendant_or_self
56 when :following, :following_sibling,
57 :ancestor, :ancestor_or_self, :descendant,
58 :namespace, :preceding, :preceding_sibling
59 string << "/" unless string.size == 0
60 string << op.to_s.tr("_", "-")
65 string << prefix+":" if prefix.size > 0
69 string << predicate_to_string( path.shift ) {|x| abbreviate( x ) }
76 string << predicate_to_string( path.shift[0] ) {|x| abbreviate( x )}
79 string << %Q{ "#{path.shift}" }
81 string << "/" unless string.size == 0
87 string = "/"+string if document
92 path = path.kind_of?(String) ? parse( path ) : path
100 when :attribute, :child, :following, :following_sibling,
101 :ancestor, :ancestor_or_self, :descendant, :descendant_or_self,
102 :namespace, :preceding, :preceding_sibling, :self, :parent
103 string << "/" unless string.size == 0
104 string << op.to_s.tr("_", "-")
111 string << prefix+":" if prefix.size > 0
115 string << predicate_to_string( path.shift ) { |x| expand(x) }
120 string << "/" unless string.size == 0
126 string = "/"+string if document
130 def predicate_to_string( path, &block )
133 when :and, :or, :mult, :plus, :minus, :neq, :eq, :lt, :gt, :lteq, :gteq, :div, :mod, :union
151 left = predicate_to_string( path.shift, &block )
152 right = predicate_to_string( path.shift, &block )
165 string << predicate_to_string( path.shift, &block )
170 string << path.shift.inspect
174 string << yield( path )
177 return string.squeeze(" ")
182 # | RelativeLocationPath
183 # | '/' RelativeLocationPath?
184 # | '//' RelativeLocationPath
185 def LocationPath path, parsed
186 #puts "LocationPath '#{path}'"
191 parsed << :descendant_or_self
199 return RelativeLocationPath( path, parsed ) if path.size > 0
202 #RelativeLocationPath
204 # | (AXIS_NAME '::' | '@' | '') AxisSpecifier
207 # | '.' | '..' AbbreviatedStep
208 # | RelativeLocationPath '/' Step
209 # | RelativeLocationPath '//' Step
210 AXIS = /^(ancestor|ancestor-or-self|attribute|child|descendant|descendant-or-self|following|following-sibling|namespace|parent|preceding|preceding-sibling|self)::/
211 def RelativeLocationPath path, parsed
212 #puts "RelativeLocationPath #{path}"
214 # (axis or @ or <child::>) nodetest predicate >
234 parsed << $1.tr('-','_').intern
241 #puts "NODETESTING '#{path}'"
243 path = NodeTest( path, n)
244 #puts "NODETEST RETURNED '#{path}'"
247 path = Predicate( path, n )
256 parsed << :descendant_or_self
270 # Returns a 1-1 map of the nodeset
271 # The contents of the resulting array are either:
272 # true/false, if a positive match
273 # String, if a name match
275 # | ('*' | NCNAME ':' '*' | QNAME) NameTest
276 # | NODE_TYPE '(' ')' NodeType
277 # | PI '(' LITERAL ')' PI
278 # | '[' expr ']' Predicate
279 NCNAMETEST= /^(#{NCNAME_STR}):\*/u
280 QNAME = Namespace::NAMESPLIT
281 NODE_TYPE = /^(comment|text|node)\(\s*\)/m
282 PI = /^processing-instruction\(/
283 def NodeTest path, parsed
284 #puts "NodeTest with #{path}"
293 parsed << type.tr('-', '_').intern
301 raise ParseException.new("Missing ')' after processing instruction") if path[0] != ?)
304 parsed << :processing_instruction
305 parsed << (literal || '')
317 prefix = "" unless prefix
325 # Filters the supplied nodeset on the predicate(s)
326 def Predicate path, parsed
327 #puts "PREDICATE with #{path}"
328 return nil unless path[0] == ?[
331 path, expr = get_group(path)
332 predicates << expr[1..-2] if expr
334 #puts "PREDICATES = #{predicates.inspect}"
335 predicates.each{ |expr|
336 #puts "ORING #{expr}"
342 #puts "PREDICATES = #{predicates.inspect}"
346 # The following return arrays of true/false, a 1-1 mapping of the
347 # supplied nodeset, except for axe(), which returns a filtered
350 #| OrExpr S 'or' S AndExpr
352 def OrExpr path, parsed
353 #puts "OR >>> #{path}"
355 rest = AndExpr( path, n )
356 #puts "OR <<< #{rest}"
358 while rest =~ /^\s*( or )/
360 rest = AndExpr( $', n[-1] )
363 if parsed.size == 0 and n.size != 0
371 #| AndExpr S 'and' S EqualityExpr
373 def AndExpr path, parsed
374 #puts "AND >>> #{path}"
376 rest = EqualityExpr( path, n )
377 #puts "AND <<< #{rest}"
379 while rest =~ /^\s*( and )/
381 #puts "AND >>> #{rest}"
382 rest = EqualityExpr( $', n[-1] )
383 #puts "AND <<< #{rest}"
386 if parsed.size == 0 and n.size != 0
394 #| EqualityExpr ('=' | '!=') RelationalExpr
396 def EqualityExpr path, parsed
397 #puts "EQUALITY >>> #{path}"
399 rest = RelationalExpr( path, n )
400 #puts "EQUALITY <<< #{rest}"
402 while rest =~ /^\s*(!?=)\s*/
408 rest = RelationalExpr( $', n[-1] )
411 if parsed.size == 0 and n.size != 0
419 #| RelationalExpr ('<' | '>' | '<=' | '>=') AdditiveExpr
421 def RelationalExpr path, parsed
422 #puts "RELATION >>> #{path}"
424 rest = AdditiveExpr( path, n )
425 #puts "RELATION <<< #{rest}"
427 while rest =~ /^\s*([<>]=?)\s*/
433 sym << "eq" if $1[-1] == ?=
434 n = [ sym.intern, n, [] ]
435 rest = AdditiveExpr( $', n[-1] )
438 if parsed.size == 0 and n.size != 0
446 #| AdditiveExpr ('+' | S '-') MultiplicativeExpr
447 #| MultiplicativeExpr
448 def AdditiveExpr path, parsed
449 #puts "ADDITIVE >>> #{path}"
451 rest = MultiplicativeExpr( path, n )
452 #puts "ADDITIVE <<< #{rest}"
454 while rest =~ /^\s*(\+| -)\s*/
458 n = [ :minus, n, [] ]
460 rest = MultiplicativeExpr( $', n[-1] )
463 if parsed.size == 0 and n.size != 0
471 #| MultiplicativeExpr ('*' | S ('div' | 'mod') S) UnaryExpr
473 def MultiplicativeExpr path, parsed
474 #puts "MULT >>> #{path}"
476 rest = UnaryExpr( path, n )
477 #puts "MULT <<< #{rest}"
479 while rest =~ /^\s*(\*| div | mod )\s*/
482 elsif $1.include?( "div" )
487 rest = UnaryExpr( $', n[-1] )
490 if parsed.size == 0 and n.size != 0
500 def UnaryExpr path, parsed
503 if $1 and (($1.size % 2) != 0)
508 parsed << :neg if mult < 0
510 #puts "UNARY >>> #{path}"
512 path = UnionExpr( path, n )
513 #puts "UNARY <<< #{path}"
518 #| UnionExpr '|' PathExpr
520 def UnionExpr path, parsed
521 #puts "UNION >>> #{path}"
523 rest = PathExpr( path, n )
524 #puts "UNION <<< #{rest}"
526 while rest =~ /^\s*(\|)\s*/
527 n = [ :union, n, [] ]
528 rest = PathExpr( $', n[-1] )
531 if parsed.size == 0 and n.size != 0
540 #| FilterExpr ('/' | '//') RelativeLocationPath
541 def PathExpr path, parsed
544 #puts "PATH >>> #{path}"
546 rest = FilterExpr( path, n )
547 #puts "PATH <<< '#{rest}'"
549 if rest and rest[0] == ?/
550 return RelativeLocationPath(rest, n)
553 #puts "BEFORE WITH '#{rest}'"
554 rest = LocationPath(rest, n) if rest =~ /\A[\/\.\@\[\w_*]/
559 #| FilterExpr Predicate
561 def FilterExpr path, parsed
562 #puts "FILTER >>> #{path}"
564 path = PrimaryExpr( path, n )
565 #puts "FILTER <<< #{path}"
566 path = Predicate(path, n) if path and path[0] == ?[
567 #puts "FILTER <<< #{path}"
572 #| VARIABLE_REFERENCE
577 VARIABLE_REFERENCE = /^\$(#{NAME_STR})/u
578 NUMBER = /^(\d*\.?\d+)/
579 NT = /^comment|text|processing-instruction|node$/
580 def PrimaryExpr path, parsed
583 when VARIABLE_REFERENCE
588 #arry << @variables[ varname ]
589 when /^(\w[-\w]*)(?:\()/
590 #puts "PrimaryExpr :: Function >>> #$1 -- '#$''"
593 #puts "#{fname} =~ #{NT.inspect}"
594 return path if fname =~ NT
598 path = FunctionCall(path, parsed)
600 #puts "LITERAL or NUMBER: #$1"
601 varname = $1.nil? ? $2 : $1
604 parsed << (varname.include?('.') ? varname.to_f : varname.to_i)
606 #puts "LITERAL or NUMBER: #$1"
607 varname = $1.nil? ? $2 : $1
612 path, contents = get_group(path)
613 contents = contents[1..-2]
615 OrExpr( contents, n )
621 #| FUNCTION_NAME '(' ( expr ( ',' expr )* )? ')'
622 def FunctionCall rest, parsed
623 path, arguments = parse_args(rest)
625 for argument in arguments
627 OrExpr( argument, args )
634 # get_group( '[foo]bar' ) -> ['bar', '[foo]']
639 en = (st == "(" ? ")" : "]")
648 end while depth > 0 and ind < string.length
649 return nil unless depth==0
650 [string[ind..-1], string[0..ind-1]]
653 def parse_args( string )
662 inquot = !inquot unless inapos
664 inapos = !inapos unless inquot
666 unless inquot or inapos
671 string = string[1..-1]
677 s = string[0,ind].strip
678 arguments << s unless s == ""
679 string = string[ind+1..-1]
683 s = string[0,ind].strip
684 arguments << s unless s == ""
685 string = string[ind+1..-1]
692 end while depth > 0 and ind < string.length
693 return nil unless depth==0