Module: Ruote::XmlReader

Defined in:
lib/ruote/reader/xml.rb

Overview

Turns an XML string into a process definition tree.

Defined Under Namespace

Classes: Node

Class Method Summary collapse

Class Method Details

.read(s, opt = nil) ⇒ Object

Parses the XML string into a process definition tree (array of arrays).



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
# File 'lib/ruote/reader/xml.rb', line 71

def self.read(s, opt=nil)

  parser = REXML::Parsers::SAX2Parser.new(s)

  root = nil
  current = nil

  # u, l, q, a <=> url, local, qname, attributes

  parser.listen(:start_element) do |u, l, q, a|
    current = Node.new(current, l.gsub(/-/, '_'), a)
    root ||= current
  end
  parser.listen(:end_element) do |u, l, q, a|
    current = current.parent
  end

  parser.listen(:characters) do |text|
    t = text.strip
    current.attributes[t] = nil if t.size > 0
  end

  parser.parse

  root.to_a
end

.understands?(s) ⇒ Boolean

Returns true if the string seems to be an XML string.

Returns:

  • (Boolean)


38
39
40
41
# File 'lib/ruote/reader/xml.rb', line 38

def self.understands?(s)

  s.match(/<[a-z]+>/) != nil
end