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); } }
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); } }
static XmlValidator createValidator(Resource[] schemaResources, String schemaLanguage) throws IOException { InputSource[] inputSources = new InputSource[schemaResources.length]; for (int i = 0; i < schemaResources.length; i++) { inputSources[i] = SaxUtils.createInputSource(schemaResources[i]); } return new Jaxp10Validator(inputSources, schemaLanguage); }