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/08/01 09:25:19 UTC
svn commit: r1367873 - in /xmlgraphics/commons/branches/Temp_URI_Resolution:
src/java/org/apache/xmlgraphics/io/ test/java/org/apache/xmlgraphics/io/
test/resources/org/apache/xmlgraphics/io/
Author: mehdi
Date: Wed Aug 1 07:25:19 2012
New Revision: 1367873
URL: http://svn.apache.org/viewvc?rev=1367873&view=rev
Log:
Added an adapter for javax.xml.transform.URIResolver and a test for a CatalogResolver - the unit test doesn't compile without JAXB in classpath
Added:
xmlgraphics/commons/branches/Temp_URI_Resolution/src/java/org/apache/xmlgraphics/io/URIResolverAdapter.java (with props)
xmlgraphics/commons/branches/Temp_URI_Resolution/test/java/org/apache/xmlgraphics/io/URIResolverAdapterTestCase.java (with props)
xmlgraphics/commons/branches/Temp_URI_Resolution/test/resources/org/apache/xmlgraphics/io/
xmlgraphics/commons/branches/Temp_URI_Resolution/test/resources/org/apache/xmlgraphics/io/test-catalog.xml (with props)
xmlgraphics/commons/branches/Temp_URI_Resolution/test/resources/org/apache/xmlgraphics/io/testResource.txt (with props)
Added: xmlgraphics/commons/branches/Temp_URI_Resolution/src/java/org/apache/xmlgraphics/io/URIResolverAdapter.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/branches/Temp_URI_Resolution/src/java/org/apache/xmlgraphics/io/URIResolverAdapter.java?rev=1367873&view=auto
==============================================================================
--- xmlgraphics/commons/branches/Temp_URI_Resolution/src/java/org/apache/xmlgraphics/io/URIResolverAdapter.java (added)
+++ xmlgraphics/commons/branches/Temp_URI_Resolution/src/java/org/apache/xmlgraphics/io/URIResolverAdapter.java Wed Aug 1 07:25:19 2012
@@ -0,0 +1,86 @@
+/*
+ * 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.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.URI;
+import java.net.URL;
+
+import javax.xml.transform.Source;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.URIResolver;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.sax.SAXSource;
+import javax.xml.transform.stream.StreamResult;
+import javax.xml.transform.stream.StreamSource;
+
+/**
+ * An adapter between {@link URIResolver} to {@link ResourceResolver}. This adapter allows users
+ * to utilize the resolvers from the XML library for resource acquisition.
+ */
+public class URIResolverAdapter implements ResourceResolver {
+
+ private final URIResolver resolver;
+
+ private final ResourceResolver outputStreamResolver;
+
+ /**
+ * @param resolver the desired {@link URIResolver}
+ * @param outputStreamResolver a resource resolver used only for creating {@link OutputStream}s
+ */
+ public URIResolverAdapter(URIResolver resolver, ResourceResolver outputStreamResolver) {
+ this.resolver = resolver;
+ this.outputStreamResolver = outputStreamResolver;
+ }
+
+ /** {@inheritDoc} */
+ public Resource getResource(URI uri) throws IOException {
+ try {
+ Source src = resolver.resolve(uri.toASCIIString(), null);
+ InputStream resourceStream = null;
+ if (src instanceof StreamSource) {
+ resourceStream = ((StreamSource) src).getInputStream();
+ } else if (src instanceof DOMSource) {
+ ByteArrayOutputStream outStream = new ByteArrayOutputStream();
+ StreamResult xmlSource = new StreamResult(outStream);
+ TransformerFactory.newInstance().newTransformer().transform(src, xmlSource);
+ resourceStream = new ByteArrayInputStream(outStream.toByteArray());
+ } else if (src instanceof SAXSource) {
+ resourceStream = ((SAXSource) src).getInputSource().getByteStream();
+ }
+
+ if (resourceStream == null) {
+ URL url = new URL(src.getSystemId());
+ resourceStream = url.openStream();
+ }
+ return new Resource(resourceStream);
+ } catch (TransformerException e) {
+ throw new IOException(e.getMessage());
+ }
+ }
+
+ /** {@inheritDoc} */
+ public OutputStream getOutputStream(URI uri) throws IOException {
+ return outputStreamResolver.getOutputStream(uri);
+ }
+}
Propchange: xmlgraphics/commons/branches/Temp_URI_Resolution/src/java/org/apache/xmlgraphics/io/URIResolverAdapter.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: xmlgraphics/commons/branches/Temp_URI_Resolution/test/java/org/apache/xmlgraphics/io/URIResolverAdapterTestCase.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/branches/Temp_URI_Resolution/test/java/org/apache/xmlgraphics/io/URIResolverAdapterTestCase.java?rev=1367873&view=auto
==============================================================================
--- xmlgraphics/commons/branches/Temp_URI_Resolution/test/java/org/apache/xmlgraphics/io/URIResolverAdapterTestCase.java (added)
+++ xmlgraphics/commons/branches/Temp_URI_Resolution/test/java/org/apache/xmlgraphics/io/URIResolverAdapterTestCase.java Wed Aug 1 07:25:19 2012
@@ -0,0 +1,66 @@
+/*
+ * 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.io.IOException;
+import java.io.InputStream;
+import java.io.StringWriter;
+import java.net.URI;
+import java.net.URL;
+
+import javax.xml.transform.Source;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.sax.SAXSource;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+import org.apache.commons.io.IOUtils;
+
+import com.sun.org.apache.xml.internal.resolver.tools.CatalogResolver;
+
+public class URIResolverAdapterTestCase {
+
+ private final URI textFileURI = URI.create("test:catalog:resolver:testResource.txt");
+
+ @Test
+ public void testCatalogResolver() throws TransformerException, IOException {
+ System.setProperty("xml.catalog.files",
+ "test/resources/org/apache/xmlgraphics/io/test-catalog.xml");
+ CatalogResolver catalogResolver = new CatalogResolver();
+ Source src = catalogResolver.resolve(textFileURI.toASCIIString(), null);
+ if (src instanceof SAXSource) {
+ testInputStream(new URL(src.getSystemId()).openStream());
+ }
+ }
+
+ @Test
+ public void testCatalogResolverInAdapter() throws IOException {
+ System.setProperty("xml.catalog.files",
+ "test/resources/org/apache/xmlgraphics/io/test-catalog.xml");
+ ResourceResolver resourceResolver = new URIResolverAdapter(new CatalogResolver(), null);
+ testInputStream(resourceResolver.getResource(textFileURI));
+ }
+
+ private void testInputStream(InputStream stream) throws IOException {
+ StringWriter writer = new StringWriter();
+ IOUtils.copy(stream, writer);
+ assertEquals("This is a text file used to test the CatalogResolver\n", writer.toString());
+ }
+}
Propchange: xmlgraphics/commons/branches/Temp_URI_Resolution/test/java/org/apache/xmlgraphics/io/URIResolverAdapterTestCase.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: xmlgraphics/commons/branches/Temp_URI_Resolution/test/resources/org/apache/xmlgraphics/io/test-catalog.xml
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/branches/Temp_URI_Resolution/test/resources/org/apache/xmlgraphics/io/test-catalog.xml?rev=1367873&view=auto
==============================================================================
--- xmlgraphics/commons/branches/Temp_URI_Resolution/test/resources/org/apache/xmlgraphics/io/test-catalog.xml (added)
+++ xmlgraphics/commons/branches/Temp_URI_Resolution/test/resources/org/apache/xmlgraphics/io/test-catalog.xml Wed Aug 1 07:25:19 2012
@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<catalog xmlns="urn:oasis:names:tc:entity:xmlns:xml:catalog">
+ <rewriteURI uriStartString="test:catalog:resolver:" rewritePrefix="./"/>
+</catalog>
Propchange: xmlgraphics/commons/branches/Temp_URI_Resolution/test/resources/org/apache/xmlgraphics/io/test-catalog.xml
------------------------------------------------------------------------------
svn:eol-style = native
Added: xmlgraphics/commons/branches/Temp_URI_Resolution/test/resources/org/apache/xmlgraphics/io/testResource.txt
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/branches/Temp_URI_Resolution/test/resources/org/apache/xmlgraphics/io/testResource.txt?rev=1367873&view=auto
==============================================================================
--- xmlgraphics/commons/branches/Temp_URI_Resolution/test/resources/org/apache/xmlgraphics/io/testResource.txt (added)
+++ xmlgraphics/commons/branches/Temp_URI_Resolution/test/resources/org/apache/xmlgraphics/io/testResource.txt Wed Aug 1 07:25:19 2012
@@ -0,0 +1 @@
+This is a text file used to test the CatalogResolver
Propchange: xmlgraphics/commons/branches/Temp_URI_Resolution/test/resources/org/apache/xmlgraphics/io/testResource.txt
------------------------------------------------------------------------------
svn:eol-style = native
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: commits-help@xmlgraphics.apache.org