private void validateDOMSource(DOMSource domSource, SAXParser parser, ValidationErrorHandler errorHandler) throws IOException, SAXException { try { // Sadly, JAXP 1.0 DOM doesn't implement DOM level 3, so we cannot use Document.normalizeDocument() // Instead, we write the Document to a Stream, and validate that Transformer transformer = transformerFactory.newTransformer(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); transformer.transform(domSource, new StreamResult(outputStream)); ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray()); validateStreamSource(new StreamSource(inputStream), parser, errorHandler); } catch (TransformerException ex) { throw new XmlValidationException("Could not validate DOM source: " + ex.getMessage(), ex); } }
private SAXParser createSAXParser() { try { SAXParser parser = parserFactory.newSAXParser(); parser.setProperty(SCHEMA_LANGUAGE, schemaLanguage); parser.setProperty(SCHEMA_SOURCE, schemaInputSources); return parser; } catch (ParserConfigurationException ex) { throw new XmlValidationException("Could not create SAXParser: " + ex.getMessage(), ex); } catch (SAXException ex) { throw new XmlValidationException("Could not create SAXParser: " + ex.getMessage(), ex); } } }
public SAXParseException[] validate(Source source) throws IOException { javax.xml.validation.Validator validator = schema.newValidator(); ValidationErrorHandler errorHandler = new ValidationErrorHandler(); validator.setErrorHandler(errorHandler); try { validator.validate(source); return errorHandler.getErrors(); } catch (SAXException ex) { throw new XmlValidationException("Could not validate source: " + ex.getMessage(), ex); } } }
static XmlValidator createValidator(Resource[] resources, String schemaLanguage) throws IOException { try { Schema schema = SchemaLoaderUtils.loadSchema(resources, schemaLanguage); return new Jaxp15Validator(schema); } catch (SAXException ex) { throw new XmlValidationException("Could not create Schema: " + ex.getMessage(), ex); } }
static XmlValidator createValidator(Resource[] resources, String schemaLanguage) throws IOException { try { Schema schema = SchemaLoaderUtils.loadSchema(resources, schemaLanguage); return new Jaxp13Validator(schema); } catch (SAXException ex) { throw new XmlValidationException("Could not create Schema: " + ex.getMessage(), ex); } }
static XmlValidator createValidator(Resource[] resources, String schemaLanguage) throws IOException { try { Schema schema = SchemaLoaderUtils.loadSchema(resources, schemaLanguage); return new Jaxp13Validator(schema); } catch (SAXException ex) { throw new XmlValidationException("Could not create Schema: " + ex.getMessage(), ex); } }
static XmlValidator createValidator(Resource[] resources, String schemaLanguage) throws IOException { try { Schema schema = SchemaLoaderUtils.loadSchema(resources, schemaLanguage); return new Jaxp13Validator(schema); } catch (SAXException ex) { throw new XmlValidationException("Could not create Schema: " + ex.getMessage(), ex); } }
static XmlValidator createValidator(Resource[] resources, String schemaLanguage) throws IOException { try { Schema schema = SchemaLoaderUtils.loadSchema(resources, schemaLanguage); return new Jaxp15Validator(schema); } catch (SAXException ex) { throw new XmlValidationException("Could not create Schema: " + ex.getMessage(), ex); } }
@Override public SAXParseException[] validate(Source source, ValidationErrorHandler errorHandler) throws IOException { if (errorHandler == null) { errorHandler = new DefaultValidationErrorHandler(); } Validator validator = schema.newValidator(); validator.setErrorHandler(errorHandler); try { validator.validate(source); return errorHandler.getErrors(); } catch (SAXException ex) { throw new XmlValidationException("Could not validate source: " + ex.getMessage(), ex); } } }
@Override public SAXParseException[] validate(Source source, ValidationErrorHandler errorHandler) throws IOException { if (errorHandler == null) { errorHandler = new DefaultValidationErrorHandler(); } Validator validator = schema.newValidator(); validator.setErrorHandler(errorHandler); try { validator.validate(source); return errorHandler.getErrors(); } catch (SAXException ex) { throw new XmlValidationException("Could not validate source: " + ex.getMessage(), ex); } } }
@Override public SAXParseException[] validate(Source source, ValidationErrorHandler errorHandler) throws IOException { if (errorHandler == null) { errorHandler = new DefaultValidationErrorHandler(); } Validator validator = schema.newValidator(); try { validator.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, ""); } catch (SAXNotRecognizedException | SAXNotSupportedException e) { if (log.isWarnEnabled()) { log.warn(XMLConstants.ACCESS_EXTERNAL_DTD + " property not supported by " + validator.getClass().getCanonicalName()); } } try { validator.setProperty(XMLConstants.ACCESS_EXTERNAL_SCHEMA, ""); } catch (SAXNotRecognizedException | SAXNotSupportedException e) { if (log.isWarnEnabled()) { log.warn(XMLConstants.ACCESS_EXTERNAL_SCHEMA + " property not supported by " + validator.getClass().getCanonicalName()); } } validator.setErrorHandler(errorHandler); try { validator.validate(source); return errorHandler.getErrors(); } catch (SAXException ex) { throw new XmlValidationException("Could not validate source: " + ex.getMessage(), ex); } } }
@Override public SAXParseException[] validate(Source source, ValidationErrorHandler errorHandler) throws IOException { if (errorHandler == null) { errorHandler = new DefaultValidationErrorHandler(); } Validator validator = schema.newValidator(); try { validator.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, ""); } catch (SAXNotRecognizedException | SAXNotSupportedException e) { if (log.isWarnEnabled()) { log.warn(XMLConstants.ACCESS_EXTERNAL_DTD + " property not supported by " + validator.getClass().getCanonicalName()); } } try { validator.setProperty(XMLConstants.ACCESS_EXTERNAL_SCHEMA, ""); } catch (SAXNotRecognizedException | SAXNotSupportedException e) { if (log.isWarnEnabled()) { log.warn(XMLConstants.ACCESS_EXTERNAL_SCHEMA + " property not supported by " + validator.getClass().getCanonicalName()); } } validator.setErrorHandler(errorHandler); try { validator.validate(source); return errorHandler.getErrors(); } catch (SAXException ex) { throw new XmlValidationException("Could not validate source: " + ex.getMessage(), ex); } } }
public SAXParseException[] validate(Source source) throws IOException { SAXParser parser = createSAXParser(); ValidationErrorHandler errorHandler = new ValidationErrorHandler(); try { if (source instanceof SAXSource) { validateSAXSource((SAXSource) source, parser, errorHandler); } else if (source instanceof StreamSource) { validateStreamSource((StreamSource) source, parser, errorHandler); } else if (source instanceof DOMSource) { validateDOMSource((DOMSource) source, parser, errorHandler); } else { throw new IllegalArgumentException("Source [" + source.getClass().getName() + "] is neither SAXSource, DOMSource, nor StreamSource"); } return errorHandler.getErrors(); } catch (SAXException ex) { throw new XmlValidationException("Could not validate source: " + ex.getMessage(), ex); } }