Class: XmlSimple
- Inherits:
-
Object
- Object
- XmlSimple
- Includes:
- REXML
- Defined in:
- lib/action_controller/vendor/xml_simple.rb
Overview
Easy API to maintain XML (especially configuration files).
Defined Under Namespace
Classes: Cache
Constant Summary collapse
Class Method Summary collapse
-
.xml_in(string = nil, options = nil) ⇒ Object
This is the functional version of the instance method xml_in.
-
.xml_out(hash, options = nil) ⇒ Object
This is the functional version of the instance method xml_out.
Instance Method Summary collapse
-
#initialize(defaults = nil) ⇒ XmlSimple
constructor
Creates and intializes a new XmlSimple object.
-
#xml_in(string = nil, options = nil) ⇒ Object
Converts an XML document in the same way as the Perl module XML::Simple.
-
#xml_out(ref, options = nil) ⇒ Object
Converts a data structure into an XML document.
Constructor Details
#initialize(defaults = nil) ⇒ XmlSimple
Creates and intializes a new XmlSimple object.
- defaults
-
Default values for options.
127 128 129 130 131 132 133 134 |
# File 'lib/action_controller/vendor/xml_simple.rb', line 127 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']) @options = Hash.new @_var_values = nil end |
Class Method Details
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.
148 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 |
# File 'lib/action_controller/vendor/xml_simple.rb', line 148 def xml_in(string = nil, = nil) ('in', ) # 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) @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_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.
211 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 |
# File 'lib/action_controller/vendor/xml_simple.rb', line 211 def xml_out(ref, = nil) ('out', ) 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 |