Class: Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/modsulator/validator.rb

Overview

Validates XML against the MODSulator schema.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema_file = '') ⇒ Validator

Returns a new instance of Validator.

Parameters:

  • schema_file (defaults to: '')

    Full path to the desired .xsd file. If none is given, the built-in file will be used.



10
11
12
13
14
15
16
# File 'lib/modsulator/validator.rb', line 10

def initialize(schema_file = '')
  if(schema_file == '')
    @schema = Nokogiri::XML::Schema(File.read(File.expand_path("../modsulator.xsd", __FILE__)))
  else
    @schema = Nokogiri::XML::Schema(File.read(schema_file))
  end
end

Instance Attribute Details

#schemaObject (readonly)

The Nokogiri::XML::Schema instance used for validation.



7
8
9
# File 'lib/modsulator/validator.rb', line 7

def schema
  @schema
end

Instance Method Details

#validate_xml_doc(doc) ⇒ Object

Validates an XML document.

Parameters:

  • doc

    An instance of Nokogiri::XML::Document

Returns:

  • An array containing holds Nokogiri::XML::SyntaxError elements. If this array has length zero, the document is valid.



32
33
34
35
# File 'lib/modsulator/validator.rb', line 32

def validate_xml_doc(doc)
  return doc.errors if(doc.errors.length > 0)
  return @schema.validate(doc)
end

#validate_xml_string(xml) ⇒ Object

Validates an XML string.

Parameters:

  • xml

    An XML document as a string.

Returns:

  • An array containing holds Nokogiri::XML::SyntaxError elements. If this array has length zero, the document is valid.



22
23
24
25
# File 'lib/modsulator/validator.rb', line 22

def validate_xml_string(xml)
  xml_doc = Nokogiri::XML(xml)
  return validate_xml_doc(xml_doc)
end