This is an automated email from the ASF dual-hosted git repository. andy pushed a commit to branch jena5 in repository https://gitbox.apache.org/repos/asf/jena.git
commit 53baf40c39d565df2f16db5589045819df471f78 Author: Andy Seaborne <a...@apache.org> AuthorDate: Sun Oct 22 20:22:15 2023 +0100 Fix IRIs.asBase --- .../src/main/java/org/apache/jena/irix/IRIs.java | 30 ++++++++++++++-------- .../main/java/org/apache/jena/irix/SystemIRIx.java | 2 -- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/jena-core/src/main/java/org/apache/jena/irix/IRIs.java b/jena-core/src/main/java/org/apache/jena/irix/IRIs.java index 2cdbbc8bd4..12209317de 100644 --- a/jena-core/src/main/java/org/apache/jena/irix/IRIs.java +++ b/jena-core/src/main/java/org/apache/jena/irix/IRIs.java @@ -76,24 +76,32 @@ public class IRIs { /** * Given a candidate baseURI string, which may be a filename, * turn it into a IRI suitable as a base IRI. + * This includes encoding characters in a filename (e.g. spaces). */ - public static String toBase(String baseURI) { - if ( baseURI == null ) + public static String toBase(String uriForBase) { + if ( uriForBase == null ) return getBaseStr(); - String scheme = scheme(baseURI); + String scheme = scheme(uriForBase); if ( Sys.isWindows ) { // Assume a scheme of one letter is a Windows drive letter. if ( scheme != null && scheme.length() == 1 ) scheme = "file"; } - // File scheme if the URI is "file:" or there is no scheme - // and the system base is a "file:" URI - boolean isFile = ( scheme != null ) - ? scheme.equals("file") - : IRIs.getSystemBase().hasScheme("file"); - if ( isFile ) - baseURI = IRILib.encodeFileURL(baseURI); - return IRIs.getSystemBase().resolve(baseURI).toString(); + if ( scheme == null ) { + // Relative name: it the base is a file: URI, encode the relative + // name if it does not look like it is already encoded. + boolean isFileBase = IRIs.getSystemBase().hasScheme("file"); + if ( isFileBase && ! uriForBase.contains("%") ) + uriForBase = IRILib.encodeFileURL(uriForBase); + } else { + // If the scheme of the proposed base URI is file: then assume it is a legal IRI as intended. + // Pragmatically, fix-up a few characters that are illegal. + if ( scheme.equals("file") ) { + uriForBase = uriForBase.replace(" ", "%20"); + uriForBase = uriForBase.replace("\\", "/"); + } + } + return IRIs.getSystemBase().resolve(uriForBase).toString(); } /** Return a general purpose resolver, with the current system base as its base IRI. */ diff --git a/jena-core/src/main/java/org/apache/jena/irix/SystemIRIx.java b/jena-core/src/main/java/org/apache/jena/irix/SystemIRIx.java index a22fbb740d..ccffc388bf 100644 --- a/jena-core/src/main/java/org/apache/jena/irix/SystemIRIx.java +++ b/jena-core/src/main/java/org/apache/jena/irix/SystemIRIx.java @@ -123,8 +123,6 @@ public class SystemIRIx { if ( baseStr == null ) return IRIx.create(fallbackBaseURI); try { - if ( !baseStr.endsWith("/") ) - baseStr = baseStr+"/"; IRIx base = IRIx.create(baseStr); if ( ! base.isReference() ) Log.error(IRIs.class, "System base URI is not a reference URI: must have scheme, host and path");