You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xmlgraphics.apache.org by me...@apache.org on 2012/07/30 12:47:18 UTC

svn commit: r1367055 - in /xmlgraphics/commons/branches/Temp_URI_Resolution: src/java/org/apache/xmlgraphics/io/ test/java/org/apache/xmlgraphics/io/

Author: mehdi
Date: Mon Jul 30 10:47:17 2012
New Revision: 1367055

URL: http://svn.apache.org/viewvc?rev=1367055&view=rev
Log:
Moved I/O interfaces from FOP to commence resource resolution

Added:
    xmlgraphics/commons/branches/Temp_URI_Resolution/src/java/org/apache/xmlgraphics/io/
    xmlgraphics/commons/branches/Temp_URI_Resolution/src/java/org/apache/xmlgraphics/io/Resource.java   (with props)
    xmlgraphics/commons/branches/Temp_URI_Resolution/src/java/org/apache/xmlgraphics/io/ResourceResolver.java   (with props)
    xmlgraphics/commons/branches/Temp_URI_Resolution/src/java/org/apache/xmlgraphics/io/TempResourceResolver.java   (with props)
    xmlgraphics/commons/branches/Temp_URI_Resolution/src/java/org/apache/xmlgraphics/io/TempResourceURIGenerator.java   (with props)
    xmlgraphics/commons/branches/Temp_URI_Resolution/test/java/org/apache/xmlgraphics/io/
    xmlgraphics/commons/branches/Temp_URI_Resolution/test/java/org/apache/xmlgraphics/io/TempResourceURIGeneratorTestCase.java   (with props)

Added: xmlgraphics/commons/branches/Temp_URI_Resolution/src/java/org/apache/xmlgraphics/io/Resource.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/branches/Temp_URI_Resolution/src/java/org/apache/xmlgraphics/io/Resource.java?rev=1367055&view=auto
==============================================================================
--- xmlgraphics/commons/branches/Temp_URI_Resolution/src/java/org/apache/xmlgraphics/io/Resource.java (added)
+++ xmlgraphics/commons/branches/Temp_URI_Resolution/src/java/org/apache/xmlgraphics/io/Resource.java Mon Jul 30 10:47:17 2012
@@ -0,0 +1,57 @@
+/*
+ * 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.io;
+
+import java.io.FilterInputStream;
+import java.io.InputStream;
+
+/**
+ * This class represents a resolved resource.  The type property is used by XGC to identify the
+ * resource content.
+ */
+public class Resource extends FilterInputStream {
+
+    private final String type;
+
+    /**
+     * @param type resource type
+     * @param inputStream input stream of the resource
+     */
+    public Resource(String type, InputStream inputStream) {
+        super(inputStream);
+        this.type = type;
+    }
+
+    /**
+     * Constructs a resource of 'unknown' type.
+     *
+     * @param inputStream input stream of the resource
+     */
+    public Resource(InputStream inputStream) {
+        this("unknown", inputStream);
+    }
+
+    /**
+     * @return the resource type
+     */
+    public String getType() {
+        return this.type;
+    }
+}

Propchange: xmlgraphics/commons/branches/Temp_URI_Resolution/src/java/org/apache/xmlgraphics/io/Resource.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: xmlgraphics/commons/branches/Temp_URI_Resolution/src/java/org/apache/xmlgraphics/io/ResourceResolver.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/branches/Temp_URI_Resolution/src/java/org/apache/xmlgraphics/io/ResourceResolver.java?rev=1367055&view=auto
==============================================================================
--- xmlgraphics/commons/branches/Temp_URI_Resolution/src/java/org/apache/xmlgraphics/io/ResourceResolver.java (added)
+++ xmlgraphics/commons/branches/Temp_URI_Resolution/src/java/org/apache/xmlgraphics/io/ResourceResolver.java Mon Jul 30 10:47:17 2012
@@ -0,0 +1,51 @@
+/*
+ * 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.io;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.net.URI;
+
+/**
+ * Implementations of this resource resolver allow XGC users to control the URI resolution
+ * mechanism. All resource and output stream acquisition goes through this when its implementation
+ * is given to the {@link org.apache.fop.apps.EnvironmentProfile}.
+ */
+public interface ResourceResolver {
+
+    /**
+     * Get a resource given the URI pointing to said resource.
+     *
+     * @param uri the resource URI
+     * @return the resource
+     * @throws IOException if an I/O error occured during resource acquisition
+     */
+    Resource getResource(URI uri) throws IOException;
+
+    /**
+     * Gets an output stream of a given URI.
+     *
+     * @param uri the output stream URI
+     * @return the output stream
+     * @throws IOException if an I/O error occured while creating an output stream
+     */
+    OutputStream getOutputStream(URI uri) throws IOException;
+
+}

Propchange: xmlgraphics/commons/branches/Temp_URI_Resolution/src/java/org/apache/xmlgraphics/io/ResourceResolver.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: xmlgraphics/commons/branches/Temp_URI_Resolution/src/java/org/apache/xmlgraphics/io/TempResourceResolver.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/branches/Temp_URI_Resolution/src/java/org/apache/xmlgraphics/io/TempResourceResolver.java?rev=1367055&view=auto
==============================================================================
--- xmlgraphics/commons/branches/Temp_URI_Resolution/src/java/org/apache/xmlgraphics/io/TempResourceResolver.java (added)
+++ xmlgraphics/commons/branches/Temp_URI_Resolution/src/java/org/apache/xmlgraphics/io/TempResourceResolver.java Mon Jul 30 10:47:17 2012
@@ -0,0 +1,48 @@
+/*
+ * 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.io;
+
+import java.io.IOException;
+import java.io.OutputStream;
+
+/**
+ * Implementations of this interface resolve URIs for temporary files used by XGC. The temporary-
+ * resource URI scheme comes from {@link TempResourceURIGenerator#TMP_SCHEME}.
+ */
+public interface TempResourceResolver {
+
+    /**
+     * Get a temporary-resource given the identifier pointing to said resource.
+     *
+     * @param id the resource identifier
+     * @return the resource
+     * @throws IOException if an I/O error occured during resource acquisition
+     */
+    Resource getResource(String id) throws IOException;
+
+    /**
+     * Gets an temporary-output stream of a given identifier.
+     *
+     * @param id the output stream identifier
+     * @return the output stream
+     * @throws IOException if an I/O error occured while creating an output stream
+     */
+    OutputStream getOutputStream(String id) throws IOException;
+}

Propchange: xmlgraphics/commons/branches/Temp_URI_Resolution/src/java/org/apache/xmlgraphics/io/TempResourceResolver.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: xmlgraphics/commons/branches/Temp_URI_Resolution/src/java/org/apache/xmlgraphics/io/TempResourceURIGenerator.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/branches/Temp_URI_Resolution/src/java/org/apache/xmlgraphics/io/TempResourceURIGenerator.java?rev=1367055&view=auto
==============================================================================
--- xmlgraphics/commons/branches/Temp_URI_Resolution/src/java/org/apache/xmlgraphics/io/TempResourceURIGenerator.java (added)
+++ xmlgraphics/commons/branches/Temp_URI_Resolution/src/java/org/apache/xmlgraphics/io/TempResourceURIGenerator.java Mon Jul 30 10:47:17 2012
@@ -0,0 +1,57 @@
+/*
+ * 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.
+ */
+
+package org.apache.xmlgraphics.io;
+
+import java.net.URI;
+import java.util.concurrent.atomic.AtomicLong;
+
+/**
+ * Creates a URI for any temporary resource used within XGC.
+ */
+public final class TempResourceURIGenerator {
+
+    public static final String TMP_SCHEME = "tmp";
+
+    private final String tempURIPrefix;
+
+    private final AtomicLong counter;
+
+    /**
+     * @param uriPrefix a prefix used to name the unique URI
+     */
+    public TempResourceURIGenerator(String uriPrefix) {
+        counter = new AtomicLong();
+        tempURIPrefix = URI.create(TMP_SCHEME + ":///" + uriPrefix).toASCIIString();
+    }
+
+    /**
+     * Generate a unique URI for a temporary resource
+     * @return the URI
+     */
+    public URI generate() {
+        return URI.create(tempURIPrefix + getUniqueId());
+    }
+
+    private String getUniqueId() {
+        return Long.toHexString(counter.getAndIncrement());
+    }
+
+    public static boolean isTempURI(URI uri) {
+        return TMP_SCHEME.equals(uri.getScheme());
+    }
+}

Propchange: xmlgraphics/commons/branches/Temp_URI_Resolution/src/java/org/apache/xmlgraphics/io/TempResourceURIGenerator.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: xmlgraphics/commons/branches/Temp_URI_Resolution/test/java/org/apache/xmlgraphics/io/TempResourceURIGeneratorTestCase.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/branches/Temp_URI_Resolution/test/java/org/apache/xmlgraphics/io/TempResourceURIGeneratorTestCase.java?rev=1367055&view=auto
==============================================================================
--- xmlgraphics/commons/branches/Temp_URI_Resolution/test/java/org/apache/xmlgraphics/io/TempResourceURIGeneratorTestCase.java (added)
+++ xmlgraphics/commons/branches/Temp_URI_Resolution/test/java/org/apache/xmlgraphics/io/TempResourceURIGeneratorTestCase.java Mon Jul 30 10:47:17 2012
@@ -0,0 +1,68 @@
+/*
+ * 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.
+ */
+
+package org.apache.xmlgraphics.io;
+
+import java.net.URI;
+import java.util.HashSet;
+import java.util.Set;
+import java.util.regex.Pattern;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotSame;
+import static org.junit.Assert.assertTrue;
+
+public class TempResourceURIGeneratorTestCase {
+
+    private TempResourceURIGenerator sut = new TempResourceURIGenerator("test");
+
+    @Test
+    public void testGenerate() {
+        URI first = sut.generate();
+        URI second = sut.generate();
+        Pattern regex = Pattern.compile("tmp:///test.*");
+        assertTrue(regex.matcher(first.toASCIIString()).matches());
+        assertTrue(regex.matcher(second.toASCIIString()).matches());
+        assertNotSame(first, second);
+
+        // Test that they are unique over a large number of calls to generate()
+        Set<URI> uniqueSet = new HashSet<URI>();
+        int numberOfTests = 1000;
+        for (int i = 0; i < numberOfTests; i++) {
+            uniqueSet.add(sut.generate());
+        }
+        assertEquals(numberOfTests, uniqueSet.size());
+    }
+
+    @Test
+    public void testIsTemURI() {
+        assertTrue(testTempURI("tmp:///test"));
+        assertTrue(testTempURI("tmp://test"));
+        assertTrue(testTempURI("tmp:/test"));
+        assertTrue(testTempURI("tmp:test"));
+
+        assertFalse(testTempURI("tmp/test"));
+        assertFalse(testTempURI("temp:///test"));
+    }
+
+    private boolean testTempURI(String uriString) {
+        return TempResourceURIGenerator.isTempURI(URI.create(uriString));
+    }
+}

Propchange: xmlgraphics/commons/branches/Temp_URI_Resolution/test/java/org/apache/xmlgraphics/io/TempResourceURIGeneratorTestCase.java
------------------------------------------------------------------------------
    svn:eol-style = native



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