/** * Creates a new {@link ValidatorAdapter} that adapts the given * <code>SchematronValidator</code>. * * @return an adapted validator. */ public ValidatorAdapter schematronValidator() { ValidatorAdapter adapter = new ValidatorAdapter(new SchematronValidator()); adapter.input(Builder.bodyAs(StreamSource.class)); return adapter; }
public void validate(Source message, SchematronProfile profile) { SchematronOutput svrl = schematronTransmogrifier.zap(message, profile); List<ValidationException> exceptions = new ArrayList<>(); svrl.getActivePatternAndFiredRuleAndFailedAssert().stream() .filter(o -> o instanceof FailedAssert) .forEach(o -> { FailedAssert failedAssert = (FailedAssert) o; exceptions.add(new ValidationException(message(failedAssert))); }); if (!exceptions.isEmpty()) { throw new SchematronValidationException(exceptions, svrl); } }
@Test public void testValidate() throws IOException { Source testXml = new StreamSource(getClass().getResourceAsStream("/schematron/schematron-test.xml")); v.validate(testXml, new SchematronProfile("/schematron/schematron-test-rules.xml")); }
public void validate(String message, CombinedXmlValidationProfile profile) throws ValidationException { requireNonNull(profile, "validation profile must be not null"); // check whether the root element name is valid String rootElementName = rootElementName(requireNonNull(message, "message must be not null")); if (! profile.isValidRootElement(rootElementName)) { throw new ValidationException("Invalid root element '" + rootElementName + "'"); } // XSD validation String xsdPath = profile.getXsdPath(rootElementName); if (xsdPath != null) { XSD_VALIDATOR.validate(source(message), xsdPath); } // Schematron validation String schematronPath = profile.getSchematronPath(rootElementName); if (schematronPath != null) { SchematronProfile schematronProfile = new SchematronProfile( schematronPath, profile.getCustomSchematronParameters(rootElementName)); SCHEMATRON_VALIDATOR.validate(source(message), schematronProfile); } } }
/** * Interprets the defined profile as Schematron rules location and validates against it */ public ValidatorAdapterDefinition schematron() { validator = new SchematronValidator(); return (ValidatorAdapterDefinition)input(bodyAs(StreamSource.class)); }
@Test public void testValidateFail() throws IOException { Source testXml = new StreamSource(getClass().getResourceAsStream( "/schematron/schematron-test-fail.xml")); try { v.validate(testXml, new SchematronProfile("/schematron/schematron-test-rules.xml")); fail("This line shall be not reachable"); } catch (SchematronValidationException e) { Throwable[] cause = e.getCauses(); assertEquals(3, cause.length); assertEquals(10, e.getSvrl().getActivePatternAndFiredRuleAndFailedAssert().size()); } }
@Before public void setUp() throws Exception { v = new SchematronValidator(); }