Class: DBus::IntrospectXMLParser
- Inherits:
-
Object
- Object
- DBus::IntrospectXMLParser
- Defined in:
- lib/dbus/introspect.rb
Overview
D-Bus introspect XML parser class
This class parses introspection XML of an object and constructs a tree of Node, Interface, Method, Signal instances.
Instance Method Summary collapse
-
#initialize(xml) ⇒ IntrospectXMLParser
constructor
Creates a new parser for XML data in string xml.
-
#parse ⇒ Object
make it easy to switch.
-
#parse_hpricot ⇒ Object
Parses the XML, constructing the tree, using hpricot.
-
#parse_rexml ⇒ Object
Parses the XML, constructing the tree, using rexml.
-
#parse_subnodes ⇒ Object
Recursively parses the subnodes, constructing the tree.
Constructor Details
#initialize(xml) ⇒ IntrospectXMLParser
Creates a new parser for XML data in string xml.
201 202 203 204 |
# File 'lib/dbus/introspect.rb', line 201 def initialize(xml) @xml = xml @hpricot = false end |
Instance Method Details
#parse ⇒ Object
make it easy to switch
225 226 227 228 229 |
# File 'lib/dbus/introspect.rb', line 225 def parse ret = parse_rexml unless @hpricot ret = parse_hpricot if @hpricot return ret end |
#parse_hpricot ⇒ Object
Parses the XML, constructing the tree, using hpricot
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/dbus/introspect.rb', line 232 def parse_hpricot ret = Array.new subnodes = Array.new d = Hpricot.XML(@xml) (d/"node/node").each { |e| subnodes << e.attributes['name'] } (d/"node/interface").each do |e| i = Interface.new(e.attributes['name']) (e/"method").each do |me| m = Method.new(me.attributes['name']) parse_methsig_hpricot(me, m) i << m end (e/"signal").each do |se| s = Signal.new(se.attributes['name']) parse_methsig_hpricot(se, s) i << s end ret << i end [ret, subnodes] end |
#parse_rexml ⇒ Object
Parses the XML, constructing the tree, using rexml
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 |
# File 'lib/dbus/introspect.rb', line 255 def parse_rexml ret = Array.new subnodes = Array.new t = Time.now d = REXML::Document.new(@xml) d.elements.each("node/node") do |e| subnodes << e.attributes["name"] end d.elements.each("node/interface") do |e| i = Interface.new(e.attributes["name"]) e.elements.each("method") do |me| m = Method.new(me.attributes["name"]) parse_methsig_rexml(me, m) i << m end e.elements.each("signal") do |se| s = Signal.new(se.attributes["name"]) parse_methsig_rexml(se, s) i << s end ret << i end d = Time.now - t if d > 2 wlog "Some XML took more that two secs to parse. Optimize me! (Hpricot?)" end [ret, subnodes] end |
#parse_subnodes ⇒ Object
Recursively parses the subnodes, constructing the tree.
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/dbus/introspect.rb', line 207 def parse_subnodes subnodes = Array.new t = Time.now if @hpricot d = Hpricot.XML(@xml) (d/"node/node").each { |e| subnodes << e.attributes['name'] } return subnodes end unless @hpricot d = REXML::Document.new(@xml) d.elements.each("node/node") do |e| subnodes << e.attributes["name"] end return subnodes end end |