+ public static Document readAndValidate(InputStream aIs, InputStream aSchema)
+ throws XMLException {
+
+ try {
+ final Schema schema = SchemaFactory.newInstance(
+ XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(
+ new StreamSource(aSchema));
+
+ final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+ factory.setValidating(true);
+ factory.setNamespaceAware(true);
+ factory.setSchema(schema);
+
+ return factory.newDocumentBuilder().parse(aIs);
+ } catch (SAXException e) {
+ throw new XMLException(e.getMessage(), e);
+ } catch (IOException e) {
+ throw new XMLException(e.getMessage(), e);
+ } catch (ParserConfigurationException e) {
+ throw new XMLException(e.getMessage(), e);
+ } finally {
+ try {
+ aSchema.close();
+ } catch (Exception e) {
+ LOG.warn("Error closing schema", e);
+ }
+ try {
+ aIs.close();
+ } catch (Exception e) {
+ LOG.warn("Error closing XML file", e);
+ }
+ }
+
+ }
+
+ /**
+ * Serializes an XML document to a stream.
+ *
+ * @param aDocument
+ * Document to serialize.
+ * @param aOs
+ * Output stream.
+ */
+ public static void serialize(Document aDocument, OutputStream aOs)
+ throws IOException {