You are viewing a plain text version of this content. The canonical link for it is here.
Posted to fop-commits@xmlgraphics.apache.org by je...@apache.org on 2005/09/05 17:31:08 UTC

svn commit: r278779 - /xmlgraphics/fop/trunk/src/java/org/apache/fop/apps/FOURIResolver.java

Author: jeremias
Date: Mon Sep  5 08:31:04 2005
New Revision: 278779

URL: http://svn.apache.org/viewcvs?rev=278779&view=rev
Log:
Not sure if it's the best solution but this changes the following:
- Enable plain filenames (without "file:" scheme) as URI. FOURIResolver tries to create a java.io.File to access the file.
- If after removing the scheme from a file: URL the string still contains a ":" it is likely an absolute Windows path for which the user forgot to specify a leading "/", i.e.:
file:C:/Temp/test.txt --> file:/C:/Temp/test.txt

Modified:
    xmlgraphics/fop/trunk/src/java/org/apache/fop/apps/FOURIResolver.java

Modified: xmlgraphics/fop/trunk/src/java/org/apache/fop/apps/FOURIResolver.java
URL: http://svn.apache.org/viewcvs/xmlgraphics/fop/trunk/src/java/org/apache/fop/apps/FOURIResolver.java?rev=278779&r1=278778&r2=278779&view=diff
==============================================================================
--- xmlgraphics/fop/trunk/src/java/org/apache/fop/apps/FOURIResolver.java (original)
+++ xmlgraphics/fop/trunk/src/java/org/apache/fop/apps/FOURIResolver.java Mon Sep  5 08:31:04 2005
@@ -18,6 +18,7 @@
 
 package org.apache.fop.apps;
 
+import java.io.File;
 import java.net.MalformedURLException;
 import java.net.URL;
 import javax.xml.transform.Source;
@@ -62,52 +63,65 @@
         throws javax.xml.transform.TransformerException {
         
         URL absoluteURL = null;
-        URL baseURL = toBaseURL(base);
-        if (baseURL == null) {
-            // We don't have a valid baseURL just use the URL as given
+        File f = new File(href);
+        if (f.exists()) {
             try {
-                absoluteURL = new URL(href);
-            } catch (MalformedURLException mue) {
-                try {
-                    // the above failed, we give it another go in case
-                    // the href contains only a path then file: is assumed
-                    absoluteURL = new URL("file:" + href);
-                } catch (MalformedURLException mfue) {
-                    log.error("Error with URL '" + href + "': " + mue.getMessage(), mue);
-                    return null;
-                }
+                absoluteURL = f.toURL();
+            } catch (MalformedURLException mfue) {
+                log.error("Could not convert filename to URL: " + mfue.getMessage(), mfue); 
             }
         } else {
-            try {
-                /*
-                    This piece of code is based on the following statement in RFC2396 section 5.2:
+            URL baseURL = toBaseURL(base);
+            if (baseURL == null) {
+                // We don't have a valid baseURL just use the URL as given
+                try {
+                    absoluteURL = new URL(href);
+                } catch (MalformedURLException mue) {
+                    try {
+                        // the above failed, we give it another go in case
+                        // the href contains only a path then file: is assumed
+                        absoluteURL = new URL("file:" + href);
+                    } catch (MalformedURLException mfue) {
+                        log.error("Error with URL '" + href + "': " + mue.getMessage(), mue);
+                        return null;
+                    }
+                }
+            } else {
+                try {
+                    /*
+                        This piece of code is based on the following statement in RFC2396 section 5.2:
 
-                    3) If the scheme component is defined, indicating that the reference
-                       starts with a scheme name, then the reference is interpreted as an
-                       absolute URI and we are done.  Otherwise, the reference URI's
-                       scheme is inherited from the base URI's scheme component.
-
-                       Due to a loophole in prior specifications [RFC1630], some parsers
-                       allow the scheme name to be present in a relative URI if it is the
-                       same as the base URI scheme.  Unfortunately, this can conflict
-                       with the correct parsing of non-hierarchical URI.  For backwards
-                       compatibility, an implementation may work around such references
-                       by removing the scheme if it matches that of the base URI and the
-                       scheme is known to always use the <hier_part> syntax.
-
-                    The URL class does not implement this work around, so we do.
-                */
-
-                String scheme = baseURL.getProtocol() + ":";
-                if (href.startsWith(scheme)) {
-                    href = href.substring(scheme.length());
+                        3) If the scheme component is defined, indicating that the reference
+                           starts with a scheme name, then the reference is interpreted as an
+                           absolute URI and we are done.  Otherwise, the reference URI's
+                           scheme is inherited from the base URI's scheme component.
+
+                           Due to a loophole in prior specifications [RFC1630], some parsers
+                           allow the scheme name to be present in a relative URI if it is the
+                           same as the base URI scheme.  Unfortunately, this can conflict
+                           with the correct parsing of non-hierarchical URI.  For backwards
+                           compatibility, an implementation may work around such references
+                           by removing the scheme if it matches that of the base URI and the
+                           scheme is known to always use the <hier_part> syntax.
+
+                        The URL class does not implement this work around, so we do.
+                    */
+
+                    String scheme = baseURL.getProtocol() + ":";
+                    if (href.startsWith(scheme)) {
+                        href = href.substring(scheme.length());
+                    }
+                    if ("file:".equals(scheme) && href.indexOf(':') >= 0) {
+                        href = "/" + href; //Absolute file URL doesn't have a leading slash
+                    }
+                    absoluteURL = new URL(baseURL, href);
+                } catch (MalformedURLException mfue) {
+                    log.error("Error with URL '" + href + "': " + mfue.getMessage(), mfue);
+                    return null;
                 }
-                absoluteURL = new URL(baseURL, href);
-            } catch (MalformedURLException mfue) {
-                log.error("Error with URL '" + href + "': " + mfue.getMessage(), mfue);
-                return null;
             }
         }
+        
         try {
             return new StreamSource(absoluteURL.openStream(), absoluteURL.toExternalForm());
         } catch (java.io.IOException ioe) {



---------------------------------------------------------------------
To unsubscribe, e-mail: fop-commits-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: fop-commits-help@xmlgraphics.apache.org