Class: Saxy::Parser
- Inherits:
-
Nokogiri::XML::SAX::Document
- Object
- Nokogiri::XML::SAX::Document
- Saxy::Parser
- Includes:
- Enumerable
- Defined in:
- lib/saxy/parser.rb
Instance Attribute Summary collapse
-
#callback ⇒ Object
readonly
Will yield objects inside the callback after they’re built.
-
#elements ⇒ Object
readonly
Stack of elements built while traversing XML tree.
-
#tags ⇒ Object
readonly
Stack of XML tags built while traversing XML tree.
Instance Method Summary collapse
- #cdata_block(cdata) ⇒ Object
- #characters(chars) ⇒ Object
- #current_element ⇒ Object
- #each(&blk) ⇒ Object
- #end_element(tag) ⇒ Object
- #error(message) ⇒ Object
-
#initialize(object, object_tag, encoding = nil) ⇒ Parser
constructor
A new instance of Parser.
- #start_element(tag, attributes = []) ⇒ Object
Constructor Details
#initialize(object, object_tag, encoding = nil) ⇒ Parser
Returns a new instance of Parser.
19 20 21 22 23 |
# File 'lib/saxy/parser.rb', line 19 def initialize(object, object_tag, encoding=nil) @object, @object_tag = object, object_tag @tags, @elements = [], [] @encoding = encoding end |
Instance Attribute Details
#callback ⇒ Object (readonly)
Will yield objects inside the callback after they’re built
17 18 19 |
# File 'lib/saxy/parser.rb', line 17 def callback @callback end |
#elements ⇒ Object (readonly)
Stack of elements built while traversing XML tree
First element is pushed to the stack only after finding the object_tag in the XML tree.
14 15 16 |
# File 'lib/saxy/parser.rb', line 14 def elements @elements end |
#tags ⇒ Object (readonly)
Stack of XML tags built while traversing XML tree
8 9 10 |
# File 'lib/saxy/parser.rb', line 8 def @tags end |
Instance Method Details
#cdata_block(cdata) ⇒ Object
50 51 52 |
# File 'lib/saxy/parser.rb', line 50 def cdata_block(cdata) current_element.append_value(cdata) if current_element end |
#characters(chars) ⇒ Object
54 55 56 |
# File 'lib/saxy/parser.rb', line 54 def characters(chars) current_element.append_value(chars) if current_element end |
#current_element ⇒ Object
62 63 64 |
# File 'lib/saxy/parser.rb', line 62 def current_element elements.last end |
#each(&blk) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/saxy/parser.rb', line 66 def each(&blk) return to_enum unless blk @callback = blk args = [self, @encoding].compact parser = Nokogiri::XML::SAX::Parser.new(*args) if @object.respond_to?(:read) && @object.respond_to?(:close) parser.parse_io(@object) else parser.parse_file(@object) end end |
#end_element(tag) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/saxy/parser.rb', line 37 def end_element(tag) .pop if element = elements.pop object = element.to_h if current_element current_element.set_attribute(tag, object) elsif callback callback.call(object) end end end |
#error(message) ⇒ Object
58 59 60 |
# File 'lib/saxy/parser.rb', line 58 def error() raise ParsingError.new() end |
#start_element(tag, attributes = []) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/saxy/parser.rb', line 25 def start_element(tag, attributes=[]) @tags << tag if tag == @object_tag || elements.any? elements << Element.new attributes.each do |(attr, value)| current_element.set_attribute(attr, value) end end end |