Module: Rsxml

Extended by:
Util
Defined in:
lib/rsxml.rb,
lib/rsxml/xml.rb,
lib/rsxml/sexp.rb,
lib/rsxml/util.rb,
lib/rsxml/visitor.rb,
lib/rsxml/namespace.rb

Defined Under Namespace

Modules: Namespace, Sexp, Util, Visitor, Xml

Constant Summary collapse

TO_XML_OPTS =
{:ns=>nil}
TO_RSXML_OPTS =
{:ns=>nil}.merge(Visitor::BuildRsxmlVisitor::OPTS)

Class Attribute Summary collapse

Class Method Summary collapse

Methods included from Util

check_opts

Class Attribute Details

.loggerObject

Returns the value of attribute logger.



14
15
16
# File 'lib/rsxml.rb', line 14

def logger
  @logger
end

Class Method Details

.compare(xml_or_sexp_a, xml_or_sexp_b) ⇒ Object

compare two documents in XML or Rsxml. returns true if they are identical, and if not raises ComparisonError describing where they differ



54
55
56
57
58
# File 'lib/rsxml.rb', line 54

def compare(xml_or_sexp_a, xml_or_sexp_b)
  sexp_a = xml_or_sexp_a.is_a?(String) ? to_rsxml(xml_or_sexp_a) : xml_or_sexp_a
  sexp_b = xml_or_sexp_b.is_a?(String) ? to_rsxml(xml_or_sexp_b) : xml_or_sexp_b
  Sexp.compare(sexp_a, sexp_b)
end

.log {|logger| ... } ⇒ Object

Yields:



19
20
21
# File 'lib/rsxml.rb', line 19

def log
  yield(logger) if logger
end

.to_rsxml(doc, opts = {}) ⇒ Object

convert an XML string to an Rsxml s-expression representation

Rsxml.to_rsxml('<Foo foofoo="10"><Bar>barbar</Bar><Baz></Baz></Foo>')
 => ["Foo", {"foofoo"=>"10"}, ["Bar", "barbar"], ["Baz"]]

if ns_prefixes is a Hash, then doc is assumed to be a fragment, and is wrapped in an element with namespace declarations according to ns_prefixes

fragment = '<foo:Foo foo:foofoo="10"><Bar>barbar</Bar><Baz></Baz></Foo>'
Rsxml.to_rsxml(fragment, {"foo"=>"http://foo.com/foo", ""=>"http://baz.com/baz"})
 => ["foo:Foo", {"foo:foofoo"=>"10", "xmlns:foo"=>"http://foo.com/foo", "xmlns"=>"http://baz.com/baz"}, ["Bar", "barbar"], ["Baz"]]


45
46
47
48
49
50
# File 'lib/rsxml.rb', line 45

def to_rsxml(doc, opts={})
  opts = check_opts(TO_RSXML_OPTS, opts)
  doc = Xml.wrap_fragment(doc, opts.delete(:ns))
  root = Xml.unwrap_fragment(Nokogiri::XML(doc).children.first)
  Xml.traverse(root, Visitor::BuildRsxmlVisitor.new(opts)).sexp
end

.to_xml(rsxml, opts = {}) ⇒ Object

convert an Rsxml s-expression representation of an XML document to XML

Rsxml.to_xml(["Foo", {"foofoo"=>"10"}, ["Bar", "barbar"] ["Baz"]])
 => '<Foo foofoo="10"><Bar>barbar</Bar><Baz></Baz></Foo>'


28
29
30
31
# File 'lib/rsxml.rb', line 28

def to_xml(rsxml, opts={})
  opts = check_opts(TO_XML_OPTS, opts)
  Sexp.traverse(rsxml, Visitor::WriteXmlVisitor.new, Visitor::Context.new(opts[:ns])).to_s
end