Skip to content

Commit

Permalink
convert to_xml method to use Sexp::WriteXmlVisitor
Browse files Browse the repository at this point in the history
  • Loading branch information
mccraigmccraig committed May 10, 2011
1 parent b3acec1 commit a3793c6
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 82 deletions.
4 changes: 1 addition & 3 deletions lib/rsxml.rb
Expand Up @@ -20,9 +20,7 @@ def check_opts(constraints, opts)
# Rsxml.to_xml(["Foo", {"foofoo"=>"10"}, ["Bar", "barbar"] ["Baz"]])
# => '<Foo foofoo="10"><Bar>barbar</Bar><Baz></Baz></Foo>'
def to_xml(rsxml, &transformer)
xml = Builder::XmlMarkup.new
Sexp.write_xml(xml, rsxml, &transformer)
xml.target!
Sexp.traverse(rsxml, Sexp::WriteXmlVisitor.new).to_s
end

TO_RSXML_OPTS = {:ns=>nil}
Expand Down
54 changes: 2 additions & 52 deletions lib/rsxml/sexp.rb
Expand Up @@ -30,8 +30,8 @@ def processed_node(node)

class WriteXmlVisitor
attr_reader :xml
def initialize()
@xml = Builder::XmlMarkup.new
def initialize(xml_builder=nil)
@xml = xml_builder || Builder::XmlMarkup.new
end

def tag(context, qname, qattrs)
Expand Down Expand Up @@ -96,56 +96,6 @@ def traverse(sexp, visitor, context=Context.new)
visitor
end

def write_xml(xml, sexp, ns_stack=[], path=[""], &transformer)
tag, attrs, children = decompose_sexp(sexp)

ns_declared = Namespace::extract_declared_namespace_bindings(attrs)

ns_stack_decl = ns_stack + [ns_declared]
utag = Namespace::explode_qname(ns_stack_decl, tag)
uattrs = Namespace::explode_attr_qnames(ns_stack_decl, attrs)

ns_explicit = Namespace::extract_explicit_namespace_bindings(utag, uattrs)
ns_undeclared = Namespace::undeclared_namespace_bindings(ns_stack_decl, ns_explicit)
ns_undeclared_decls = Namespace::exploded_namespace_declarations(ns_undeclared)
uattrs = uattrs.merge(ns_undeclared_decls)

ns_new_context = Namespace::merge_namespace_bindings(ns_declared, ns_undeclared)


if transformer
txtag, txattrs = transformer.call(utag, uattrs, path.join("/"))
raise "transformer returned nil tag from \ntag: #{tag.inspect}\nattrs: #{attrs.inspect}>\npath: #{path.inspect}" if !txtag
else
txtag, txattrs = [utag, uattrs]
end

# figure out which explicit namespaces need declaring

ns_stack.push(ns_new_context)
begin

qname = Namespace::compact_qname(ns_stack, txtag)
qattrs = Namespace::compact_attr_qnames(ns_stack, txattrs)
xml.__send__(qname, qattrs) do
children.each_with_index do |child, i|
begin
path.push("#{tag}[#{i}]")
if child.is_a?(Array)
write_xml(xml, child, ns_stack, path, &transformer)
else
xml << child
end
ensure
path.pop
end
end
end
ensure
ns_stack.pop
end
end

def decompose_sexp(sexp)
raise "invalid rsxml: #{rsxml.inspect}" if sexp.length<1
if sexp[0].is_a?(Array)
Expand Down
27 changes: 0 additions & 27 deletions spec/rsxml_spec.rb
Expand Up @@ -126,33 +126,6 @@
r.attributes["baz"].namespace.href.should == "http://foo.com/foo"
r.attributes["baz"].namespace.prefix.should == "foo"
end

it "should transform a single tag if a transformer is supplied" do
xml = Rsxml.to_xml([:foo]) do |tag,attrs,path|
path.should == ""
[tag.upcase, attrs]
end.should ==
"<FOO></FOO>"
end

it "should transform nested tags if a transformer is supplied" do
txs = {"/foo"=>"Blub", "/foo[0]/bar"=>"Wub"}
xml = Rsxml.to_xml([:foo, [:bar]]) do |tag,attrs,path|
attrs.should == {}
[txs[[path, tag].join("/")], attrs]
end.should ==
"<Blub><Wub></Wub></Blub>"
end

it "should transform a tag with attributes if a transformer is supplied" do
xml = Rsxml.to_xml([:foo, {:bar=>"bar"}]) do |tag,attrs,path|
path.should == ""
[tag.upcase, Hash[*attrs.map{|k,v| [k.to_s.upcase,v]}.flatten]]
end.should ==
'<FOO BAR="bar"></FOO>'
end


end

describe "to_rsxml" do
Expand Down

0 comments on commit a3793c6

Please sign in to comment.