import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
+import javax.xml.validation.Validator;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
+import org.w3c.dom.bootstrap.DOMImplementationRegistry;
+import org.w3c.dom.ls.DOMImplementationLS;
+import org.w3c.dom.ls.LSException;
+import org.w3c.dom.ls.LSInput;
+import org.w3c.dom.ls.LSParser;
import org.xml.sax.SAXException;
/**
*/
public static Document read(InputStream aIs) throws XMLException {
try {
- DocumentBuilder builder = DocumentBuilderFactory.newInstance()
- .newDocumentBuilder();
+ DOMImplementationRegistry registry = DOMImplementationRegistry
+ .newInstance();
- return builder.parse(aIs);
- } catch (SAXException e) {
+ DOMImplementationLS impl = (DOMImplementationLS) registry
+ .getDOMImplementation("LS");
+
+ LSParser builder = impl.createLSParser(
+ DOMImplementationLS.MODE_SYNCHRONOUS, null);
+ LSInput input = impl.createLSInput();
+ input.setByteStream(aIs);
+ return builder.parse(input);
+ } catch (IllegalAccessException e) {
throw new XMLException(e.getMessage(), e);
- } catch (IOException e) {
+ } catch (InstantiationException e) {
throw new XMLException(e.getMessage(), e);
- } catch (ParserConfigurationException e) {
+ } catch (ClassNotFoundException e) {
+ throw new XMLException(e.getMessage(), e);
+ } catch (LSException e) {
throw new XMLException(e.getMessage(), e);
} finally {
try {
public static Document readAndValidate(InputStream aIs, InputStream aSchema)
throws XMLException {
try {
+ Document doc = read(aIs);
final Schema schema = SchemaFactory.newInstance(
XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(
new StreamSource(aSchema));
+ Validator validator = schema.newValidator();
+ validator.validate(new DOMSource(doc));
- final DocumentBuilderFactory factory = DocumentBuilderFactory
- .newInstance();
- factory.setValidating(true);
- factory.setNamespaceAware(true);
- factory.setSchema(schema);
-
- return factory.newDocumentBuilder().parse(aIs);
+ return doc;
} 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();