Class: ChupaText::Decomposers::XML

Inherits:
ChupaText::Decomposer show all
Includes:
Loggable
Defined in:
lib/chupa-text/decomposers/xml.rb

Defined Under Namespace

Classes: Listener

Instance Method Summary collapse

Methods inherited from ChupaText::Decomposer

#initialize, registry, #target_score

Constructor Details

This class inherits a constructor from ChupaText::Decomposer

Instance Method Details

#decompose(data) {|text_data| ... } ⇒ Object

Yields:

  • (text_data)

31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/chupa-text/decomposers/xml.rb', line 31

def decompose(data)
  text = ""
  listener = Listener.new(text)
  data.open do |input|
    begin
      parser = SAXParser.new(input, listener)
      parser.parse
    rescue SAXParser::ParseError => xml_error
      error do
        message = "#{log_tag} Failed to parse XML: "
        message << "#{xml_error.class}: #{xml_error.message}\n"
        message << xml_error.backtrace.join("\n")
        message
      end
      return
    end
  end
  text_data = TextData.new(text, :source_data => data)
  yield(text_data)
end

#target?(data) ⇒ Boolean

Returns:

  • (Boolean)

26
27
28
29
# File 'lib/chupa-text/decomposers/xml.rb', line 26

def target?(data)
  data.extension == "xml" or
    data.mime_type == "text/xml"
end