/** * Extracts (constructs) resource creation/application parameters from * validator/transmogrifier parameters. * * @param params validator/transmogrifier parameters. * @return resource creation/application parameters as a Map, * or <code>null</code> if not found. */ protected Map<String, Object> resourceParameters(Object... params) { if (params[0] instanceof Map) { return (Map<String, Object>) params[0]; } else if (params[0] instanceof SchematronProfile) { SchematronProfile p = (SchematronProfile) params[0]; return p.getParameters(); } else if (params.length > 1 && params[1] instanceof Map) { return (Map<String, Object>) params[1]; } return null; }
/** * Extracts (constructs) resource location from validator/transmogrifier parameters. * * @param params validator/transmogrifier parameters. * @return resource location as a String. */ protected String resourceLocation(Object... params) { if (params[0] instanceof String) { return (String) params[0]; } else if (params[0] instanceof Map) { return (String) ((Map<?, ?>) params[0]).get(RESOURCE_LOCATION); } else if (params[0] instanceof SchematronProfile) { SchematronProfile p = (SchematronProfile) params[0]; return p.getRules(); } throw new IllegalStateException("Cannot extract resource location"); }
@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); } } }
@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()); } }
@Override protected String resourceCacheKey(Object... params) { String phase = null; if (params[0] instanceof SchematronProfile) { SchematronProfile schematronProfile = (SchematronProfile) params[0]; Map<String, Object> parameters = schematronProfile.getParameters(); if (parameters != null) { phase = (String) parameters.get("phase"); } } return (phase != null) ? resourceLocation(params) + '\n' + phase : resourceLocation(params); }
@Override public void configure() throws Exception { onException(ValidationException.class).to("mock:error"); from("direct:validator-test").process( helper.validator("testValidator").staticProfile("correct")); from("direct:validator-xml-test").process( helper.xsdValidator().staticProfile("/xsd/test.xsd")).setBody() .constant("passed"); from("direct:validator-schematron-test").process( helper.schematronValidator().staticProfile( new SchematronProfile( "/schematron/schematron-test-rules.xml"))) .setBody().constant("passed"); }