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/24 15:11:14 UTC

svn commit: r688507 - in /xmlgraphics/commons/trunk: src/java/org/apache/xmlgraphics/util/ src/java/org/apache/xmlgraphics/util/uri/ src/resources/META-INF/services/ test/java/org/apache/xmlgraphics/util/uri/

Author: maxberger
Date: Sun Aug 24 06:11:14 2008
New Revision: 688507

URL: http://svn.apache.org/viewvc?rev=688507&view=rev
Log:
Moved DataURIResolver from FOP to commons; created a new URIResolver registry; made DataURIResolver into a service

Added:
    xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/util/WriterOutputStream.java
      - copied, changed from r688093, xmlgraphics/fop/trunk/src/java/org/apache/fop/util/WriterOutputStream.java
    xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/util/uri/
    xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/util/uri/CommonURIResolver.java   (with props)
    xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/util/uri/DataURIResolver.java
      - copied, changed from r688093, xmlgraphics/fop/trunk/src/java/org/apache/fop/util/DataURIResolver.java
    xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/util/uri/DataURLUtil.java
      - copied, changed from r688093, xmlgraphics/fop/trunk/src/java/org/apache/fop/util/DataURLUtil.java
    xmlgraphics/commons/trunk/src/resources/META-INF/services/javax.xml.transform.URIResolver
    xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/util/uri/
    xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/util/uri/CommonURIResolverTestCase.java   (with props)
    xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/util/uri/DataURIResolverTestCase.java
      - copied, changed from r688093, xmlgraphics/fop/trunk/test/java/org/apache/fop/util/DataURIResolverTestCase.java

Copied: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/util/WriterOutputStream.java (from r688093, xmlgraphics/fop/trunk/src/java/org/apache/fop/util/WriterOutputStream.java)
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/util/WriterOutputStream.java?p2=xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/util/WriterOutputStream.java&p1=xmlgraphics/fop/trunk/src/java/org/apache/fop/util/WriterOutputStream.java&r1=688093&r2=688507&rev=688507&view=diff
==============================================================================
--- xmlgraphics/fop/trunk/src/java/org/apache/fop/util/WriterOutputStream.java (original)
+++ xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/util/WriterOutputStream.java Sun Aug 24 06:11:14 2008
@@ -17,7 +17,7 @@
 
 /* $Id$ */
 
-package org.apache.fop.util;
+package org.apache.xmlgraphics.util;
 
 import java.io.IOException;
 import java.io.OutputStream;

Added: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/util/uri/CommonURIResolver.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/util/uri/CommonURIResolver.java?rev=688507&view=auto
==============================================================================
--- xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/util/uri/CommonURIResolver.java (added)
+++ xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/util/uri/CommonURIResolver.java Sun Aug 24 06:11:14 2008
@@ -0,0 +1,103 @@
+/*
+ * 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.xmlgraphics.util.uri;
+
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+
+import javax.xml.transform.Source;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.URIResolver;
+
+import org.apache.xmlgraphics.util.Service;
+
+/**
+ * A URI Resolver which supports pluggable entities via the {@link Service}
+ * mechanism.
+ * <p>
+ * This resolver will try all resolvers registered as an {@link URIResolver}
+ * class. For proper operation, the registers URIResolvers must return null if
+ * they cannot handle the given URI.
+ */
+public final class CommonURIResolver implements URIResolver {
+
+    private final List uriResolvers = new LinkedList();
+
+    private final static class SingletonHolder {
+        private static final CommonURIResolver INSTANCE = new CommonURIResolver();
+    }
+
+    private CommonURIResolver() {
+        Iterator iter = Service.providers(URIResolver.class);
+        while (iter.hasNext()) {
+            URIResolver resolver = (URIResolver) iter.next();
+            register(resolver);
+        }
+    }
+
+    public static CommonURIResolver getInstance() {
+        return SingletonHolder.INSTANCE;
+    }
+
+    /** {@inheritDoc} */
+    public Source resolve(String href, String base) {
+        synchronized (uriResolvers) {
+            Iterator it = uriResolvers.iterator();
+            while (it.hasNext()) {
+                final URIResolver currentResolver = (URIResolver) it.next();
+                try {
+                    final Source result = currentResolver.resolve(href, base);
+                    if (result != null) {
+                        return result;
+                    }
+                } catch (TransformerException e) {
+                    // Ignore.
+                }
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Register a given {@link URIResolver} while the software is running.
+     * 
+     * @param uriResolver
+     *            the resolver to register.
+     */
+    public void register(URIResolver uriResolver) {
+        synchronized (uriResolvers) {
+            uriResolvers.add(uriResolver);
+        }
+    }
+
+    /**
+     * Unregister a given {@link URIResolver} while the software is running.
+     * 
+     * @param uriResolver
+     *            the resolver to unregister.
+     */
+    public void unregister(URIResolver uriResolver) {
+        synchronized (uriResolvers) {
+            uriResolvers.remove(uriResolver);
+        }
+    }
+
+}

Propchange: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/util/uri/CommonURIResolver.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/util/uri/CommonURIResolver.java
------------------------------------------------------------------------------
    svn:keywords = Id

Propchange: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/util/uri/CommonURIResolver.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Copied: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/util/uri/DataURIResolver.java (from r688093, xmlgraphics/fop/trunk/src/java/org/apache/fop/util/DataURIResolver.java)
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/util/uri/DataURIResolver.java?p2=xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/util/uri/DataURIResolver.java&p1=xmlgraphics/fop/trunk/src/java/org/apache/fop/util/DataURIResolver.java&r1=688093&r2=688507&rev=688507&view=diff
==============================================================================
--- xmlgraphics/fop/trunk/src/java/org/apache/fop/util/DataURIResolver.java (original)
+++ xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/util/uri/DataURIResolver.java Sun Aug 24 06:11:14 2008
@@ -17,7 +17,7 @@
 
 /* $Id$ */
 
-package org.apache.fop.util;
+package org.apache.xmlgraphics.util.uri;
 
 import java.io.ByteArrayInputStream;
 

Copied: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/util/uri/DataURLUtil.java (from r688093, xmlgraphics/fop/trunk/src/java/org/apache/fop/util/DataURLUtil.java)
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/util/uri/DataURLUtil.java?p2=xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/util/uri/DataURLUtil.java&p1=xmlgraphics/fop/trunk/src/java/org/apache/fop/util/DataURLUtil.java&r1=688093&r2=688507&rev=688507&view=diff
==============================================================================
--- xmlgraphics/fop/trunk/src/java/org/apache/fop/util/DataURLUtil.java (original)
+++ xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/util/uri/DataURLUtil.java Sun Aug 24 06:11:14 2008
@@ -17,7 +17,7 @@
 
 /* $Id$ */
 
-package org.apache.fop.util;
+package org.apache.xmlgraphics.util.uri;
 
 import java.io.IOException;
 import java.io.InputStream;
@@ -25,6 +25,7 @@
 import java.io.Writer;
 
 import org.apache.commons.io.IOUtils;
+import org.apache.xmlgraphics.util.WriterOutputStream;
 import org.apache.xmlgraphics.util.io.Base64EncodeStream;
 
 /**

Added: xmlgraphics/commons/trunk/src/resources/META-INF/services/javax.xml.transform.URIResolver
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/src/resources/META-INF/services/javax.xml.transform.URIResolver?rev=688507&view=auto
==============================================================================
--- xmlgraphics/commons/trunk/src/resources/META-INF/services/javax.xml.transform.URIResolver (added)
+++ xmlgraphics/commons/trunk/src/resources/META-INF/services/javax.xml.transform.URIResolver Sun Aug 24 06:11:14 2008
@@ -0,0 +1 @@
+org.apache.xmlgraphics.util.uri.DataURIResolver
\ No newline at end of file

Added: xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/util/uri/CommonURIResolverTestCase.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/util/uri/CommonURIResolverTestCase.java?rev=688507&view=auto
==============================================================================
--- xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/util/uri/CommonURIResolverTestCase.java (added)
+++ xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/util/uri/CommonURIResolverTestCase.java Sun Aug 24 06:11:14 2008
@@ -0,0 +1,42 @@
+/*
+ * 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.xmlgraphics.util.uri;
+
+import javax.xml.transform.URIResolver;
+
+import junit.framework.TestCase;
+
+/**
+ * Test case for the {@link CommonURIResolver}.
+ */
+public class CommonURIResolverTestCase extends TestCase {
+
+    /**
+     * Test the DataURIResolver with correct values.
+     * 
+     * @throws Exception
+     *             if an error occurs
+     */
+    public void testDataURLHandling() throws Exception {
+        URIResolver resolver = CommonURIResolver.getInstance();
+        DataURIResolverTestCase.actualURLHAndlingTest(resolver);
+    }
+
+}

Propchange: xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/util/uri/CommonURIResolverTestCase.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/util/uri/CommonURIResolverTestCase.java
------------------------------------------------------------------------------
    svn:keywords = Id

Propchange: xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/util/uri/CommonURIResolverTestCase.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Copied: xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/util/uri/DataURIResolverTestCase.java (from r688093, xmlgraphics/fop/trunk/test/java/org/apache/fop/util/DataURIResolverTestCase.java)
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/util/uri/DataURIResolverTestCase.java?p2=xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/util/uri/DataURIResolverTestCase.java&p1=xmlgraphics/fop/trunk/test/java/org/apache/fop/util/DataURIResolverTestCase.java&r1=688093&r2=688507&rev=688507&view=diff
==============================================================================
--- xmlgraphics/fop/trunk/test/java/org/apache/fop/util/DataURIResolverTestCase.java (original)
+++ xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/util/uri/DataURIResolverTestCase.java Sun Aug 24 06:11:14 2008
@@ -17,7 +17,7 @@
 
 /* $Id$ */
 
-package org.apache.fop.util;
+package org.apache.xmlgraphics.util.uri;
 
 import java.io.ByteArrayInputStream;
 
@@ -25,42 +25,52 @@
 import javax.xml.transform.URIResolver;
 import javax.xml.transform.stream.StreamSource;
 
-import org.apache.commons.io.IOUtils;
-
 import junit.framework.TestCase;
 
+import org.apache.commons.io.IOUtils;
+
 /**
  * Test case for the RFC 2397 data URL/URI resolver.
  */
 public class DataURIResolverTestCase extends TestCase {
 
-    private static final byte[] TESTDATA = new byte[] {0, 1, 2, 3, 4, 5};
+    private static final byte[] TESTDATA = new byte[] { 0, 1, 2, 3, 4, 5 };
 
     /**
      * Tests DataURLUtil.
-     * @throws Exception if an error occurs
+     * 
+     * @throws Exception
+     *             if an error occurs
      */
     public void testRFC2397Generator() throws Exception {
-        String url = DataURLUtil.createDataURL(new ByteArrayInputStream(TESTDATA), null);
-        assertEquals("Generated data URL is wrong", "data:;base64,AAECAwQF", url);
-
-        url = DataURLUtil.createDataURL(new ByteArrayInputStream(TESTDATA), "application/pdf");
-        assertEquals("Generated data URL is wrong", "data:application/pdf;base64,AAECAwQF", url);
+        String url = DataURLUtil.createDataURL(new ByteArrayInputStream(
+                TESTDATA), null);
+        assertEquals("Generated data URL is wrong", "data:;base64,AAECAwQF",
+                url);
+
+        url = DataURLUtil.createDataURL(new ByteArrayInputStream(TESTDATA),
+                "application/pdf");
+        assertEquals("Generated data URL is wrong",
+                "data:application/pdf;base64,AAECAwQF", url);
     }
 
     /**
-     * Test the URIResolver contract if the protocol doesn't match. Resolver must return null
-     * in this case.
-     * @throws Exception if an error occurs
+     * Test the URIResolver contract if the protocol doesn't match. Resolver
+     * must return null in this case.
+     * 
+     * @throws Exception
+     *             if an error occurs
      */
     public void testNonMatchingContract() throws Exception {
         URIResolver resolver = new DataURIResolver();
         Source src;
 
-        src = resolver.resolve("http://xmlgraphics.apache.org/fop/index.html", null);
+        src = resolver.resolve("http://xmlgraphics.apache.org/fop/index.html",
+                null);
         assertNull(src);
 
-        src = resolver.resolve("index.html", "http://xmlgraphics.apache.org/fop/");
+        src = resolver.resolve("index.html",
+                "http://xmlgraphics.apache.org/fop/");
         assertNull(src);
     }
 
@@ -75,42 +85,54 @@
 
     /**
      * Test the DataURIResolver with correct values.
-     * @throws Exception if an error occurs
+     * 
+     * @throws Exception
+     *             if an error occurs
      */
     public void testDataURLHandling() throws Exception {
         URIResolver resolver = new DataURIResolver();
+        actualURLHAndlingTest(resolver);
+    }
+
+    static final void actualURLHAndlingTest(URIResolver resolver)
+            throws Exception {
         Source src;
 
         src = resolver.resolve("data:;base64,AAECAwQF", null);
         assertNotNull(src);
-        StreamSource streamSource = (StreamSource)src;
+        StreamSource streamSource = (StreamSource) src;
         byte[] data = IOUtils.toByteArray(streamSource.getInputStream());
-        assertTrue("Decoded data doesn't match the test data", byteCmp(TESTDATA, 0, data));
+        assertTrue("Decoded data doesn't match the test data", byteCmp(
+                TESTDATA, 0, data));
 
-        src = resolver.resolve(
-                "data:application/octet-stream;interpreter=fop;base64,AAECAwQF", null);
+        src = resolver
+                .resolve(
+                        "data:application/octet-stream;interpreter=fop;base64,AAECAwQF",
+                        null);
         assertNotNull(src);
-        streamSource = (StreamSource)src;
+        streamSource = (StreamSource) src;
         assertNotNull(streamSource.getInputStream());
         assertNull(streamSource.getReader());
         data = IOUtils.toByteArray(streamSource.getInputStream());
-        assertTrue("Decoded data doesn't match the test data", byteCmp(TESTDATA, 0, data));
+        assertTrue("Decoded data doesn't match the test data", byteCmp(
+                TESTDATA, 0, data));
 
         src = resolver.resolve("data:,FOP", null);
         assertNotNull(src);
-        streamSource = (StreamSource)src;
+        streamSource = (StreamSource) src;
         assertNull(streamSource.getInputStream());
         assertNotNull(streamSource.getReader());
         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);
-        */
+        /*
+         * 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);
+         */
+
     }
 
 }



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