Class: JXML::Validator

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

Constant Summary collapse

JAR_FILE =
File.join File.dirname(__FILE__), '..', '..', 'ext', 'xmlvalidator.jar'
J_File =
Rjb.import 'java.io.File'
J_Validator =
load_validator_class

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeValidator

Returns a new instance of Validator.



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

def initialize
  @jvalidator = J_Validator.new
end

Class Method Details

.load_validator_classObject



10
11
12
13
14
15
16
17
18
19
# File 'lib/jxml/validator.rb', line 10

def Validator.load_validator_class
  j_URI = Rjb.import 'java.net.URL'
  uri = j_URI.new "file://#{File.expand_path JAR_FILE}"

  j_URLClassLoader = Rjb.import 'java.net.URLClassLoader'
  loader = j_URLClassLoader.new [uri]

  j_Class = Rjb.import 'java.lang.Class'
  j_Class.forName "edu.fcla.da.xml.Validator", true, loader
end

Instance Method Details

#validate(src) ⇒ Object

return a result set hash with the keys :fatals, :errors, :warnings that maps to an array each containing the keys :line, :message, :column



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/jxml/validator.rb', line 31

def validate src

  tio = Tempfile.open 'jxmlvalidator'
  tio.write src.to_s
  tio.flush
  tio.close
  jfile = J_File.new tio.path
  jchecker = @jvalidator.validate jfile
  tio.unlink

  results = {
    :fatals => j2r(jchecker.getFatals),
    :errors => j2r(jchecker.getErrors),
    :warnings => j2r(jchecker.getWarnings)
  }

end