Module: CqmValidators::Schematron::JavaProcessor

Included in:
Validator
Defined in:
lib/schematron/java_processor.rb

Defined Under Namespace

Classes: HdsUrlResolver

Constant Summary collapse

ISO_SCHEMATRON2 =
File.join(DIR, 'lib/schematron/iso-schematron-xslt2/iso_svrl_for_xslt2.xsl')

Instance Method Summary collapse

Instance Method Details

#build_transformer(xslt, input_file, url = false) ⇒ Object



79
80
81
82
83
84
85
86
87
# File 'lib/schematron/java_processor.rb', line 79

def build_transformer(xslt, input_file, url = false)
  factory = TransformerFactory.newInstance(TRANSFORMER_FACTORY_IMPL, nil)
  factory.uri_resolver = HdsUrlResolver.new(@schematron_file) if url
  transformer = factory.new_transformer(StreamSource.new(xslt))
  sw = StringWriter.new
  output = StreamResult.new(sw)
  transformer.transform(input_file, output)
  sw.to_s
end

#get_document_j(doc) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/schematron/java_processor.rb', line 46

def get_document_j(doc)
  case doc
  when File
    java.io.File.new(doc.path)
  else
    StringReader.new(doc.to_s)
  end
end

#get_errors(document) ⇒ Object



40
41
42
43
44
# File 'lib/schematron/java_processor.rb', line 40

def get_errors(document)
  document_j = get_document_j(document)
  output = build_transformer(StringReader.new(processor), StreamSource.new(document_j), true)
  Nokogiri::XML(output)
end

#processorObject



55
56
57
# File 'lib/schematron/java_processor.rb', line 55

def processor
  @processor ||= build_transformer(java.io.File.new(ISO_SCHEMATRON2), schematron_file)
end

#schematron_fileObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/schematron/java_processor.rb', line 59

def schematron_file
  # this allows us to run the validation utility app in jBoss/TorqueBox
  # for some reason it breaks the first time you call DocumentBuilderFactory,
  # so the solution is to catch the error and retry
  # TODO: pull this out when the above is no longer the case.
  begin
    dbf = DocumentBuilderFactory.new_instance
  rescue Exception
    retry
  end
  dbf.setIgnoringElementContentWhitespace(true)
  db = dbf.new_document_builder
  document = db.parse(java.io.File.new(@schematron_file))

  root = document.document_element
  root.set_attribute('defaultPhase', 'errors')

  DOMSource.new(root)
end