Class: REXML::Parsers::XPathParser

Inherits:
Object
  • Object
show all
Includes:
XMLTokens
Defined in:
lib/rexml/parsers/xpathparser.rb

Overview

You don’t want to use this class. Really. Use XPath, which is a wrapper for this class. Believe me. You don’t want to poke around in here. There is strange, dark magic at work in this code. Beware. Go back! Go back while you still can!

Constant Summary collapse

LITERAL =
/^'([^']*)'|^"([^"]*)"/u

Constants included from XMLTokens

XMLTokens::NAME, XMLTokens::NAMECHAR, XMLTokens::NAME_CHAR, XMLTokens::NAME_START_CHAR, XMLTokens::NAME_STR, XMLTokens::NCNAME_STR, XMLTokens::NMTOKEN, XMLTokens::NMTOKENS, XMLTokens::REFERENCE

Instance Method Summary collapse

Instance Method Details

#abbreviate(path) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/rexml/parsers/xpathparser.rb', line 35

def abbreviate( path )
  path = path.kind_of?(String) ? parse( path ) : path
  string = ""
  document = false
  while path.size > 0
    op = path.shift
    case op
    when :node
    when :attribute
      string << "/" if string.size > 0
      string << "@"
    when :child
      string << "/" if string.size > 0
    when :descendant_or_self
      string << "/"
    when :self
      string << "."
    when :parent
      string << ".."
    when :any
      string << "*"
    when :text
      string << "text()"
    when :following, :following_sibling,
          :ancestor, :ancestor_or_self, :descendant,
          :namespace, :preceding, :preceding_sibling
      string << "/" unless string.size == 0
      string << op.to_s.tr("_", "-")
      string << "::"
    when :qname
      prefix = path.shift
      name = path.shift
      string << prefix+":" if prefix.size > 0
      string << name
    when :predicate
      string << '['
      string << predicate_to_string( path.shift ) {|x| abbreviate( x ) }
      string << ']'
    when :document
      document = true
    when :function
      string << path.shift
      string << "( "
      string << predicate_to_string( path.shift[0] ) {|x| abbreviate( x )}
      string << " )"
    when :literal
      string << %Q{ "#{path.shift}" }
    else
      string << "/" unless string.size == 0
      string << "UNKNOWN("
      string << op.inspect
      string << ")"
    end
  end
  string = "/"+string if document
  return string
end

#expand(path) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/rexml/parsers/xpathparser.rb', line 93

def expand( path )
  path = path.kind_of?(String) ? parse( path ) : path
  string = ""
  document = false
  while path.size > 0
    op = path.shift
    case op
    when :node
      string << "node()"
    when :attribute, :child, :following, :following_sibling,
          :ancestor, :ancestor_or_self, :descendant, :descendant_or_self,
          :namespace, :preceding, :preceding_sibling, :self, :parent
      string << "/" unless string.size == 0
      string << op.to_s.tr("_", "-")
      string << "::"
    when :any
      string << "*"
    when :qname
      prefix = path.shift
      name = path.shift
      string << prefix+":" if prefix.size > 0
      string << name
    when :predicate
      string << '['
      string << predicate_to_string( path.shift ) { |x| expand(x) }
      string << ']'
    when :document
      document = true
    else
      string << "/" unless string.size == 0
      string << "UNKNOWN("
      string << op.inspect
      string << ")"
    end
  end
  string = "/"+string if document
  return string
end

#namespaces=(namespaces) ⇒ Object



15
16
17
18
# File 'lib/rexml/parsers/xpathparser.rb', line 15

def namespaces=( namespaces )
  Functions::namespace_context = namespaces
  @namespaces = namespaces
end

#parse(path) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/rexml/parsers/xpathparser.rb', line 20

def parse path
  path = path.dup
  path.gsub!(/([\(\[])\s+/, '\1') # Strip ignorable spaces
  path.gsub!( /\s+([\]\)])/, '\1')
  parsed = []
  OrExpr(path, parsed)
  parsed
end

#predicate(path) ⇒ Object



29
30
31
32
33
# File 'lib/rexml/parsers/xpathparser.rb', line 29

def predicate path
  parsed = []
  Predicate( "[#{path}]", parsed )
  parsed
end

#predicate_to_string(path, &block) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/rexml/parsers/xpathparser.rb', line 132

def predicate_to_string( path, &block )
  string = ""
  case path[0]
  when :and, :or, :mult, :plus, :minus, :neq, :eq, :lt, :gt, :lteq, :gteq, :div, :mod, :union
    op = path.shift
    case op
    when :eq
      op = "="
    when :lt
      op = "<"
    when :gt
      op = ">"
    when :lteq
      op = "<="
    when :gteq
      op = ">="
    when :neq
      op = "!="
    when :union
      op = "|"
    end
    left = predicate_to_string( path.shift, &block )
    right = predicate_to_string( path.shift, &block )
    string << " "
    string << left
    string << " "
    string << op.to_s
    string << " "
    string << right
    string << " "
  when :function
    path.shift
    name = path.shift
    string << name
    string << "( "
    string << predicate_to_string( path.shift, &block )
    string << " )"
  when :literal
    path.shift
    string << " "
    string << path.shift.inspect
    string << " "
  else
    string << " "
    string << yield( path )
    string << " "
  end
  return string.squeeze(" ")
end