Class: Opener::Coreference::ErrorLayer
- Inherits:
-
Object
- Object
- Opener::Coreference::ErrorLayer
- Defined in:
- lib/opener/coreference/error_layer.rb
Overview
Add Error Layer to KAF file instead of throwing an error.
Instance Attribute Summary collapse
-
#document ⇒ Object
Returns the value of attribute document.
-
#error ⇒ Object
Returns the value of attribute error.
-
#input ⇒ Object
Returns the value of attribute input.
-
#klass ⇒ Object
Returns the value of attribute klass.
Instance Method Summary collapse
- #add ⇒ Object
-
#add_error ⇒ Object
Add the actual error to the errors layer.
-
#add_errors_layer ⇒ Object
Add errors element to the XML file.
-
#add_root ⇒ Object
Add root element to the XML file.
-
#add_text ⇒ Object
Add the text file incase it is not a valid XML document.
-
#has_errors_layer? ⇒ Boolean
Check if the document already has an errors layer.
-
#initialize(input, error, klass) ⇒ ErrorLayer
constructor
A new instance of ErrorLayer.
-
#is_xml? ⇒ Boolean
Check if the document is a valid XML file.
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
#document ⇒ Object
Returns the value of attribute document.
7 8 9 |
# File 'lib/opener/coreference/error_layer.rb', line 7 def document @document end |
#error ⇒ Object
Returns the value of attribute error.
7 8 9 |
# File 'lib/opener/coreference/error_layer.rb', line 7 def error @error end |
#input ⇒ Object
Returns the value of attribute input.
7 8 9 |
# File 'lib/opener/coreference/error_layer.rb', line 7 def input @input end |
#klass ⇒ Object
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
#add ⇒ Object
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_error ⇒ Object
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_layer ⇒ Object
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_root ⇒ Object
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_text ⇒ Object
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.
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.
37 38 39 |
# File 'lib/opener/coreference/error_layer.rb', line 37 def is_xml? !!document.root end |