File: xpath.rb

package info (click to toggle)
ruby-xmlparser 0.7.3-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 980 kB
  • sloc: ruby: 9,342; ansic: 2,017; makefile: 11
file content (398 lines) | stat: -rw-r--r-- 7,340 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
#
# xpath-dom.rb
#
#   Copyright (C) Ueno Katsuhiro 2000
#   DOM2 support by yoshidam
#
# $Id: xpath.rb,v 1.2 2003/03/12 06:38:28 yoshidam Exp $
#

require 'xml/dom2/core'
require 'xml/xpath'

module XMLScan
  XPath = ::XPath unless
    defined?(::XMLScan::XPath)

module XPath

  module DOM

    class AbstractNodeAdapter < NullNodeAdapter

      def wrap(node, visitor)
        @node = node
        self
      end

      attr_reader :node

      def root
        @node.ownerDocument
      end

      def parent
        @node.parentNode
      end

      def children
        @node.childNodes.to_a
      end

      def each_following_siblings
        node = @node
        yield node while node = node.nextSibling
      end

      def each_preceding_siblings
        node = @node
        yield node while node = node.previousSibling
      end

      def index
        @node.parentNode.childNodes.to_a.index(@node)
      end

      def lang
        node = @node
        lang = nil
        until a = node.attributes and lang = a.getNamedItem('xml:lang')
          node = node.parentNode
        end
        lang and lang.nodeValue
      end

    end


    class TextNodeAdapter < AbstractNodeAdapter

      def node_type
        :text
      end

      def string_value
        @node.nodeValue
      end

    end


    class CommentNodeAdapter < TextNodeAdapter

      def node_type
        :comment
      end

    end


    class PINodeAdapter < AbstractNodeAdapter

      def node_type
        :processing_instruction
      end

      def name_localpart
        @node.nodeName
      end

      def string_value
        @node.nodeValue
      end

    end


    class ParentNodeAdapter < AbstractNodeAdapter

      def string_value
        dst = ''
        stack = @node.childNodes.to_a.reverse
        while node = stack.pop
          s = node.nodeValue
          dst << s if s
          stack.concat node.childNodes.to_a.reverse
        end
        dst
      end

    end


    class RootNodeAdapter < ParentNodeAdapter

      def node_type
        :root
      end

      alias root node

      def index
        0
      end

    end


    class ElementNodeAdapter < ParentNodeAdapter

      def wrap(node, visitor)
        @node = node
        @visitor = visitor
        self
      end

      def node_type
        :element
      end

      def name_localpart
        @node.nodeName
      end

      def namespace_uri
        @node.namespaceURI
      end

      def qualified_name
        @node.nodeName
      end

      def attributes
        map = @node.attributes
        attrs = @visitor.get_attributes(@node)
        unless attrs then
          attrs = []
          map.length.times { |i| attrs.push map.item(i) }
          @visitor.regist_attributes @node, attrs
        end
        attrs
      end

    end


    class AttrNodeAdapter < AbstractNodeAdapter

      def wrap(node, visitor)
        @node = node
        @visitor = visitor
        self
      end

      def node_type
        :attribute
      end

      def name_localpart
        @node.nodeName
      end

      def namespace_uri
        @node.namespaceURI
      end

      def qualified_name
        @node.nodeName
      end

      def parent
        @visitor.get_attr_parent @node
      end

      def index
        -@visitor.get_attributes(parent).index(@node)
      end

      def string_value
        @node.nodeValue
      end

    end



    class NodeVisitor

      def initialize
        @adapters = Array.new(12, NullNodeAdapter.new)
        @adapters[XML::DOM::Node::ELEMENT_NODE] = ElementNodeAdapter.new
        @adapters[XML::DOM::Node::ATTRIBUTE_NODE] = AttrNodeAdapter.new
        @adapters[XML::DOM::Node::TEXT_NODE] =
          @adapters[XML::DOM::Node::CDATA_SECTION_NODE] = TextNodeAdapter.new
        @adapters[XML::DOM::Node::PROCESSING_INSTRUCTION_NODE] =
          PINodeAdapter.new
        @adapters[XML::DOM::Node::COMMENT_NODE] = CommentNodeAdapter.new
        @adapters[XML::DOM::Node::DOCUMENT_NODE] = RootNodeAdapter.new
        @attr = {}
      end

      def visit(node)
        @adapters[node.nodeType].wrap(node, self)
      end

      def regist_attributes(node, attrs)
        @attr[node] = attrs
        attrs.each { |i| @attr[i] = node }
      end

      def get_attributes(node)
        @attr[node]
      end

      def get_attr_parent(node)
        @attr[node]
      end

    end



    class Context < XMLScan::XPath::Context

      def initialize(node, namespace = nil, variable = nil)
        super node, namespace, variable, NodeVisitor.new
      end

    end


  end

end ## module XPath
end ## module XMLScan



module XML

  module DOM

    class Node

      def __collectDescendatNS(ns = {})
        childNodes.each do |node|
          next if node.nodeType != ELEMENT_NODE
          prefix = node.prefix
          uri = node.namespaceURI
          ns[prefix] = uri unless ns.has_key?(prefix)
          node.__collectDescendatNS(ns)
        end
      end

      def __collectAncestorNS(ns = {})
        node = self
        while node
          prefix = node.prefix
          uri = node.namespaceURI
          ns[prefix] = uri unless ns.has_key?(prefix)
          node = node.parentNode
        end
      end

      def getNodesByXPath(xpath, ns = {})
        xpath = XMLScan::XPath.compile(xpath) unless xpath.is_a? XMLScan::XPath
        if ns.length == 0
          ## collect namespaces
          __collectAncestorNS(ns)
          __collectDescendatNS(ns)
        end
        ret = xpath.call(XPath::DOM::Context.new(self, ns))
        raise "return value is not NodeSet" unless ret.is_a? Array
        ret
      end

      def _getMyLocationInXPath(parent)
        n = parent.childNodes.index(self)
        "node()[#{n + 1}]"
      end

      def makeXPath
        dst = []
        node = self
        while parent = node.parentNode
          dst.push node._getMyLocationInXPath(parent)
          node = parent
        end
        dst.reverse!
        '/' + dst.join('/')
      end

    end


    class Element

      def _getMyLocationInXPath(parent)
        name = nodeName
        n = parent.childNodes.to_a.select { |i|
          i.nodeType == ELEMENT_NODE and i.nodeName == name
        }.index(self)
        "#{name}[#{n + 1}]"
      end

    end


    class Text

      def _getMyLocationInXPath(parent)
        n = parent.childNodes.to_a.select { |i|
          i.nodeType == TEXT_NODE or i.nodeType == CDATA_SECTION_NODE
        }.index(self)
        "text()[#{n + 1}]"
      end

    end


    class CDATASection

      def _getMyLocationInXPath(parent)
        n = parent.childNodes.to_a.select { |i|
          i.nodeType == TEXT_NODE or i.nodeType == CDATA_SECTION_NODE
        }.index(self)
        "text()[#{n + 1}]"
      end

    end


    class Comment

      def _getMyLocationInXPath(parent)
        n = parent.childNodes.to_a.select { |i|
          i.nodeType == COMMENT_NODE
        }.index(self)
        "comment()[#{n + 1}]"
      end

    end


    class ProcessingInstruction

      def _getMyLocationInXPath(parent)
        n = parent.childNodes.to_a.select { |i|
          i.nodeType == PROCESSING_INSTRUCTION_NODE
        }.index(self)
        "processing-instruction()[#{n + 1}]"
      end

    end


    class Attr

      def makeXPath
        '@' + nodeName
      end

    end


  end

end