/** * Creates a new {@link ValidatorAdapter} that adapts the given * <code>XsdValidator</code>. * * @return an adapted validator. */ public ValidatorAdapter xsdValidator() { ValidatorAdapter adapter = new ValidatorAdapter(new XsdValidator()); adapter.input(Builder.bodyAs(StreamSource.class)); return adapter; }
@Override public void validate(Source message, String schema) { List<ValidationException> exceptions = doValidate(message, schema); if (! exceptions.isEmpty()) { throw new ValidationException(exceptions); } }
@Override protected Schema createResource(Object... params) { // SchemaFactory is neither thread-safe nor reentrant SchemaFactory factory = SchemaFactory.newInstance(getSchemaLanguage()); // Register resource resolver to resolve external XML schemas factory.setResourceResolver(RESOURCE_RESOLVER); try { return factory.newSchema(resourceContent(params)); } catch (SAXException e) { throw new IllegalArgumentException("Could not initialize XSD schema", e); } }
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); } }
/** * @param message * the message to be validated * @param schemaResource * the XML schema to validate against * @return an array of validation exceptions */ protected List<ValidationException> doValidate(Source message, String schemaResource) { try { LOG.debug("Validating XML message"); Schema schema = resource(schemaResource); javax.xml.validation.Validator validator = schema.newValidator(); CollectingErrorHandler errorHandler = new CollectingErrorHandler(); validator.setErrorHandler(errorHandler); validator.validate(message); List<ValidationException> exceptions = errorHandler.getExceptions(); if (! exceptions.isEmpty()) { LOG.debug("Message validation found {} problems", exceptions.size()); } else { LOG.debug("Message validation successful"); } return exceptions; } catch (Exception e) { return Collections.singletonList(new ValidationException( "Unexpected validation failure because " + e.getMessage(), e)); } }
@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); } }
/** * Interprets the defined profile as W3C schema location and validates against it */ public ValidatorAdapterDefinition xsd() { validator = new XsdValidator(); return (ValidatorAdapterDefinition)input(bodyAs(StreamSource.class)); }
@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()); } }
@Before public void setUp() throws Exception { validator = new XsdValidator(); Field field = XsdValidator.class.getDeclaredField("XSD_CACHE"); field.setAccessible(true); cache = (Map<String, ?>) field.get(null); }
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); } } }