You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ode.apache.org by va...@apache.org on 2015/06/09 18:04:38 UTC
[3/3] ode git commit: fixes ODE-1019.
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/f8228144
Tree: http://git-wip-us.apache.org/repos/asf/ode/tree/f8228144
Diff: http://git-wip-us.apache.org/repos/asf/ode/diff/f8228144
Branch: refs/heads/ode-1.3.x
Commit: f822814461c3cf0c4c506eb0b20c0afab36634db
Parents: 274a89c
Author: Tammo van Lessen <tv...@gmail.com>
Authored: Tue Jun 9 15:36:47 2015 +0200
Committer: Tammo van Lessen <tv...@gmail.com>
Committed: Tue Jun 9 18:04:07 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 | 87 +++++++++++++-------
4 files changed, 114 insertions(+), 40 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/ode/blob/f8228144/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/f8228144/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/f8228144/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 19dd3f0..ccac792 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/f8228144/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 be1f953..2a2acd9 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,6 +18,14 @@
*/
package org.apache.ode.utils.xsd;
+import static org.junit.Assert.assertEquals;
+
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URI;
+import java.util.Map;
+
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.ode.utils.StreamUtils;
@@ -26,46 +34,61 @@ 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 java.io.FileInputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.net.URI;
-import java.util.Map;
-
-import junit.framework.TestCase;
+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 = new FileInputStream(TestResources.getRetailerSchema().getFile());
- 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;
}
-
- return src;
+ }, 0);
+ // we expect the root schema and three includes
+ __log.debug("loaded " + s.keySet());
+ assertEquals(5, s.size());
+ }
+
+ /**
+ * 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");
}
-
}