| Class | EimXML::Element |
| In: |
lib/eim_xml.rb
|
| Parent: | Object |
| NEST | = | " " |
| attributes | [R] | |
| contents | [R] | |
| name | [R] |
# File lib/eim_xml.rb, line 65
65: def initialize(name, attributes={})
66: @name = name.to_sym
67: @attributes = Hash.new
68: @contents = Array.new
69:
70: attributes.each do |k, v|
71: @attributes[k.to_sym] = v
72: end
73:
74: yield(self) if block_given?
75: end
# File lib/eim_xml.rb, line 127
127: def ==(xml)
128: return false unless xml.is_a?(Element)
129: @name==xml.name && @attributes==xml.attributes && @contents==xml.contents
130: end
# File lib/eim_xml.rb, line 137
137: def [](key)
138: if key.is_a?(Fixnum)
139: @contents[key]
140: else
141: @attributes[key.to_sym]
142: end
143: end
# File lib/eim_xml.rb, line 82
82: def add(v)
83: case v
84: when nil
85: when Array
86: v.each{|i| self.add(i)}
87: else
88: @contents << v
89: end
90: self
91: end
# File lib/eim_xml.rb, line 132
132: def add_attribute(key, value)
133: @attributes[key.to_sym] = value
134: end
# File lib/eim_xml.rb, line 145
145: def del_attribute(key)
146: @attributes.delete(key.to_sym)
147: end
# File lib/eim_xml.rb, line 195
195: def find(obj, dst=Element.new(:found))
196: return find(Element.new(obj, dst)) if dst.is_a?(Hash)
197:
198: dst << self if match(obj)
199: @contents.each do |i|
200: case
201: when i.is_a?(Element)
202: i.find(obj, dst)
203: when obj.is_a?(Module) && i.is_a?(obj)
204: dst << i
205: end
206: end
207: dst
208: end
# File lib/eim_xml.rb, line 181
181: def has?(obj, attr=nil)
182: return has?(Element.new(obj, attr)) if attr
183:
184: @contents.any? do |i|
185: if i.is_a?(Element)
186: i.match(obj) || i.has?(obj)
187: else
188: obj.is_a?(Module) && i.is_a?(obj)
189: end
190: end
191: end
# File lib/eim_xml.rb, line 153
153: def match(obj, attr=nil)
154: return match(Element.new(obj, attr)) if attr
155: return obj=~@name.to_s if obj.is_a?(Regexp)
156: return @name==obj if obj.is_a?(Symbol)
157: return is_a?(obj) if obj.is_a?(Module)
158:
159: raise ArgumentError unless obj.is_a?(Element)
160:
161: return false unless @name==obj.name
162:
163: obj.attributes.all? do |k, v|
164: (v.nil? && !@attributes.include?(k)) ||
165: (@attributes.include?(k) && (v.is_a?(Regexp) ? v =~ @attributes[k] : PCString[v] == PCString[@attributes[k]]))
166: end and obj.contents.all? do |i|
167: case i
168: when Element
169: has_element?(i)
170: when String
171: pcstring_contents.include?(PCString.new(i))
172: when PCString
173: pcstring_contents.include?(i)
174: when Regexp
175: @contents.any?{|c| c.is_a?(String) and i=~c}
176: end
177: end
178: end
# File lib/eim_xml.rb, line 94
94: def name_and_attributes(out="")
95: out << "#{@name}"
96: @attributes.each do |k, v|
97: next unless v
98: out << " #{k}='#{PCString===v ? v : PCString.encode(v.to_s)}'"
99: end
100: end
# File lib/eim_xml.rb, line 149
149: def pcstring_contents
150: @contents.select{|c| c.is_a?(String)||c.is_a?(PCString)}.map{|c| c.is_a?(String) ? PCString.new(c) : c}
151: end
# File lib/eim_xml.rb, line 102
102: def write_to(out = "")
103: out << "<"
104: name_and_attributes(out)
105:
106: if @contents.empty?
107: out << " />"
108: else
109: out << ">"
110: @contents.each do |c|
111: case c
112: when Element
113: c.write_to(out)
114: when PCString
115: out << c.to_s
116: else
117: out << PCString.encode(c.to_s)
118: end
119: end
120: out << "</#{@name}>"
121: end
122: out
123: end