Permalink
Browse files

Convert to Nokogiri for XML parsing

  • Loading branch information...
1 parent 4bb4a48 commit 26c6e439488f51bc75b99f1c5afefb715fe3f3e8 @alexdunae alexdunae committed Oct 21, 2010
View
@@ -26,3 +26,7 @@
== Version 1.0.2
* Added support for proxy servers
* Fixed to minor unit test errors
+
+== Version 1.1
+ * Ruby 1.9 compatibility
+ * Switched to Nokogiri for XML parsing
@@ -109,30 +109,31 @@ def get_request_options(options) # :nodoc:
def parse_soap_response(response) # :nodoc:
- doc = REXML::Document.new(response)
+ doc = Nokogiri::XML(response)
+ doc.remove_namespaces!
result_params = {}
{:uri => 'uri', :checked_by => 'checkedby', :validity => 'validity', :css_level => 'csslevel'}.each do |local_key, remote_key|
- if val = doc.elements["//*[local-name()='cssvalidationresponse']/*[local-name()='#{remote_key.to_s}']"]
+ if val = doc.at('cssvalidationresponse ' + remote_key)
result_params[local_key] = val.text
end
end
results = Results.new(result_params)
['warninglist', 'errorlist'].each do |list_type|
- doc.elements.each("//*[local-name()='#{list_type.to_s}']") do |message_list|
+ doc.css(list_type).each do |message_list|
- if uri_node = message_list.elements["*[local-name()='uri']"]
+ if uri_node = message_list.at('uri')
uri = uri_node.text
end
[:warning, :error].each do |msg_type|
- message_list.elements.each("*[local-name()='#{msg_type.to_s}']") do |message|
+ message_list.css(msg_type.to_s).each do |message|
message_params = {}
- message.each_element_with_text do |el|
- message_params[el.name.to_sym] = el.text
+ message.children.each do |el|
+ message_params[el.name.to_sym] = el.text unless el.blank?
end
message_params[:uri] = uri
results.add_message(msg_type, message_params)
@@ -81,23 +81,24 @@ def get_request_options(options) # :nodoc:
#
# Returns W3CValidators::Results.
def parse_soap_response(response) # :nodoc:
- doc = REXML::Document.new(response)
+ doc = Nokogiri::XML(response)
+ doc.remove_namespaces!
result_params = {}
{:uri => 'uri', :checked_by => 'checkedby', :validity => 'validity'}.each do |local_key, remote_key|
- if val = doc.elements["//*[local-name()='feedvalidationresponse']/*[local-name()='#{remote_key.to_s}']"]
+ if val = doc.at('feedvalidationresponse ' + remote_key)
result_params[local_key] = val.text
end
end
results = Results.new(result_params)
[:warning, :error].each do |msg_type|
- doc.elements.each("//*[local-name()='#{msg_type.to_s}']") do |message|
+ doc.css(msg_type.to_s).each do |message|
message_params = {}
- message.each_element_with_text do |el|
- message_params[el.name.to_sym] = el.text
+ message.children.each do |el|
+ message_params[el.name.to_sym] = el.text unless el.blank?
end
results.add_message(msg_type, message_params)
end
@@ -130,7 +130,6 @@ def validate(options, quick = false) # :nodoc:
else
response = send_request(options, :post)
end
-
@results = parse_soap_response(response.body)
end
@results
@@ -169,36 +168,35 @@ def get_request_options(options) # :nodoc:
#
# Returns W3CValidators::Results.
def parse_soap_response(response) # :nodoc:
- doc = REXML::Document.new(response)
+ doc = Nokogiri::XML(response)
+ doc.remove_namespaces!
result_params = {}
- {:doctype => 'm:doctype', :uri => 'm:uri', :charset => 'm:charset',
- :checked_by => 'm:checkedby', :validity => 'm:validity'}.each do |local_key, remote_key|
- if val = doc.elements["env:Envelope/env:Body/m:markupvalidationresponse/#{remote_key}"]
+ {:doctype => 'doctype', :uri => 'uri', :charset => 'charset',
+ :checked_by => 'checkedby', :validity => 'validity'}.each do |local_key, remote_key|
+ if val = doc.css(remote_key)
result_params[local_key] = val.text
end
end
results = Results.new(result_params)
- {:warning => 'm:warnings/m:warninglist/m:warning', :error => 'm:errors/m:errorlist/m:error'}.each do |local_type, remote_type|
- doc.elements.each("env:Envelope/env:Body/m:markupvalidationresponse/#{remote_type}") do |message|
+ {:warning => 'warnings warning', :error => 'errorlist error'}.each do |local_type, remote_type|
+ doc.css(remote_type).each do |message|
message_params = {}
- message.each_element_with_text do |el|
- message_params[el.name.to_sym] = el.text
+ message.children do |el|
+ message_params[el.name.to_sym] = el.text unless el.blank?
end
results.add_message(local_type, message_params)
end
end
- doc.elements.each("env:Envelope/env:Body/env:Fault/env:Reason") do |message|
- message.elements.each("env:Text") do |m|
- results.add_message(:error, {:mesage => m.text})
- end
+ doc.css("Fault Reason Text").each do |message|
+ results.add_message(:error, {:mesage => message.text})
end
-
- doc.elements.each("env:Envelope/env:Body/m:markupvalidationresponse/m:debug") do |debug|
+
+ doc.css("markupvalidationresponse debug").each do |debug|
results.add_debug_message(debug.attribute('name').value, debug.text)
end
return results
@@ -2,7 +2,7 @@
module W3CValidators
class NuValidator < Validator
- MARKUP_VALIDATOR_URI = 'http://validator.nu/'
+ MARKUP_VALIDATOR_URI = 'http://html5.validator.nu/'
# Create a new instance of the NuValidator.
#
@@ -16,6 +16,7 @@ class NuValidator < Validator
#
# See Validator#new for proxy server options.
def initialize(options = {})
+ options[:parser] = 'html'
if options[:validator_uri]
@validator_uri = URI.parse(options[:validator_uri])
options.delete(options[:validator_uri])
@@ -62,11 +63,11 @@ def validate_file(file)
protected
def validate(options) # :nodoc:
options = get_request_options(options)
-
+
if options.has_key?(:doc)
response = send_request(options, :get)
else
- response = send_request(options, :post)
+ response = send_request(options, :post, false, :content)
end
@results = parse_json_response(response.body)
@@ -1,6 +1,7 @@
require 'cgi'
require 'net/http'
require 'uri'
+require 'json'
require 'nokogiri'
require 'w3c_validators/exceptions'
@@ -39,7 +40,7 @@ def initialize(options = {})
# +request_mode+ must be either <tt>:get</tt>, <tt>:head</tt> or <tt>:post</tt>.
#
# Returns Net::HTTPResponse.
- def send_request(options, request_mode = :get, following_redirect = false, params_to_post = [])
+ def send_request(options, request_mode = :get, following_redirect = false, params_to_post = nil)
response = nil
results = nil
@@ -60,9 +61,14 @@ def send_request(options, request_mode = :get, following_redirect = false, param
response = http.get(@validator_uri.path + '?' + query)
when :post
# send a multipart form request
- post = {}
- [params_to_post].flatten.each do |param|
- post[param] = options.delete(param)
+ if params_to_post
+ post = {}
+ [params_to_post].flatten.each do |param|
+ post[param] = options.delete(param)
+ end
+ else
+ post = options
+ options = {}
end
qs = create_query_string_data(options)
@@ -147,7 +153,7 @@ def handle_exception(e, msg = '') # :nodoc:
when Net::HTTPServerException, SocketError
msg = "unable to connect to the validator at #{@validator_uri} (response was #{e.message})."
raise ValidatorUnavailable, msg, caller
- when REXML::ParseException
+ when JSON::ParserError, Nokogiri::XML::SyntaxError
msg = "unable to parse the response from the validator."
raise ParsingError, msg, caller
else
View
@@ -35,14 +35,15 @@
Gem::Specification.new do |s|
s.name = "w3c_validators"
s.platform = Gem::Platform::RUBY
- s.version = "1.0.2"
- s.date = "2009-11-20"
+ s.version = "1.1"
+ s.date = "2010-10-20"
s.summary = "Wrapper for the World Wide Web Consortium's online validation services."
s.email = "code@dunae.ca"
s.homepage = "http://code.dunae.ca/w3c_validators"
s.description = "W3C Validators is a Ruby wrapper for the World Wide Web Consortium's online validation services."
s.has_rdoc = true
s.author = "Alex Dunae"
+ s.add_dependency('nokogiri')
s.extra_rdoc_files = ['README.rdoc', 'CHANGELOG', 'LICENSE']
s.rdoc_options << '--all' << '--inline-source' << '--line-numbers' << '--charset' << 'utf-8'
EOH
View
@@ -1,7 +1,5 @@
-$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__), '../'))
-$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__), '../lib/'))
-
-require 'rubygems'
+$:.unshift(File.dirname(__FILE__) + '/../lib')
+require 'iconv'
require 'test/unit'
require 'w3c_validators'
View
@@ -3,16 +3,17 @@
Gem::Specification.new do |s|
s.name = "w3c_validators"
s.platform = Gem::Platform::RUBY
- s.version = "1.0.2"
- s.date = "2009-11-20"
+ s.version = "1.1"
+ s.date = "2010-10-20"
s.summary = "Wrapper for the World Wide Web Consortium's online validation services."
s.email = "code@dunae.ca"
s.homepage = "http://code.dunae.ca/w3c_validators"
s.description = "W3C Validators is a Ruby wrapper for the World Wide Web Consortium's online validation services."
s.has_rdoc = true
s.author = "Alex Dunae"
+ s.add_dependency('nokogiri')
s.extra_rdoc_files = ['README.rdoc', 'CHANGELOG', 'LICENSE']
s.rdoc_options << '--all' << '--inline-source' << '--line-numbers' << '--charset' << 'utf-8'
s.test_files = ['test/test_css_validator.rb','test/test_exceptions.rb','test/test_feed_validator.rb','test/test_helper.rb','test/test_html5_validator.rb','test/test_markup_validator.rb','test/test_proxy.rb']
- s.files = ['lib/w3c_validators','lib/w3c_validators.rb','lib/w3c_validators/constants.rb','lib/w3c_validators/css_validator.rb','lib/w3c_validators/exceptions.rb','lib/w3c_validators/feed_validator.rb','lib/w3c_validators/markup_validator.rb','lib/w3c_validators/message.rb','lib/w3c_validators/nu_validator.rb','lib/w3c_validators/results.rb','lib/w3c_validators/validator.rb']
+ s.files = ['lib/w3c_validators','lib/w3c_validators/constants.rb','lib/w3c_validators/css_validator.rb','lib/w3c_validators/exceptions.rb','lib/w3c_validators/feed_validator.rb','lib/w3c_validators/markup_validator.rb','lib/w3c_validators/message.rb','lib/w3c_validators/nu_validator.rb','lib/w3c_validators/results.rb','lib/w3c_validators/validator.rb','lib/w3c_validators.rb']
end

0 comments on commit 26c6e43

Please sign in to comment.