use of com.helger.schematron.pure.exchange.PSReader in project ph-schematron by phax.
the class DocumentationExamples method validateXMLViaPureSchematron2.
public static boolean validateXMLViaPureSchematron2(@Nonnull final File aSchematronFile, @Nonnull final File aXMLFile) throws Exception {
// Read the schematron from file
final PSSchema aSchema = new PSReader(new FileSystemResource(aSchematronFile)).readSchema();
if (!aSchema.isValid(new DoNothingPSErrorHandler()))
throw new IllegalArgumentException("Invalid Schematron!");
// Resolve the query binding to use
final IPSQueryBinding aQueryBinding = PSQueryBindingRegistry.getQueryBindingOfNameOrThrow(aSchema.getQueryBinding());
// Pre-process schema
final PSPreprocessor aPreprocessor = new PSPreprocessor(aQueryBinding);
aPreprocessor.setKeepTitles(true);
final PSSchema aPreprocessedSchema = aPreprocessor.getAsPreprocessedSchema(aSchema);
// Bind the pre-processed schema
final IPSBoundSchema aBoundSchema = aQueryBinding.bind(aPreprocessedSchema, null, null, null, null);
// Read the XML file
final Document aXMLNode = DOMReader.readXMLDOM(aXMLFile);
if (aXMLNode == null)
return false;
// Perform the validation
return aBoundSchema.validatePartially(aXMLNode, FileHelper.getAsURLString(aXMLFile)).isValid();
}
use of com.helger.schematron.pure.exchange.PSReader in project ph-schematron by phax.
the class PSXPathBoundSchemaTest method testBindAllInvalidSchematrons.
@Test
public void testBindAllInvalidSchematrons() {
for (final IReadableResource aRes : SchematronTestHelper.getAllInvalidSchematronFiles()) {
System.out.println(aRes);
try {
// Parse the schema
final PSSchema aSchema = new PSReader(aRes).readSchema();
final CollectingPSErrorHandler aCEH = new CollectingPSErrorHandler();
PSXPathQueryBinding.getInstance().bind(aSchema, null, aCEH);
// Either an ERROR was collected or an exception was thrown
assertTrue(aCEH.getErrorList().getMostSevereErrorLevel().isGE(EErrorLevel.ERROR));
} catch (final SchematronException ex) {
System.out.println(" " + ex.getMessage());
}
}
}
use of com.helger.schematron.pure.exchange.PSReader in project ph-schematron by phax.
the class PSXPathBoundSchemaTest method testSchematronValidation.
@Test
public void testSchematronValidation() throws SAXException, SchematronException {
for (int i = 0; i < SCH.length; ++i) {
final IReadableResource aSchRes = new ClassPathResource("test-sch/" + SCH[i]);
final IReadableResource aXmlRes = new ClassPathResource("test-xml/" + XML[i]);
// Resolve all includes
final IMicroDocument aDoc = SchematronHelper.getWithResolvedSchematronIncludes(aSchRes);
assertNotNull(aDoc);
// Read to domain object
final PSReader aReader = new PSReader(aSchRes);
final PSSchema aSchema = aReader.readSchemaFromXML(aDoc.getDocumentElement());
assertNotNull(aSchema);
// Create a compiled schema
final String sPhaseID = null;
final IPSErrorHandler aErrorHandler = null;
final IPSBoundSchema aBoundSchema = PSXPathQueryBinding.getInstance().bind(aSchema, sPhaseID, aErrorHandler);
// Validate completely
final SchematronOutputType aSVRL = aBoundSchema.validateComplete(DOMReader.readXMLDOM(aXmlRes), aXmlRes.getAsURL().toExternalForm());
assertNotNull(aSVRL);
if (false)
System.out.println(new SVRLMarshaller().getAsString(aSVRL));
}
}
use of com.helger.schematron.pure.exchange.PSReader in project ph-schematron by phax.
the class Issue48Test method validateAndProduceSVRL.
public static void validateAndProduceSVRL(@Nonnull final File aSchematron, final File aXML) throws Exception {
final PSSchema aSchema = new PSReader(new FileSystemResource(aSchematron)).readSchema();
final PSPreprocessor aPreprocessor = new PSPreprocessor(PSXPathQueryBinding.getInstance());
final PSSchema aPreprocessedSchema = aPreprocessor.getAsPreprocessedSchema(aSchema);
final String sSCH = new PSWriter(new PSWriterSettings().setXMLWriterSettings(new XMLWriterSettings())).getXMLString(aPreprocessedSchema);
if (false)
System.out.println(sSCH);
final SchematronResourceSCH aSCH = new SchematronResourceSCH(new ReadableResourceString(sSCH, StandardCharsets.UTF_8));
// Perform validation
final SchematronOutputType aSVRL = aSCH.applySchematronValidationToSVRL(new FileSystemResource(aXML));
assertNotNull(aSVRL);
if (false)
System.out.println(new SVRLMarshaller().getAsString(aSVRL));
}
use of com.helger.schematron.pure.exchange.PSReader in project ph-schematron by phax.
the class SchematronPreprocess method execute.
@Override
public void execute() throws BuildException {
boolean bCanRun = false;
if (m_aSrcFile == null)
_error("No source Schematron file specified!");
else if (m_aSrcFile.exists() && !m_aSrcFile.isFile())
_error("The specified source Schematron file " + m_aSrcFile + " is not a file!");
else if (m_aDstFile == null)
_error("No destination Schematron file specified!");
else if (m_aDstFile.exists() && !m_aDstFile.isFile())
_error("The specified destination Schematron file " + m_aDstFile + " is not a file!");
else
bCanRun = true;
if (bCanRun)
try {
// Read source
final PSSchema aSchema = new PSReader(new FileSystemResource(m_aSrcFile)).readSchema();
// Setup preprocessor
final PSPreprocessor aPreprocessor = new PSPreprocessor(PSXPathQueryBinding.getInstance());
aPreprocessor.setKeepTitles(m_bKeepTitles);
aPreprocessor.setKeepDiagnostics(m_bKeepDiagnostics);
aPreprocessor.setKeepReports(m_bKeepReports);
aPreprocessor.setKeepEmptyPatterns(m_bKeepEmptyPatterns);
aPreprocessor.setKeepEmptySchema(true);
// Main pre-processing
final PSSchema aPreprocessedSchema = aPreprocessor.getAsPreprocessedSchema(aSchema);
// Write the result file
new PSWriter(new PSWriterSettings().setXMLWriterSettings(new XMLWriterSettings())).writeToFile(aPreprocessedSchema, m_aDstFile);
log("Successfully pre-processed Schematron " + m_aSrcFile + " to " + m_aDstFile);
} catch (final SchematronReadException | SchematronPreprocessException ex) {
_error("Error processing Schemtron " + m_aSrcFile.getAbsolutePath(), ex);
}
}
Aggregations