diff --git a/daffodil-core/src/main/scala/org/apache/daffodil/core/runtime1/SchemaSetRuntime1Mixin.scala b/daffodil-core/src/main/scala/org/apache/daffodil/core/runtime1/SchemaSetRuntime1Mixin.scala index c055940fd1..a7929ca94b 100644 --- a/daffodil-core/src/main/scala/org/apache/daffodil/core/runtime1/SchemaSetRuntime1Mixin.scala +++ b/daffodil-core/src/main/scala/org/apache/daffodil/core/runtime1/SchemaSetRuntime1Mixin.scala @@ -20,6 +20,7 @@ package org.apache.daffodil.core.runtime1 import org.apache.daffodil.core.dsom.SchemaSet import org.apache.daffodil.core.dsom.SequenceTermBase import org.apache.daffodil.lib.util.Logger +import org.apache.daffodil.lib.xml.XMLUtils import org.apache.daffodil.runtime1.api.DFDL import org.apache.daffodil.runtime1.layers.LayerRuntimeCompiler import org.apache.daffodil.runtime1.layers.LayerRuntimeData @@ -30,6 +31,8 @@ import org.apache.daffodil.runtime1.processors.VariableMap import org.apache.daffodil.runtime1.processors.parsers.NotParsableParser import org.apache.daffodil.runtime1.processors.unparsers.NotUnparsableUnparser +import java.io.FileNotFoundException + trait SchemaSetRuntime1Mixin { self: SchemaSet => @@ -79,7 +82,14 @@ trait SchemaSetRuntime1Mixin { "The root element cannot have the dfdl:outputValueCalc property." ) // stored transiently in the SSRD, only used for full validation - val mainSchemaURI = self.schemaSource.uriForLoading + val mainSchemaURI = + XMLUtils.resolveSchemaLocation(self.schemaSource.uriForLoading.toString, None) match { + case Some((uss, _)) => uss.uri + case None => + throw new FileNotFoundException( + s"Could not find file or resource ${self.schemaSource.uriForLoading}" + ) + } val p = if (!root.isError) parser else null val u = if (!root.isError) unparser else null val ssrd = 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 6462367b81..8a8e9fbb70 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 @@ -23,10 +23,9 @@ import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.File; -import java.io.IOException; +import java.io.*; +import java.net.URI; +import java.net.URISyntaxException; import java.nio.ByteBuffer; import java.nio.channels.Channels; import java.nio.channels.ReadableByteChannel; @@ -1444,5 +1443,45 @@ public void testJavaAPICompileResource() throws IOException, ClassNotFoundExcept } } + @Test + public void testJavaAPICompileSource1() throws IOException, URISyntaxException, InvalidUsageException { + org.apache.daffodil.japi.Compiler c = Daffodil.compiler(); + URI uri = new URI("/test/japi/mySchema1.dfdl.xsd"); + ProcessorFactory pf = c.compileSource(uri); + DataProcessor dp = pf.onPath("/").withValidationMode(ValidationMode.Full); + + java.io.File file = getResource("/test/japi/myDataBroken.dat"); + java.io.FileInputStream fis = new java.io.FileInputStream(file); + try (InputSourceDataInputStream dis = new InputSourceDataInputStream(fis)) { + JDOMInfosetOutputter outputter = new JDOMInfosetOutputter(); + ParseResult res = dp.parse(dis, outputter); + assertTrue(res.isError()); + + Diagnostic d = res.getDiagnostics().get(0); + LocationInSchemaFile loc = d.getLocationsInSchemaFiles().get(0); + assertTrue(loc.toString().replace("\\", "/").contains("in " + uri.getPath())); + } + } + + @Test + public void testJavaAPICompileSource2() throws IOException { + org.apache.daffodil.japi.Compiler c = Daffodil.compiler(); + File tempFile = File.createTempFile("testJavaAPI", ".schema"); + File schemaFile = getResource("/test/japi/mySchema2.dfdl.xsd"); + FileUtils.copyFile(schemaFile, tempFile); + ProcessorFactory pf = c.compileSource(tempFile.toURI()); + try { + if (!pf.isError()) { + tempFile.delete(); + pf.onPath("/"); + } else { + tempFile.delete(); + fail(); + } + } catch (Exception e) { + assertTrue(e.getMessage().contains("Could not find file or resource")); + } + } + } diff --git a/daffodil-sapi/src/test/scala/org/apache/daffodil/example/TestScalaAPI.scala b/daffodil-sapi/src/test/scala/org/apache/daffodil/example/TestScalaAPI.scala index 4dcee6dd27..9697a6f6cd 100644 --- a/daffodil-sapi/src/test/scala/org/apache/daffodil/example/TestScalaAPI.scala +++ b/daffodil-sapi/src/test/scala/org/apache/daffodil/example/TestScalaAPI.scala @@ -22,6 +22,7 @@ import java.io.ByteArrayOutputStream import java.io.File import java.io.ObjectInputStream import java.io.ObjectOutputStream +import java.net.URI import java.nio.ByteBuffer import java.nio.channels.Channels import java.nio.charset.StandardCharsets @@ -1407,4 +1408,44 @@ class TestScalaAPI { } } + @Test + def testScalaAPICompileSource1(): Unit = { + val c = Daffodil.compiler() + val uri = new URI("/test/sapi/mySchema1.dfdl.xsd") + val pf = c.compileSource(uri) + val dp = pf.onPath("/").withValidationMode(ValidationMode.Full) + + val file = getResource("/test/sapi/myDataBroken.dat") + val fis = new java.io.FileInputStream(file) + using(new InputSourceDataInputStream(fis)) { input => + val outputter = new ScalaXMLInfosetOutputter() + val res = dp.parse(input, outputter) + assertTrue(res.isError()) + + val d = res.getDiagnostics.head + val loc = d.getLocationsInSchemaFiles.head + assertTrue(loc.toString().replace("\\", "/").contains("in " + uri.getPath)) + } + } + + @Test + def testJavaAPICompileSource2(): Unit = { + val c = Daffodil.compiler() + val tempFile = File.createTempFile("testScalaAPI", ".schema") + val schemaFile = getResource("/test/sapi/mySchema2.dfdl.xsd") + FileUtils.copyFile(schemaFile, tempFile) + val pf = c.compileSource(tempFile.toURI) + try + if (!pf.isError ) { + tempFile.delete + pf.onPath("/") + } else { + tempFile.delete + fail() + } + catch { + case e: Exception => + assertTrue(e.getMessage.contains("Could not find file or resource")) + } + } }