diff --git a/.travis.yml b/.travis.yml index 9411bbe..c1ae2f5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,5 +9,3 @@ rvm: - 2.3.1 - 2.2.2 - 2.1.6 - - 2.0.0 - \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index 8312880..3babeea 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,8 @@ * [#36](https://github.com/soulcutter/saxerator/issues/36): Rubocop config (@fanantoxa) * [#45](https://github.com/soulcutter/saxerator/issues/45): Drop support ruby 1.9.3 (@fanantoxa) * [#2](https://github.com/soulcutter/saxerator/issues/2): Implement Ox support (@soulcutter) +* [#43](https://github.com/soulcutter/saxerator/issues/43): Update XmlBuilder to use REXML class (@fanantoxa) +* [#46](https://github.com/soulcutter/saxerator/pull/46): Bump ruby version requirement to 2.1+ (@soulcutter) ### Bug fixes @@ -18,5 +20,6 @@ * [#33](https://github.com/soulcutter/saxerator/pull/33): Extract an adapter interface for supporting other parsers. (@soulcutter) * [#38](https://github.com/soulcutter/saxerator/pull/38): Ox adapter. (@soulcutter) * [#39](https://github.com/soulcutter/saxerator/pull/39): Added validation for output_type validation for difference parsers. (@fanantoxa) +* [#46](https://github.com/soulcutter/saxerator/pull/46): First scratch of rexml implementation. (@fanantoxa) -## 0.9.5 (2014-11-07) \ No newline at end of file +## 0.9.5 (2014-11-07) diff --git a/README.md b/README.md index e41a06f..274379f 100644 --- a/README.md +++ b/README.md @@ -100,7 +100,7 @@ end | Setting | Default | Values | Description |:------------------|:--------|-----------------|------------ | `adapter` | `:nokogiri` | `:nokogiri`, `:ox` | The XML parser used by Saxerator | -| `output_type` | `:hash` | `:hash`, `:xml` | The type of object generated by Saxerator's parsing. `:hash` should be self-explanatory, `:xml` generates a `Nokogiri::XML::Document` +| `output_type` | `:hash` | `:hash`, `:xml` | The type of object generated by Saxerator's parsing. `:hash` should be self-explanatory, `:xml` generates a `REXML::Document` | `symbolize_keys!` | n/a | n/a | Call this method if you want the hash keys to be symbols rather than strings | `ignore_namespaces!`| n/a | n/a | Call this method if you want to treat the XML document as if it has no namespace information. It differs slightly from `strip_namespaces!` since it deals with how the XML is processed rather than how it is output | `strip_namespaces!`| n/a | user-specified | Called with no arguments this strips all namespaces, or you may specify an arbitrary number of namespaces to strip, i.e. `config.strip_namespaces! :rss, :soapenv` @@ -139,6 +139,10 @@ When I fetch a tag that has one or more elements, sometimes I get an `Array`, an > occasionally comes up is for elements that are sometimes-empty. Empty elements behave mostly like an > empty `Hash`, however you may convert it to a more `String`-like object via `#to_s` +### Contribution ### + +For running tests for all parsers run `rake spec:adapters` + ### Acknowledgements ### Saxerator was inspired by - but not affiliated with - [nori](https://github.com/savonrb/nori) and [Gregory Brown](http://majesticseacreature.com/)'s [Practicing Ruby](http://practicingruby.com/) diff --git a/lib/saxerator/builder/xml_builder.rb b/lib/saxerator/builder/xml_builder.rb index 702e257..ed0cc8b 100644 --- a/lib/saxerator/builder/xml_builder.rb +++ b/lib/saxerator/builder/xml_builder.rb @@ -1,3 +1,5 @@ +require 'rexml/document' + module Saxerator module Builder class XmlBuilder @@ -17,19 +19,21 @@ def add_node(node) end def to_xml(builder) + element = REXML::Element.new(name, nil, attribute_quote: :quote) + element.add_attributes(@attributes) if @text - builder.send("#{name}_", @attributes, @children.join) + element.add_text(@children.join) else - builder.send("#{name}_", @attributes) do |xml| - @children.each { |child| child.to_xml(xml) } - end + @children.each { |child| child.to_xml(element) } end + builder.elements << element end def block_variable - builder = Nokogiri::XML::Builder.new + builder = REXML::Document.new + builder << REXML::XMLDecl.new('1.0') to_xml(builder) - builder.doc + builder end end end diff --git a/lib/saxerator/configuration.rb b/lib/saxerator/configuration.rb index 6f8d336..50be968 100644 --- a/lib/saxerator/configuration.rb +++ b/lib/saxerator/configuration.rb @@ -3,10 +3,7 @@ class Configuration attr_writer :hash_key_generator attr_reader :output_type - ALLOWED_OUTPUT_TYPES = { - nokogiri: [:hash, :xml], - ox: [:hash] - }.freeze + ALLOWED_OUTPUT_TYPES = [:ox, :nokogiri] def initialize @adapter = :nokogiri @@ -16,7 +13,9 @@ def initialize end def adapter=(name) - raise ArgumentError.new("Unknown adapter '#{name.inspect}'") unless ALLOWED_OUTPUT_TYPES.has_key?(name) + unless ALLOWED_OUTPUT_TYPES.include?(name) + raise ArgumentError, "Unknown adapter '#{name.inspect}'" + end @adapter = name end @@ -27,10 +26,6 @@ def adapter def output_type=(val) raise ArgumentError, "Unknown output_type '#{val.inspect}'" unless Builder.valid?(val) - unless ALLOWED_OUTPUT_TYPES[@adapter].include?(val) - raise ArgumentError, "Adapter '#{adapter.inspect}' not allow to use \ - output_type '#{val.inspect}'" - end @output_type = val raise_error_if_using_put_attributes_in_hash_with_xml end diff --git a/saxerator.gemspec b/saxerator.gemspec index d0933aa..6590561 100644 --- a/saxerator.gemspec +++ b/saxerator.gemspec @@ -17,7 +17,7 @@ Gem::Specification.new do |s| s.rubyforge_project = 'saxerator' - s.required_ruby_version = '>= 2.0.0' + s.required_ruby_version = '>= 2.1.0' s.files = [ 'LICENSE', diff --git a/spec/lib/builder/xml_builder_spec.rb b/spec/lib/builder/xml_builder_spec.rb index d192940..4975218 100644 --- a/spec/lib/builder/xml_builder_spec.rb +++ b/spec/lib/builder/xml_builder_spec.rb @@ -1,7 +1,7 @@ # encoding: utf-8 require 'spec_helper' -describe 'Saxerator xml format', :nokogiri_only do +describe 'Saxerator xml format' do let(:xml) { fixture_file('nested_elements.xml') } subject(:entry) do @@ -10,29 +10,9 @@ end.for_tag(:entry).first end - it { is_expected.to be_a(Nokogiri::XML::Node) } + it { is_expected.to be_a(REXML::Document) } it 'looks like the original document' do - expected_xml = <<-eos - - - 1 - 2012-01-01T16:17:00-06:00 - 2012-01-01T16:17:00-06:00 - - How to eat an airplane - <p>Airplanes are very large — this can present difficulty in digestion.</p> - - - Soul<utter - - - Jane Doe - - - Leviticus Alabaster - - - eos - expect(entry.to_xml).to eq(expected_xml) + expected_xml = '12012-01-01T16:17:00-06:002012-01-01T16:17:00-06:00How to eat an airplane<p>Airplanes are very large — this can present difficulty in digestion.</p>Soul<utterJane DoeLeviticus Alabaster' + expect(entry.to_s).to eq(expected_xml) end end diff --git a/spec/lib/saxerator_spec.rb b/spec/lib/saxerator_spec.rb index 027a4a9..5f76e71 100644 --- a/spec/lib/saxerator_spec.rb +++ b/spec/lib/saxerator_spec.rb @@ -52,9 +52,9 @@ specify { expect(parser.all).to eq('bar' => 'baz') } end - context 'with config.output_type = :xml', :nokogiri_only do + context 'with config.output_type = :xml' do let(:output_type) { :xml } - specify { expect(parser.all).to be_a Nokogiri::XML::Document } + specify { expect(parser.all).to be_a REXML::Document } specify { expect(parser.all.to_s).to include '' } end