2017-07-09 08:06:36 -04:00
|
|
|
# frozen_string_literal: true
|
2017-07-10 09:39:13 -04:00
|
|
|
|
2016-08-06 11:58:50 -04:00
|
|
|
require "libxml"
|
2017-10-21 09:11:29 -04:00
|
|
|
require "active_support/core_ext/object/blank"
|
2016-08-06 11:58:50 -04:00
|
|
|
require "stringio"
|
2010-01-01 06:57:36 -05:00
|
|
|
|
|
|
|
module ActiveSupport
|
2012-03-06 14:34:14 -05:00
|
|
|
module XmlMini_LibXMLSAX #:nodoc:
|
2010-01-01 06:57:36 -05:00
|
|
|
extend self
|
|
|
|
|
|
|
|
# Class that will build the hash while the XML document
|
|
|
|
# is being parsed using SAX events.
|
|
|
|
class HashBuilder
|
|
|
|
include LibXML::XML::SaxParser::Callbacks
|
|
|
|
|
2018-02-27 23:33:37 -05:00
|
|
|
CONTENT_KEY = "__content__"
|
|
|
|
HASH_SIZE_KEY = "__hash_size__"
|
2010-01-01 06:57:36 -05:00
|
|
|
|
|
|
|
attr_reader :hash
|
|
|
|
|
|
|
|
def current_hash
|
|
|
|
@hash_stack.last
|
|
|
|
end
|
|
|
|
|
|
|
|
def on_start_document
|
2018-05-17 04:32:27 -04:00
|
|
|
@hash = { CONTENT_KEY => +"" }
|
2010-01-01 06:57:36 -05:00
|
|
|
@hash_stack = [@hash]
|
|
|
|
end
|
|
|
|
|
|
|
|
def on_end_document
|
2010-01-01 07:44:42 -05:00
|
|
|
@hash = @hash_stack.pop
|
|
|
|
@hash.delete(CONTENT_KEY)
|
2010-01-01 06:57:36 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def on_start_element(name, attrs = {})
|
2018-05-17 04:32:27 -04:00
|
|
|
new_hash = { CONTENT_KEY => +"" }.merge!(attrs)
|
2010-01-01 06:57:36 -05:00
|
|
|
new_hash[HASH_SIZE_KEY] = new_hash.size + 1
|
|
|
|
|
|
|
|
case current_hash[name]
|
2016-08-06 14:20:22 -04:00
|
|
|
when Array then current_hash[name] << new_hash
|
|
|
|
when Hash then current_hash[name] = [current_hash[name], new_hash]
|
|
|
|
when nil then current_hash[name] = new_hash
|
2010-01-01 06:57:36 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
@hash_stack.push(new_hash)
|
|
|
|
end
|
|
|
|
|
|
|
|
def on_end_element(name)
|
2016-08-06 11:58:50 -04:00
|
|
|
if current_hash.length > current_hash.delete(HASH_SIZE_KEY) && current_hash[CONTENT_KEY].blank? || current_hash[CONTENT_KEY] == ""
|
2010-01-01 06:57:36 -05:00
|
|
|
current_hash.delete(CONTENT_KEY)
|
|
|
|
end
|
|
|
|
@hash_stack.pop
|
|
|
|
end
|
|
|
|
|
|
|
|
def on_characters(string)
|
|
|
|
current_hash[CONTENT_KEY] << string
|
|
|
|
end
|
|
|
|
|
|
|
|
alias_method :on_cdata_block, :on_characters
|
|
|
|
end
|
|
|
|
|
|
|
|
attr_accessor :document_class
|
|
|
|
self.document_class = HashBuilder
|
|
|
|
|
|
|
|
def parse(data)
|
|
|
|
if !data.respond_to?(:read)
|
2016-08-06 11:58:50 -04:00
|
|
|
data = StringIO.new(data || "")
|
2010-01-01 06:57:36 -05:00
|
|
|
end
|
|
|
|
|
2017-04-20 16:31:50 -04:00
|
|
|
if data.eof?
|
2010-01-01 06:57:36 -05:00
|
|
|
{}
|
|
|
|
else
|
2010-01-01 07:44:42 -05:00
|
|
|
LibXML::XML::Error.set_handler(&LibXML::XML::Error::QUIET_HANDLER)
|
2010-01-01 06:57:36 -05:00
|
|
|
parser = LibXML::XML::SaxParser.io(data)
|
2017-01-05 03:20:57 -05:00
|
|
|
document = document_class.new
|
2010-01-01 07:44:42 -05:00
|
|
|
|
2010-01-01 06:57:36 -05:00
|
|
|
parser.callbacks = document
|
|
|
|
parser.parse
|
|
|
|
document.hash
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-01-18 14:45:52 -05:00
|
|
|
end
|