Repository: ode Updated Branches: refs/heads/master 1f3f429a8 -> 7812077ce refs/heads/ode-1.3.x eb46426e7 -> f82281446
fixes ODE-1019. Project: http://git-wip-us.apache.org/repos/asf/ode/repo Commit: http://git-wip-us.apache.org/repos/asf/ode/commit/7812077c Tree: http://git-wip-us.apache.org/repos/asf/ode/tree/7812077c Diff: http://git-wip-us.apache.org/repos/asf/ode/diff/7812077c Branch: refs/heads/master Commit: 7812077ce9df8a0e440d203de30282d415169cb3 Parents: 1f3f429 Author: Tammo van Lessen <[email protected]> Authored: Tue Jun 9 15:36:47 2015 +0200 Committer: Tammo van Lessen <[email protected]> Committed: Tue Jun 9 15:36:47 2015 +0200 ---------------------------------------------------------------------- .../java/org/apache/ode/utils/xsd/XSUtils.java | 58 +++++++++++-- .../org/apache/ode/utils/xsd/XsdException.java | 5 ++ .../org/apache/ode/utils/xsd/XsdMessages.java | 4 + .../apache/ode/utils/xsd/SchemaCaptureTest.java | 86 +++++++++++++------- 4 files changed, 115 insertions(+), 38 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ode/blob/7812077c/utils/src/main/java/org/apache/ode/utils/xsd/XSUtils.java ---------------------------------------------------------------------- diff --git a/utils/src/main/java/org/apache/ode/utils/xsd/XSUtils.java b/utils/src/main/java/org/apache/ode/utils/xsd/XSUtils.java index 841bf00..0c2cc26 100644 --- a/utils/src/main/java/org/apache/ode/utils/xsd/XSUtils.java +++ b/utils/src/main/java/org/apache/ode/utils/xsd/XSUtils.java @@ -18,25 +18,28 @@ */ package org.apache.ode.utils.xsd; +import java.io.ByteArrayInputStream; +import java.net.URI; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.ode.utils.msg.MessageBundle; import org.apache.xerces.dom.DOMInputImpl; +import org.apache.xerces.impl.Constants; import org.apache.xerces.impl.xs.XMLSchemaLoader; import org.apache.xerces.xni.XNIException; import org.apache.xerces.xni.parser.XMLEntityResolver; import org.apache.xerces.xni.parser.XMLErrorHandler; import org.apache.xerces.xni.parser.XMLParseException; import org.apache.xerces.xs.XSModel; +import org.w3c.dom.DOMError; +import org.w3c.dom.DOMErrorHandler; import org.w3c.dom.ls.LSInput; -import java.io.ByteArrayInputStream; -import java.net.URI; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - /** * Various utility methods related to XML Schema processing. @@ -93,6 +96,9 @@ public class XSUtils { LoggingXmlErrorHandler eh = new LoggingXmlErrorHandler(__log); schemaLoader.setErrorHandler(eh); + LoggingDOMErrorHandler deh = new LoggingDOMErrorHandler(__log); + schemaLoader.setParameter(Constants.DOM_ERROR_HANDLER, deh); + XSModel model = schemaLoader.load(input); // The following mess is due to XMLSchemaLoaders funkyness in error @@ -103,15 +109,26 @@ public class XSUtils { * Someone inside Xerces will have eaten this exception, for no good * reason. */ + XsdException ex = null; + List<XMLParseException> errors = eh.getErrors(); if (errors.size() != 0) { __log.error("captureSchema: XMLParseException(s) in " + input); - XsdException ex = null; for (XMLParseException xpe : errors) { ex = new XsdException(ex, xpe.getMessage(), xpe.getLineNumber(), xpe.getColumnNumber(), xpe.getLiteralSystemId()); } + } + + List<Exception> exceptions = deh.getExceptions(); + if (exceptions.size() != 0) { + for (Exception e : exceptions) { + ex = new XsdException(ex, e.getMessage()); + } + } + + if (ex != null) { throw ex; } @@ -188,4 +205,29 @@ public class XSUtils { throw new XNIException("Unknown XSD error state; domain=" + domain + ", key=" +key); } } + + static class LoggingDOMErrorHandler implements DOMErrorHandler { + + private ArrayList<Exception> _exceptions = new ArrayList<Exception>(); + private Log _log; + + public LoggingDOMErrorHandler(Log log) { + assert log != null; + _log = log; + } + + public boolean handleError(DOMError error) { + if (_log.isDebugEnabled()) { + _log.debug("Exception occurred during parsing schema: " + error.getMessage()); + } + if (error != null) { + _exceptions.add((Exception) error.getRelatedException()); + } + return false; + } + + public ArrayList<Exception> getExceptions() { + return _exceptions; + } + } } http://git-wip-us.apache.org/repos/asf/ode/blob/7812077c/utils/src/main/java/org/apache/ode/utils/xsd/XsdException.java ---------------------------------------------------------------------- diff --git a/utils/src/main/java/org/apache/ode/utils/xsd/XsdException.java b/utils/src/main/java/org/apache/ode/utils/xsd/XsdException.java index ad7e16b..e82cab3 100644 --- a/utils/src/main/java/org/apache/ode/utils/xsd/XsdException.java +++ b/utils/src/main/java/org/apache/ode/utils/xsd/XsdException.java @@ -58,6 +58,11 @@ public class XsdException extends Exception { _systemId = literalSystemId; _previous = previous; } + + public XsdException(XsdException previous, String message) { + super(__msgs.msgXsdExceptionMessage(message)); + _message = message; + } public String getDetailMessage() { return _message; http://git-wip-us.apache.org/repos/asf/ode/blob/7812077c/utils/src/main/java/org/apache/ode/utils/xsd/XsdMessages.java ---------------------------------------------------------------------- diff --git a/utils/src/main/java/org/apache/ode/utils/xsd/XsdMessages.java b/utils/src/main/java/org/apache/ode/utils/xsd/XsdMessages.java index f91a396..a3051ac 100644 --- a/utils/src/main/java/org/apache/ode/utils/xsd/XsdMessages.java +++ b/utils/src/main/java/org/apache/ode/utils/xsd/XsdMessages.java @@ -100,6 +100,10 @@ public class XsdMessages extends MessageBundle { systemId, lineNumber, columnNumber); } + public String msgXsdExceptionMessage(String message) { + return this.format("Unable to process XML Schema: {0}", message); + } + /** An unknown error occured processing schema at {0}" */ public String msgXsdUnknownError(String systemId) { return this.format("An unknown error occured processing schema at {0}", systemId); http://git-wip-us.apache.org/repos/asf/ode/blob/7812077c/utils/src/test/java/org/apache/ode/utils/xsd/SchemaCaptureTest.java ---------------------------------------------------------------------- diff --git a/utils/src/test/java/org/apache/ode/utils/xsd/SchemaCaptureTest.java b/utils/src/test/java/org/apache/ode/utils/xsd/SchemaCaptureTest.java index c0ff13d..aa09424 100644 --- a/utils/src/test/java/org/apache/ode/utils/xsd/SchemaCaptureTest.java +++ b/utils/src/test/java/org/apache/ode/utils/xsd/SchemaCaptureTest.java @@ -18,14 +18,7 @@ */ package org.apache.ode.utils.xsd; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.apache.ode.utils.StreamUtils; -import org.apache.ode.utils.TestResources; -import org.apache.xerces.xni.XMLResourceIdentifier; -import org.apache.xerces.xni.XNIException; -import org.apache.xerces.xni.parser.XMLEntityResolver; -import org.apache.xerces.xni.parser.XMLInputSource; +import static org.junit.Assert.*; import java.io.FileInputStream; import java.io.IOException; @@ -35,37 +28,70 @@ import java.util.Map; import junit.framework.TestCase; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.ode.utils.StreamUtils; +import org.apache.ode.utils.TestResources; +import org.apache.xerces.xni.XMLResourceIdentifier; +import org.apache.xerces.xni.XNIException; +import org.apache.xerces.xni.parser.XMLEntityResolver; +import org.apache.xerces.xni.parser.XMLInputSource; +import org.junit.Test; + /** * Test schema capture functionality. */ -public class SchemaCaptureTest extends TestCase { +public class SchemaCaptureTest { private static Log __log = LogFactory.getLog(SchemaCaptureTest.class); - public void testSchemaCapture() throws Exception { - __log.debug("GETTING RESOURCE " + TestResources.getRetailerSchema()); - InputStream xsdStream = TestResources.getRetailerSchema().openStream(); - byte[] data; - try { - data = StreamUtils.read(xsdStream); - } finally { - xsdStream.close(); - } + @Test + public void testSchemaCapture() throws Exception { + __log.debug("GETTING RESOURCE " + TestResources.getRetailerSchema()); + InputStream xsdStream = TestResources.getRetailerSchema().openStream(); + byte[] data; + try { + data = StreamUtils.read(xsdStream); + } finally { + xsdStream.close(); + } + + Map<URI, byte[]> s = XSUtils.captureSchema(URI.create("schema.xsd"), data, new XMLEntityResolver() { + public XMLInputSource resolveEntity(XMLResourceIdentifier resourceIdentifier) throws XNIException, IOException { + XMLInputSource src = new XMLInputSource(resourceIdentifier); + String literalUri = resourceIdentifier.getLiteralSystemId(); - Map<URI, byte[]> s = XSUtils.captureSchema(URI.create("schema.xsd"), data, new XMLEntityResolver() { - public XMLInputSource resolveEntity(XMLResourceIdentifier resourceIdentifier) throws XNIException, IOException { - XMLInputSource src = new XMLInputSource(resourceIdentifier); - String literalUri = resourceIdentifier.getLiteralSystemId(); + if (literalUri != null) { + src.setByteStream(getClass().getClassLoader().getResourceAsStream(literalUri)); + } - if (literalUri != null) { - src.setByteStream(getClass().getClassLoader().getResourceAsStream(literalUri)); + return src; } + }, 0); + // we expect the root schema and three includes + __log.debug("loaded " + s.keySet()); + assertEquals(5, s.size()); + } - return src; + /** + * Test for ODE-1019, provided by Igor Vorobiov + */ + @Test(expected = Exception.class) + public void testSchemaCaptureException() throws Exception { + InputStream xsdStream = new FileInputStream(TestResources.getRetailerSchema().getFile()); + byte[] data; + try { + data = StreamUtils.read(xsdStream); + } finally { + xsdStream.close(); } - }, 0); - // we expect the root schema and three includes - __log.debug("loaded " + s.keySet()); - assertEquals(5, s.size()); - } + XSUtils.captureSchema(URI.create("schema.xsd"), data, new XMLEntityResolver() { + public XMLInputSource resolveEntity(XMLResourceIdentifier resourceIdentifier) throws XNIException, IOException { + // !!! cause NullPointerException + return null; + } + }, 0); + + __log.error("mustn't reach this place"); + } }
