Skip to content

Commit 055d960

Browse files
committed
Don't output crazy, context-less namespaces and local-name xpaths when trying to output namespaced attributes
1 parent 69a8077 commit 055d960

File tree

9 files changed

+198
-5
lines changed

9 files changed

+198
-5
lines changed

History.txt

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
* attributes collected :as => [] were previously output improperly,
1111
as a single attribute on a single node
12+
* don't output crazy, context-less namespaces and local-name xpaths
13+
when trying to output namespaced attributes
1214

1315
== 3.1.2 (October 18, 2009)
1416

README.rdoc

+3-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,9 @@ in this case.
9595

9696
=== Namespace Support
9797

98-
Namespaces are supported via the xml_namespace declaration and the :from and :namespace attr options. See spec/xml/namespace_spec.rb for usage.
98+
Namespaced nodes are supported via the xml_namespace and xml_namespaces declarations and the :from and :namespace attr options. See spec/xml/namespace_spec.rb for usage.
99+
100+
Note that ROXML does not currently support outputting namespaced nodes. This is planned for a future version.
99101

100102
== Manipulation
101103

TODO

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ Planned:
22

33
v 3.2
44

5+
* Support outputting namespaced attrs
6+
57
* Consider class_inheritable_attribute rather than superclass.try stuff.
68

79
* Do some benchmarking

lib/roxml.rb

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ module InstanceMethods # :nodoc:
2222
# Returns an XML object representing this object
2323
def to_xml(params = {})
2424
params.reverse_merge!(:name => self.class.tag_name, :namespace => self.class.roxml_namespace)
25+
params[:namespace] = nil if ['*', 'xmlns'].include?(params[:namespace])
2526
XML::Node.create([params[:namespace], params[:name]].compact.join(':')).tap do |root|
2627
refs = (self.roxml_references.present? \
2728
? self.roxml_references \

lib/roxml/xml/references.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,10 @@ def update_xml(xml, value)
177177
xml.name = value
178178
elsif array?
179179
value.each do |v|
180-
add(xml.add_child(XML::Node.create(xpath_name)), v)
180+
add(xml.add_child(XML::Node.create(name)), v)
181181
end
182182
else
183-
add(xml.add_child(XML::Node.create(xpath_name)), value)
183+
add(xml.add_child(XML::Node.create(name)), value)
184184
end
185185
end
186186
end
@@ -294,7 +294,7 @@ def update_xml(xml, value)
294294
elsif value.is_a?(ROXML)
295295
xml.add_child(value.to_xml(params))
296296
else
297-
node = XML::Node.create(xpath_name)
297+
node = XML::Node.create(name)
298298
node.content = value.to_xml
299299
xml.add_child(node)
300300
end

spec/xml/attributes_spec.rb

+33-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<node name="third" />
1010
</myxml>)
1111
end
12-
12+
1313
context "plain vanilla" do
1414
before do
1515
@ref = ROXML::XMLAttributeRef.new(OpenStruct.new(:name => 'name', :wrapper => 'node', :array? => false), RoxmlObject.new)
@@ -36,4 +36,36 @@
3636
xml.to_s.squeeze(' ').should == @xml.root.to_s.squeeze(' ')
3737
end
3838
end
39+
40+
context "when the namespaces are different" do
41+
before do
42+
@xml = ROXML::XML::Parser.parse %(
43+
<document>
44+
<myxml xmlns="http://example.com/three" xmlns:one="http://example.com/one" xmlns:two="http://example.com/two">
45+
<one:node name="first" />
46+
<two:node name="second" />
47+
<node name="third" />
48+
</myxml>
49+
</document>
50+
)
51+
end
52+
53+
context "with :namespace => '*'" do
54+
before do
55+
@ref = ROXML::XMLAttributeRef.new(OpenStruct.new(:name => 'name', :wrapper => 'node', :array? => true, :namespace => '*'), RoxmlObject.new)
56+
end
57+
58+
it "should collect all instances" do
59+
pending "Test bug?"
60+
@ref.value_in(@xml).should == ["first", "second", "third"]
61+
end
62+
63+
it "should output all instances with namespaces" do
64+
pending "Full namespace write support"
65+
xml = ROXML::XML::Node.create('result')
66+
@ref.update_xml(xml, ["first", "second", "third"])
67+
xml.should == @xml.root
68+
end
69+
end
70+
end
3971
end

spec/xml/namespace_spec.rb

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class VApp
5151
it "should reproduce the input xml" do
5252
output = ROXML::XML::Document.new
5353
output.root = VApp.from_xml(@xml).to_xml
54+
pending "Full namespace write support"
5455
output.should == ROXML::XML::Parser.parse(@xml)
5556
end
5657
end

spec/xml/object_spec.rb

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
require 'spec/spec_helper'
2+
3+
describe ROXML::XMLObjectRef do
4+
class SubObject
5+
include ROXML
6+
7+
xml_reader :value, :from => :attr
8+
9+
def initialize(value = nil)
10+
@value = value
11+
end
12+
end
13+
14+
before do
15+
@xml = ROXML::XML::Parser.parse %(
16+
<myxml>
17+
<node>
18+
<name value="first" />
19+
<name value="second" />
20+
<name value="third" />
21+
</node>
22+
</myxml>)
23+
end
24+
25+
context "plain vanilla" do
26+
before do
27+
@ref = ROXML::XMLObjectRef.new(OpenStruct.new(:name => 'name', :wrapper => 'node', :array? => false, :sought_type => SubObject), RoxmlObject.new)
28+
end
29+
30+
it "should return one instance" do
31+
@ref.value_in(@xml).value.should == "first"
32+
end
33+
it "should output one instance"
34+
end
35+
36+
context "with :as => []" do
37+
before do
38+
@ref = ROXML::XMLObjectRef.new(OpenStruct.new(:name => 'name', :wrapper => 'node', :array? => true, :sought_type => SubObject), RoxmlObject.new)
39+
end
40+
41+
it "should collect all instances" do
42+
@ref.value_in(@xml).map(&:value).should == ["first", "second", "third"]
43+
end
44+
45+
it "should output all instances" do
46+
xml = ROXML::XML::Node.create('myxml')
47+
@ref.update_xml(xml, ["first", "second", "third"].map {|value| SubObject.new(value) })
48+
xml.to_s.squeeze(' ').should == @xml.root.to_s.squeeze(' ')
49+
end
50+
end
51+
52+
context "when the namespaces are different" do
53+
before do
54+
@xml = ROXML::XML::Parser.parse %(
55+
<myxml xmlns="http://example.com/three" xmlns:one="http://example.com/one" xmlns:two="http://example.com/two">
56+
<node>
57+
<one:name>first</one:name>
58+
<two:name>second</two:name>
59+
<name>third</name>
60+
</node>
61+
</myxml>)
62+
end
63+
64+
context "with :namespace => '*'" do
65+
before do
66+
@ref = ROXML::XMLObjectRef.new(OpenStruct.new(:name => 'name', :wrapper => 'node', :array? => true, :namespace => '*', :sought_type => SubObject), RoxmlObject.new)
67+
end
68+
69+
it "should collect all instances" do
70+
pending "Test bug?"
71+
@ref.value_in(@xml).map(&:value).should == ["first", "second", "third"]
72+
end
73+
74+
it "should output all instances with namespaces" do
75+
pending "Full namespace write support"
76+
xml = ROXML::XML::Node.create('myxml')
77+
@ref.update_xml(xml, ["first", "second", "third"].map {|value| SubObject.new(value) })
78+
xml.should == @xml.root
79+
end
80+
end
81+
end
82+
end

spec/xml/text_spec.rb

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
require 'spec/spec_helper'
2+
3+
describe ROXML::XMLTextRef do
4+
before do
5+
@xml = ROXML::XML::Parser.parse %(
6+
<myxml>
7+
<node>
8+
<name>first</name>
9+
<name>second</name>
10+
<name>third</name>
11+
</node>
12+
</myxml>)
13+
end
14+
15+
context "plain vanilla" do
16+
before do
17+
@ref = ROXML::XMLTextRef.new(OpenStruct.new(:name => 'name', :wrapper => 'node', :array? => false), RoxmlObject.new)
18+
end
19+
20+
it "should return one instance" do
21+
@ref.value_in(@xml).should == "first"
22+
end
23+
it "should output one instance"
24+
end
25+
26+
context "with :as => []" do
27+
before do
28+
@ref = ROXML::XMLTextRef.new(OpenStruct.new(:name => 'name', :wrapper => 'node', :array? => true), RoxmlObject.new)
29+
end
30+
31+
it "should collect all instances" do
32+
@ref.value_in(@xml).should == ["first", "second", "third"]
33+
end
34+
35+
it "should output all instances" do
36+
xml = ROXML::XML::Node.create('myxml')
37+
@ref.update_xml(xml, ["first", "second", "third"])
38+
xml.to_s.squeeze(' ').should == @xml.root.to_s.squeeze(' ')
39+
end
40+
end
41+
42+
context "when the namespaces are different" do
43+
before do
44+
@xml = ROXML::XML::Parser.parse %(
45+
<myxml xmlns="http://example.com/three" xmlns:one="http://example.com/one" xmlns:two="http://example.com/two">
46+
<node>
47+
<one:name>first</one:name>
48+
<two:name>second</two:name>
49+
<name>third</name>
50+
</node>
51+
</myxml>)
52+
end
53+
54+
context "with :namespace => '*'" do
55+
before do
56+
@ref = ROXML::XMLTextRef.new(OpenStruct.new(:name => 'name', :wrapper => 'node', :array? => true, :namespace => '*'), RoxmlObject.new)
57+
end
58+
59+
it "should collect all instances" do
60+
@ref.value_in(@xml).should == ["first", "second", "third"]
61+
end
62+
63+
it "should output all instances with namespaces" do
64+
pending "Full namespace write support"
65+
xml = ROXML::XML::Node.create('myxml')
66+
@ref.update_xml(xml, ["first", "second", "third"])
67+
xml.should == @xml.root
68+
end
69+
end
70+
end
71+
end

0 commit comments

Comments
 (0)