From: Mehdi Houshmand Date: Wed, 19 Sep 2012 15:37:57 +0000 (+0000) Subject: Moved the I/O interfaces to XGC and updated code with new resolution mechanism X-Git-Tag: fop-2_0~288^2~1 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=c9cbf7e0a418fb7735f6d4a42632b867f3dab36c;p=xmlgraphics-fop.git Moved the I/O interfaces to XGC and updated code with new resolution mechanism git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/branches/Temp_XGC_URI_Resolution@1387645 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/examples/plan/src/org/apache/fop/plan/PreloaderPlan.java b/examples/plan/src/org/apache/fop/plan/PreloaderPlan.java index 8544ce544..d88658c37 100644 --- a/examples/plan/src/org/apache/fop/plan/PreloaderPlan.java +++ b/examples/plan/src/org/apache/fop/plan/PreloaderPlan.java @@ -43,6 +43,7 @@ import org.apache.xmlgraphics.image.loader.ImageSize; import org.apache.xmlgraphics.image.loader.impl.AbstractImagePreloader; import org.apache.xmlgraphics.image.loader.impl.ImageXMLDOM; import org.apache.xmlgraphics.image.loader.util.ImageUtil; +import org.apache.xmlgraphics.io.XmlSourceUtil; import org.apache.fop.util.DefaultErrorListener; import org.apache.fop.util.UnclosableInputStream; diff --git a/src/java/org/apache/fop/afp/AFPStreamer.java b/src/java/org/apache/fop/afp/AFPStreamer.java index fb2b282f6..5e6b5a79e 100644 --- a/src/java/org/apache/fop/afp/AFPStreamer.java +++ b/src/java/org/apache/fop/afp/AFPStreamer.java @@ -32,10 +32,11 @@ import org.apache.commons.io.IOUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.apache.xmlgraphics.io.TempResourceURIGenerator; + import org.apache.fop.afp.modca.ResourceGroup; import org.apache.fop.afp.modca.StreamedResourceGroup; import org.apache.fop.apps.io.InternalResourceResolver; -import org.apache.fop.apps.io.TempResourceURIGenerator; /** * Manages the streaming of the AFP output diff --git a/src/java/org/apache/fop/apps/EnvironmentProfile.java b/src/java/org/apache/fop/apps/EnvironmentProfile.java index c964120e3..1a585353e 100644 --- a/src/java/org/apache/fop/apps/EnvironmentProfile.java +++ b/src/java/org/apache/fop/apps/EnvironmentProfile.java @@ -21,7 +21,9 @@ package org.apache.fop.apps; import java.net.URI; -import org.apache.fop.apps.io.ResourceResolver; +import org.apache.xmlgraphics.image.loader.impl.AbstractImageSessionContext.FallbackResolver; +import org.apache.xmlgraphics.io.ResourceResolver; + import org.apache.fop.fonts.FontManager; /** @@ -52,4 +54,7 @@ public interface EnvironmentProfile { * @return the default base URI */ URI getDefaultBaseURI(); + + /** @see FopFactoryConfig#getFallbackResolver() */ + FallbackResolver getFallbackResolver(); } diff --git a/src/java/org/apache/fop/apps/EnvironmentalProfileFactory.java b/src/java/org/apache/fop/apps/EnvironmentalProfileFactory.java index 9ba7632d2..922ecff4b 100644 --- a/src/java/org/apache/fop/apps/EnvironmentalProfileFactory.java +++ b/src/java/org/apache/fop/apps/EnvironmentalProfileFactory.java @@ -21,8 +21,12 @@ package org.apache.fop.apps; import java.net.URI; +import org.apache.xmlgraphics.image.loader.impl.AbstractImageSessionContext.FallbackResolver; +import org.apache.xmlgraphics.image.loader.impl.AbstractImageSessionContext.RestrictedFallbackResolver; +import org.apache.xmlgraphics.image.loader.impl.AbstractImageSessionContext.UnrestrictedFallbackResolver; +import org.apache.xmlgraphics.io.ResourceResolver; + import org.apache.fop.apps.io.InternalResourceResolver; -import org.apache.fop.apps.io.ResourceResolver; import org.apache.fop.apps.io.ResourceResolverFactory; import org.apache.fop.fonts.FontCacheManager; import org.apache.fop.fonts.FontCacheManagerFactory; @@ -51,7 +55,8 @@ public final class EnvironmentalProfileFactory { return new Profile(defaultBaseUri, resourceResolver, createFontManager(defaultBaseUri, resourceResolver, FontDetectorFactory.createDefault(), - FontCacheManagerFactory.createDefault())); + FontCacheManagerFactory.createDefault()), + new UnrestrictedFallbackResolver()); } /** @@ -67,7 +72,8 @@ public final class EnvironmentalProfileFactory { return new Profile(defaultBaseUri, resourceResolver, createFontManager(defaultBaseUri, resourceResolver, FontDetectorFactory.createDisabled(), - FontCacheManagerFactory.createDisabled())); + FontCacheManagerFactory.createDisabled()), + new RestrictedFallbackResolver()); } private static final class Profile implements EnvironmentProfile { @@ -78,8 +84,10 @@ public final class EnvironmentalProfileFactory { private final URI defaultBaseURI; + private final FallbackResolver fallbackResolver; + private Profile(URI defaultBaseURI, ResourceResolver resourceResolver, - FontManager fontManager) { + FontManager fontManager, FallbackResolver fallbackResolver) { if (defaultBaseURI == null) { throw new IllegalArgumentException("Default base URI must not be null"); } @@ -92,6 +100,7 @@ public final class EnvironmentalProfileFactory { this.defaultBaseURI = defaultBaseURI; this.resourceResolver = resourceResolver; this.fontManager = fontManager; + this.fallbackResolver = fallbackResolver; } public ResourceResolver getResourceResolver() { @@ -105,6 +114,10 @@ public final class EnvironmentalProfileFactory { public URI getDefaultBaseURI() { return defaultBaseURI; } + + public FallbackResolver getFallbackResolver() { + return fallbackResolver; + } } private static FontManager createFontManager(URI defaultBaseUri, ResourceResolver resourceResolver, diff --git a/src/java/org/apache/fop/apps/FOUserAgent.java b/src/java/org/apache/fop/apps/FOUserAgent.java index 0ed5b7228..68064343e 100644 --- a/src/java/org/apache/fop/apps/FOUserAgent.java +++ b/src/java/org/apache/fop/apps/FOUserAgent.java @@ -131,21 +131,7 @@ public class FOUserAgent { /** Set of keywords applicable to this document. */ protected String keywords = null; - private ImageSessionContext imageSessionContext = new AbstractImageSessionContext() { - - public ImageContext getParentContext() { - return factory; - } - - public float getTargetResolution() { - return FOUserAgent.this.getTargetResolution(); - } - - public Source resolveURI(String uri) { - return FOUserAgent.this.resolveURI(uri); - } - - }; + private final ImageSessionContext imageSessionContext; /** * Main constructor. This constructor should not be called directly. Please use the @@ -154,11 +140,25 @@ public class FOUserAgent { * @param resourceResolver the resolver used to acquire resources * @see org.apache.fop.apps.FopFactory */ - FOUserAgent(FopFactory factory, InternalResourceResolver resourceResolver) { + FOUserAgent(final FopFactory factory, InternalResourceResolver resourceResolver) { this.factory = factory; this.resourceResolver = resourceResolver; setTargetResolution(factory.getTargetResolution()); setAccessibility(factory.isAccessibilityEnabled()); + imageSessionContext = new AbstractImageSessionContext(factory.getFallbackResolver()) { + + public ImageContext getParentContext() { + return factory; + } + + public float getTargetResolution() { + return FOUserAgent.this.getTargetResolution(); + } + + public Source resolveURI(String uri) { + return FOUserAgent.this.resolveURI(uri); + } + }; } /** diff --git a/src/java/org/apache/fop/apps/FopConfParser.java b/src/java/org/apache/fop/apps/FopConfParser.java index c6e3dbd19..b4918ef30 100644 --- a/src/java/org/apache/fop/apps/FopConfParser.java +++ b/src/java/org/apache/fop/apps/FopConfParser.java @@ -39,9 +39,9 @@ import org.apache.commons.logging.LogFactory; import org.apache.xmlgraphics.image.loader.spi.ImageImplRegistry; import org.apache.xmlgraphics.image.loader.util.Penalty; +import org.apache.xmlgraphics.io.ResourceResolver; import org.apache.fop.apps.io.InternalResourceResolver; -import org.apache.fop.apps.io.ResourceResolver; import org.apache.fop.apps.io.ResourceResolverFactory; import org.apache.fop.fonts.FontManagerConfigurator; import org.apache.fop.hyphenation.HyphenationTreeCache; diff --git a/src/java/org/apache/fop/apps/FopFactory.java b/src/java/org/apache/fop/apps/FopFactory.java index 80c957f9f..6bc744b7e 100644 --- a/src/java/org/apache/fop/apps/FopFactory.java +++ b/src/java/org/apache/fop/apps/FopFactory.java @@ -36,6 +36,7 @@ import org.apache.commons.logging.LogFactory; import org.apache.xmlgraphics.image.loader.ImageContext; import org.apache.xmlgraphics.image.loader.ImageManager; +import org.apache.xmlgraphics.image.loader.impl.AbstractImageSessionContext.FallbackResolver; import org.apache.xmlgraphics.util.UnitConv; import org.apache.fop.apps.io.InternalResourceResolver; @@ -418,6 +419,11 @@ public final class FopFactory implements ImageContext { return config.getFontManager(); } + /** @see FopFactoryConfig#getFallbackResolver() */ + FallbackResolver getFallbackResolver() { + return config.getFallbackResolver(); + } + /** * Returns the color space cache for this instance. *

diff --git a/src/java/org/apache/fop/apps/FopFactoryBuilder.java b/src/java/org/apache/fop/apps/FopFactoryBuilder.java index cfc47496c..b1fd5e979 100644 --- a/src/java/org/apache/fop/apps/FopFactoryBuilder.java +++ b/src/java/org/apache/fop/apps/FopFactoryBuilder.java @@ -30,8 +30,9 @@ import org.apache.avalon.framework.configuration.Configuration; import org.apache.xmlgraphics.image.loader.ImageContext; import org.apache.xmlgraphics.image.loader.ImageManager; +import org.apache.xmlgraphics.image.loader.impl.AbstractImageSessionContext.FallbackResolver; +import org.apache.xmlgraphics.io.ResourceResolver; -import org.apache.fop.apps.io.ResourceResolver; import org.apache.fop.apps.io.ResourceResolverFactory; import org.apache.fop.fonts.FontManager; import org.apache.fop.layoutmgr.LayoutManagerMaker; @@ -464,6 +465,10 @@ public final class FopFactoryBuilder { public Map getHyphenationPatternNames() { return hyphPatNames; } + + public FallbackResolver getFallbackResolver() { + return enviro.getFallbackResolver(); + } } private interface FopFactoryConfigBuilder { diff --git a/src/java/org/apache/fop/apps/FopFactoryConfig.java b/src/java/org/apache/fop/apps/FopFactoryConfig.java index 60e8d98e7..d3ea3127f 100644 --- a/src/java/org/apache/fop/apps/FopFactoryConfig.java +++ b/src/java/org/apache/fop/apps/FopFactoryConfig.java @@ -26,8 +26,9 @@ import java.util.Set; import org.apache.avalon.framework.configuration.Configuration; import org.apache.xmlgraphics.image.loader.ImageManager; +import org.apache.xmlgraphics.image.loader.impl.AbstractImageSessionContext.FallbackResolver; +import org.apache.xmlgraphics.io.ResourceResolver; -import org.apache.fop.apps.io.ResourceResolver; import org.apache.fop.fonts.FontManager; import org.apache.fop.layoutmgr.LayoutManagerMaker; @@ -163,4 +164,11 @@ public interface FopFactoryConfig { /** @return the hyphenation pattern names */ Map getHyphenationPatternNames(); + + /** + * Controls the mechanisms that are used in the event that {@link javax.xml.transform.Source} + * used for resources couldn't be read. + * @return the fallback resolver + */ + FallbackResolver getFallbackResolver(); } diff --git a/src/java/org/apache/fop/apps/io/InternalResourceResolver.java b/src/java/org/apache/fop/apps/io/InternalResourceResolver.java index 4d664c8f5..af0a26478 100644 --- a/src/java/org/apache/fop/apps/io/InternalResourceResolver.java +++ b/src/java/org/apache/fop/apps/io/InternalResourceResolver.java @@ -29,12 +29,14 @@ import javax.xml.transform.Source; import javax.xml.transform.TransformerException; import javax.xml.transform.stream.StreamSource; +import org.apache.xmlgraphics.io.Resource; +import org.apache.xmlgraphics.io.ResourceResolver; import org.apache.xmlgraphics.util.uri.DataURIResolver; /** * This object holds the base URI from which to resolve URIs against as well as the resolver for * resource acquisition. It also does some URI sanitization of common URI syntactical errors. This - * class takes in a {@link org.apache.fop.apps.io.ResourceResolver} and delegates all relevant + * class takes in a {@link org.apache.xmlgraphics.io.ResourceResolver} and delegates all relevant * URIs to it. */ public class InternalResourceResolver { diff --git a/src/java/org/apache/fop/apps/io/Resource.java b/src/java/org/apache/fop/apps/io/Resource.java deleted file mode 100644 index 0a8b8c22a..000000000 --- a/src/java/org/apache/fop/apps/io/Resource.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * 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.apps.io; - -import java.io.FilterInputStream; -import java.io.InputStream; - -/** - * This class represents a resolved resource. The type property is used by FOP 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; - } - -} diff --git a/src/java/org/apache/fop/apps/io/ResourceResolver.java b/src/java/org/apache/fop/apps/io/ResourceResolver.java deleted file mode 100644 index a3a9cf0c3..000000000 --- a/src/java/org/apache/fop/apps/io/ResourceResolver.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * 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.apps.io; - -import java.io.IOException; -import java.io.OutputStream; -import java.net.URI; - -/** - * Implementations of this resource resolver allow FOP 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; - -} diff --git a/src/java/org/apache/fop/apps/io/ResourceResolverFactory.java b/src/java/org/apache/fop/apps/io/ResourceResolverFactory.java index 72eac456d..cdc9438b9 100644 --- a/src/java/org/apache/fop/apps/io/ResourceResolverFactory.java +++ b/src/java/org/apache/fop/apps/io/ResourceResolverFactory.java @@ -28,6 +28,11 @@ import java.util.Collections; import java.util.HashMap; import java.util.Map; +import org.apache.xmlgraphics.io.Resource; +import org.apache.xmlgraphics.io.ResourceResolver; +import org.apache.xmlgraphics.io.TempResourceResolver; +import org.apache.xmlgraphics.io.TempResourceURIGenerator; + /** * A factory class for {@link ResourceResolver}s. */ @@ -70,10 +75,10 @@ public final class ResourceResolverFactory { } /** - * Creates a temporary-resource-schema aware resource resolver. Temporary resource URIs are + * Creates a temporary-resource-scheme aware resource resolver. Temporary resource URIs are * created by {@link TempResourceURIGenerator}. * - * @param tempResourceResolver the temporary-resource-schema resolver to use + * @param tempResourceResolver the temporary-resource-scheme resolver to use * @param defaultResourceResolver the default resource resolver to use * @return the ressource resolver */ @@ -84,17 +89,17 @@ public final class ResourceResolverFactory { } /** - * This creates the builder class for binding URI schemas to implementations of - * {@link ResourceResolver}. This allows users to define their own URI schemas such that they + * This creates the builder class for binding URI schemes to implementations of + * {@link ResourceResolver}. This allows users to define their own URI schemes such that they * have finer control over the acquisition of resources. * * @param defaultResolver the default resource resolver that should be used in the event that - * none of the other registered resolvers match the schema - * @return the schema aware {@link ResourceResolver} builder + * none of the other registered resolvers match the scheme + * @return the scheme aware {@link ResourceResolver} builder */ - public static SchemaAwareResourceResolverBuilder createSchemaAwareResourceResolverBuilder( + public static SchemeAwareResourceResolverBuilder createSchemeAwareResourceResolverBuilder( ResourceResolver defaultResolver) { - return new SchemaAwareResourceResolverBuilderImpl(defaultResolver); + return new SchemeAwareResourceResolverBuilderImpl(defaultResolver); } private static final class DefaultResourceResolver implements ResourceResolver { @@ -132,13 +137,13 @@ public final class ResourceResolverFactory { this.defaultResourceResolver = defaultResourceResolver; } - private static boolean isTempUri(URI uri) { - return TempResourceURIGenerator.isTempUri(uri); + private static boolean isTempURI(URI uri) { + return TempResourceURIGenerator.isTempURI(uri); } /** {@inheritDoc} */ public Resource getResource(URI uri) throws IOException { - if (isTempUri(uri)) { + if (isTempURI(uri)) { return tempResourceResolver.getResource(uri.getPath()); } else { return defaultResourceResolver.getResource(uri); @@ -147,7 +152,7 @@ public final class ResourceResolverFactory { /** {@inheritDoc} */ public OutputStream getOutputStream(URI uri) throws IOException { - if (isTempUri(uri)) { + if (isTempURI(uri)) { return tempResourceResolver.getOutputStream(uri.getPath()); } else { return defaultResourceResolver.getOutputStream(uri); @@ -188,23 +193,23 @@ public final class ResourceResolverFactory { } } - private static final class SchemaAwareResourceResolver implements ResourceResolver { + private static final class SchemeAwareResourceResolver implements ResourceResolver { - private final Map schemaHandlingResourceResolvers; + private final Map schemeHandlingResourceResolvers; private final ResourceResolver defaultResolver; - private SchemaAwareResourceResolver( - Map schemaHandlingResourceResolvers, + private SchemeAwareResourceResolver( + Map schemEHandlingResourceResolvers, ResourceResolver defaultResolver) { - this.schemaHandlingResourceResolvers = schemaHandlingResourceResolvers; + this.schemeHandlingResourceResolvers = schemEHandlingResourceResolvers; this.defaultResolver = defaultResolver; } - private ResourceResolver getResourceResolverForSchema(URI uri) { - String schema = uri.getScheme(); - if (schemaHandlingResourceResolvers.containsKey(schema)) { - return schemaHandlingResourceResolvers.get(schema); + private ResourceResolver getResourceResolverForScheme(URI uri) { + String scheme = uri.getScheme(); + if (schemeHandlingResourceResolvers.containsKey(scheme)) { + return schemeHandlingResourceResolvers.get(scheme); } else { return defaultResolver; } @@ -212,58 +217,58 @@ public final class ResourceResolverFactory { /** {@inheritDoc} */ public Resource getResource(URI uri) throws IOException { - return getResourceResolverForSchema(uri).getResource(uri); + return getResourceResolverForScheme(uri).getResource(uri); } /** {@inheritDoc} */ public OutputStream getOutputStream(URI uri) throws IOException { - return getResourceResolverForSchema(uri).getOutputStream(uri); + return getResourceResolverForScheme(uri).getOutputStream(uri); } } /** * Implementations of this interface will be builders for {@link ResourceResolver}, they bind - * URI schemas to their respective resolver. This gives users more control over the mechanisms + * URI schemes to their respective resolver. This gives users more control over the mechanisms * by which URIs are resolved. *

* Here is an example of how this could be used: *

*

- * SchemaAwareResourceResolverBuilder builder - * = ResourceResolverFactory.createSchemaAwareResourceResolverBuilder(defaultResolver); - * builder.registerResourceResolverForSchema("test", testResolver); - * builder.registerResourceResolverForSchema("anotherTest", test2Resolver); + * SchemeAwareResourceResolverBuilder builder + * = ResourceResolverFactory.createSchemeAwareResourceResolverBuilder(defaultResolver); + * builder.registerResourceResolverForScheme("test", testResolver); + * builder.registerResourceResolverForScheme("anotherTest", test2Resolver); * ResourceResolver resolver = builder.build(); *

* This will result in all URIs for the form "test:///..." will be resolved using the * testResolver object; URIs of the form "anotherTest:///..." will be resolved * using test2Resolver; all other URIs will be resolved from the defaultResolver. */ - public interface SchemaAwareResourceResolverBuilder { + public interface SchemeAwareResourceResolverBuilder { /** - * Register a schema with its respective {@link ResourceResolver}. This resolver will be - * used as the only resolver for the specified schema. + * Register a scheme with its respective {@link ResourceResolver}. This resolver will be + * used as the only resolver for the specified scheme. * - * @param schema the schema to be used with the given resolver + * @param scheme the scheme to be used with the given resolver * @param resourceResolver the resource resolver */ - void registerResourceResolverForSchema(String schema, ResourceResolver resourceResolver); + void registerResourceResolverForScheme(String scheme, ResourceResolver resourceResolver); /** * Builds a {@link ResourceResolver} that will delegate to the respective resource resolver - * when a registered URI schema is given + * when a registered URI scheme is given * - * @return a resolver that delegates to the appropriate schema resolver + * @return a resolver that delegates to the appropriate scheme resolver */ ResourceResolver build(); } - private static final class CompletedSchemaAwareResourceResolverBuilder - implements SchemaAwareResourceResolverBuilder { + private static final class CompletedSchemeAwareResourceResolverBuilder + implements SchemeAwareResourceResolverBuilder { - private static final SchemaAwareResourceResolverBuilder INSTANCE - = new CompletedSchemaAwareResourceResolverBuilder(); + private static final SchemeAwareResourceResolverBuilder INSTANCE + = new CompletedSchemeAwareResourceResolverBuilder(); /** {@inheritDoc} */ public ResourceResolver build() { @@ -271,59 +276,58 @@ public final class ResourceResolverFactory { } /** {@inheritDoc} */ - public void registerResourceResolverForSchema(String schema, + public void registerResourceResolverForScheme(String scheme, ResourceResolver resourceResolver) { throw new IllegalStateException("Resource resolver already built"); } } - private static final class ActiveSchemaAwareResourceResolverBuilder - implements SchemaAwareResourceResolverBuilder { + private static final class ActiveSchemeAwareResourceResolverBuilder + implements SchemeAwareResourceResolverBuilder { - private final Map schemaHandlingResourceResolvers + private final Map schemeHandlingResourceResolvers = new HashMap(); private final ResourceResolver defaultResolver; - private ActiveSchemaAwareResourceResolverBuilder(ResourceResolver defaultResolver) { + private ActiveSchemeAwareResourceResolverBuilder(ResourceResolver defaultResolver) { this.defaultResolver = defaultResolver; } /** {@inheritDoc} */ - public void registerResourceResolverForSchema(String schema, + public void registerResourceResolverForScheme(String scheme, ResourceResolver resourceResolver) { - schemaHandlingResourceResolvers.put(schema, resourceResolver); + schemeHandlingResourceResolvers.put(scheme, resourceResolver); } /** {@inheritDoc} */ public ResourceResolver build() { - return new SchemaAwareResourceResolver( - Collections.unmodifiableMap(schemaHandlingResourceResolvers), defaultResolver); + return new SchemeAwareResourceResolver( + Collections.unmodifiableMap(schemeHandlingResourceResolvers), defaultResolver); } } - private static final class SchemaAwareResourceResolverBuilderImpl - implements SchemaAwareResourceResolverBuilder { + private static final class SchemeAwareResourceResolverBuilderImpl + implements SchemeAwareResourceResolverBuilder { - private SchemaAwareResourceResolverBuilder delegate; + private SchemeAwareResourceResolverBuilder delegate; - private SchemaAwareResourceResolverBuilderImpl(ResourceResolver defaultResolver) { - this.delegate = new ActiveSchemaAwareResourceResolverBuilder(defaultResolver); + private SchemeAwareResourceResolverBuilderImpl(ResourceResolver defaultResolver) { + this.delegate = new ActiveSchemeAwareResourceResolverBuilder(defaultResolver); } /** {@inheritDoc} */ - public void registerResourceResolverForSchema(String schema, + public void registerResourceResolverForScheme(String scheme, ResourceResolver resourceResolver) { - delegate.registerResourceResolverForSchema(schema, resourceResolver); + delegate.registerResourceResolverForScheme(scheme, resourceResolver); } /** {@inheritDoc} */ public ResourceResolver build() { ResourceResolver resourceResolver = delegate.build(); - delegate = CompletedSchemaAwareResourceResolverBuilder.INSTANCE; + delegate = CompletedSchemeAwareResourceResolverBuilder.INSTANCE; return resourceResolver; } } - } diff --git a/src/java/org/apache/fop/apps/io/TempResourceResolver.java b/src/java/org/apache/fop/apps/io/TempResourceResolver.java deleted file mode 100644 index cf307fab6..000000000 --- a/src/java/org/apache/fop/apps/io/TempResourceResolver.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * 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.apps.io; - -import java.io.IOException; -import java.io.OutputStream; - -/** - * Implementations of this interface resolve URIs for temporary files used by FOP. The temporary- - * resource URI scheme comes from {@link TempResourceURIGenerator#TMP_SCHEMA}. - */ -public interface TempResourceResolver { - - /** - * Get a temporary-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(String uri) throws IOException; - - /** - * Gets an temporary-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(String uri) throws IOException; -} diff --git a/src/java/org/apache/fop/apps/io/TempResourceURIGenerator.java b/src/java/org/apache/fop/apps/io/TempResourceURIGenerator.java deleted file mode 100644 index 8e6c3606c..000000000 --- a/src/java/org/apache/fop/apps/io/TempResourceURIGenerator.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * 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.fop.apps.io; - -import java.net.URI; -import java.util.concurrent.atomic.AtomicLong; - -/** - * Creates a URI for any temporary resource used within FOP. - */ -public final class TempResourceURIGenerator { - - public static final String TMP_SCHEMA = "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_SCHEMA + ":///" + 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_SCHEMA.equals(uri.getScheme()); - } -} diff --git a/src/java/org/apache/fop/area/CachedRenderPagesModel.java b/src/java/org/apache/fop/area/CachedRenderPagesModel.java index fcc82cdd3..0837e5ae5 100644 --- a/src/java/org/apache/fop/area/CachedRenderPagesModel.java +++ b/src/java/org/apache/fop/area/CachedRenderPagesModel.java @@ -35,9 +35,10 @@ import org.xml.sax.SAXException; import org.apache.commons.io.IOUtils; +import org.apache.xmlgraphics.io.TempResourceURIGenerator; + import org.apache.fop.apps.FOPException; import org.apache.fop.apps.FOUserAgent; -import org.apache.fop.apps.io.TempResourceURIGenerator; import org.apache.fop.fonts.FontInfo; /** diff --git a/src/java/org/apache/fop/fonts/FontManagerConfigurator.java b/src/java/org/apache/fop/fonts/FontManagerConfigurator.java index cf9296826..fc2ce06a6 100644 --- a/src/java/org/apache/fop/fonts/FontManagerConfigurator.java +++ b/src/java/org/apache/fop/fonts/FontManagerConfigurator.java @@ -29,9 +29,10 @@ import org.apache.avalon.framework.configuration.ConfigurationException; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.apache.xmlgraphics.io.ResourceResolver; + import org.apache.fop.apps.FOPException; import org.apache.fop.apps.io.InternalResourceResolver; -import org.apache.fop.apps.io.ResourceResolver; import org.apache.fop.apps.io.ResourceResolverFactory; import org.apache.fop.fonts.substitute.FontSubstitutions; import org.apache.fop.fonts.substitute.FontSubstitutionsConfigurator; diff --git a/src/java/org/apache/fop/image/loader/batik/PreloaderSVG.java b/src/java/org/apache/fop/image/loader/batik/PreloaderSVG.java index 022ff462f..3aa340a4a 100644 --- a/src/java/org/apache/fop/image/loader/batik/PreloaderSVG.java +++ b/src/java/org/apache/fop/image/loader/batik/PreloaderSVG.java @@ -45,6 +45,7 @@ import org.apache.xmlgraphics.image.loader.ImageSize; import org.apache.xmlgraphics.image.loader.impl.AbstractImagePreloader; import org.apache.xmlgraphics.image.loader.impl.ImageXMLDOM; import org.apache.xmlgraphics.image.loader.util.ImageUtil; +import org.apache.xmlgraphics.io.XmlSourceUtil; import org.apache.xmlgraphics.util.MimeConstants; import org.apache.xmlgraphics.util.UnitConv; @@ -79,7 +80,7 @@ public class PreloaderSVG extends AbstractImagePreloader { } } if (info != null) { - ImageUtil.closeQuietly(src); //Image is fully read + XmlSourceUtil.closeQuietly(src); //Image is fully read } return info; } @@ -119,7 +120,7 @@ public class PreloaderSVG extends AbstractImagePreloader { DOMSource domSrc = (DOMSource)src; doc = (SVGDocument)domSrc.getNode(); } else { - in = new UnclosableInputStream(ImageUtil.needInputStream(src)); + in = new UnclosableInputStream(XmlSourceUtil.needInputStream(src)); int length = in.available(); in.mark(length + 1); SAXSVGDocumentFactory factory = new SAXSVGDocumentFactory( diff --git a/src/java/org/apache/fop/image/loader/batik/PreloaderWMF.java b/src/java/org/apache/fop/image/loader/batik/PreloaderWMF.java index 3bef0f41d..43341cbe6 100644 --- a/src/java/org/apache/fop/image/loader/batik/PreloaderWMF.java +++ b/src/java/org/apache/fop/image/loader/batik/PreloaderWMF.java @@ -38,6 +38,7 @@ import org.apache.xmlgraphics.image.loader.ImageInfo; import org.apache.xmlgraphics.image.loader.ImageSize; import org.apache.xmlgraphics.image.loader.impl.AbstractImagePreloader; import org.apache.xmlgraphics.image.loader.util.ImageUtil; +import org.apache.xmlgraphics.io.XmlSourceUtil; import org.apache.fop.util.UnclosableInputStream; @@ -69,7 +70,7 @@ public class PreloaderWMF extends AbstractImagePreloader { } } if (info != null) { - ImageUtil.closeQuietly(src); //Image is fully read + XmlSourceUtil.closeQuietly(src); //Image is fully read } return info; } @@ -88,7 +89,7 @@ public class PreloaderWMF extends AbstractImagePreloader { ImageContext context) { // parse document and get the size attributes of the svg element - InputStream in = new UnclosableInputStream(ImageUtil.needInputStream(src)); + InputStream in = new UnclosableInputStream(XmlSourceUtil.needInputStream(src)); try { in.mark(4 + 1); diff --git a/src/java/org/apache/fop/render/ps/PSDocumentHandler.java b/src/java/org/apache/fop/render/ps/PSDocumentHandler.java index 0ec5fdda6..897ed8fb8 100644 --- a/src/java/org/apache/fop/render/ps/PSDocumentHandler.java +++ b/src/java/org/apache/fop/render/ps/PSDocumentHandler.java @@ -39,6 +39,7 @@ import org.apache.commons.io.IOUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.apache.xmlgraphics.io.TempResourceURIGenerator; import org.apache.xmlgraphics.java2d.Dimension2DDouble; import org.apache.xmlgraphics.ps.DSCConstants; import org.apache.xmlgraphics.ps.PSDictionary; @@ -53,7 +54,6 @@ import org.apache.xmlgraphics.ps.dsc.events.DSCCommentBoundingBox; import org.apache.xmlgraphics.ps.dsc.events.DSCCommentHiResBoundingBox; import org.apache.fop.apps.MimeConstants; -import org.apache.fop.apps.io.TempResourceURIGenerator; import org.apache.fop.render.intermediate.AbstractBinaryWritingIFDocumentHandler; import org.apache.fop.render.intermediate.IFContext; import org.apache.fop.render.intermediate.IFDocumentHandlerConfigurator; diff --git a/src/java/org/apache/fop/servlet/FopServlet.java b/src/java/org/apache/fop/servlet/FopServlet.java index 9dcf10a2b..c65b6af3b 100644 --- a/src/java/org/apache/fop/servlet/FopServlet.java +++ b/src/java/org/apache/fop/servlet/FopServlet.java @@ -41,14 +41,15 @@ import javax.xml.transform.stream.StreamSource; import org.apache.commons.io.output.ByteArrayOutputStream; +import org.apache.xmlgraphics.io.Resource; +import org.apache.xmlgraphics.io.ResourceResolver; + import org.apache.fop.apps.FOPException; import org.apache.fop.apps.FOUserAgent; import org.apache.fop.apps.Fop; import org.apache.fop.apps.FopFactory; import org.apache.fop.apps.FopFactoryBuilder; import org.apache.fop.apps.MimeConstants; -import org.apache.fop.apps.io.Resource; -import org.apache.fop.apps.io.ResourceResolver; /** * Example servlet to generate a PDF from a servlet. diff --git a/test/java/org/apache/fop/URIResolutionTestCase.java b/test/java/org/apache/fop/URIResolutionTestCase.java index f1dd89725..e1015306e 100644 --- a/test/java/org/apache/fop/URIResolutionTestCase.java +++ b/test/java/org/apache/fop/URIResolutionTestCase.java @@ -46,14 +46,15 @@ import org.apache.commons.io.output.ByteArrayOutputStream; import org.apache.xpath.XPathAPI; import org.apache.xpath.objects.XObject; +import org.apache.xmlgraphics.io.Resource; +import org.apache.xmlgraphics.io.ResourceResolver; + import org.apache.fop.apps.FOPException; import org.apache.fop.apps.FOUserAgent; import org.apache.fop.apps.Fop; import org.apache.fop.apps.FopFactory; import org.apache.fop.apps.FopFactoryBuilder; import org.apache.fop.apps.MimeConstants; -import org.apache.fop.apps.io.Resource; -import org.apache.fop.apps.io.ResourceResolver; import org.apache.fop.apps.io.ResourceResolverFactory; import org.apache.fop.render.xml.XMLRenderer; diff --git a/test/java/org/apache/fop/apps/EnvironmentalProfileFactoryTestCase.java b/test/java/org/apache/fop/apps/EnvironmentalProfileFactoryTestCase.java index 7b033969b..e94403e71 100644 --- a/test/java/org/apache/fop/apps/EnvironmentalProfileFactoryTestCase.java +++ b/test/java/org/apache/fop/apps/EnvironmentalProfileFactoryTestCase.java @@ -21,11 +21,12 @@ import java.net.URI; import org.junit.Test; -import org.apache.fop.apps.io.ResourceResolver; import static org.junit.Assert.assertEquals; import static org.mockito.Mockito.mock; +import org.apache.xmlgraphics.io.ResourceResolver; + public class EnvironmentalProfileFactoryTestCase { private final URI testURI = URI.create("this.is.purely.for.test.purposes"); diff --git a/test/java/org/apache/fop/apps/MutableConfig.java b/test/java/org/apache/fop/apps/MutableConfig.java index 79f038f24..3ff3c10fa 100644 --- a/test/java/org/apache/fop/apps/MutableConfig.java +++ b/test/java/org/apache/fop/apps/MutableConfig.java @@ -24,8 +24,9 @@ import java.util.Set; import org.apache.avalon.framework.configuration.Configuration; import org.apache.xmlgraphics.image.loader.ImageManager; +import org.apache.xmlgraphics.image.loader.impl.AbstractImageSessionContext.FallbackResolver; +import org.apache.xmlgraphics.io.ResourceResolver; -import org.apache.fop.apps.io.ResourceResolver; import org.apache.fop.fonts.FontManager; import org.apache.fop.layoutmgr.LayoutManagerMaker; @@ -130,4 +131,8 @@ public final class MutableConfig implements FopFactoryConfig { public Map getHyphenationPatternNames() { return delegate.getHyphenationPatternNames(); } + + public FallbackResolver getFallbackResolver() { + return delegate.getFallbackResolver(); + } } diff --git a/test/java/org/apache/fop/apps/io/BaseURIResolutionTest.java b/test/java/org/apache/fop/apps/io/BaseURIResolutionTest.java index ee337c39d..2131407c1 100644 --- a/test/java/org/apache/fop/apps/io/BaseURIResolutionTest.java +++ b/test/java/org/apache/fop/apps/io/BaseURIResolutionTest.java @@ -37,6 +37,8 @@ import org.xml.sax.SAXException; import org.apache.commons.io.IOUtils; import org.apache.commons.io.output.ByteArrayOutputStream; +import org.apache.xmlgraphics.io.ResourceResolver; + import org.apache.fop.apps.FOPException; import org.apache.fop.apps.FOUserAgent; import org.apache.fop.apps.Fop; diff --git a/test/java/org/apache/fop/apps/io/FontURIResolver.java b/test/java/org/apache/fop/apps/io/FontURIResolver.java index d6f836783..40d6c7402 100644 --- a/test/java/org/apache/fop/apps/io/FontURIResolver.java +++ b/test/java/org/apache/fop/apps/io/FontURIResolver.java @@ -39,6 +39,8 @@ import org.apache.fop.apps.PDFRendererConfBuilder; import static org.junit.Assert.assertTrue; +import org.apache.xmlgraphics.io.Resource; + public class FontURIResolver extends BaseURIResolutionTest { public enum Event { diff --git a/test/java/org/apache/fop/apps/io/ResourceResolverFactoryTestCase.java b/test/java/org/apache/fop/apps/io/ResourceResolverFactoryTestCase.java index b6b2eea00..6ae34b4f9 100644 --- a/test/java/org/apache/fop/apps/io/ResourceResolverFactoryTestCase.java +++ b/test/java/org/apache/fop/apps/io/ResourceResolverFactoryTestCase.java @@ -36,6 +36,10 @@ import static org.mockito.Mockito.never; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; +import org.apache.xmlgraphics.io.Resource; +import org.apache.xmlgraphics.io.ResourceResolver; +import org.apache.xmlgraphics.io.TempResourceResolver; + public class ResourceResolverFactoryTestCase { private static final byte[] DATA = new byte[]{(byte) 0, (byte) 1, (byte) 2}; @@ -193,10 +197,10 @@ public class ResourceResolverFactoryTestCase { final ResourceResolver sut; TestCreateSchemaAwareResourceResolverBuilderHelper() { - ResourceResolverFactory.SchemaAwareResourceResolverBuilder builder - = ResourceResolverFactory.createSchemaAwareResourceResolverBuilder( + ResourceResolverFactory.SchemeAwareResourceResolverBuilder builder + = ResourceResolverFactory.createSchemeAwareResourceResolverBuilder( defaultResourceResolver); - builder.registerResourceResolverForSchema(SCHEMA, registedResourceResolver); + builder.registerResourceResolverForScheme(SCHEMA, registedResourceResolver); sut = builder.build(); } diff --git a/test/java/org/apache/fop/apps/io/TestingResourceResolver.java b/test/java/org/apache/fop/apps/io/TestingResourceResolver.java index 532353337..f1cd06138 100644 --- a/test/java/org/apache/fop/apps/io/TestingResourceResolver.java +++ b/test/java/org/apache/fop/apps/io/TestingResourceResolver.java @@ -24,6 +24,8 @@ import java.util.Collections; import java.util.HashMap; import java.util.Map; +import org.apache.xmlgraphics.io.ResourceResolver; + abstract class TestingResourceResolver implements ResourceResolver { private final Map checker; diff --git a/test/java/org/apache/fop/apps/io/URIResolverWrapperTestCase.java b/test/java/org/apache/fop/apps/io/URIResolverWrapperTestCase.java index 440b16abb..b2044e3b9 100644 --- a/test/java/org/apache/fop/apps/io/URIResolverWrapperTestCase.java +++ b/test/java/org/apache/fop/apps/io/URIResolverWrapperTestCase.java @@ -35,6 +35,8 @@ import static org.mockito.Matchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; +import org.apache.xmlgraphics.io.ResourceResolver; + public class URIResolverWrapperTestCase { private static final List BASE_URIS = Collections.unmodifiableList(Arrays.asList( diff --git a/test/java/org/apache/fop/intermediate/TestAssistant.java b/test/java/org/apache/fop/intermediate/TestAssistant.java index 2be41dca1..2b138de55 100644 --- a/test/java/org/apache/fop/intermediate/TestAssistant.java +++ b/test/java/org/apache/fop/intermediate/TestAssistant.java @@ -42,8 +42,11 @@ import org.w3c.dom.Element; import org.apache.xpath.XPathAPI; import org.apache.xpath.objects.XObject; +import org.apache.fop.apps.EnvironmentProfile; +import org.apache.fop.apps.EnvironmentalProfileFactory; import org.apache.fop.apps.FopFactory; import org.apache.fop.apps.FopFactoryBuilder; +import org.apache.fop.apps.io.ResourceResolverFactory; /** * Helper class for running FOP tests. @@ -115,7 +118,10 @@ public class TestAssistant { public FopFactory getFopFactory(Document testDoc) { boolean base14KerningEnabled = isBase14KerningEnabled(testDoc); boolean strictValidation = isStrictValidation(testDoc); - FopFactoryBuilder builder = new FopFactoryBuilder(testDir.getParentFile().toURI()); + EnvironmentProfile envProfile = EnvironmentalProfileFactory.createRestrictedIO( + testDir.getParentFile().toURI(), + ResourceResolverFactory.createDefaultResourceResolver()); + FopFactoryBuilder builder = new FopFactoryBuilder(envProfile); builder.setStrictFOValidation(strictValidation); builder.getFontManager().setBase14KerningEnabled(base14KerningEnabled); return builder.build(); diff --git a/test/java/org/apache/fop/pdf/PDFFactoryTestCase.java b/test/java/org/apache/fop/pdf/PDFFactoryTestCase.java index 971471fa2..5e4b9e2e4 100644 --- a/test/java/org/apache/fop/pdf/PDFFactoryTestCase.java +++ b/test/java/org/apache/fop/pdf/PDFFactoryTestCase.java @@ -26,8 +26,9 @@ import org.junit.Test; import static org.junit.Assert.assertEquals; +import org.apache.xmlgraphics.io.ResourceResolver; + import org.apache.fop.apps.io.InternalResourceResolver; -import org.apache.fop.apps.io.ResourceResolver; import org.apache.fop.apps.io.ResourceResolverFactory; import org.apache.fop.fonts.CIDSet; import org.apache.fop.fonts.CIDSubset;