Sha256: 4da81585046579368ece43b4e0e4f2df5b1a26ece6056b944fe1ae9b9d3205f5

Contents?: true

Size: 1.24 KB

Versions: 2

Compression:

Stored size: 1.24 KB

Contents

# frozen_string_literal: true

class Validator
  def self.run_validator(hpxml_doc, stron_path)
    errors = []
    doc = XMLHelper.parse_file(stron_path)
    XMLHelper.get_elements(doc, '/sch:schema/sch:pattern/sch:rule').each do |rule|
      context_xpath = XMLHelper.get_attribute_value(rule, 'context').gsub('h:', '')

      begin
        context_elements = hpxml_doc.xpath(context_xpath)
      rescue
        fail "Invalid xpath: #{context_xpath}"
      end
      next if context_elements.empty? # Skip if context element doesn't exist

      XMLHelper.get_elements(rule, 'sch:assert').each do |assert_element|
        assert_test = XMLHelper.get_attribute_value(assert_element, 'test').gsub('h:', '')

        context_elements.each do |context_element|
          begin
            xpath_result = context_element.xpath(assert_test)
          rescue
            fail "Invalid xpath: #{assert_test}"
          end
          next if xpath_result # check if assert_test is false

          assert_value = assert_element.children.text # the value of sch:assert
          error_message = assert_value.gsub(': ', ": #{context_xpath}: ") # insert context xpath into the error message
          errors << error_message
        end
      end
    end

    return errors
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
urbanopt-cli-0.4.1 example_files/resources/hpxml-measures/HPXMLtoOpenStudio/resources/validator.rb
urbanopt-cli-0.4.0 example_files/resources/hpxml-measures/HPXMLtoOpenStudio/resources/validator.rb