xml-simple 1.0.16 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/xmlsimple.rb +42 -38
  2. metadata +3 -6
@@ -11,7 +11,7 @@ require 'stringio'
11
11
  class XmlSimple
12
12
  include REXML
13
13
 
14
- @@VERSION = '1.0.15'
14
+ @@VERSION = '1.1.0'
15
15
 
16
16
  # A simple cache for XML documents that were already transformed
17
17
  # by xml_in.
@@ -23,7 +23,7 @@ class XmlSimple
23
23
  end
24
24
 
25
25
  # Saves a data structure into a file.
26
- #
26
+ #
27
27
  # data::
28
28
  # Data structure to be saved.
29
29
  # filename::
@@ -122,7 +122,7 @@ class XmlSimple
122
122
  @@cache = Cache.new
123
123
 
124
124
  # Creates and intializes a new XmlSimple object.
125
- #
125
+ #
126
126
  # defaults::
127
127
  # Default values for options.
128
128
  def initialize(defaults = nil)
@@ -143,7 +143,7 @@ class XmlSimple
143
143
  # - filename: Tries to load and parse filename.
144
144
  # - IO object: Reads from object until EOF is detected and parses result.
145
145
  # - XML string: Parses string.
146
- #
146
+ #
147
147
  # options::
148
148
  # Options to be used.
149
149
  def xml_in(string = nil, options = nil)
@@ -182,7 +182,7 @@ class XmlSimple
182
182
  return content if content
183
183
  }
184
184
  end
185
-
185
+
186
186
  @doc = load_xml_file(filename)
187
187
  end
188
188
  elsif string.kind_of?(IO) || string.kind_of?(StringIO) || string.kind_of?(Zlib::GzipReader)
@@ -202,7 +202,7 @@ class XmlSimple
202
202
  xml_simple = XmlSimple.new
203
203
  xml_simple.xml_in(string, options)
204
204
  end
205
-
205
+
206
206
  # Converts a data structure into an XML document.
207
207
  #
208
208
  # ref::
@@ -258,7 +258,7 @@ class XmlSimple
258
258
  xml_simple = XmlSimple.new
259
259
  xml_simple.xml_out(hash, options)
260
260
  end
261
-
261
+
262
262
  private
263
263
 
264
264
  # Declare options that are valid for xml_in and xml_out.
@@ -267,7 +267,7 @@ class XmlSimple
267
267
  keyattr keeproot forcecontent contentkey noattr
268
268
  searchpath forcearray suppressempty anonymoustag
269
269
  cache grouptags normalisespace normalizespace
270
- variables varattr keytosymbol attrprefix
270
+ variables varattr keytosymbol attrprefix conversions
271
271
  ),
272
272
  'out' => %w(
273
273
  keyattr keeproot contentkey noattr rootname
@@ -285,7 +285,7 @@ class XmlSimple
285
285
  DEF_FORCE_ARRAY = true
286
286
  DEF_INDENTATION = ' '
287
287
  DEF_KEY_TO_SYMBOL = false
288
-
288
+
289
289
  # Normalizes option names in a hash, i.e., turns all
290
290
  # characters to lower case and removes all underscores.
291
291
  # Additionally, this method checks, if an unknown option
@@ -307,9 +307,9 @@ class XmlSimple
307
307
  }
308
308
  result
309
309
  end
310
-
310
+
311
311
  # Merges a set of options with the default options.
312
- #
312
+ #
313
313
  # direction::
314
314
  # 'in': If options should be handled for xml_in.
315
315
  # 'out': If options should be handled for xml_out.
@@ -495,9 +495,9 @@ class XmlSimple
495
495
  end
496
496
  }
497
497
  end
498
-
498
+
499
499
  # Fold Hashes containing a single anonymous Array up into just the Array.
500
- if count == 1
500
+ if count == 1
501
501
  anonymoustag = @options['anonymoustag']
502
502
  if result.has_key?(anonymoustag) && result[anonymoustag].instance_of?(Array)
503
503
  return result[anonymoustag]
@@ -537,7 +537,7 @@ class XmlSimple
537
537
  end
538
538
 
539
539
  # Folds all arrays in a Hash.
540
- #
540
+ #
541
541
  # hash::
542
542
  # Hash to be folded.
543
543
  def fold_arrays(hash)
@@ -585,7 +585,7 @@ class XmlSimple
585
585
  hash = collapse_content(hash) if @options['collapseagain']
586
586
  hash
587
587
  end
588
-
588
+
589
589
  # Folds an Array to a Hash, if possible. Folding happens
590
590
  # according to the content of keyattr, which has to be
591
591
  # a Hash.
@@ -628,7 +628,7 @@ class XmlSimple
628
628
  }
629
629
  hash
630
630
  end
631
-
631
+
632
632
  # Adds a new key/value pair to an existing Hash. If the key to be added
633
633
  # does already exist and the existing value associated with key is not
634
634
  # an Array, it will be converted into an Array. Then the new value is
@@ -644,11 +644,15 @@ class XmlSimple
644
644
  if value.instance_of?(String)
645
645
  value = normalise_space(value) if @options['normalisespace'] == 2
646
646
 
647
+ if conv = @options['conversions'] and conv = conv.find {|c,_| c.match(key)} and conv = conv.at(1)
648
+ value = conv.call(value)
649
+ end
650
+
647
651
  # do variable substitutions
648
652
  unless @_var_values.nil? || @_var_values.empty?
649
653
  value.gsub!(/\$\{(\w+)\}/) { |x| get_var($1) }
650
654
  end
651
-
655
+
652
656
  # look for variable definitions
653
657
  if @options.has_key?('varattr')
654
658
  varattr = @options['varattr']
@@ -657,14 +661,14 @@ class XmlSimple
657
661
  end
658
662
  end
659
663
  end
660
-
664
+
661
665
  #patch for converting keys to symbols
662
666
  if @options.has_key?('keytosymbol')
663
667
  if @options['keytosymbol'] == true
664
668
  key = key.to_s.downcase.to_sym
665
669
  end
666
670
  end
667
-
671
+
668
672
  if hash.has_key?(key)
669
673
  if hash[key].instance_of?(Array)
670
674
  hash[key] << value
@@ -682,7 +686,7 @@ class XmlSimple
682
686
  end
683
687
  hash
684
688
  end
685
-
689
+
686
690
  # Checks, if the 'forcearray' option has to be used for
687
691
  # a certain key.
688
692
  def force_array?(key)
@@ -690,13 +694,13 @@ class XmlSimple
690
694
  return true if @options['forcearray'] == true
691
695
  forcearray = @options['forcearray']
692
696
  if forcearray.instance_of?(Hash)
693
- return true if forcearray.has_key?(key)
697
+ return true if forcearray.has_key?(key)
694
698
  return false unless forcearray.has_key?('_regex')
695
699
  forcearray['_regex'].each { |x| return true if key =~ x }
696
700
  end
697
701
  return false
698
702
  end
699
-
703
+
700
704
  # Converts the attributes array of a document node into a Hash.
701
705
  # Returns an empty Hash, if node has no attributes.
702
706
  #
@@ -711,7 +715,7 @@ class XmlSimple
711
715
  end
712
716
  attributes
713
717
  end
714
-
718
+
715
719
  # Determines, if a document element has mixed content.
716
720
  #
717
721
  # element::
@@ -722,7 +726,7 @@ class XmlSimple
722
726
  end
723
727
  false
724
728
  end
725
-
729
+
726
730
  # Called when a variable definition is encountered in the XML.
727
731
  # A variable definition looks like
728
732
  # <element attrname="name">value</element>
@@ -740,7 +744,7 @@ class XmlSimple
740
744
  return "${#{name}}"
741
745
  end
742
746
  end
743
-
747
+
744
748
  # Recurses through a data structure building up and returning an
745
749
  # XML representation of that structure as a string.
746
750
  #
@@ -782,7 +786,7 @@ class XmlSimple
782
786
  end
783
787
  }
784
788
  end
785
-
789
+
786
790
  nested = []
787
791
  text_content = nil
788
792
  if named
@@ -800,7 +804,7 @@ class XmlSimple
800
804
  end
801
805
 
802
806
  # Check for the '@' attribute prefix to allow separation of attributes and elements
803
-
807
+
804
808
  if (@options['noattr'] ||
805
809
  (@options['attrprefix'] && !(key =~ /^@(.*)/)) ||
806
810
  !scalar(value)
@@ -861,10 +865,10 @@ class XmlSimple
861
865
  @ancestors.pop if !scalar(ref)
862
866
  result.join('')
863
867
  end
864
-
868
+
865
869
  # Checks, if a certain value is a "scalar" value. Whatever
866
870
  # that will be in Ruby ... ;-)
867
- #
871
+ #
868
872
  # value::
869
873
  # Value to be checked.
870
874
  def scalar(value)
@@ -875,9 +879,9 @@ class XmlSimple
875
879
  # Attempts to unfold a hash of hashes into an array of hashes. Returns
876
880
  # a reference to th array on success or the original hash, if unfolding
877
881
  # is not possible.
878
- #
882
+ #
879
883
  # parent::
880
- #
884
+ #
881
885
  # hashref::
882
886
  # Reference to the hash to be unfolded.
883
887
  def hash_to_array(parent, hashref)
@@ -894,7 +898,7 @@ class XmlSimple
894
898
  }
895
899
  arrayref
896
900
  end
897
-
901
+
898
902
  # Replaces XML markup characters by their external entities.
899
903
  #
900
904
  # data::
@@ -902,7 +906,7 @@ class XmlSimple
902
906
  def escape_value(data)
903
907
  Text::normalize(data)
904
908
  end
905
-
909
+
906
910
  # Removes leading and trailing whitespace and sequences of
907
911
  # whitespaces from a string.
908
912
  #
@@ -927,7 +931,7 @@ class XmlSimple
927
931
  return value.nil?
928
932
  end
929
933
  end
930
-
934
+
931
935
  # Converts a document node into a String.
932
936
  # If the node could not be converted into a String
933
937
  # for any reason, default will be returned.
@@ -937,7 +941,7 @@ class XmlSimple
937
941
  # default::
938
942
  # Value to be returned, if node could not be converted.
939
943
  def node_to_text(node, default = nil)
940
- if node.instance_of?(REXML::Element)
944
+ if node.instance_of?(REXML::Element)
941
945
  node.texts.map { |t| t.value }.join('')
942
946
  elsif node.instance_of?(REXML::Attribute)
943
947
  node.value.nil? ? default : node.value.strip
@@ -960,7 +964,7 @@ class XmlSimple
960
964
  def parse(xml_string)
961
965
  Document.new(xml_string)
962
966
  end
963
-
967
+
964
968
  # Searches in a list of paths for a certain file. Returns
965
969
  # the full path to the file, if it could be found. Otherwise,
966
970
  # an exception will be raised.
@@ -987,14 +991,14 @@ class XmlSimple
987
991
  end
988
992
  raise ArgumentError, "Could not find <#{filename}> in <#{searchpath.join(':')}>"
989
993
  end
990
-
994
+
991
995
  # Loads and parses an XML configuration file.
992
996
  #
993
997
  # filename::
994
998
  # Name of the configuration file to be loaded.
995
999
  #
996
1000
  # The following exceptions may be raised:
997
- #
1001
+ #
998
1002
  # Errno::ENOENT::
999
1003
  # If the specified file does not exist.
1000
1004
  # REXML::ParseException::
metadata CHANGED
@@ -1,13 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: xml-simple
3
3
  version: !ruby/object:Gem::Version
4
- hash: 55
5
4
  prerelease: false
6
5
  segments:
7
6
  - 1
7
+ - 1
8
8
  - 0
9
- - 16
10
- version: 1.0.16
9
+ version: 1.1.0
11
10
  platform: ruby
12
11
  authors:
13
12
  - Maik Schmidt
@@ -15,7 +14,7 @@ autorequire:
15
14
  bindir: bin
16
15
  cert_chain: []
17
16
 
18
- date: 2011-05-29 00:00:00 +02:00
17
+ date: 2011-06-18 00:00:00 +02:00
19
18
  default_executable:
20
19
  dependencies: []
21
20
 
@@ -43,7 +42,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
43
42
  requirements:
44
43
  - - ">="
45
44
  - !ruby/object:Gem::Version
46
- hash: 3
47
45
  segments:
48
46
  - 0
49
47
  version: "0"
@@ -52,7 +50,6 @@ required_rubygems_version: !ruby/object:Gem::Requirement
52
50
  requirements:
53
51
  - - ">="
54
52
  - !ruby/object:Gem::Version
55
- hash: 3
56
53
  segments:
57
54
  - 0
58
55
  version: "0"