Expose the DFDLCatalogResolver to the SAPI/JAPI
DAFFODIL-2739
diff --git a/daffodil-japi/src/main/scala/org/apache/daffodil/japi/Daffodil.scala b/daffodil-japi/src/main/scala/org/apache/daffodil/japi/Daffodil.scala
index af3840b..a287ec3 100644
--- a/daffodil-japi/src/main/scala/org/apache/daffodil/japi/Daffodil.scala
+++ b/daffodil-japi/src/main/scala/org/apache/daffodil/japi/Daffodil.scala
@@ -61,6 +61,7 @@
import org.apache.daffodil.util.MaybeULong
import org.apache.daffodil.xml.NS
import org.apache.daffodil.xml.XMLUtils
+import org.apache.daffodil.xml.DFDLCatalogResolver
/**
* API Suitable for Java programmers to use.
@@ -1155,3 +1156,28 @@
override def skippedEntity(name: String): Unit =
contentHandler.skippedEntity(name)
}
+
+/**
+ * Returns the EntityResolver used by Daffodil to resolve import/include
+ * schemaLocations.
+ *
+ * The entity resolver attempts to resolve namespaces and systemId's in the
+ * following order:
+ *
+ * 1. Use an org.apache.xml.resolver.Catalog/CatalogManager. By default the
+ * Catalog only includes the daffodil-built-in-catalog.xml, but additional
+ * catalogs can be added by putting CatalogManager.properties on the
+ * classpath when daffodil is run.
+ *
+ * 2. If not resolved in step 1, schemaLocations are resolved relative to the
+ * importing schema URI, which could either be a file on the filesystem or in
+ * a jar on the classpath.
+ *
+ * The EntityResolver isn't thread safe, but it also is expensive and stateful,
+ * so we use ThreadLocal to only create one instance per thread.
+ */
+object DaffodilXMLEntityResolver {
+ def getEntityResolver: org.xml.sax.EntityResolver = DFDLCatalogResolver.get
+ def getXMLEntityResolver: org.apache.xerces.xni.parser.XMLEntityResolver = DFDLCatalogResolver.get
+ def getLSResourceResolver: org.w3c.dom.ls.LSResourceResolver = DFDLCatalogResolver.get
+}
diff --git a/daffodil-japi/src/test/java/org/apache/daffodil/example/TestJavaAPI.java b/daffodil-japi/src/test/java/org/apache/daffodil/example/TestJavaAPI.java
index a506918..11a0235 100644
--- a/daffodil-japi/src/test/java/org/apache/daffodil/example/TestJavaAPI.java
+++ b/daffodil-japi/src/test/java/org/apache/daffodil/example/TestJavaAPI.java
@@ -1205,4 +1205,12 @@
assertEquals(expectedData, bos.toString());
}
+ @Test
+ public void testJavaAPI26() throws IOException, ClassNotFoundException, ExternalVariableException {
+ // Demonstrates the use of the various EntityResolver methods from Java
+
+ assertTrue(DaffodilXMLEntityResolver.getEntityResolver() != null);
+ assertTrue(DaffodilXMLEntityResolver.getXMLEntityResolver() != null);
+ assertTrue(DaffodilXMLEntityResolver.getLSResourceResolver() != null);
+ }
}
diff --git a/daffodil-sapi/src/main/scala/org/apache/daffodil/sapi/Daffodil.scala b/daffodil-sapi/src/main/scala/org/apache/daffodil/sapi/Daffodil.scala
index fac600f..f72225d 100644
--- a/daffodil-sapi/src/main/scala/org/apache/daffodil/sapi/Daffodil.scala
+++ b/daffodil-sapi/src/main/scala/org/apache/daffodil/sapi/Daffodil.scala
@@ -58,6 +58,7 @@
import org.apache.daffodil.util.MaybeULong
import org.apache.daffodil.xml.NS
import org.apache.daffodil.xml.XMLUtils
+import org.apache.daffodil.xml.DFDLCatalogResolver
private class Daffodil private {
// Having this empty but private companion class removes the constructor from
@@ -1089,3 +1090,28 @@
override def skippedEntity(name: String): Unit =
contentHandler.skippedEntity(name)
}
+
+/**
+ * Returns the EntityResolver used by Daffodil to resolve import/include
+ * schemaLocations.
+ *
+ * The entity resolver attempts to resolve namespaces and systemId's in the
+ * following order:
+ *
+ * 1. Use an org.apache.xml.resolver.Catalog/CatalogManager. By default the
+ * Catalog only includes the daffodil-built-in-catalog.xml, but additional
+ * catalogs can be added by putting CatalogManager.properties on the
+ * classpath when daffodil is run.
+ *
+ * 2. If not resolved in step 1, schemaLocations are resolved relative to the
+ * importing schema URI, which could either be a file on the filesystem or in
+ * a jar on the classpath.
+ *
+ * The DFDLCatalogResolver isn't thread safe, but it also is expensive and stateful,
+ * so we use ThreadLocal to only create one instance per thread.
+ */
+object DaffodilXMLEntityResolver {
+ def getEntityResolver: org.xml.sax.EntityResolver = DFDLCatalogResolver.get
+ def getXMLEntityResolver: org.apache.xerces.xni.parser.XMLEntityResolver = DFDLCatalogResolver.get
+ def getLSResourceResolver: org.w3c.dom.ls.LSResourceResolver = DFDLCatalogResolver.get
+}