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

+ (Object) read(s, opt = nil)

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



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

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

+ (Boolean) understands?(s)

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



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

def self.understands?(s)

  !! s.strip.match(/<.+>/)
end