xml-simple 1.1.2 → 1.1.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/xmlsimple.rb +48 -20
- metadata +8 -11
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 0740fe90e1641ef7fbbbbf60cb1c1e6ae5795f1be7460d957de7d47bdbd20bfc
|
4
|
+
data.tar.gz: 9c5f0471d8cd779ed728827493cff987b9a878afb9fc6182e36cafe67541285f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 225b8fbe390650a575c1bbae0e14ba66b08aeaf07cd50547e5950b875e66773df31f9747981625cfdf12dce8b74f403aae2a112dab03d5d33bca73ca18feb2c4
|
7
|
+
data.tar.gz: fb90812c2c2a455920dbb85adb555ec95f3c1dcfb7221d7fbc6288064658e20d5c43d9dda0c9c17e88813943837033fd085e928f94bca57668e2e7e9651535a6
|
data/lib/xmlsimple.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# = XmlSimple
|
2
2
|
#
|
3
3
|
# Author:: Maik Schmidt <contact@maik-schmidt.de>
|
4
|
-
# Copyright:: Copyright (c) 2003-
|
4
|
+
# Copyright:: Copyright (c) 2003-2020 Maik Schmidt
|
5
5
|
# License:: Distributes under the same terms as Ruby.
|
6
6
|
#
|
7
7
|
require 'rexml/document'
|
@@ -11,7 +11,7 @@ require 'stringio'
|
|
11
11
|
class XmlSimple
|
12
12
|
include REXML
|
13
13
|
|
14
|
-
@@VERSION = '1.1.
|
14
|
+
@@VERSION = '1.1.8'
|
15
15
|
|
16
16
|
# A simple cache for XML documents that were already transformed
|
17
17
|
# by xml_in.
|
@@ -121,7 +121,7 @@ class XmlSimple
|
|
121
121
|
# Create a "global" cache.
|
122
122
|
@@cache = Cache.new
|
123
123
|
|
124
|
-
# Creates and
|
124
|
+
# Creates and initializes a new XmlSimple object.
|
125
125
|
#
|
126
126
|
# defaults::
|
127
127
|
# Default values for options.
|
@@ -264,15 +264,15 @@ class XmlSimple
|
|
264
264
|
# Declare options that are valid for xml_in and xml_out.
|
265
265
|
KNOWN_OPTIONS = {
|
266
266
|
'in' => %w(
|
267
|
-
keyattr keeproot forcecontent contentkey noattr
|
268
|
-
|
269
|
-
|
270
|
-
|
267
|
+
keyattr keeproot forcecontent contentkey noattr searchpath
|
268
|
+
forcearray suppressempty anonymoustag cache grouptags
|
269
|
+
normalisespace normalizespace variables varattr keytosymbol
|
270
|
+
attrtosymbol attrprefix conversions kebabtosnakecase
|
271
271
|
),
|
272
272
|
'out' => %w(
|
273
273
|
keyattr keeproot contentkey noattr rootname
|
274
274
|
xmldeclaration outputfile noescape suppressempty
|
275
|
-
anonymoustag indent grouptags noindent attrprefix
|
275
|
+
anonymoustag indent grouptags noindent attrprefix selfclose
|
276
276
|
)
|
277
277
|
}
|
278
278
|
|
@@ -285,11 +285,13 @@ class XmlSimple
|
|
285
285
|
DEF_FORCE_ARRAY = true
|
286
286
|
DEF_INDENTATION = ' '
|
287
287
|
DEF_KEY_TO_SYMBOL = false
|
288
|
+
DEF_ATTR_TO_SYMBOL = false
|
289
|
+
DEF_KEBAB_TO_SNAKE = false
|
288
290
|
|
289
291
|
# Normalizes option names in a hash, i.e., turns all
|
290
292
|
# characters to lower case and removes all underscores.
|
291
|
-
# Additionally, this method checks
|
292
|
-
# was used and raises an according exception.
|
293
|
+
# Additionally, this method checks if an unknown option
|
294
|
+
# was used, and raises an according exception.
|
293
295
|
#
|
294
296
|
# options::
|
295
297
|
# Hash to be normalized.
|
@@ -301,7 +303,7 @@ class XmlSimple
|
|
301
303
|
options.each { |key, value|
|
302
304
|
lkey = key.to_s.downcase.gsub(/_/, '')
|
303
305
|
if !known_options.member?(lkey)
|
304
|
-
raise ArgumentError, "
|
306
|
+
raise ArgumentError, "Unrecognized option: #{lkey}."
|
305
307
|
end
|
306
308
|
result[lkey] = value
|
307
309
|
}
|
@@ -353,6 +355,8 @@ class XmlSimple
|
|
353
355
|
|
354
356
|
@options['keytosymbol'] = DEF_KEY_TO_SYMBOL unless @options.has_key?('keytosymbol')
|
355
357
|
|
358
|
+
@options['attrtosymbol'] = DEF_ATTR_TO_SYMBOL unless @options.has_key?('attrtosymbol')
|
359
|
+
|
356
360
|
if @options.has_key?('contentkey')
|
357
361
|
if @options['contentkey'] =~ /^-(.*)$/
|
358
362
|
@options['contentkey'] = $1
|
@@ -451,6 +455,8 @@ class XmlSimple
|
|
451
455
|
elsif @options.has_key?('varattr')
|
452
456
|
@_var_values = {}
|
453
457
|
end
|
458
|
+
|
459
|
+
@options['kebabtosnakecase'] = DEF_KEBAB_TO_SNAKE unless @options.has_key?('kebabtosnakecase')
|
454
460
|
end
|
455
461
|
|
456
462
|
# Actually converts an XML document element into a data structure.
|
@@ -479,7 +485,7 @@ class XmlSimple
|
|
479
485
|
result[@options['contentkey']] = content
|
480
486
|
end
|
481
487
|
elsif element.has_text? # i.e. it has only text.
|
482
|
-
return collapse_text_node(result, element)
|
488
|
+
return collapse_text_node(result, element) # calls merge, which converts
|
483
489
|
end
|
484
490
|
|
485
491
|
# Turn Arrays into Hashes if key fields present.
|
@@ -488,8 +494,11 @@ class XmlSimple
|
|
488
494
|
# Disintermediate grouped tags.
|
489
495
|
if @options.has_key?('grouptags')
|
490
496
|
result.each { |key, value|
|
497
|
+
# In results, key should already be converted
|
498
|
+
raise("Unconverted key '#{key}' found. Should be '#{kebab_to_snake_case key}'.") if (key != kebab_to_snake_case(key))
|
491
499
|
next unless (value.is_a?(Hash) && (value.size == 1))
|
492
500
|
child_key, child_value = value.to_a[0]
|
501
|
+
child_key = kebab_to_snake_case child_key # todo test whether necessary
|
493
502
|
if @options['grouptags'][key] == child_key
|
494
503
|
result[key] = child_value
|
495
504
|
end
|
@@ -545,6 +554,7 @@ class XmlSimple
|
|
545
554
|
keyattr = @options['keyattr']
|
546
555
|
if (keyattr.is_a?(Array) || keyattr.is_a?(Hash))
|
547
556
|
hash.each { |key, value|
|
557
|
+
key = kebab_to_snake_case key
|
548
558
|
if value.is_a?(Array)
|
549
559
|
if keyattr.is_a?(Array)
|
550
560
|
hash[key] = fold_array(value)
|
@@ -641,6 +651,7 @@ class XmlSimple
|
|
641
651
|
# value::
|
642
652
|
# Value to be associated with key.
|
643
653
|
def merge(hash, key, value)
|
654
|
+
key = kebab_to_snake_case key
|
644
655
|
if value.is_a?(String)
|
645
656
|
value = normalise_space(value) if @options['normalisespace'] == 2
|
646
657
|
|
@@ -655,7 +666,7 @@ class XmlSimple
|
|
655
666
|
|
656
667
|
# look for variable definitions
|
657
668
|
if @options.has_key?('varattr')
|
658
|
-
varattr = @options['varattr']
|
669
|
+
varattr = kebab_to_snake_case @options['varattr']
|
659
670
|
if hash.has_key?(varattr)
|
660
671
|
set_var(hash[varattr], value)
|
661
672
|
end
|
@@ -709,10 +720,14 @@ class XmlSimple
|
|
709
720
|
def get_attributes(node)
|
710
721
|
attributes = {}
|
711
722
|
if @options['attrprefix']
|
712
|
-
node.attributes.each { |n,v| attributes["@" + n] = v }
|
723
|
+
node.attributes.each { |n,v| attributes["@" + kebab_to_snake_case(n)] = v }
|
724
|
+
elsif @options.has_key?('attrtosymbol') and @options['attrtosymbol'] == true
|
725
|
+
#patch for converting attribute names to symbols
|
726
|
+
node.attributes.each { |n,v| attributes[kebab_to_snake_case(n).to_sym] = v }
|
713
727
|
else
|
714
|
-
node.attributes.each { |n,v| attributes[n] = v }
|
728
|
+
node.attributes.each { |n,v| attributes[kebab_to_snake_case(n)] = v }
|
715
729
|
end
|
730
|
+
|
716
731
|
attributes
|
717
732
|
end
|
718
733
|
|
@@ -721,10 +736,9 @@ class XmlSimple
|
|
721
736
|
# element::
|
722
737
|
# Document element to be checked.
|
723
738
|
def has_mixed_content?(element)
|
724
|
-
|
725
|
-
|
726
|
-
|
727
|
-
false
|
739
|
+
element.has_text? &&
|
740
|
+
element.has_elements? &&
|
741
|
+
!element.texts.join('').strip.empty?
|
728
742
|
end
|
729
743
|
|
730
744
|
# Called when a variable definition is encountered in the XML.
|
@@ -821,7 +835,7 @@ class XmlSimple
|
|
821
835
|
end
|
822
836
|
end
|
823
837
|
}
|
824
|
-
|
838
|
+
elsif !@options['selfclose']
|
825
839
|
text_content = ''
|
826
840
|
end
|
827
841
|
|
@@ -1029,6 +1043,20 @@ class XmlSimple
|
|
1029
1043
|
}
|
1030
1044
|
end
|
1031
1045
|
end
|
1046
|
+
|
1047
|
+
# Substitutes underscores for hyphens if the KebabToSnakeCase option is selected. For when you don't
|
1048
|
+
# want to refer to keys by hash[:'my-key'] but instead as hash[:my_key]
|
1049
|
+
#
|
1050
|
+
# key::
|
1051
|
+
# Key to be converted.
|
1052
|
+
def kebab_to_snake_case(key)
|
1053
|
+
return key unless (@options['kebabtosnakecase'])
|
1054
|
+
|
1055
|
+
is_symbol = key.is_a? Symbol
|
1056
|
+
key = key.to_s.gsub(/-/, '_')
|
1057
|
+
key = key.to_sym if is_symbol
|
1058
|
+
key
|
1059
|
+
end
|
1032
1060
|
end
|
1033
1061
|
|
1034
1062
|
# vim:sw=2
|
metadata
CHANGED
@@ -1,15 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: xml-simple
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
5
|
-
prerelease:
|
4
|
+
version: 1.1.8
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Maik Schmidt
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2020-12-22 00:00:00.000000000 Z
|
13
12
|
dependencies: []
|
14
13
|
description:
|
15
14
|
email: contact@maik-schmidt.de
|
@@ -20,27 +19,25 @@ files:
|
|
20
19
|
- lib/xmlsimple.rb
|
21
20
|
homepage: https://github.com/maik/xml-simple
|
22
21
|
licenses:
|
23
|
-
-
|
22
|
+
- MIT
|
23
|
+
metadata: {}
|
24
24
|
post_install_message:
|
25
25
|
rdoc_options: []
|
26
26
|
require_paths:
|
27
27
|
- lib
|
28
28
|
required_ruby_version: !ruby/object:Gem::Requirement
|
29
|
-
none: false
|
30
29
|
requirements:
|
31
|
-
- -
|
30
|
+
- - ">="
|
32
31
|
- !ruby/object:Gem::Version
|
33
32
|
version: '0'
|
34
33
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
35
|
-
none: false
|
36
34
|
requirements:
|
37
|
-
- -
|
35
|
+
- - ">="
|
38
36
|
- !ruby/object:Gem::Version
|
39
37
|
version: '0'
|
40
38
|
requirements: []
|
41
|
-
|
42
|
-
rubygems_version: 1.8.24
|
39
|
+
rubygems_version: 3.1.2
|
43
40
|
signing_key:
|
44
|
-
specification_version:
|
41
|
+
specification_version: 4
|
45
42
|
summary: A simple API for XML processing.
|
46
43
|
test_files: []
|