From aa1c8ff8ba92eedc86fec8c85a349ab6e529c6d6 Mon Sep 17 00:00:00 2001
From: Vincent Hennebert <vhennebert@apache.org>
Date: Tue, 27 Feb 2007 09:16:14 +0000
Subject: Add an ending slash to base URLs when they don't have one, in order
 to better match user expectations. Submitted by: Adrian Cumiskey (fop-dev AT
 cumiskey DOT com)

git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@512185 13f79535-47bb-0310-9956-ffa450edef68
---
 src/java/org/apache/fop/apps/FOURIResolver.java | 30 ++++++++++++++++---------
 1 file changed, 20 insertions(+), 10 deletions(-)

diff --git a/src/java/org/apache/fop/apps/FOURIResolver.java b/src/java/org/apache/fop/apps/FOURIResolver.java
index db0cae75e..5b66ffa57 100644
--- a/src/java/org/apache/fop/apps/FOURIResolver.java
+++ b/src/java/org/apache/fop/apps/FOURIResolver.java
@@ -87,7 +87,12 @@ public class FOURIResolver
                 log.error("Could not convert filename to URL: " + mfue.getMessage()); 
             }
         } else {
-            URL baseURL = toBaseURL(base);
+            URL baseURL = null;
+            try {
+                baseURL = toBaseURL(base);
+            } catch (MalformedURLException mfue) {
+                log.error("Error with base URL \"" + base + "\"): " + mfue.getMessage());
+            }
             if (baseURL == null) {
                 // We don't have a valid baseURL just use the URL as given
                 try {
@@ -155,7 +160,7 @@ public class FOURIResolver
             //Note: This is on "debug" level since the caller is supposed to handle this
             log.debug("File not found: " + effURL);
         } catch (java.io.IOException ioe) {
-            log.error("Error with opening URL '" + href + "': " + ioe.getMessage());
+            log.error("Error with opening URL '" + effURL + "': " + ioe.getMessage());
         }
         return null;
     }
@@ -201,15 +206,20 @@ public class FOURIResolver
      * @param baseURL the base URL
      * @returns the base URL as java.net.URL
      */
-    private URL toBaseURL(String baseURL) {
-        try {
-            return new URL(baseURL == null 
-                            ? new java.io.File("").toURL().toExternalForm() 
-                            : baseURL);
-        } catch (MalformedURLException mfue) {
-            log.error("Error with base URL \"" + baseURL + "\"): " + mfue.getMessage());
+    private URL toBaseURL(String base) throws MalformedURLException {
+        if (base == null) {
+            return new java.io.File("").toURL();
         }
-        return null;
+        if (!base.endsWith("/")) {
+            // The behavior described by RFC 3986 regarding resolution of relative
+            // references may be misleading for normal users:
+            // file://path/to/resources + myResource.res -> file://path/to/myResource.res
+            // file://path/to/resources/ + myResource.res -> file://path/to/resources/myResource.res
+            // We assume that even when the ending slash is missing, users have the second
+            // example in mind
+            base += "/";
+        }
+        return new URL(base);
     }
 
     /**
-- 
cgit v1.2.3