File: nodelist.rb

package info (click to toggle)
ruby-xmlparser 0.7.3-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 984 kB
  • sloc: ruby: 9,342; ansic: 2,017; makefile: 11
file content (231 lines) | stat: -rw-r--r-- 4,021 bytes parent folder | download | duplicates (10)
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
## -*- Ruby -*-
## XML::DOM
## 1998-2001 by yoshidam
##

module XML
  module DOM
=begin
== Class XML::DOM::NodeList


=end
    class NodeList

=begin
=== Class Methods

    --- NodeList.new(nodes = nil)

creates a new NodeList.
=end
      def initialize(nodes = nil)
        if nodes.nil?
          @nodes = []
        elsif nodes.is_a?(Array)
          @nodes = nodes
        else
          raise "parameter error"
        end
      end

=begin
=== Methods

    --- NodeList#item(index)

[DOM]
return the indexth item in the NodeList.
=end
      ## [DOM]
      def item(index)
        @nodes[index]
      end

=begin
    --- NodeList#size()

return size of NodeList.
=end
      def size
        @nodes.length
      end
      alias length size

=begin
    --- NodeList#[](index)

return indexth node of the NodeList.
=end
      def [](index)
        @nodes[index]
      end

=begin
    --- NodeList#[]=(*p)

set node of indexth node of the NodeList.
=end
      def []=(*p)
        if p.length == 2
          @nodes[p[0]] = p[1]
        elsif p.length == 3
          @nodes[p[0], p[1]] = p[2]
        end
      end

=begin
    --- NodeList#each

iterates over each node of the NodeList.
=end
      def each
        @nodes.each do |value|
          yield(value)
        end
      end

=begin
    --- NodeList#reversible_each(reverse = false)

iterates over each node of the reversed NodeList.
=end
      ## [Masaki Fukushima]
      def reversible_each(reverse = false)
        if !reverse
          @nodes.each do |value|
            yield(value)
          end
        else
          @nodes.reverse_each do |value|
            yield(value)
          end
        end
      end

=begin
    --- NodeList#push(*nodes)

adds nodes into the NodeList.
=end
      def push(*nodes)
        nodes.each do |node|
          if node.is_a?(Array)
            self.push(*node)
          elsif node.is_a?(NodeList)
            @nodes.concat(node.to_a)
          elsif node.is_a?(Node)
            @nodes << node
          else
            raise "parameter error"
          end
        end
        self
      end

=begin
    --- NodeList#concat(*nodes)

alias of NodeList#push.
=end
      alias concat push

=begin
    --- NodeList#pop

pops and returns the last node of the NodeList.
=end
      def pop
        @nodes.pop
      end

=begin
    --- NodeList#shift

removes and returns the first node of the NodeList.
=end
      def shift
        @nodes.shift
      end

=begin
    --- NodeList#to_s

returns the string representation of the NodeList.
=end
      def to_s
        @nodes.to_s
      end

=begin
    --- NodeList#reverse

returns the reversed NodeList.
=end
      def reverse
        @nodes.reverse
      end

=begin
    --- NodeList#to_a

converts the NodeList into an array.
=end
      def to_a
        @nodes
      end

=begin
    --- NodeList#+(nodes)

return the newly created concatenated NodeList.
=end
      def +(nodes)
        if nodes.nil?
          NodeList.new(@nodes)
        elsif nodes.is_a?(Array)
          NodeList.new(@nodes + nodes)
        elsif nodes.is_a?(NodeList)
          NodeList.new(@nodes + nodes.to_a)
        elsif nodes.is_a?(Node)
          NodeList.new(@nodes + [nodes])
        else
          raise "parameter error"
        end
      end

=begin
    --- NodeList#<<(nodes)

appends nodes to the NodeList.
=end
      ## modified by Masaki Fukushima
      def <<(nodes)
        if nodes.nil?
          ## no change
        elsif nodes.is_a?(Array)
          @nodes.concat(nodes)
        elsif nodes.is_a?(NodeList)
          @nodes.concat(nodes.to_a)
        elsif nodes.is_a?(Node)
          @nodes << nodes
        else
          raise "parameter error"
        end
        self
      end

      ## get nodeValues by names
      ##   names ::= name ('|' name)*
      def _getValues(names)
        ret = []
        names.split('|').each do |name|
          if !@nodes[name].nil?
            ret.push(@nodes[name].nodeValue)
          end
        end
        ret
      end
    end
  end
end