Skip to content

Commit

Permalink
!fixup De-personalize Diagnostic File URLs in Compiled Schemas
Browse files Browse the repository at this point in the history
- use uriToDiagnosticFile only when uri is all we have available
- update comments for clarity
- revert relative file path changes in TestScalaAPI

DAFFODIL-2195
  • Loading branch information
olabusayoT committed Mar 20, 2024
1 parent 16babd6 commit fe43e6b
Show file tree
Hide file tree
Showing 12 changed files with 39 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ class Compiler private (
optRootName: Option[String] = None,
optRootNamespace: Option[String] = None,
): ProcessorFactory = {
val source = URISchemaSource(Misc.uriToDiagnosticFile(file.toURI), file.toURI)
val source = URISchemaSource(file, file.toURI)
compileSource(source, optRootName, optRootNamespace)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import java.io.InputStream
import java.nio.channels.Channels
import java.nio.channels.ReadableByteChannel
import java.nio.channels.WritableByteChannel
import java.nio.file.Paths
import scala.collection.mutable.ArrayBuffer
import scala.util.Try
import scala.xml._
Expand Down Expand Up @@ -314,7 +315,7 @@ object TestUtils {
val nullChannel = java.nio.channels.Channels.newChannel(nos)
val compiler = Compiler()
val uri = Misc.getRequiredResource(resourcePathString)
val schemaSource = URISchemaSource(Misc.uriToDiagnosticFile(uri), uri)
val schemaSource = URISchemaSource(Paths.get(resourcePathString).toFile, uri)
val theTry = Timer.getResult(compileAndSave(compiler, schemaSource, nullChannel))
theTry.get
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ class TestXMLLoaderWithLocation {
using(new java.io.FileWriter(tmpXMLFileName)) { fw =>
fw.write(testXML.toString())
}
val tmpXMLFile = new File(tmpXMLFileName)
val res =
URISchemaSource(Paths.get(tmpXMLFileName).toFile, new File(tmpXMLFileName).toURI)
URISchemaSource(tmpXMLFile, tmpXMLFile.toURI)
val eh = new BasicErrorHandler
val node = (new DaffodilXMLLoader(eh)).load(
res,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ object DaffodilConfig {
URISchemaSource(Misc.uriToDiagnosticFile(uri), uri),
)

def fromFile(file: File) = fromURI(file.toURI)
def fromFile(file: File) = fromSchemaSource(
URISchemaSource(file, file.toURI),
)

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import java.nio.file.Paths
import scala.xml.Node

import org.apache.daffodil.lib.exceptions.Assert
import org.apache.daffodil.lib.util.Misc
import org.apache.daffodil.lib.xml.XMLUtils

import org.apache.commons.io.input.XmlStreamReader
Expand Down Expand Up @@ -168,7 +167,7 @@ class InputStreamSchemaSource(
}
override def uriForLoading = tempURI

override def diagnosticFile: File = Misc.uriToDiagnosticFile(tempURI)
override def diagnosticFile: File = tempSchemaFile
}

protected sealed abstract class NodeSchemaSourceBase(
Expand All @@ -177,7 +176,7 @@ protected sealed abstract class NodeSchemaSourceBase(
tmpDir: Option[File],
tempSchemaFileFromNode: File,
) extends URISchemaSource(
Misc.uriToDiagnosticFile(tempSchemaFileFromNode.toURI),
tempSchemaFileFromNode,
tempSchemaFileFromNode.toURI,
) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -714,8 +714,6 @@ object Misc {
*/
def uriToDiagnosticFile(uri: URI): File = {
uri.getScheme match {
// in windows, some jar uris have file as the schema, so we treat
// them both the same, just in case
case "jar" => Paths.get(uri.toString.split("\\.jar!").last).toFile
case "file" => Paths.get(uri).toFile
case _ => Paths.get(uri.getPath).toFile
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,10 +247,13 @@ class DFDLCatalogResolver private ()
val optURI =
try {
val baseURI = new URI(baseURIString)
// we set diagnostic file path to empty string here because baseURIString has a scheme
// and that can cause problems down the road in resolveSchemaLocation. We don't care
// about the diagnosticFilepath here since this information is from Xerces, so we're fine
// with it being blank
// DaffodilXMLLoader implements a Xerces API, and Xerces doesn't have a concept of diagnostic path
// that we need for a URISchemaSource to pass in as the context for resolveSchemaLocation.
// We could use some heuristic to come up with a diagnostic path (e.g. call uriToDiagosticPath).
// But we don't actually use the diagnosticPath returned by resolveSchemaLocation here, all we care
// about is getting a URI for Xerces. So the diagnostic path in the schema source doesn't really
// matter as long as it doesn't break resolveSchemaLocation, and an empty diagnostic path works
// fine for that.
val uriSchemaSource = URISchemaSource(Paths.get("").toFile, baseURI)
val contextURI = Some(uriSchemaSource)
val resolved = XMLUtils.resolveSchemaLocation(sysId, contextURI)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1477,7 +1477,7 @@ Differences were (path, expected, actual):
// class is in.
val resource = this.getClass.getResource(uri.getPath)
if (resource != null) {
val uss = URISchemaSource(Misc.uriToDiagnosticFile(uri), resource.toURI)
val uss = URISchemaSource(new File(uri.getPath), resource.toURI)
Some((uss, false))
} else
None
Expand All @@ -1487,7 +1487,7 @@ Differences were (path, expected, actual):
// must exist. If it does not exist, this orElse block results in a None
val file = Paths.get(uri.getPath).toFile
if (file.exists) {
val uss = URISchemaSource(Misc.uriToDiagnosticFile(uri), file.toURI)
val uss = URISchemaSource(file, file.toURI)
Some((uss, false))
} else {
None
Expand All @@ -1500,28 +1500,29 @@ Differences were (path, expected, actual):
val contextURI = optContextURI.get
val optResolvedRelative = None
.orElse {
val contextURIDiagnosticFile = contextURI.diagnosticFile
val relativeDiagnosticFile =
contextURIDiagnosticFile.toPath
.resolveSibling(Paths.get(uri.getPath))
.normalize()
.toFile
// This is a relative path, so look up the schemaLocation path relative to the context
val relativeURI =
Misc.getResourceRelativeOnlyOption(uri.getPath, contextURI.uriForLoading).map {
u =>
val contextURIDiagnosticFile = contextURI.diagnosticFile
val relativeDiagnosticFile =
contextURIDiagnosticFile.toPath
.resolveSibling(Paths.get(uri.getPath))
.normalize()
.toFile
(URISchemaSource(relativeDiagnosticFile, u), false)
}
relativeURI
}
.orElse {
val uriPath = "/" + uri.getPath
// The user might have meant an absolute schemaLocation but left off the leading
// slash accidentally. Past versions of Daffodil allowed this, so we allow it too,
// but return a boolean if a relative path was found absolutely so callers can warn
// if needed. Future versions of Daffodil may want to remove this orElse block so we
// are strict about how absolute vs relative schemaLocations are resolved.
val pair = Option(this.getClass.getResource("/" + uri.getPath))
.map { r => URISchemaSource(Misc.uriToDiagnosticFile(r.toURI), r.toURI) }
val pair = Option(this.getClass.getResource(uriPath))
.map { r => URISchemaSource(new File(uriPath), r.toURI) }
.map { (_, true) }
pair
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ class TunableGenerator(schemaRootConfig: scala.xml.Node, schemaRootExt: scala.xm
|//
|////////////////////////////////////////////////////////////////////////////////////////////
|
|import java.nio.file.Paths
|
|import org.apache.daffodil.lib.exceptions.Assert
|import org.apache.daffodil.lib.exceptions.ThrowsSDE
|import org.apache.daffodil.lib.schema.annotation.props.EmptyElementParsePolicy
Expand All @@ -75,7 +77,7 @@ class TunableGenerator(schemaRootConfig: scala.xml.Node, schemaRootExt: scala.xm
| val configTunables: Map[String, String] =
| if (configOpt.isDefined) {
| val loader = new DaffodilXMLLoader()
| val node = loader.load(URISchemaSource(Misc.uriToDiagnosticFile(configOpt.get), configOpt.get), Some(XMLUtils.dafextURI))
| val node = loader.load(URISchemaSource(Paths.get(configPath).toFile, configOpt.get), Some(XMLUtils.dafextURI))
| tunablesMap(node)
| } else {
| Map.empty
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import org.apache.daffodil.lib.api.URISchemaSource
import org.apache.daffodil.lib.exceptions.Assert
import org.apache.daffodil.lib.exceptions.ThrowsSDE
import org.apache.daffodil.lib.externalvars._
import org.apache.daffodil.lib.util.Misc
import org.apache.daffodil.lib.util.Misc._
import org.apache.daffodil.lib.xml.DaffodilXMLLoader
import org.apache.daffodil.lib.xml.XMLUtils
Expand Down Expand Up @@ -82,7 +81,7 @@ object ExternalVariablesLoader {
val ldr = new DaffodilXMLLoader()
val dafextURI = XMLUtils.dafextURI
val node = ldr.load(
URISchemaSource(Misc.uriToDiagnosticFile(file.toURI), file.toURI),
URISchemaSource(file, file.toURI),
Some(dafextURI),
)
nodeToBindings(node)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,9 +345,7 @@ class TestScalaAPI {
def testScalaAPI4b(): Unit = {
val c = Daffodil.compiler()

val schemaFileName = new File(
"daffodil-sapi/src/test/resources/test/sapi/mySchema3.dfdl.xsd",
)
val schemaFileName = getResource("/test/sapi/mySchema3.dfdl.xsd")
val pf = c.compileFile(schemaFileName, Some("e4"), None)
val dp1 = pf.onPath("/")
val dp = reserializeDataProcessor(dp1)
Expand Down Expand Up @@ -375,9 +373,7 @@ class TestScalaAPI {
def testScalaAPI5(): Unit = {
val c = Daffodil.compiler()

val schemaFileName = new File(
"daffodil-sapi/src/test/resources/test/sapi/mySchema3.dfdl.xsd",
)
val schemaFileName = getResource("/test/sapi/mySchema3.dfdl.xsd")
// element
val pf = c.compileFile(schemaFileName, Some("e4"), None) // e4 is a 4-byte long string
val dp1 = pf.onPath("/")
Expand Down Expand Up @@ -630,9 +626,7 @@ class TestScalaAPI {
val c = Daffodil.compiler()

val extVarsFile = getResource("/test/sapi/external_vars_1.xml")
val schemaFile = new File(
"daffodil-sapi/src/test/resources/test/sapi/mySchemaWithVars.dfdl.xsd",
)
val schemaFile = getResource("/test/sapi/mySchemaWithVars.dfdl.xsd")
val pf = c.compileFile(schemaFile)

val dp1 = pf.onPath("/")
Expand Down Expand Up @@ -665,9 +659,7 @@ class TestScalaAPI {
val c = Daffodil.compiler()

val extVarFile = getResource("/test/sapi/external_vars_1.xml")
val schemaFile = new File(
"daffodil-sapi/src/test/resources/test/sapi/mySchemaWithVars.dfdl.xsd",
)
val schemaFile = getResource("/test/sapi/mySchemaWithVars.dfdl.xsd")
val pf = c.compileFile(schemaFile)
val dp1 = pf.onPath("/")
val dp = reserializeDataProcessor(dp1)
Expand Down Expand Up @@ -1079,9 +1071,7 @@ class TestScalaAPI {
// Demonstrates error cases of setting external variables
val c = Daffodil.compiler()

val schemaFile = new File(
"daffodil-sapi/src/test/resources/test/sapi/mySchemaWithComplexVars1.dfdl.xsd",
)
val schemaFile = getResource("/test/sapi/mySchemaWithComplexVars1.dfdl.xsd")
val pf = c.compileFile(schemaFile)
var dp = pf.onPath("/")
dp = reserializeDataProcessor(dp)
Expand Down Expand Up @@ -1282,9 +1272,7 @@ class TestScalaAPI {

def doXMLTextEscapeStyleTest(expect: String, data: String, schemaType: String): Unit = {
val c = Daffodil.compiler()
val schemaFile = new File(
"daffodil-sapi/src/test/resources/test/sapi/mySchemaCDATA.dfdl.xsd",
)
val schemaFile = getResource("/test/sapi/mySchemaCDATA.dfdl.xsd")
val pf = c.compileFile(schemaFile, Some(schemaType), None)
var dp = pf.onPath("/")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ class DFDLTestSuite private[tdml] (
val uri = tdmlFile.toURI()
val newNode =
loader.load(
URISchemaSource(Misc.uriToDiagnosticFile(uri), uri),
URISchemaSource(tdmlFile, uri),
optTDMLSchema,
addPositionAttributes = true,
)
Expand Down

0 comments on commit fe43e6b

Please sign in to comment.