Class: Saxy::Parser

Inherits:
Nokogiri::XML::SAX::Document
  • Object
show all
Includes:
Enumerable
Defined in:
lib/saxy/parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, object_tag, options = {}) ⇒ Parser

Returns a new instance of Parser.



25
26
27
28
# File 'lib/saxy/parser.rb', line 25

def initialize(object, object_tag, options={})
  @object, @object_tag, @options = object, object_tag, options
  @tags, @elements = [], []
end

Instance Attribute Details

#callbackObject (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

#contextObject (readonly)

Parser context



20
21
22
# File 'lib/saxy/parser.rb', line 20

def context
  @context
end

#elementsObject (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

#optionsObject (readonly)

Parser options



23
24
25
# File 'lib/saxy/parser.rb', line 23

def options
  @options
end

#tagsObject (readonly)

Stack of XML tags built while traversing XML tree



8
9
10
# File 'lib/saxy/parser.rb', line 8

def tags
  @tags
end

Instance Method Details

#cdata_block(cdata) ⇒ Object



56
57
58
# File 'lib/saxy/parser.rb', line 56

def cdata_block(cdata)
  current_element.append_value(cdata) if current_element
end

#characters(chars) ⇒ Object



60
61
62
# File 'lib/saxy/parser.rb', line 60

def characters(chars)
  current_element.append_value(chars) if current_element
end

#context_blkObject



93
94
95
96
97
98
99
100
101
# File 'lib/saxy/parser.rb', line 93

def context_blk
  proc { |context|
    [:recovery, :replace_entities].each do |key|
      context.send("#{key}=", options[key]) if options.has_key?(key)
    end

    @context = context
  }
end

#current_elementObject



74
75
76
# File 'lib/saxy/parser.rb', line 74

def current_element
  elements.last
end

#each(&blk) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/saxy/parser.rb', line 78

def each(&blk)
  return to_enum unless blk

  @callback = blk

  args   = [self, options[:encoding]].compact
  parser = Nokogiri::XML::SAX::Parser.new(*args)

  if @object.respond_to?(:read) && @object.respond_to?(:close)
    parser.parse_io(@object, &context_blk)
  else
    parser.parse_file(@object, &context_blk)
  end
end

#end_element(tag) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/saxy/parser.rb', line 42

def end_element(tag)
  tags.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



64
65
66
67
68
69
70
71
72
# File 'lib/saxy/parser.rb', line 64

def error(message)
  error = ParsingError.new(message, context)

  if options[:error_handler].respond_to?(:call)
    options[:error_handler].call(error)
  else
    raise error
  end
end

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



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/saxy/parser.rb', line 30

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