Class: Services::MdsSaxHandler

Inherits:
Object
  • Object
show all
Includes:
LibXML::XML::SaxParser::Callbacks
Defined in:
app/utils/services/mds_xml_file_parser.rb

Overview

—————————————————————————-# Using LibXML with SAX API for performance reasons. # —————————————————————————-#

Constant Summary collapse

ROOT_NESTING_LEVEL =
0
ATTRIBUTE_NESTING_LEVEL =
1
TEXT_NESTING_LEVEL =
2

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMdsSaxHandler

Returns a new instance of MdsSaxHandler.



74
75
76
77
78
79
# File 'app/utils/services/mds_xml_file_parser.rb', line 74

def initialize
  @assessment_hash = {}
  @file_errors = []
  @xml_nesting_level = ROOT_NESTING_LEVEL
  @element_text = nil
end

Instance Attribute Details

#assessment_hashObject (readonly)

Returns the value of attribute assessment_hash.



68
69
70
# File 'app/utils/services/mds_xml_file_parser.rb', line 68

def assessment_hash
  @assessment_hash
end

#errorsObject (readonly)

Returns the value of attribute errors.



68
69
70
# File 'app/utils/services/mds_xml_file_parser.rb', line 68

def errors
  @errors
end

Instance Method Details

#on_characters(text) ⇒ Object



89
90
91
# File 'app/utils/services/mds_xml_file_parser.rb', line 89

def on_characters(text)
  @element_text << text if @xml_nesting_level == TEXT_NESTING_LEVEL
end

#on_end_element(element_name) ⇒ Object



93
94
95
96
# File 'app/utils/services/mds_xml_file_parser.rb', line 93

def on_end_element(element_name)
  @assessment_hash[element_name.downcase] = @element_text.strip if !@element_text.nil? && @xml_nesting_level == TEXT_NESTING_LEVEL 
  @xml_nesting_level -= 1
end

#on_error(error) ⇒ Object



98
99
100
# File 'app/utils/services/mds_xml_file_parser.rb', line 98

def on_error(error)
  @file_errors << error.message
end

#on_start_element(element_name, attributes) ⇒ Object



81
82
83
84
85
86
87
# File 'app/utils/services/mds_xml_file_parser.rb', line 81

def on_start_element(element_name, attributes)
  if @xml_nesting_level == ROOT_NESTING_LEVEL
    @file_errors << "XML root element ASSESSMENT not found." unless element_name == "ASSESSMENT"
  end
  @xml_nesting_level += 1
  @element_text = ''
end