Class: Ape::SchemaValidator

Inherits:
Validator show all
Defined in:
lib/ape/validators/schema_validator.rb

Instance Attribute Summary

Attributes inherited from Validator

#authent, #reporter

Instance Method Summary collapse

Methods inherited from Validator

custom_validators, instance

Methods included from Util

extended, included

Methods included from ValidatorDsl

included

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Ape::Validator

Instance Method Details

#rnc_validate(schema, text, name, ape) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/ape/validators/schema_validator.rb', line 25

def rnc_validate(schema, text, name, ape)
  schemaError = StringWriter.new
  schemaEH = ErrorHandlerImpl.new(schemaError)
  properties = PropertyMapBuilder.new
  properties.put(ValidateProperty::ERROR_HANDLER, schemaEH)
  error = nil
  driver = ValidationDriver.new(properties.toPropertyMap, CompactSchemaReader.getInstance)
  if driver.loadSchema(InputSource.new(StringReader.new(schema)))
    begin
      if !driver.validate(InputSource.new(StringReader.new(text)))
        error = schemaError.toString
      end
    rescue org.xml.sax.SAXParseException
      error = $!.to_s.sub(/\n.*$/, '')
    end
  else
    error = schemaError.toString
  end

  if !error
    reporter.add(self, :success, "#{name} passed schema validation.")
    true
  else
    # this kind of sucks, but I spent a looong time lost in a maze of twisty
    #  little passages without being able to figure out how to
    #  tell jing what name I'd like to call the InputSource
    reporter.add(self, :error, "#{name} failed schema validation:\n" + error.gsub('(unknown file):', 'Line '))
    false
  end
end

#validate(opts = {}) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/ape/validators/schema_validator.rb', line 16

def validate(opts = {})
  if RUBY_PLATFORM =~ /java/
    rcn_validate(opts[:schema], opts[:doc].body, opts[:title])
  else
    reporter.add(self, :info, "Schema validation is just available building the ape with jruby.")
    true
  end
end