You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xmlgraphics.apache.org by ma...@apache.org on 2008/08/26 14:08:09 UTC

svn commit: r689042 - in /xmlgraphics/commons/trunk: src/java/org/apache/xmlgraphics/util/uri/DataURIResolver.java test/java/org/apache/xmlgraphics/util/uri/DataURIResolverTestCase.java

Author: maxberger
Date: Tue Aug 26 05:08:09 2008
New Revision: 689042

URL: http://svn.apache.org/viewvc?rev=689042&view=rev
Log:
Completed implementation of RFC 2397 data URLs

Modified:
    xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/util/uri/DataURIResolver.java
    xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/util/uri/DataURIResolverTestCase.java

Modified: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/util/uri/DataURIResolver.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/util/uri/DataURIResolver.java?rev=689042&r1=689041&r2=689042&view=diff
==============================================================================
--- xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/util/uri/DataURIResolver.java (original)
+++ xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/util/uri/DataURIResolver.java Tue Aug 26 05:08:09 2008
@@ -20,13 +20,16 @@
 package org.apache.xmlgraphics.util.uri;
 
 import java.io.ByteArrayInputStream;
+import java.io.UnsupportedEncodingException;
+import java.net.URLDecoder;
 
 import javax.xml.transform.Source;
 import javax.xml.transform.TransformerException;
 import javax.xml.transform.URIResolver;
 import javax.xml.transform.stream.StreamSource;
 
-// base64 support for "data" urls
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 import org.apache.xmlgraphics.util.io.Base64DecodeStream;
 
 /**
@@ -37,6 +40,10 @@
  */
 public class DataURIResolver implements URIResolver {
 
+    /** logger */
+    private static Log LOG = LogFactory.getLog(URIResolver.class);
+
+    
     /**
      * {@inheritDoc}
      */
@@ -66,13 +73,23 @@
                     encodedStream);
             return new StreamSource(decodedStream);
         } else {
-            // Note that this is not quite the full story here. But since we are
-            // only interested
-            // in base64-encoded binary data, the next line will probably never
-            // be called.
-            //TODO Handle un-escaping of special URL chars like %20
-            return new StreamSource(new java.io.StringReader(data));
+            String encoding = "UTF-8";
+            final int charsetpos = header.indexOf(";charset=");
+            if (charsetpos > 0) {
+                encoding = header.substring(charsetpos + 9);
+            }
+            try {
+                final String unescapedString = URLDecoder
+                        .decode(data, encoding);
+                return new StreamSource(new java.io.StringReader(
+                        unescapedString));
+            } catch (IllegalArgumentException e) {
+                LOG.warn(e.getMessage());
+            } catch (UnsupportedEncodingException e) {
+                LOG.warn(e.getMessage());
+            }
         }
+        return null;
     }
 
 }

Modified: xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/util/uri/DataURIResolverTestCase.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/util/uri/DataURIResolverTestCase.java?rev=689042&r1=689041&r2=689042&view=diff
==============================================================================
--- xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/util/uri/DataURIResolverTestCase.java (original)
+++ xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/util/uri/DataURIResolverTestCase.java Tue Aug 26 05:08:09 2008
@@ -125,14 +125,17 @@
         String text = IOUtils.toString(streamSource.getReader());
         assertEquals("FOP", text);
 
-        /*
-         * TODO Un-escaping of special URL chars like %20 hasn't been
-         * implemented, yet. src = resolver.resolve("data:,A%20brief%20note",
-         * null); assertNotNull(src); streamSource = (StreamSource)src; text =
-         * IOUtils.toString(streamSource.getReader());
-         * assertEquals("A brief note", text);
-         */
-
+        src = resolver.resolve("data:,A%20brief%20note", null);
+        assertNotNull(src);
+        streamSource = (StreamSource) src;
+        text = IOUtils.toString(streamSource.getReader());
+        assertEquals("A brief note", text);
+        
+        src = resolver.resolve("data:text/plain;charset=iso-8859-7,%be%f9%be", null);
+        assertNotNull(src);
+        streamSource = (StreamSource) src;
+        text = IOUtils.toString(streamSource.getReader());
+        assertEquals("\u038e\u03c9\u038e", text);
     }
 
 }



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