Skip to content

Commit

Permalink
Add Oga SAX handler
Browse files Browse the repository at this point in the history
  • Loading branch information
krasnoukhov committed Nov 17, 2014
1 parent aaaf8db commit 357a05f
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 3 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ env:
matrix:
- HANDLER="nokogiri"
- HANDLER="ox"
- HANDLER="oga"
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ group :development, :test do
gem 'activerecord', '~> 4.1'
gem 'nokogiri', '~> 1.6'
gem 'ox', '>= 2.1.2', platforms: [:mri, :rbx]
gem 'oga', '>= 0.2.0'
end
14 changes: 11 additions & 3 deletions lib/sax-machine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,16 @@ def self.handler=(handler)
end
end

begin
SAXMachine.handler = :ox
rescue LoadError
# Try handlers
[:ox, :oga].each do |handler|
begin
SAXMachine.handler = handler
break
rescue LoadError
end
end

# Still no handler, use Nokogiri
if !SAXMachine.handler
SAXMachine.handler = :nokogiri
end
37 changes: 37 additions & 0 deletions lib/sax-machine/handlers/sax_oga_handler.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
require 'sax-machine/handlers/sax_abstract_handler'
require 'oga'

module SAXMachine
class SAXOgaHandler
include SAXAbstractHandler

def initialize(*args)
_initialize(*args)
end

def sax_parse(xml_text)
Oga.sax_parse_xml(self, xml_text)
end

def on_element(namespace, name, attrs)
_start_element(node_name(namespace, name), attrs.map { |a| [a.name, a.value] })
end

def after_element(namespace, name)
_end_element(node_name(namespace, name))
end

def on_error(*args)
_error(args.join(" "))
end

alias_method :on_text, :_characters
alias_method :on_cdata, :_characters

private

def node_name(namespace, name)
namespace ? [namespace, name].join(":") : name
end
end
end

0 comments on commit 357a05f

Please sign in to comment.