Class: Saxaphone::Document

Inherits:
Nokogiri::XML::SAX::Document
  • Object
show all
Defined in:
lib/saxaphone/document.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root_element_class) ⇒ Document

Returns a new instance of Document.



15
16
17
18
# File 'lib/saxaphone/document.rb', line 15

def initialize(root_element_class)
  @element_stack = []
  @root_element_class = root_element_class
end

Instance Attribute Details

#element_stackObject

Returns the value of attribute element_stack.



14
15
16
# File 'lib/saxaphone/document.rb', line 14

def element_stack
  @element_stack
end

#root_elementObject

Returns the value of attribute root_element.



14
15
16
# File 'lib/saxaphone/document.rb', line 14

def root_element
  @root_element
end

#root_element_classObject

Returns the value of attribute root_element_class.



14
15
16
# File 'lib/saxaphone/document.rb', line 14

def root_element_class
  @root_element_class
end

Class Method Details

.parse(io, root_element_class) ⇒ Object



6
7
8
9
10
11
# File 'lib/saxaphone/document.rb', line 6

def parse(io, root_element_class)
  document = new(root_element_class)
  parser = Nokogiri::XML::SAX::Parser.new(document)
  parser.parse(io)
  document.root_element
end

Instance Method Details

#cdata_block(string) ⇒ Object



39
40
41
# File 'lib/saxaphone/document.rb', line 39

def cdata_block(string)
  element_stack.last.append_content(string)
end

#characters(string) ⇒ Object



35
36
37
# File 'lib/saxaphone/document.rb', line 35

def characters(string)
  element_stack.last.append_content(string)
end

#end_element(name) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/saxaphone/document.rb', line 26

def end_element(name)
  ending_element = element_stack.pop
  if element_stack.empty?
    self.root_element = ending_element
  else
    element_stack.last.add_element(ending_element)
  end
end

#start_element(name, attributes = []) ⇒ Object



20
21
22
23
24
# File 'lib/saxaphone/document.rb', line 20

def start_element(name, attributes = [])
  element_class = element_stack.empty? ? root_element_class : element_stack.last.element_for(name)
  new_element = element_class.new(name, '', attributes)
  element_stack << new_element
end