Class: Threatinator::Parsers::XML::SAXDocument

Inherits:
Nokogiri::XML::SAX::Document
  • Object
show all
Defined in:
lib/threatinator/parsers/xml/sax_document.rb

Instance Method Summary collapse

Constructor Details

#initialize(pattern, cb) ⇒ SAXDocument

Returns a new instance of SAXDocument.



9
10
11
12
13
14
15
16
17
# File 'lib/threatinator/parsers/xml/sax_document.rb', line 9

def initialize(pattern, cb)
  @pattern = pattern
  @max_depth = @pattern.max_depth
  @cb = cb
  @element_stack = Threatinator::Parsers::XML::Path.new
  @parsing_stack = []
  @current_node = nil
  super()
end

Instance Method Details

#characters(str) ⇒ Object Also known as: cdata_block



24
25
26
27
# File 'lib/threatinator/parsers/xml/sax_document.rb', line 24

def characters(str)
  return if @current_node.nil?
  @current_node.append_text(str)
end

#end_element_namespace(name, prefix = nil, uri = nil) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/threatinator/parsers/xml/sax_document.rb', line 47

def end_element_namespace(name, prefix = nil, uri = nil)
  name_sym = name.to_sym
  @element_stack.pop
  return if @parsing_stack.empty?
  @parsing_stack.pop

  if parent = @parsing_stack.last
    parent.add_child(@current_node.build)
    @current_node = parent
  else
    @cb.call(@current_node.build)
    @current_node = nil
  end
end

#start_element_namespace(name, attrs = [], prefix = nil, uri = nil, ns = []) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/threatinator/parsers/xml/sax_document.rb', line 31

def start_element_namespace(name, attrs = [], prefix = nil, uri = nil, ns = [])
  @element_stack.push(name)

  if @parsing_stack.empty?
    if @element_stack.length > @max_depth
      return
    end

    if @pattern.match?(@element_stack)
      start_parsing(name, attrs)
    end
  else
    start_parsing(name, attrs)
  end
end

#start_parsing(name, attributes) ⇒ Object



19
20
21
22
# File 'lib/threatinator/parsers/xml/sax_document.rb', line 19

def start_parsing(name, attributes)
  @current_node = Threatinator::Parsers::XML::NodeBuilder.new(name, attributes)
  @parsing_stack.push(@current_node)
end