From 6137cf8633ca46531c9f3385138c63a8956f668b Mon Sep 17 00:00:00 2001 From: olabusayoT <50379531+olabusayoT@users.noreply.github.com> Date: Fri, 11 Oct 2024 13:09:58 -0400 Subject: [PATCH] fixup! Fix Resolve Schema Location for xsi:SchemaLocation in Config files - convert systemId to a URI and get its path when comparing with resolvedURI - undo change to resolveSchemaLocation DAFFODIL-2339 --- .../daffodil/lib/xml/DaffodilXMLLoader.scala | 2 +- .../apache/daffodil/lib/xml/XMLUtils.scala | 31 ++++++++++++------- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/daffodil-lib/src/main/scala/org/apache/daffodil/lib/xml/DaffodilXMLLoader.scala b/daffodil-lib/src/main/scala/org/apache/daffodil/lib/xml/DaffodilXMLLoader.scala index ded5498447..c184866cd9 100644 --- a/daffodil-lib/src/main/scala/org/apache/daffodil/lib/xml/DaffodilXMLLoader.scala +++ b/daffodil-lib/src/main/scala/org/apache/daffodil/lib/xml/DaffodilXMLLoader.scala @@ -221,7 +221,7 @@ class DFDLCatalogResolver private () resolvedSystem } else if ( resolvedUri != null && ((systemId == null) || (systemId != null && resolvedUri.endsWith( - systemId + (new URI(systemId)).getPath ))) ) { resolvedUri diff --git a/daffodil-lib/src/main/scala/org/apache/daffodil/lib/xml/XMLUtils.scala b/daffodil-lib/src/main/scala/org/apache/daffodil/lib/xml/XMLUtils.scala index 83aa1e61f3..dc41c6504f 100644 --- a/daffodil-lib/src/main/scala/org/apache/daffodil/lib/xml/XMLUtils.scala +++ b/daffodil-lib/src/main/scala/org/apache/daffodil/lib/xml/XMLUtils.scala @@ -1459,27 +1459,34 @@ Differences were (path, expected, actual): ) } + val uriIsJustPathComponent = + uri.getScheme == null && + uri.getAuthority == null && + uri.getQuery == null && + uri.getFragment == null && + uri.getPath != null + val optResolved: Option[(URISchemaSource, Boolean)] = - if (uri.isAbsolute && uri.getScheme.contains("jar")) { - // an absolute URI is one with a scheme. In the case that it is a jar uri - // we expect to be able to resolve the URI and do not try anything else - // (e.g. filesystem, classpath). Since this function + if (uri.isAbsolute) { + // an absolute URI is one with a scheme. In this case, we expect to be able to resolve + // the URI and do not try anything else (e.g. filesystem, classpath). Since this function // is for schemaLocation attributes, we may eventually want to disallow this, and only // allow relative URIs (i.e. URIs without a scheme). We do have some places that use // absolute URIs in includes/imports and cannot remove this yet. try { - uri.toURL.openStream.close() + uri.toURL.openStream.close val uss = URISchemaSource(Misc.uriToDiagnosticFile(uri), uri) Some(uss, false) } catch { case e: IOException => None } - } - // we want to attempt to resolve the URI whether the non-jar uri has a scheme or not, - // this is relevant for when we are validating with Xerces, and it calls resolvesEntity - // we get URIs that look like file:/path/to/not/absolute/path ex: file:/org/apache/daffodil/xsd/dafext.xsd - // that fail to be found in the above case, so we have to look them up - else if (uri.getPath.startsWith("/")) { + } else if (!uriIsJustPathComponent) { + // this is not an absolute URI so we don't have a scheme. This should just be a path, so + // throw an IllegalArgumentException if that's not the case + val msg = + s"Non-absolute schemaLocation URI can only contain a path component: $schemaLocation" + throw new IllegalArgumentException(msg) + } else if (uri.getPath.startsWith("/")) { // The None.orElse{ ... }.orElse { ... } pattern below is useful to evaluate each // alternative way to resolve a schema location, stopping only when a Some is returned. // This makes for easily adding/removing/reordering resolution approaches by changing @@ -1499,7 +1506,7 @@ Differences were (path, expected, actual): .orElse { // Search for the schemaLocation path on the file system. This path is absolute so it // must exist. If it does not exist, this orElse block results in a None - val file = new File(uri.getPath) + val file = Paths.get(uri.getPath).toFile if (file.exists) { val uss = URISchemaSource(file, file.toURI) Some((uss, false))