Permalink
Browse files

fixed default namespaces

  • Loading branch information...
1 parent 8f6aa68 commit 943d060c5918384c5273c4406dd48e03211ec1ef @benoist committed Nov 1, 2012
Showing with 32 additions and 25 deletions.
  1. +1 −3 lib/happymapper.rb
  2. +21 −21 lib/happymapper/item.rb
  3. +2 −1 spec/fixtures/default_namespace_combi.xml
  4. +8 −0 spec/happymapper_spec.rb
View
@@ -305,9 +305,7 @@ def parse(xml, options = {})
namespace = options[:namespace]
elsif namespaces.has_key?("xmlns")
namespace ||= DEFAULT_NS
- default_namespace = namespaces.delete("xmlns")
- namespaces[namespace] ||= default_namespace
- namespaces["xmlns:#{namespaces.key(default_namespace)}"] = default_namespace
+ namespaces[DEFAULT_NS] = namespaces.delete("xmlns")
elsif namespaces.has_key?(DEFAULT_NS)
namespace ||= DEFAULT_NS
end
View
@@ -26,19 +26,19 @@ def initialize(name, type, o={})
def constant
@constant ||= constantize(type)
end
-
+
#
# @param [XMLNode] node the xml node that is being parsed
# @param [String] namespace the name of the namespace
# @param [Hash] xpath_options additional xpath options
- #
+ #
def from_xml_node(node, namespace, xpath_options)
-
+
# If the item is defined as a primitive type then cast the value to that type
# else if the type is XMLContent then store the xml value
# else the type, specified, needs to handle the parsing.
#
-
+
if primitive?
find(node, namespace, xpath_options) do |n|
if n.respond_to?(:content)
@@ -53,11 +53,11 @@ def from_xml_node(node, namespace, xpath_options)
n.respond_to?(:to_xml) ? n.to_xml : n.to_s
end
else
-
+
# When not a primitive type or XMLContent then default to using the
- # class method #parse of the type class. If the option 'parser' has been
+ # class method #parse of the type class. If the option 'parser' has been
# defined then call that method on the type class instead of #parse
-
+
if options[:parser]
find(node, namespace, xpath_options) do |n|
if n.respond_to?(:content) && !options[:raw]
@@ -86,7 +86,7 @@ def xpath(namespace = self.namespace)
#puts "xpath: #{xpath}"
xpath
end
-
+
# @return [Boolean] true if the type defined for the item is defined in the
# list of primite types {Types}.
def primitive?
@@ -113,16 +113,16 @@ def method_name
# When the type of the item is a primitive type, this will convert value specifed
# to the particular primitive type. If it fails during this process it will
# return the original String value.
- #
+ #
# @param [String] value the string value parsed from the XML value that will
# be converted to the particular primitive type.
- #
+ #
# @return [String,Float,Time,Date,DateTime,Boolean,Integer] the converted value
# to the new type.
#
def typecast(value)
return value if value.kind_of?(constant) || value.nil?
- begin
+ begin
if constant == String then value.to_s
elsif constant == Float then value.to_f
elsif constant == Time then Time.parse(value.to_s) rescue Time.at(value.to_i)
@@ -155,7 +155,7 @@ def typecast(value)
#
# Convert any String defined types into their constant version so that
# the method #parse or the custom defined parser method would be used.
- #
+ #
# @param [String,Constant] type is the name of the class or the constant
# for the class.
# @return [Constant] the constant of the type
@@ -180,18 +180,18 @@ def constantize(type)
def find(node, namespace, xpath_options, &block)
- if self.namespace && xpath_options["xmlns:#{self.namespace}"]
+ if self.namespace
# from the class definition
namespace = self.namespace
- elsif options[:namespace] && xpath_options["xmlns:#{options[:namespace]}"]
+ elsif options[:namespace]
namespace = options[:namespace]
end
-
+
if element?
if options[:single]
-
+
result = nil
-
+
if options[:xpath]
result = node.xpath(options[:xpath], xpath_options)
else
@@ -205,9 +205,9 @@ def find(node, namespace, xpath_options, &block)
value
end
else
-
+
target_path = options[:xpath] ? options[:xpath] : xpath(namespace)
-
+
results = node.xpath(target_path, xpath_options).collect do |result|
value = yield(result)
handle_attributes_option(result, value, xpath_options)
@@ -216,13 +216,13 @@ def find(node, namespace, xpath_options, &block)
results
end
elsif attribute?
-
+
if options[:xpath]
yield(node.xpath(options[:xpath],xpath_options))
else
yield(node[tag])
end
-
+
else # text node
yield(node.children.detect{|c| c.text?})
end
@@ -1,5 +1,6 @@
<?xml version="1.0"?>
-<bk:book xmlns="urn:ISBN:0-395-36341-6" xmlns:bk="urn:loc.gov:books">
+<bk:book xmlns:bk="urn:loc.gov:books" xmlns:p="urn:loc.gov:people" xmlns="urn:ISBN:0-395-36341-6">
+ <p:author>Frank Gilbreth</p:author>
<bk:title>Cheaper by the Dozen</bk:title>
<number>1568491379</number>
</bk:book>
View
@@ -282,6 +282,8 @@ class User
class Status
include HappyMapper
+ register_namespace 'fake', "faka:namespace"
+
element :id, Integer
element :text, String
element :created_at, Time
@@ -553,12 +555,14 @@ class DefaultNamespaceCombi
register_namespace 'bk', "urn:loc.gov:books"
register_namespace 'isbn', "urn:ISBN:0-395-36341-6"
+ register_namespace 'p', "urn:loc.gov:people"
namespace 'bk'
tag 'book'
element :title, String, :namespace => 'bk', :tag => "title"
element :number, String, :namespace => 'isbn', :tag => "number"
+ element :author, String, :namespace => 'p', :tag => "author"
end
describe HappyMapper do
@@ -972,6 +976,10 @@ class Thing
@book = DefaultNamespaceCombi.parse(file_contents, :single => true)
end
+ it "should parse author" do
+ @book.author.should == "Frank Gilbreth"
+ end
+
it "should parse title" do
@book.title.should == "Cheaper by the Dozen"
end

0 comments on commit 943d060

Please sign in to comment.