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

Creates a new Validator, using either a provided XML Schema file or the built-in one.

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.



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

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.



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

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.



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

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