lib/ruote/reader/xml.rb in ruote-2.2.0 vs lib/ruote/reader/xml.rb in ruote-2.3.0
- old
+ new
@@ -1,7 +1,7 @@
#--
-# Copyright (c) 2005-2011, John Mettraux, jmettraux@gmail.com
+# Copyright (c) 2005-2012, John Mettraux, jmettraux@gmail.com
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
@@ -31,25 +31,29 @@
#
# Turns an XML string into a process definition tree.
#
module XmlReader
+ # Returns true if the string seems to be an XML string.
#
+ def self.understands?(s)
+
+ !! s.strip.match(/<.+>/)
+ end
+
+ #
# A helper class to store the temporary tree while it gets read.
#
class Node
attr_reader :parent, :attributes, :children
- def initialize(parent, name, attributes)
+ def initialize(parent, name, atts)
@parent = parent
@name = name
- @attributes = attributes.inject({}) { |h, (k, v)|
- h[k.gsub(/-/, '_')] = v
- h
- }
+ @attributes = atts.remap { |(k, v), h| h[k.gsub(/-/, '_')] = v }
@children = []
parent.children << self if parent
end
@@ -57,13 +61,12 @@
[ @name, @attributes, @children.collect { |c| c.to_a } ]
end
end
- #
# Parses the XML string into a process definition tree (array of arrays).
#
- def self.read(s)
+ def self.read(s, opt=nil)
parser = REXML::Parsers::SAX2Parser.new(s)
root = nil
current = nil