~ubuntu-branches/ubuntu/saucy/ruby-xmlparser/saucy

« back to all changes in this revision

Viewing changes to lib/xml/dom/visitor.rb

  • Committer: Bazaar Package Importer
  • Author(s): Lucas Nussbaum
  • Date: 2011-06-16 21:57:51 UTC
  • Revision ID: james.westby@ubuntu.com-20110616215751-qi0xn6fsoy0yqv1y
Tags: upstream-0.7.2
ImportĀ upstreamĀ versionĀ 0.7.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
## -*- Ruby -*-
 
2
## XML::DOM::Visitor
 
3
## 1998 by yoshidam
 
4
##
 
5
## Oct 23, 1998 yoshidam Fix each
 
6
##
 
7
 
 
8
 
 
9
=begin
 
10
= XML::DOM::Visitor
 
11
 
 
12
== Module XML
 
13
 
 
14
=end
 
15
module XML
 
16
 
 
17
=begin
 
18
== Module XML::DOM (XML::SimpleTree)
 
19
 
 
20
=end
 
21
  module DOM
 
22
 
 
23
=begin
 
24
== Class XML::DOM::Visitor
 
25
 
 
26
Skelton class of Visitor.
 
27
 
 
28
You can override the following methods and implement the other
 
29
"visit_TYPE" methods.
 
30
 
 
31
You should implement some "visit_name_NAME" methods and
 
32
"method_missing" method for accept_name.
 
33
 
 
34
=end
 
35
    ## Skeleton visitor
 
36
    class Visitor
 
37
      ## You can override the following methods and implement the other
 
38
      ## "visit_TYPE" methods.
 
39
      ## You should implement some "visit_name_NAME" methods and
 
40
      ## "method_missing" method for accept_name.
 
41
 
 
42
=begin
 
43
=== Methods
 
44
 
 
45
    --- Visitor#visit_Document(grove, *rest)
 
46
 
 
47
callback method.
 
48
=end
 
49
      def visit_Document(grove, *rest)
 
50
        grove.children_accept(self, *rest)
 
51
      end
 
52
 
 
53
=begin
 
54
    --- Visitor#visit_Element(element, *rest)
 
55
 
 
56
callback method.
 
57
=end
 
58
      def visit_Element(element, *rest)
 
59
        element.children_accept(self, *rest)
 
60
      end
 
61
 
 
62
=begin
 
63
    --- Visitor#visit_Text(text, *rest)
 
64
 
 
65
callback method.
 
66
=end
 
67
      def visit_Text(text, *rest)
 
68
      end
 
69
 
 
70
=begin
 
71
    --- Visitor#visit_CDATASection(text, *rest)
 
72
 
 
73
callback method.
 
74
=end
 
75
      def visit_CDATASection(text, *rest)
 
76
      end
 
77
 
 
78
=begin
 
79
    --- Visitor#visit_Comment(comment, *rest)
 
80
 
 
81
callback method.
 
82
=end
 
83
      def visit_Comment(comment, *rest)
 
84
      end
 
85
 
 
86
=begin
 
87
    --- Visitor#visit_ProcessingInstruction(pi, *rest)
 
88
 
 
89
callback method.
 
90
=end
 
91
      def visit_ProcessingInstruction(pi, *rest)
 
92
      end
 
93
 
 
94
    end
 
95
 
 
96
=begin
 
97
 
 
98
== Class XML::DOM::Node
 
99
 
 
100
XML::Grove::Visitor like interfaces.
 
101
=end
 
102
    class Node
 
103
 
 
104
=begin
 
105
    --- Node#accept(visitor, *rest)
 
106
 
 
107
call back visit_* method.
 
108
=end
 
109
      ## XML::Grove::Visitor like interfaces
 
110
      def accept(visitor, *rest)
 
111
        typename = self.class.to_s.sub(/.*?([^:]+)$/, '\1')
 
112
        visitor.send("visit_" + typename, self, *rest)
 
113
      end
 
114
 
 
115
=begin
 
116
    --- Node#accept_name(visitor, *rest)
 
117
 
 
118
call back visit_name_* method.
 
119
=end
 
120
      def accept_name(visitor, *rest)
 
121
        if nodeType == ELEMENT_NODE
 
122
          name_method = "visit_name_" + nodeName
 
123
          visitor.send(name_method, self, *rest)
 
124
        else
 
125
          self.accept(visitor, *rest)
 
126
        end
 
127
      end
 
128
 
 
129
=begin
 
130
    --- Node#children_accept(visitor, *rest)
 
131
 
 
132
for each children, call back visit_* methods.
 
133
=end
 
134
      def children_accept(visitor, *rest)
 
135
        ret = []
 
136
        @children && @children.each { |node|
 
137
          ret.push(node.accept(visitor, *rest))
 
138
        }
 
139
        ret
 
140
      end
 
141
 
 
142
=begin
 
143
    --- Node#children_accept_name(visitor, *rest)
 
144
 
 
145
for each children, call back visit_name_* method.
 
146
=end
 
147
      def children_accept_name(visitor, *rest)
 
148
        ret = []
 
149
        @children && @children.each { |node|
 
150
          ret.push(node.accept_name(visitor, *rest))
 
151
        }
 
152
        ret
 
153
      end
 
154
 
 
155
=begin
 
156
    --- Node#each
 
157
 
 
158
iterator interface.
 
159
=end
 
160
      ## Iterator interface
 
161
      include Enumerable
 
162
      def each
 
163
        sibstack = []
 
164
        siblings = [ self ]
 
165
        while true
 
166
          if siblings.length == 0
 
167
            break if sibstack.length == 0
 
168
            siblings = sibstack.pop
 
169
            next
 
170
          end
 
171
          node = siblings.shift
 
172
          yield(node)
 
173
          children = node.childNodes
 
174
          if !children.nil?
 
175
            sibstack.push(siblings)
 
176
            siblings = children.to_a.dup
 
177
          end
 
178
        end
 
179
      end
 
180
    end
 
181
  end
 
182
end