jordi-xml-object 0.9.6 → 0.9.7

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -11,6 +11,13 @@ is using XML for its intended purpose, but cumbersome when one always sends
11
11
  the same XML structure, and always process all of it in the same way. This
12
12
  one aims to be a bit different.
13
13
 
14
+ At the moment, I aim for compatibility with Ruby 1.8, 1.9, and JRuby 1.1.
15
+
16
+ == Dependencies
17
+
18
+ None outside of Ruby, though some optional gems add additional features. See
19
+ below on "Adapters" and "Collection pluralization".
20
+
14
21
  == Installation instructions
15
22
 
16
23
  gem install xml-object
@@ -86,6 +93,19 @@ ambiguity.
86
93
 
87
94
  == Features & Problems
88
95
 
96
+ === Adapters
97
+
98
+ XMLObject supports different adapters to do the actual XML parsing. It ships
99
+ with +REXML+, +Hpricot+, +JREXML+, and +LibXML+ adapters. By default, the
100
+ +REXML+ adapter is used.
101
+
102
+ To use a different adapter than the +REXML+ default:
103
+
104
+ require 'xml-object' # Require XMLObject first
105
+ require 'xml-object/adapters/hpricot' # (Under MRI or JRuby)
106
+ require 'xml-object/adapters/libxml' # (Under MRI only)
107
+ require 'xml-object/adapters/jrexml' # (Under Jruby only)
108
+
89
109
  === Collection auto-folding
90
110
 
91
111
  Similar to XmlSimple, XMLObject folds same named elements at the same level.
@@ -138,14 +158,6 @@ methods it doesn't contain to the collection below, so you get:
138
158
  Strings that look like booleans are "booleanized" if called by their question
139
159
  mark names (such as +enabled?+)
140
160
 
141
- === Adapters
142
-
143
- XMLObject supports different adapters to do the actual XML parsing. It ships
144
- with +REXML+ and +Hpricot+ adapters. By default, the +REXML+ adapter is used.
145
- To use the +Hpricot+ adapter:
146
-
147
- require 'xml-object/adapters/hpricot'
148
-
149
161
  === Recursive
150
162
 
151
163
  The design of the adapters assumes parsing of the objects recursively. Deep
data/TODO CHANGED
@@ -1,2 +1 @@
1
1
  * Refactor so as to not do things recursively
2
- * Write LibXML2 adapter
data/WHATSNEW CHANGED
@@ -1,4 +1,12 @@
1
- * 0.9.6 (2008-10-??):
1
+ * 0.9.7 (2008-10-20):
2
+ - LibXML adapter added
3
+ - JREXML "adapter" added, as well as JREXML support during tests and
4
+ benchmark tasks, but only when running under JRuby
5
+ - Backwards incompatible change: Elements with no text and multiple CDATA
6
+ values no longer report their value as their first CDATA element. They
7
+ report all of their CDATA nodes joined together, a-la LibXML
8
+
9
+ * 0.9.6 (2008-10-19):
2
10
  - Ruby 1.9 compatibility
3
11
  - XMLObject no longer requires ActiveSupport. Note that proper array
4
12
  pluralization still depends on ActiveSupport's Inflector, so if it's not
@@ -6,7 +14,7 @@
6
14
  - XMLObject no longer tries to load Hpricot by default (too cumbersome
7
15
  to test). Use "require 'xml-object/adapters/hpricot'" to enable the
8
16
  Hpricot adapter.
9
- - Backwards compatible change. For the following XML:
17
+ - Backwards incompatible change. For the following XML:
10
18
 
11
19
  <foo>
12
20
  <bar>
@@ -1,3 +1,4 @@
1
+ require 'rubygems'
1
2
  require 'hpricot'
2
3
 
3
4
  module XMLObject::Adapters::Hpricot
@@ -18,30 +19,19 @@ module XMLObject::Adapters::Hpricot
18
19
 
19
20
  class Element < XMLObject::Adapters::Base::Element # :nodoc:
20
21
  def initialize(xml)
21
- self.raw, self.name, self.attributes = xml, xml.name, xml.attributes
22
- self.children = xml.children.select { |e| e.elem? }.map do |raw_xml|
23
- self.class.new(raw_xml)
24
- end
25
-
26
- self.value = case
27
- when (not text_value(xml).blank?) then text_value(xml)
28
- when (not cdata_value(xml).blank?) then cdata_value(xml)
29
- else ''
30
- end
31
- end
22
+ @raw, @name, @attributes = xml, xml.name, xml.attributes
32
23
 
33
- private ################################################################
24
+ @element_nodes = xml.children.select { |c| c.elem? }
34
25
 
35
- def text_value(xml)
36
- xml.children.select do |e|
37
- (e.class == ::Hpricot::Text) && !e.to_s.blank?
38
- end.join.to_s
39
- end
26
+ @text_nodes = xml.children.select do |c|
27
+ c.text? && !c.is_a?(::Hpricot::CData)
28
+ end.map { |c| c.to_s }
29
+
30
+ @cdata_nodes = xml.children.select do |c|
31
+ c.is_a? ::Hpricot::CData
32
+ end.map { |c| c.to_s }
40
33
 
41
- def cdata_value(xml)
42
- xml.children.select do |e|
43
- (e.class == ::Hpricot::CData) && !e.to_s.blank?
44
- end.first.to_s
34
+ super
45
35
  end
46
36
  end
47
37
  end
@@ -0,0 +1,10 @@
1
+ require 'jrexml'
2
+ require File.join(File.dirname(__FILE__), 'rexml')
3
+
4
+ module XMLObject::Adapters
5
+ JREXML = REXML
6
+ end
7
+
8
+ def XMLObject.adapter # :nodoc:
9
+ XMLObject::Adapters::JREXML
10
+ end
@@ -0,0 +1,38 @@
1
+ require 'libxml'
2
+
3
+ module XMLObject::Adapters::LibXML
4
+
5
+ # Can take a String of XML data, or anything that responds to
6
+ # either +read+ or +to_s+.
7
+ def self.new(duck)
8
+ case
9
+ when duck.is_a?(::LibXML::XML::Node) then Element.new(duck)
10
+ when duck.is_a?(::String)
11
+ then new(::LibXML::XML::Parser.string(duck).parse.root)
12
+ when duck.respond_to?(:read) then new(duck.read)
13
+ when duck.respond_to?(:to_s) then new(duck.to_s)
14
+ else raise "Don't know how to deal with '#{duck.class}' object"
15
+ end
16
+ end
17
+
18
+ private ##################################################################
19
+
20
+ class Element < XMLObject::Adapters::Base::Element # :nodoc:
21
+ def initialize(xml)
22
+ @raw, @name, @attributes = xml, xml.name, xml.attributes.to_h
23
+
24
+ @element_nodes = xml.children.select { |c| c.element? }
25
+
26
+ @text_nodes = xml.children.select { |c| c.text? }.map { |c| c.to_s }
27
+ @cdata_nodes = xml.children.select { |c| c.cdata? }.map do |c|
28
+ c.to_s.chomp(']]>').sub('<![CDATA[', '')
29
+ end
30
+
31
+ super
32
+ end
33
+ end
34
+ end
35
+
36
+ def XMLObject.adapter # :nodoc:
37
+ XMLObject::Adapters::LibXML
38
+ end
@@ -18,14 +18,19 @@ module XMLObject::Adapters::REXML
18
18
 
19
19
  class Element < XMLObject::Adapters::Base::Element # :nodoc:
20
20
  def initialize(xml)
21
- self.raw, self.name, self.attributes = xml, xml.name, xml.attributes
22
- self.children = xml.elements.map { |raw_xml| self.class.new(raw_xml) }
23
-
24
- self.value = case
25
- when (not xml.text.blank?) then xml.text.to_s
26
- when (xml.cdatas.any?) then xml.cdatas.first.to_s
27
- else ''
28
- end
21
+ @raw, @name, @attributes = xml, xml.name, xml.attributes
22
+
23
+ @element_nodes = xml.elements
24
+
25
+ @text_nodes = xml.children.select do |child|
26
+ child.class == ::REXML::Text
27
+ end.collect { |child| child.to_s }
28
+
29
+ @cdata_nodes = xml.children.select do |child|
30
+ child.class == ::REXML::CData
31
+ end.collect { |child| child.to_s }
32
+
33
+ super
29
34
  end
30
35
  end
31
36
  end
@@ -3,6 +3,27 @@ module XMLObject # :nodoc:
3
3
  module Base # :nodoc:
4
4
  class Element # :nodoc:
5
5
  attr_accessor :raw, :name, :value, :attributes, :children # :nodoc:
6
+
7
+ def initialize(*args)
8
+
9
+ @children = @element_nodes.map { |node| self.class.new(node) }
10
+
11
+ @value = case
12
+ when (not text_value.blank?) then text_value
13
+ when (not cdata_value.blank?) then cdata_value
14
+ else ''
15
+ end
16
+ end
17
+
18
+ private ###########################################################
19
+
20
+ def text_value
21
+ @text_nodes.reject { |n| n.blank? }.join
22
+ end
23
+
24
+ def cdata_value
25
+ @cdata_nodes.join
26
+ end
6
27
  end
7
28
  end
8
29
  end
data/lib/xml-object.rb CHANGED
@@ -1,12 +1,9 @@
1
1
  begin; require 'rubygems'; rescue Exception, StandardError; nil; end
2
2
  begin; require 'activesupport'; rescue Exception, StandardError; nil; end
3
3
 
4
- module XMLObject # :nodoc:
5
- VERSION = '0.9.6'
6
- end
4
+ $:.unshift File.join(File.dirname(__FILE__), 'xml-object')
7
5
 
8
6
  require 'core_ext'
9
-
10
7
  require 'adapters'
11
8
  require 'adapters/rexml'
12
9
  require 'array_notation'
data/xml-object.gemspec CHANGED
@@ -2,8 +2,8 @@ Gem::Specification.new do |gem|
2
2
  gem.rubyforge_project = 'xml-object'
3
3
 
4
4
  gem.name = 'xml-object'
5
- gem.version = '0.9.6'
6
- gem.date = '2008-10-19'
5
+ gem.version = '0.9.7'
6
+ gem.date = '2008-10-20'
7
7
  gem.author = 'Jordi Bunster'
8
8
  gem.email = 'jordi@bunster.org'
9
9
  gem.homepage = 'http://github.com/jordi/xml-object'
@@ -24,6 +24,8 @@ Gem::Specification.new do |gem|
24
24
  lib/xml-object
25
25
  lib/xml-object/adapters
26
26
  lib/xml-object/adapters/hpricot.rb
27
+ lib/xml-object/adapters/jrexml.rb
28
+ lib/xml-object/adapters/libxml.rb
27
29
  lib/xml-object/adapters/rexml.rb
28
30
  lib/xml-object/adapters.rb
29
31
  lib/xml-object/array_notation.rb
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jordi-xml-object
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.6
4
+ version: 0.9.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jordi Bunster
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-10-19 00:00:00 -07:00
12
+ date: 2008-10-20 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -31,6 +31,8 @@ files:
31
31
  - lib/xml-object
32
32
  - lib/xml-object/adapters
33
33
  - lib/xml-object/adapters/hpricot.rb
34
+ - lib/xml-object/adapters/jrexml.rb
35
+ - lib/xml-object/adapters/libxml.rb
34
36
  - lib/xml-object/adapters/rexml.rb
35
37
  - lib/xml-object/adapters.rb
36
38
  - lib/xml-object/array_notation.rb