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
|
## -*- Ruby -*-
## XML::DOM::Visitor
## 1998 by yoshidam
##
## Oct 23, 1998 yoshidam Fix each
##
=begin
= XML::DOM::Visitor
== Module XML
=end
module XML
=begin
== Module XML::DOM (XML::SimpleTree)
=end
module DOM
=begin
== Class XML::DOM::Visitor
Skelton class of Visitor.
You can override the following methods and implement the other
"visit_TYPE" methods.
You should implement some "visit_name_NAME" methods and
"method_missing" method for accept_name.
=end
## Skeleton visitor
class Visitor
## You can override the following methods and implement the other
## "visit_TYPE" methods.
## You should implement some "visit_name_NAME" methods and
## "method_missing" method for accept_name.
=begin
=== Methods
--- Visitor#visit_Document(grove, *rest)
callback method.
=end
def visit_Document(grove, *rest)
grove.children_accept(self, *rest)
end
=begin
--- Visitor#visit_Element(element, *rest)
callback method.
=end
def visit_Element(element, *rest)
element.children_accept(self, *rest)
end
=begin
--- Visitor#visit_Text(text, *rest)
callback method.
=end
def visit_Text(text, *rest)
end
=begin
--- Visitor#visit_CDATASection(text, *rest)
callback method.
=end
def visit_CDATASection(text, *rest)
end
=begin
--- Visitor#visit_Comment(comment, *rest)
callback method.
=end
def visit_Comment(comment, *rest)
end
=begin
--- Visitor#visit_ProcessingInstruction(pi, *rest)
callback method.
=end
def visit_ProcessingInstruction(pi, *rest)
end
end
=begin
== Class XML::DOM::Node
XML::Grove::Visitor like interfaces.
=end
class Node
=begin
--- Node#accept(visitor, *rest)
call back visit_* method.
=end
## XML::Grove::Visitor like interfaces
def accept(visitor, *rest)
typename = self.class.to_s.sub(/.*?([^:]+)$/, '\1')
visitor.send("visit_" + typename, self, *rest)
end
=begin
--- Node#accept_name(visitor, *rest)
call back visit_name_* method.
=end
def accept_name(visitor, *rest)
if nodeType == ELEMENT_NODE
name_method = "visit_name_" + nodeName
visitor.send(name_method, self, *rest)
else
self.accept(visitor, *rest)
end
end
=begin
--- Node#children_accept(visitor, *rest)
for each children, call back visit_* methods.
=end
def children_accept(visitor, *rest)
ret = []
@children && @children.each { |node|
ret.push(node.accept(visitor, *rest))
}
ret
end
=begin
--- Node#children_accept_name(visitor, *rest)
for each children, call back visit_name_* method.
=end
def children_accept_name(visitor, *rest)
ret = []
@children && @children.each { |node|
ret.push(node.accept_name(visitor, *rest))
}
ret
end
=begin
--- Node#each
iterator interface.
=end
## Iterator interface
include Enumerable
def each
sibstack = []
siblings = [ self ]
while true
if siblings.length == 0
break if sibstack.length == 0
siblings = sibstack.pop
next
end
node = siblings.shift
yield(node)
children = node.childNodes
if !children.nil?
sibstack.push(siblings)
siblings = children.to_a.dup
end
end
end
end
end
end
|