Class: XmlSimple

Inherits:
Object show all
Includes:
REXML
Defined in:
lib/gems/activesupport-2.2.2/lib/active_support/core_ext/hash/conversions.rb,
lib/gems/activesupport-2.2.2/lib/active_support/vendor/xml-simple-1.0.11/xmlsimple.rb

Overview

Easy API to maintain XML (especially configuration files).

Defined Under Namespace

Classes: Cache

Constant Summary collapse

@@VERSION =
'1.0.11'
@@cache =

Create a “global” cache.

Cache.new

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(defaults = nil) ⇒ XmlSimple

Creates and initializes a new XmlSimple object.

defaults

Default values for options.



128
129
130
131
132
133
134
135
# File 'lib/gems/activesupport-2.2.2/lib/active_support/vendor/xml-simple-1.0.11/xmlsimple.rb', line 128

def initialize(defaults = nil)
  unless defaults.nil? || defaults.instance_of?(Hash)
    raise ArgumentError, "Options have to be a Hash."
  end
  @default_options = normalize_option_names(defaults, (KNOWN_OPTIONS['in'] + KNOWN_OPTIONS['out']).uniq)
  @options = Hash.new
  @_var_values = nil
end

Class Method Details

.xml_in(string = nil, options = nil) ⇒ Object

This is the functional version of the instance method xml_in.



201
202
203
204
# File 'lib/gems/activesupport-2.2.2/lib/active_support/vendor/xml-simple-1.0.11/xmlsimple.rb', line 201

def XmlSimple.xml_in(string = nil, options = nil)
  xml_simple = XmlSimple.new
  xml_simple.xml_in(string, options)
end

.xml_in_string(string, options = nil) ⇒ Object



22
23
24
# File 'lib/gems/activesupport-2.2.2/lib/active_support/core_ext/hash/conversions.rb', line 22

def self.xml_in_string(string, options = nil)
  new.xml_in_string(string, options)
end

.xml_out(hash, options = nil) ⇒ Object

This is the functional version of the instance method xml_out.



257
258
259
260
# File 'lib/gems/activesupport-2.2.2/lib/active_support/vendor/xml-simple-1.0.11/xmlsimple.rb', line 257

def XmlSimple.xml_out(hash, options = nil)
  xml_simple = XmlSimple.new
  xml_simple.xml_out(hash, options)
end

Instance Method Details

#xml_in(string = nil, options = nil) ⇒ Object

Converts an XML document in the same way as the Perl module XML::Simple.

string

XML source. Could be one of the following:

  • nil: Tries to load and parse ‘<scriptname>.xml’.

  • filename: Tries to load and parse filename.

  • IO object: Reads from object until EOF is detected and parses result.

  • XML string: Parses string.

options

Options to be used.



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/gems/activesupport-2.2.2/lib/active_support/vendor/xml-simple-1.0.11/xmlsimple.rb', line 149

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.sub!(/\.[^.]+$/, '')
    string += '.xml'

    directory = File::dirname($0)
    @options['searchpath'].unshift(directory) unless directory.nil?
  end

  if string.instance_of?(String)
    if string =~ /<.*?>/m
      @doc = parse(string)
    elsif string == '-'
      @doc = parse($stdin.readlines.to_s)
    else
      filename = find_xml_file(string, @options['searchpath'])

      if @options.has_key?('cache')
        @options['cache'].each { |scheme|
          case(scheme)
          when 'storable'
            content = @@cache.restore_storable(filename)
          when 'mem_share'
            content = @@cache.restore_mem_share(filename)
          when 'mem_copy'
            content = @@cache.restore_mem_copy(filename)
          else
            raise ArgumentError, "Unsupported caching scheme: <#{scheme}>."
          end
          return content if content
        }
      end
      
      @doc = load_xml_file(filename)
    end
  elsif string.kind_of?(IO) || string.kind_of?(StringIO)
    @doc = parse(string.readlines.to_s)
  else
    raise ArgumentError, "Could not parse object of type: <#{string.type}>."
  end

  result = collapse(@doc.root)
  result = @options['keeproot'] ? merge({}, @doc.root.name, result) : result
  put_into_cache(result, filename)
  result
end

#xml_in_string(string, options = nil) ⇒ Object

Same as xml_in but doesn’t try to smartly shoot itself in the foot.



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/gems/activesupport-2.2.2/lib/active_support/core_ext/hash/conversions.rb', line 9

def xml_in_string(string, options = nil)
  handle_options('in', options)

  @doc = parse(string)
  result = collapse(@doc.root)

  if @options['keeproot']
    merge({}, @doc.root.name, result)
  else
    result
  end
end

#xml_out(ref, options = nil) ⇒ Object

Converts a data structure into an XML document.

ref

Reference to data structure to be converted into XML.

options

Options to be used.



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/gems/activesupport-2.2.2/lib/active_support/vendor/xml-simple-1.0.11/xmlsimple.rb', line 212

def xml_out(ref, options = nil)
  handle_options('out', options)
  if ref.instance_of?(Array)
    ref = { @options['anonymoustag'] => ref }
  end

  if @options['keeproot']
    keys = ref.keys
    if keys.size == 1
      ref = ref[keys[0]]
      @options['rootname'] = keys[0]
    end
  elsif @options['rootname'] == ''
    if ref.instance_of?(Hash)
      refsave = ref
      ref = {}
      refsave.each { |key, value|
        if !scalar(value)
          ref[key] = value
        else
          ref[key] = [ value.to_s ]
        end
      }
    end
  end

  @ancestors = []
  xml = value_to_xml(ref, @options['rootname'], '')
  @ancestors = nil

  if @options['xmldeclaration']
    xml = @options['xmldeclaration'] + "\n" + xml
  end

  if @options.has_key?('outputfile')
    if @options['outputfile'].kind_of?(IO)
      return @options['outputfile'].write(xml)
    else
      File.open(@options['outputfile'], "w") { |file| file.write(xml) }
    end
  end
  xml
end