You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@daffodil.apache.org by ja...@apache.org on 2022/10/28 19:36:28 UTC

[daffodil] branch main updated: Expose the DFDLCatalogResolver to the SAPI/JAPI

This is an automated email from the ASF dual-hosted git repository.

jadams pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/daffodil.git


The following commit(s) were added to refs/heads/main by this push:
     new 4f84e0472 Expose the DFDLCatalogResolver to the SAPI/JAPI
4f84e0472 is described below

commit 4f84e0472ca11390af2401a60d0c54ab9603db2f
Author: Josh Adams <ja...@tresys.com>
AuthorDate: Mon Oct 10 22:16:42 2022 -0400

    Expose the DFDLCatalogResolver to the SAPI/JAPI
    
    DAFFODIL-2739
---
 .../scala/org/apache/daffodil/japi/Daffodil.scala  | 26 ++++++++++++++++++++++
 .../org/apache/daffodil/example/TestJavaAPI.java   |  8 +++++++
 .../scala/org/apache/daffodil/sapi/Daffodil.scala  | 26 ++++++++++++++++++++++
 3 files changed, 60 insertions(+)

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 af3840b72..a287ec375 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.Maybe._
 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 @@ class DaffodilUnparseContentHandler private[japi] (sContentHandler: SDaffodilUnp
   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 a5069187c..11a0235eb 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 @@ public class TestJavaAPI {
         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 fac600fa4..f72225d56 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.Maybe._
 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 @@ class DaffodilUnparseContentHandler private[sapi] (sContentHandler: SDaffodilUnp
   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
+}