lib/xmlsimple.rb in xml-simple-1.0.11 vs lib/xmlsimple.rb in xml-simple-1.0.12

- old
+ new

@@ -1,19 +1,19 @@ # = XmlSimple # # Author:: Maik Schmidt <contact@maik-schmidt.de> -# Copyright:: Copyright (c) 2003-2006 Maik Schmidt +# Copyright:: Copyright (c) 2003-2009 Maik Schmidt # License:: Distributes under the same terms as Ruby. # require 'rexml/document' require 'stringio' # Easy API to maintain XML (especially configuration files). class XmlSimple include REXML - @@VERSION = '1.0.11' + @@VERSION = '1.0.12' # A simple cache for XML documents that were already transformed # by xml_in. class Cache # Creates and initializes a new Cache object. @@ -149,11 +149,11 @@ def xml_in(string = nil, options = nil) handle_options('in', options) # If no XML string or filename was supplied look for scriptname.xml. if string.nil? - string = File::basename($0) + string = File::basename($0).dup string.sub!(/\.[^.]+$/, '') string += '.xml' directory = File::dirname($0) @options['searchpath'].unshift(directory) unless directory.nil? @@ -161,11 +161,11 @@ if string.instance_of?(String) if string =~ /<.*?>/m @doc = parse(string) elsif string == '-' - @doc = parse($stdin.readlines.to_s) + @doc = parse($stdin.read) else filename = find_xml_file(string, @options['searchpath']) if @options.has_key?('cache') @options['cache'].each { |scheme| @@ -183,12 +183,12 @@ } end @doc = load_xml_file(filename) end - elsif string.kind_of?(IO) || string.kind_of?(StringIO) - @doc = parse(string.readlines.to_s) + elsif string.kind_of?(IO) || string.kind_of?(StringIO) || string.kind_of?(Zlib::GzipReader) + @doc = parse(string.read) else raise ArgumentError, "Could not parse object of type: <#{string.type}>." end result = collapse(@doc.root) @@ -265,16 +265,16 @@ KNOWN_OPTIONS = { 'in' => %w( keyattr keeproot forcecontent contentkey noattr searchpath forcearray suppressempty anonymoustag cache grouptags normalisespace normalizespace - variables varattr keytosymbol + variables varattr keytosymbol attrprefix ), 'out' => %w( keyattr keeproot contentkey noattr rootname xmldeclaration outputfile noescape suppressempty - anonymoustag indent grouptags noindent + anonymoustag indent grouptags noindent attrprefix ) } # Define some reasonable defaults. DEF_KEY_ATTRIBUTES = [] @@ -495,11 +495,11 @@ result[key] = child_value end } end - # Fold Hases containing a single anonymous Array up into just the Array. + # Fold Hashes containing a single anonymous Array up into just the Array. if count == 1 anonymoustag = @options['anonymoustag'] if result.has_key?(anonymoustag) && result[anonymoustag].instance_of?(Array) return result[anonymoustag] end @@ -703,11 +703,15 @@ # # node:: # Document node to extract attributes from. def get_attributes(node) attributes = {} - node.attributes.each { |n,v| attributes[n] = v } + if @options['attrprefix'] + node.attributes.each { |n,v| attributes["@" + n] = v } + else + node.attributes.each { |n,v| attributes[n] = v } + end attributes end # Determines, if a document element has mixed content. # @@ -794,19 +798,22 @@ raise ArgumentError, "Use of uninitialized value!" end value = {} end - if !scalar(value) || @options['noattr'] + # Check for the '@' attribute prefix to allow separation of attributes and elements + if @options['noattr'] || + (@options['attrprefix'] && !(key =~ /^@(.*)/)) || + !scalar(value) nested << value_to_xml(value, key, indent + @options['indent']) else value = value.to_s value = escape_value(value) unless @options['noescape'] if key == @options['contentkey'] text_content = value else - result << ' ' << key << '="' << value << '"' + result << ' ' << ($1||key) << '="' << value << '"' end end } else text_content = '' @@ -989,10 +996,10 @@ # Errno::ENOENT:: # If the specified file does not exist. # REXML::ParseException:: # If the specified file is not wellformed. def load_xml_file(filename) - parse(File.readlines(filename).to_s) + parse(IO::read(filename)) end # Caches the data belonging to a certain file. # # data::