Class: Opener::Coreference::ErrorLayer

Inherits:
Object
  • Object
show all
Defined in:
lib/opener/coreference/error_layer.rb

Overview

Add Error Layer to KAF file instead of throwing an error.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input, error, klass) ⇒ ErrorLayer

Returns a new instance of ErrorLayer.



9
10
11
12
13
14
15
# File 'lib/opener/coreference/error_layer.rb', line 9

def initialize(input, error, klass)
  @input    = input.to_s
  # Make sure there is always a document, even if it is empty.
  @document = Nokogiri::XML(input) rescue Nokogiri::XML(nil)
  @error    = error
  @klass    = klass
end

Instance Attribute Details

#documentObject

Returns the value of attribute document.



7
8
9
# File 'lib/opener/coreference/error_layer.rb', line 7

def document
  @document
end

#errorObject

Returns the value of attribute error.



7
8
9
# File 'lib/opener/coreference/error_layer.rb', line 7

def error
  @error
end

#inputObject

Returns the value of attribute input.



7
8
9
# File 'lib/opener/coreference/error_layer.rb', line 7

def input
  @input
end

#klassObject

Returns the value of attribute klass.



7
8
9
# File 'lib/opener/coreference/error_layer.rb', line 7

def klass
  @klass
end

Instance Method Details

#addObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/opener/coreference/error_layer.rb', line 17

def add
  if is_xml?
    unless has_errors_layer?
      add_errors_layer
    end
  else
    add_root
    add_text
    add_errors_layer
  end
  add_error
  
  xml = !!document.encoding ? document.to_xml : document.to_xml(:encoding => "UTF-8")
  
  return xml
end

#add_errorObject

Add the actual error to the errors layer.



78
79
80
81
82
83
84
85
# File 'lib/opener/coreference/error_layer.rb', line 78

def add_error
  node = document.at('errors')
  error_node = Nokogiri::XML::Node.new "error", node
  error_node['class']   = klass.to_s
  error_node['version'] = klass::VERSION
  error_node.inner_html = error
  node.add_child(error_node)
end

#add_errors_layerObject

Add errors element to the XML file.



59
60
61
62
# File 'lib/opener/coreference/error_layer.rb', line 59

def add_errors_layer
  node = Nokogiri::XML::Node.new "errors", document
  document.root.add_child(node)
end

#add_rootObject

Add root element to the XML file.



44
45
46
47
# File 'lib/opener/coreference/error_layer.rb', line 44

def add_root
  root = Nokogiri::XML::Node.new "KAF", document
  document.add_child(root)
end

#add_textObject

Add the text file incase it is not a valid XML document. More info for debugging.



68
69
70
71
72
73
# File 'lib/opener/coreference/error_layer.rb', line 68

def add_text
  node = Nokogiri::XML::Node.new "raw", document
  node.inner_html = input
  document.root.add_child(node)
  
end

#has_errors_layer?Boolean

Check if the document already has an errors layer.

Returns:

  • (Boolean)


52
53
54
# File 'lib/opener/coreference/error_layer.rb', line 52

def has_errors_layer?
  !!document.at('errors')
end

#is_xml?Boolean

Check if the document is a valid XML file.

Returns:

  • (Boolean)


37
38
39
# File 'lib/opener/coreference/error_layer.rb', line 37

def is_xml?
  !!document.root
end