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 2008/01/14 12:01:14 UTC

svn commit: r611766 - in /xmlgraphics/fop/trunk: src/java/org/apache/fop/datatypes/ src/java/org/apache/fop/layoutmgr/ test/java/org/apache/fop/datatypes/

Author: jeremias
Date: Mon Jan 14 03:01:11 2008
New Revision: 611766

URL: http://svn.apache.org/viewvc?rev=611766&view=rev
Log:
java.net.URI doesn't eat non-escaped URIs so I added an escaping method to URISpecification that should cover most cases. Usually, it's just about a space in a filename.

Added:
    xmlgraphics/fop/trunk/test/java/org/apache/fop/datatypes/
    xmlgraphics/fop/trunk/test/java/org/apache/fop/datatypes/URISpecificationTestCase.java   (with props)
Modified:
    xmlgraphics/fop/trunk/src/java/org/apache/fop/datatypes/URISpecification.java   (contents, props changed)
    xmlgraphics/fop/trunk/src/java/org/apache/fop/layoutmgr/ExternalDocumentLayoutManager.java

Modified: xmlgraphics/fop/trunk/src/java/org/apache/fop/datatypes/URISpecification.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/src/java/org/apache/fop/datatypes/URISpecification.java?rev=611766&r1=611765&r2=611766&view=diff
==============================================================================
--- xmlgraphics/fop/trunk/src/java/org/apache/fop/datatypes/URISpecification.java (original)
+++ xmlgraphics/fop/trunk/src/java/org/apache/fop/datatypes/URISpecification.java Mon Jan 14 03:01:11 2008
@@ -19,6 +19,9 @@
 
 package org.apache.fop.datatypes;
 
+import java.io.UnsupportedEncodingException;
+
+
 /**
  * This class contains method to deal with the <uri-specification> datatype from XSL-FO.
  */
@@ -52,5 +55,87 @@
         return href;
     }
 
+    private static final String PUNCT = ",;:$&+=";
+    private static final String RESERVED = PUNCT + "?/[]@";
+    
+    private static boolean isValidURIChar(char ch) {
+        return true;
+    }
+    
+    private static boolean isDigit(char ch) {
+        return (ch >= '0' && ch <= '9');
+    }
+    
+    private static boolean isAlpha(char ch) {
+        return (ch >= 'A' && ch <= 'Z') || (ch >= 'A' && ch <= 'z');
+    }
+    
+    private static boolean isHexDigit(char ch) {
+        return (ch >= '0' && ch <= '9') || (ch >= 'A' && ch <= 'F') || (ch >= 'a' && ch <= 'f');
+    }
+
+    private static boolean isReserved(char ch) {
+        if (RESERVED.indexOf(ch) >= 0) {
+            return true;
+        } else if ('#' == ch) {
+            //# is not a reserved character but is used for the fragment
+            return true;
+        }
+        return false;
+    }
+    
+    private static boolean isUnreserved(char ch) {
+        if (isDigit(ch) || isAlpha(ch)) {
+            return true;
+        } else if ("_-!.~\'()*".indexOf(ch) >= 0) {
+            //remaining unreserved characters
+            return true;
+        }
+        return false;
+    }
+    
+    private final static char[] HEX_DIGITS = {
+        '0', '1', '2', '3', '4', '5', '6', '7',
+        '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
+    };
+
+    private static void appendEscape(StringBuffer sb, byte b) {
+        sb.append('%').append(HEX_DIGITS[(b >> 4) & 0x0f]).append(HEX_DIGITS[(b >> 0) & 0x0f]);
+    }
+
+    /**
+     * Escapes any illegal URI character in a given URI, for example, it escapes a space to "%20".
+     * Note: This method does not "parse" the URI and therefore does not treat the individual
+     * components (user-info, path, query etc.) individually.
+     * @param uri the URI to inspect
+     * @return the escaped URI
+     */
+    public static String escapeURI(String uri) {
+        uri = getURL(uri);
+        StringBuffer sb = new StringBuffer();
+        for (int i = 0, c = uri.length(); i < c; i++) {
+            char ch = uri.charAt(i);
+            if (ch == '%') {
+                if (i < c - 3 && isHexDigit(uri.charAt(i + 1)) && isHexDigit(uri.charAt(i + 2))) {
+                    sb.append(ch);
+                    continue;
+                }
+            }
+            if (isReserved(ch) || isUnreserved(ch)) {
+                //Note: this may not be accurate for some very special cases.
+                sb.append(ch);
+            } else {
+                try {
+                    byte[] utf8 = Character.toString(ch).getBytes("UTF-8");
+                    for (int j = 0, cj = utf8.length; j < cj; j++) {
+                        appendEscape(sb, utf8[j]);
+                    }
+                } catch (UnsupportedEncodingException e) {
+                    throw new Error("Incompatible JVM. UTF-8 not supported.");
+                }
+            }
+        }
+        return sb.toString();
+    }
     
 }

Propchange: xmlgraphics/fop/trunk/src/java/org/apache/fop/datatypes/URISpecification.java
------------------------------------------------------------------------------
    svn:keywords = Id

Modified: xmlgraphics/fop/trunk/src/java/org/apache/fop/layoutmgr/ExternalDocumentLayoutManager.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/src/java/org/apache/fop/layoutmgr/ExternalDocumentLayoutManager.java?rev=611766&r1=611765&r2=611766&view=diff
==============================================================================
--- xmlgraphics/fop/trunk/src/java/org/apache/fop/layoutmgr/ExternalDocumentLayoutManager.java (original)
+++ xmlgraphics/fop/trunk/src/java/org/apache/fop/layoutmgr/ExternalDocumentLayoutManager.java Mon Jan 14 03:01:11 2008
@@ -44,6 +44,7 @@
 import org.apache.fop.area.inline.Image;
 import org.apache.fop.area.inline.Viewport;
 import org.apache.fop.datatypes.FODimension;
+import org.apache.fop.datatypes.URISpecification;
 import org.apache.fop.fo.Constants;
 import org.apache.fop.fo.extensions.ExternalDocument;
 import org.apache.fop.layoutmgr.inline.ImageLayout;
@@ -114,7 +115,7 @@
                 }
                 URI originalURI;
                 try {
-                    originalURI = new URI(uri);
+                    originalURI = new URI(URISpecification.escapeURI(uri));
                     int pageIndex = 1;
                     while (hasMoreImages) {
                         URI tempURI = new URI(originalURI.getScheme(),

Added: xmlgraphics/fop/trunk/test/java/org/apache/fop/datatypes/URISpecificationTestCase.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/test/java/org/apache/fop/datatypes/URISpecificationTestCase.java?rev=611766&view=auto
==============================================================================
--- xmlgraphics/fop/trunk/test/java/org/apache/fop/datatypes/URISpecificationTestCase.java (added)
+++ xmlgraphics/fop/trunk/test/java/org/apache/fop/datatypes/URISpecificationTestCase.java Mon Jan 14 03:01:11 2008
@@ -0,0 +1,61 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/* $Id$ */
+
+package org.apache.fop.datatypes;
+
+import junit.framework.TestCase;
+
+/**
+ * Tests for URISpecification.
+ */
+public class URISpecificationTestCase extends TestCase {
+
+    public void testGetURL() throws Exception {
+        String actual;
+        
+        actual = URISpecification.getURL("http://localhost/test");
+        assertEquals("http://localhost/test", actual);
+
+        actual = URISpecification.getURL("url(http://localhost/test)");
+        assertEquals("http://localhost/test", actual);
+
+        actual = URISpecification.getURL("url('http://localhost/test')");
+        assertEquals("http://localhost/test", actual);
+
+        actual = URISpecification.getURL("url(\"http://localhost/test\")");
+        assertEquals("http://localhost/test", actual);
+    }
+    
+    public void testEscapeURI() throws Exception {
+        String actual;
+        
+        actual = URISpecification.escapeURI("http://localhost/test");
+        assertEquals("http://localhost/test", actual);
+
+        actual = URISpecification.escapeURI("http://localhost/test%20test");
+        assertEquals("http://localhost/test%20test", actual);
+
+        actual = URISpecification.escapeURI("http://localhost/test test");
+        assertEquals("http://localhost/test%20test", actual);
+
+        actual = URISpecification.escapeURI("http://localhost/test test.pdf#page=6");
+        assertEquals("http://localhost/test%20test.pdf#page=6", actual);
+    }
+    
+}

Propchange: xmlgraphics/fop/trunk/test/java/org/apache/fop/datatypes/URISpecificationTestCase.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: xmlgraphics/fop/trunk/test/java/org/apache/fop/datatypes/URISpecificationTestCase.java
------------------------------------------------------------------------------
    svn:keywords = Id



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