Skip to content

Commit

Permalink
Resolve Main Schema URI used for Validation
Browse files Browse the repository at this point in the history
- currently we pass it in directly without trying to resolve it, which was ok in  3.8.0 because the URI got resolved before we ever got to validation, so we call resolveSchemaLocation on the uriForLoading so that Xerces Validation can use that.
- added tests to verify

DAFFODIL-2950
  • Loading branch information
olabusayoT committed Nov 2, 2024
1 parent 357f224 commit 4ed482e
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 =>

Expand Down Expand Up @@ -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 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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"));
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"))
}
}
}

0 comments on commit 4ed482e

Please sign in to comment.