Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 88 lines (70 sloc) 2.188 kB
5132081 @spastorino AS gem doesn't depend on nokogiri so shows a nicer error if users hav…
spastorino authored
1 begin
2 require 'nokogiri'
3 rescue LoadError => e
4 $stderr.puts "You don't have nokogiri installed in your application. Please add it to your Gemfile and run bundle install"
5 raise e
6 end
76f024a @fxn adds missing requires for Object#blank? and Object#present?
fxn authored
7 require 'active_support/core_ext/object/blank'
7091d80 @apotonick fixed a missing require that causes trouble when using AS in a non-ra…
apotonick authored
8 require 'stringio'
d7f9b9f @wvanbergen Added SAX-based parser for XmlMini, using Nokogiri.
wvanbergen authored
9
10 module ActiveSupport
7c90d91 @vijaydev Clean up module docs [ci skip]
vijaydev authored
11 module XmlMini_NokogiriSAX #:nodoc:
d7f9b9f @wvanbergen Added SAX-based parser for XmlMini, using Nokogiri.
wvanbergen authored
12 extend self
13
14 # Class that will build the hash while the XML document
15 # is being parsed using SAX events.
16 class HashBuilder < Nokogiri::XML::SAX::Document
17
18 CONTENT_KEY = '__content__'.freeze
19 HASH_SIZE_KEY = '__hash_size__'.freeze
20
21 attr_reader :hash
22
23 def current_hash
24 @hash_stack.last
25 end
26
27 def start_document
28 @hash = {}
29 @hash_stack = [@hash]
30 end
31
32 def end_document
33 raise "Parse stack not empty!" if @hash_stack.size > 1
34 end
35
36 def error(error_message)
689984d @wvanbergen Fixed some bugs and fixed some tests in new SAX-based XmlMini backends.
wvanbergen authored
37 raise error_message
d7f9b9f @wvanbergen Added SAX-based parser for XmlMini, using Nokogiri.
wvanbergen authored
38 end
39
40 def start_element(name, attrs = [])
254b0a4 @tenderlove using a merge with a Hash[] rather than a loop to add SAX parsed attr…
tenderlove authored
41 new_hash = { CONTENT_KEY => '' }.merge(Hash[attrs])
d7f9b9f @wvanbergen Added SAX-based parser for XmlMini, using Nokogiri.
wvanbergen authored
42 new_hash[HASH_SIZE_KEY] = new_hash.size + 1
43
44 case current_hash[name]
45 when Array then current_hash[name] << new_hash
46 when Hash then current_hash[name] = [current_hash[name], new_hash]
47 when nil then current_hash[name] = new_hash
48 end
49
50 @hash_stack.push(new_hash)
51 end
52
53 def end_element(name)
54 if current_hash.length > current_hash.delete(HASH_SIZE_KEY) && current_hash[CONTENT_KEY].blank? || current_hash[CONTENT_KEY] == ''
55 current_hash.delete(CONTENT_KEY)
56 end
57 @hash_stack.pop
58 end
59
60 def characters(string)
61 current_hash[CONTENT_KEY] << string
62 end
63
64 alias_method :cdata_block, :characters
65 end
66
67 attr_accessor :document_class
68 self.document_class = HashBuilder
69
70 def parse(data)
71 if !data.respond_to?(:read)
72 data = StringIO.new(data || '')
73 end
74
75 char = data.getc
76 if char.nil?
77 {}
78 else
79 data.ungetc(char)
80 document = self.document_class.new
81 parser = Nokogiri::XML::SAX::Parser.new(document)
82 parser.parse(data)
83 document.hash
84 end
85 end
86 end
5132081 @spastorino AS gem doesn't depend on nokogiri so shows a nicer error if users hav…
spastorino authored
87 end
Something went wrong with that request. Please try again.