mirror of
https://github.com/github/rails.git
synced 2026-04-04 03:00:58 -04:00
Removed ill faded xml_node class from codebase. Use XmlSimple instead
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6511 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
require 'cgi'
|
||||
require 'action_controller/vendor/xml_node'
|
||||
require 'strscan'
|
||||
|
||||
# Static methods for parsing the query and request parameters that can be used in
|
||||
@@ -49,13 +48,10 @@ class CGIMethods #:nodoc:
|
||||
case strategy = ActionController::Base.param_parsers[mime_type]
|
||||
when Proc
|
||||
strategy.call(raw_post_data)
|
||||
when :xml_simple
|
||||
when :xml_simple, :xml_node
|
||||
raw_post_data.blank? ? {} : Hash.from_xml(raw_post_data)
|
||||
when :yaml
|
||||
YAML.load(raw_post_data)
|
||||
when :xml_node
|
||||
node = XmlNode.from_xml(raw_post_data)
|
||||
{ node.node_name => node }
|
||||
end
|
||||
rescue Exception => e # YAML, XML or Ruby code block errors
|
||||
{ "exception" => "#{e.message} (#{e.class})", "backtrace" => e.backtrace,
|
||||
|
||||
@@ -1,97 +0,0 @@
|
||||
require 'rexml/document'
|
||||
|
||||
# SimpleXML like xml parser. Written by leon breet from the ruby on rails Mailing list
|
||||
class XmlNode #:nodoc:
|
||||
attr :node
|
||||
|
||||
def initialize(node, options = {})
|
||||
@node = node
|
||||
@children = {}
|
||||
@raise_errors = options[:raise_errors]
|
||||
end
|
||||
|
||||
def self.from_xml(xml_or_io)
|
||||
document = REXML::Document.new(xml_or_io)
|
||||
if document.root
|
||||
XmlNode.new(document.root)
|
||||
else
|
||||
XmlNode.new(document)
|
||||
end
|
||||
end
|
||||
|
||||
def node_encoding
|
||||
@node.encoding
|
||||
end
|
||||
|
||||
def node_name
|
||||
@node.name
|
||||
end
|
||||
|
||||
def node_value
|
||||
@node.text
|
||||
end
|
||||
|
||||
def node_value=(value)
|
||||
@node.text = value
|
||||
end
|
||||
|
||||
def xpath(expr)
|
||||
matches = nil
|
||||
REXML::XPath.each(@node, expr) do |element|
|
||||
matches ||= XmlNodeList.new
|
||||
matches << (@children[element] ||= XmlNode.new(element))
|
||||
end
|
||||
matches
|
||||
end
|
||||
|
||||
def method_missing(name, *args)
|
||||
name = name.to_s
|
||||
nodes = nil
|
||||
@node.each_element(name) do |element|
|
||||
nodes ||= XmlNodeList.new
|
||||
nodes << (@children[element] ||= XmlNode.new(element))
|
||||
end
|
||||
nodes
|
||||
end
|
||||
|
||||
def <<(node)
|
||||
if node.is_a? REXML::Node
|
||||
child = node
|
||||
elsif node.respond_to? :node
|
||||
child = node.node
|
||||
end
|
||||
@node.add_element child
|
||||
@children[child] ||= XmlNode.new(child)
|
||||
end
|
||||
|
||||
def [](name)
|
||||
@node.attributes[name.to_s]
|
||||
end
|
||||
|
||||
def []=(name, value)
|
||||
@node.attributes[name.to_s] = value
|
||||
end
|
||||
|
||||
def to_s
|
||||
@node.to_s
|
||||
end
|
||||
|
||||
def to_i
|
||||
to_s.to_i
|
||||
end
|
||||
end
|
||||
|
||||
class XmlNodeList < Array #:nodoc:
|
||||
def [](i)
|
||||
i.is_a?(String) ? super(0)[i] : super(i)
|
||||
end
|
||||
|
||||
def []=(i, value)
|
||||
i.is_a?(String) ? self[0][i] = value : super(i, value)
|
||||
end
|
||||
|
||||
def method_missing(name, *args)
|
||||
name = name.to_s
|
||||
self[0].__send__(name, *args)
|
||||
end
|
||||
end
|
||||
@@ -41,7 +41,7 @@ class WebServiceTest < Test::Unit::TestCase
|
||||
def setup
|
||||
@controller = TestController.new
|
||||
ActionController::Base.param_parsers.clear
|
||||
ActionController::Base.param_parsers[Mime::XML] = :xml_node
|
||||
ActionController::Base.param_parsers[Mime::XML] = :xml_simple
|
||||
end
|
||||
|
||||
def test_check_parameters
|
||||
@@ -54,7 +54,7 @@ class WebServiceTest < Test::Unit::TestCase
|
||||
|
||||
assert_equal 'entry', @controller.response.body
|
||||
assert @controller.params.has_key?(:entry)
|
||||
assert_equal 'content...', @controller.params["entry"].summary.node_value
|
||||
assert_equal 'content...', @controller.params["entry"]['summary']
|
||||
assert_equal 'true', @controller.params["entry"]['attributed']
|
||||
end
|
||||
|
||||
@@ -63,7 +63,7 @@ class WebServiceTest < Test::Unit::TestCase
|
||||
|
||||
assert_equal 'entry', @controller.response.body
|
||||
assert @controller.params.has_key?(:entry)
|
||||
assert_equal 'content...', @controller.params["entry"].summary.node_value
|
||||
assert_equal 'content...', @controller.params["entry"]['summary']
|
||||
assert_equal 'true', @controller.params["entry"]['attributed']
|
||||
end
|
||||
|
||||
@@ -183,57 +183,3 @@ class WebServiceTest < Test::Unit::TestCase
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
|
||||
class XmlNodeTest < Test::Unit::TestCase
|
||||
def test_all
|
||||
xn = XmlNode.from_xml(%{<?xml version="1.0" encoding="UTF-8"?>
|
||||
<response success='true'>
|
||||
<page title='Ajax Summit' id='1133' email_address='ry87ib@backpackit.com'>
|
||||
<description>With O'Reilly and Adaptive Path</description>
|
||||
<notes>
|
||||
<note title='Hotel' id='1020' created_at='2005-05-14 16:41:11'>
|
||||
Staying at the Savoy
|
||||
</note>
|
||||
</notes>
|
||||
<tags>
|
||||
<tag name='Technology' id='4' />
|
||||
<tag name='Travel' id='5' />
|
||||
</tags>
|
||||
</page>
|
||||
</response>
|
||||
}
|
||||
)
|
||||
assert_equal 'UTF-8', xn.node.document.encoding
|
||||
assert_equal '1.0', xn.node.document.version
|
||||
assert_equal 'true', xn['success']
|
||||
assert_equal 'response', xn.node_name
|
||||
assert_equal 'Ajax Summit', xn.page['title']
|
||||
assert_equal '1133', xn.page['id']
|
||||
assert_equal "With O'Reilly and Adaptive Path", xn.page.description.node_value
|
||||
assert_equal nil, xn.nonexistent
|
||||
assert_equal "Staying at the Savoy", xn.page.notes.note.node_value.strip
|
||||
assert_equal 'Technology', xn.page.tags.tag[0]['name']
|
||||
assert_equal 'Travel', xn.page.tags.tag[1][:name]
|
||||
matches = xn.xpath('//@id').map{ |id| id.to_i }
|
||||
assert_equal [4, 5, 1020, 1133], matches.sort
|
||||
matches = xn.xpath('//tag').map{ |tag| tag['name'] }
|
||||
assert_equal ['Technology', 'Travel'], matches.sort
|
||||
assert_equal "Ajax Summit", xn.page['title']
|
||||
xn.page['title'] = 'Ajax Summit V2'
|
||||
assert_equal "Ajax Summit V2", xn.page['title']
|
||||
assert_equal "Staying at the Savoy", xn.page.notes.note.node_value.strip
|
||||
xn.page.notes.note.node_value = "Staying at the Ritz"
|
||||
assert_equal "Staying at the Ritz", xn.page.notes.note.node_value.strip
|
||||
assert_equal '5', xn.page.tags.tag[1][:id]
|
||||
xn.page.tags.tag[1]['id'] = '7'
|
||||
assert_equal '7', xn.page.tags.tag[1]['id']
|
||||
end
|
||||
|
||||
|
||||
def test_small_entry
|
||||
node = XmlNode.from_xml('<entry>hi</entry>')
|
||||
assert_equal 'hi', node.node_value
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user