private static void validateWithXsd(Object object, String schemaName) { try { Source source = new JAXBSource(JAXB_CONTEXT, object); XSD_VALIDATOR.validate(source, schemaName); } catch (ValidationException e) { throw new HpdException(e, ErrorType.MALFORMED_REQUEST); } catch (Exception e) { throw new HpdException(e, ErrorType.OTHER); } }
@Test public void testValidate() throws Exception { cache.clear(); Source testXml = new StreamSource(getClass().getResourceAsStream("/xsd/test.xml")); validator.validate(testXml, SCHEMA_RESOURCE); Assert.assertTrue(cache.containsKey(SCHEMA_RESOURCE)); }
@Override public void audit(AuditContext auditContext, AuditMessage... auditMessages) { for (AuditMessage message : auditMessages) { String s = Current.toString(message, true); log.debug(s); if (needValidation) { validator.validate(source(s), NEW_VALIDATION_SCHEMA); } messages.add(message); } }
@Test(expected = ValidationException.class) public void testValidateFails() throws Exception { boolean schemaExisted = cache.containsKey(SCHEMA_RESOURCE); int cacheSize = cache.size(); Source testXml = new StreamSource(getClass().getResourceAsStream( "/xsd/invalidtest.xml")); validator.validate(testXml, SCHEMA_RESOURCE); if (schemaExisted) { Assert.assertEquals(cacheSize, cache.size()); } else { Assert.assertEquals(cacheSize + 1, cache.size()); } }
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); } } }