xml-simple 1.1.0 → 1.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/xmlsimple.rb +38 -38
- metadata +3 -3
data/lib/xmlsimple.rb
CHANGED
@@ -126,7 +126,7 @@ class XmlSimple
|
|
126
126
|
# defaults::
|
127
127
|
# Default values for options.
|
128
128
|
def initialize(defaults = nil)
|
129
|
-
unless defaults.nil? || defaults.
|
129
|
+
unless defaults.nil? || defaults.is_a?(Hash)
|
130
130
|
raise ArgumentError, "Options have to be a Hash."
|
131
131
|
end
|
132
132
|
@default_options = normalize_option_names(defaults, (KNOWN_OPTIONS['in'] + KNOWN_OPTIONS['out']).uniq)
|
@@ -159,7 +159,7 @@ class XmlSimple
|
|
159
159
|
@options['searchpath'].unshift(directory) unless directory.nil?
|
160
160
|
end
|
161
161
|
|
162
|
-
if string.
|
162
|
+
if string.is_a?(String)
|
163
163
|
if string =~ /<.*?>/m
|
164
164
|
@doc = parse(string)
|
165
165
|
elsif string == '-'
|
@@ -211,7 +211,7 @@ class XmlSimple
|
|
211
211
|
# Options to be used.
|
212
212
|
def xml_out(ref, options = nil)
|
213
213
|
handle_options('out', options)
|
214
|
-
if ref.
|
214
|
+
if ref.is_a?(Array)
|
215
215
|
ref = { @options['anonymoustag'] => ref }
|
216
216
|
end
|
217
217
|
|
@@ -222,7 +222,7 @@ class XmlSimple
|
|
222
222
|
@options['rootname'] = keys[0]
|
223
223
|
end
|
224
224
|
elsif @options['rootname'] == ''
|
225
|
-
if ref.
|
225
|
+
if ref.is_a?(Hash)
|
226
226
|
refsave = ref
|
227
227
|
ref = {}
|
228
228
|
refsave.each { |key, value|
|
@@ -318,7 +318,7 @@ class XmlSimple
|
|
318
318
|
def handle_options(direction, options)
|
319
319
|
@options = options || Hash.new
|
320
320
|
|
321
|
-
raise ArgumentError, "Options must be a Hash!" unless @options.
|
321
|
+
raise ArgumentError, "Options must be a Hash!" unless @options.is_a?(Hash)
|
322
322
|
|
323
323
|
unless KNOWN_OPTIONS.has_key?(direction)
|
324
324
|
raise ArgumentError, "Unknown direction: <#{direction}>."
|
@@ -368,7 +368,7 @@ class XmlSimple
|
|
368
368
|
@options['normalisespace'] = 0 if @options['normalisespace'].nil?
|
369
369
|
|
370
370
|
if @options.has_key?('searchpath')
|
371
|
-
unless @options['searchpath'].
|
371
|
+
unless @options['searchpath'].is_a?(Array)
|
372
372
|
@options['searchpath'] = [ @options['searchpath'] ]
|
373
373
|
end
|
374
374
|
else
|
@@ -393,13 +393,13 @@ class XmlSimple
|
|
393
393
|
if !scalar(@options['keyattr'])
|
394
394
|
# Convert keyattr => { elem => '+attr' }
|
395
395
|
# to keyattr => { elem => ['attr', '+'] }
|
396
|
-
if @options['keyattr'].
|
396
|
+
if @options['keyattr'].is_a?(Hash)
|
397
397
|
@options['keyattr'].each { |key, value|
|
398
398
|
if value =~ /^([-+])?(.*)$/
|
399
399
|
@options['keyattr'][key] = [$2, $1 ? $1 : '']
|
400
400
|
end
|
401
401
|
}
|
402
|
-
elsif !@options['keyattr'].
|
402
|
+
elsif !@options['keyattr'].is_a?(Array)
|
403
403
|
raise ArgumentError, "'keyattr' must be String, Hash, or Array!"
|
404
404
|
end
|
405
405
|
else
|
@@ -410,17 +410,17 @@ class XmlSimple
|
|
410
410
|
end
|
411
411
|
|
412
412
|
if @options.has_key?('forcearray')
|
413
|
-
if @options['forcearray'].
|
413
|
+
if @options['forcearray'].is_a?(Regexp)
|
414
414
|
@options['forcearray'] = [ @options['forcearray'] ]
|
415
415
|
end
|
416
416
|
|
417
|
-
if @options['forcearray'].
|
417
|
+
if @options['forcearray'].is_a?(Array)
|
418
418
|
force_list = @options['forcearray']
|
419
419
|
unless force_list.empty?
|
420
420
|
@options['forcearray'] = {}
|
421
421
|
force_list.each { |tag|
|
422
|
-
if tag.
|
423
|
-
unless @options['forcearray']['_regex'].
|
422
|
+
if tag.is_a?(Regexp)
|
423
|
+
unless @options['forcearray']['_regex'].is_a?(Array)
|
424
424
|
@options['forcearray']['_regex'] = []
|
425
425
|
end
|
426
426
|
@options['forcearray']['_regex'] << tag
|
@@ -438,11 +438,11 @@ class XmlSimple
|
|
438
438
|
@options['forcearray'] = DEF_FORCE_ARRAY
|
439
439
|
end
|
440
440
|
|
441
|
-
if @options.has_key?('grouptags') && !@options['grouptags'].
|
441
|
+
if @options.has_key?('grouptags') && !@options['grouptags'].is_a?(Hash)
|
442
442
|
raise ArgumentError, "Illegal value for 'GroupTags' option - expected a Hash."
|
443
443
|
end
|
444
444
|
|
445
|
-
if @options.has_key?('variables') && !@options['variables'].
|
445
|
+
if @options.has_key?('variables') && !@options['variables'].is_a?(Hash)
|
446
446
|
raise ArgumentError, "Illegal value for 'Variables' option - expected a Hash."
|
447
447
|
end
|
448
448
|
|
@@ -488,7 +488,7 @@ class XmlSimple
|
|
488
488
|
# Disintermediate grouped tags.
|
489
489
|
if @options.has_key?('grouptags')
|
490
490
|
result.each { |key, value|
|
491
|
-
next unless (value.
|
491
|
+
next unless (value.is_a?(Hash) && (value.size == 1))
|
492
492
|
child_key, child_value = value.to_a[0]
|
493
493
|
if @options['grouptags'][key] == child_key
|
494
494
|
result[key] = child_value
|
@@ -499,7 +499,7 @@ class XmlSimple
|
|
499
499
|
# Fold Hashes containing a single anonymous Array up into just the Array.
|
500
500
|
if count == 1
|
501
501
|
anonymoustag = @options['anonymoustag']
|
502
|
-
if result.has_key?(anonymoustag) && result[anonymoustag].
|
502
|
+
if result.has_key?(anonymoustag) && result[anonymoustag].is_a?(Array)
|
503
503
|
return result[anonymoustag]
|
504
504
|
end
|
505
505
|
end
|
@@ -543,10 +543,10 @@ class XmlSimple
|
|
543
543
|
def fold_arrays(hash)
|
544
544
|
fold_amount = 0
|
545
545
|
keyattr = @options['keyattr']
|
546
|
-
if (keyattr.
|
546
|
+
if (keyattr.is_a?(Array) || keyattr.is_a?(Hash))
|
547
547
|
hash.each { |key, value|
|
548
|
-
if value.
|
549
|
-
if keyattr.
|
548
|
+
if value.is_a?(Array)
|
549
|
+
if keyattr.is_a?(Array)
|
550
550
|
hash[key] = fold_array(value)
|
551
551
|
else
|
552
552
|
hash[key] = fold_array_by_name(key, value)
|
@@ -567,13 +567,13 @@ class XmlSimple
|
|
567
567
|
def fold_array(array)
|
568
568
|
hash = Hash.new
|
569
569
|
array.each { |x|
|
570
|
-
return array unless x.
|
570
|
+
return array unless x.is_a?(Hash)
|
571
571
|
key_matched = false
|
572
572
|
@options['keyattr'].each { |key|
|
573
573
|
if x.has_key?(key)
|
574
574
|
key_matched = true
|
575
575
|
value = x[key]
|
576
|
-
return array if value.
|
576
|
+
return array if value.is_a?(Hash) || value.is_a?(Array)
|
577
577
|
value = normalise_space(value) if @options['normalisespace'] == 1
|
578
578
|
x.delete(key)
|
579
579
|
hash[value] = x
|
@@ -600,9 +600,9 @@ class XmlSimple
|
|
600
600
|
|
601
601
|
hash = Hash.new
|
602
602
|
array.each { |x|
|
603
|
-
if x.
|
603
|
+
if x.is_a?(Hash) && x.has_key?(key)
|
604
604
|
value = x[key]
|
605
|
-
return array if value.
|
605
|
+
return array if value.is_a?(Hash) || value.is_a?(Array)
|
606
606
|
value = normalise_space(value) if @options['normalisespace'] == 1
|
607
607
|
hash[value] = x
|
608
608
|
hash[value]["-#{key}"] = hash[value][key] if flag == '-'
|
@@ -623,7 +623,7 @@ class XmlSimple
|
|
623
623
|
def collapse_content(hash)
|
624
624
|
content_key = @options['contentkey']
|
625
625
|
hash.each_value { |value|
|
626
|
-
return hash unless value.
|
626
|
+
return hash unless value.is_a?(Hash) && value.size == 1 && value.has_key?(content_key)
|
627
627
|
hash.each_key { |key| hash[key] = hash[key][content_key] }
|
628
628
|
}
|
629
629
|
hash
|
@@ -641,7 +641,7 @@ class XmlSimple
|
|
641
641
|
# value::
|
642
642
|
# Value to be associated with key.
|
643
643
|
def merge(hash, key, value)
|
644
|
-
if value.
|
644
|
+
if value.is_a?(String)
|
645
645
|
value = normalise_space(value) if @options['normalisespace'] == 2
|
646
646
|
|
647
647
|
if conv = @options['conversions'] and conv = conv.find {|c,_| c.match(key)} and conv = conv.at(1)
|
@@ -670,12 +670,12 @@ class XmlSimple
|
|
670
670
|
end
|
671
671
|
|
672
672
|
if hash.has_key?(key)
|
673
|
-
if hash[key].
|
673
|
+
if hash[key].is_a?(Array)
|
674
674
|
hash[key] << value
|
675
675
|
else
|
676
676
|
hash[key] = [ hash[key], value ]
|
677
677
|
end
|
678
|
-
elsif value.
|
678
|
+
elsif value.is_a?(Array) # Handle anonymous arrays.
|
679
679
|
hash[key] = [ value ]
|
680
680
|
else
|
681
681
|
if force_array?(key)
|
@@ -693,7 +693,7 @@ class XmlSimple
|
|
693
693
|
return false if key == @options['contentkey']
|
694
694
|
return true if @options['forcearray'] == true
|
695
695
|
forcearray = @options['forcearray']
|
696
|
-
if forcearray.
|
696
|
+
if forcearray.is_a?(Hash)
|
697
697
|
return true if forcearray.has_key?(key)
|
698
698
|
return false unless forcearray.has_key?('_regex')
|
699
699
|
forcearray['_regex'].each { |x| return true if key =~ x }
|
@@ -772,12 +772,12 @@ class XmlSimple
|
|
772
772
|
end
|
773
773
|
|
774
774
|
# Unfold hash to array if possible.
|
775
|
-
if ref.
|
775
|
+
if ref.is_a?(Hash) && !ref.empty? && !@options['keyattr'].empty? && indent != ''
|
776
776
|
ref = hash_to_array(name, ref)
|
777
777
|
end
|
778
778
|
|
779
779
|
result = []
|
780
|
-
if ref.
|
780
|
+
if ref.is_a?(Hash)
|
781
781
|
# Reintermediate grouped values if applicable.
|
782
782
|
if @options.has_key?('grouptags')
|
783
783
|
ref.each { |key, value|
|
@@ -844,13 +844,13 @@ class XmlSimple
|
|
844
844
|
else
|
845
845
|
result << ' />' << nl
|
846
846
|
end
|
847
|
-
elsif ref.
|
847
|
+
elsif ref.is_a?(Array)
|
848
848
|
ref.each { |value|
|
849
849
|
if scalar(value)
|
850
850
|
result << indent << '<' << name << '>'
|
851
851
|
result << (@options['noescape'] ? value.to_s : escape_value(value.to_s))
|
852
852
|
result << '</' << name << '>' << nl
|
853
|
-
elsif value.
|
853
|
+
elsif value.is_a?(Hash)
|
854
854
|
result << value_to_xml(value, name, indent)
|
855
855
|
else
|
856
856
|
result << indent << '<' << name << '>' << nl
|
@@ -872,7 +872,7 @@ class XmlSimple
|
|
872
872
|
# value::
|
873
873
|
# Value to be checked.
|
874
874
|
def scalar(value)
|
875
|
-
return false if value.
|
875
|
+
return false if value.is_a?(Hash) || value.is_a?(Array)
|
876
876
|
return true
|
877
877
|
end
|
878
878
|
|
@@ -887,9 +887,9 @@ class XmlSimple
|
|
887
887
|
def hash_to_array(parent, hashref)
|
888
888
|
arrayref = []
|
889
889
|
hashref.each { |key, value|
|
890
|
-
return hashref unless value.
|
890
|
+
return hashref unless value.is_a?(Hash)
|
891
891
|
|
892
|
-
if @options['keyattr'].
|
892
|
+
if @options['keyattr'].is_a?(Hash)
|
893
893
|
return hashref unless @options['keyattr'].has_key?(parent)
|
894
894
|
arrayref << { @options['keyattr'][parent][0] => key }.update(value)
|
895
895
|
else
|
@@ -941,11 +941,11 @@ class XmlSimple
|
|
941
941
|
# default::
|
942
942
|
# Value to be returned, if node could not be converted.
|
943
943
|
def node_to_text(node, default = nil)
|
944
|
-
if node.
|
944
|
+
if node.is_a?(REXML::Element)
|
945
945
|
node.texts.map { |t| t.value }.join('')
|
946
|
-
elsif node.
|
946
|
+
elsif node.is_a?(REXML::Attribute)
|
947
947
|
node.value.nil? ? default : node.value.strip
|
948
|
-
elsif node.
|
948
|
+
elsif node.is_a?(REXML::Text)
|
949
949
|
node.value.strip
|
950
950
|
else
|
951
951
|
default
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 1
|
7
7
|
- 1
|
8
|
-
-
|
9
|
-
version: 1.1.
|
8
|
+
- 1
|
9
|
+
version: 1.1.1
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Maik Schmidt
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2011-
|
17
|
+
date: 2011-10-11 00:00:00 +02:00
|
18
18
|
default_executable:
|
19
19
|
dependencies: []
|
20
20
|
|