aboutsummaryrefslogtreecommitdiffstats
path: root/src/java/org/apache/fop/afp/util
diff options
context:
space:
mode:
authorJeremias Maerki <jeremias@apache.org>2009-03-10 15:10:07 +0000
committerJeremias Maerki <jeremias@apache.org>2009-03-10 15:10:07 +0000
commit1914db175fda0d8776f686cf8409ddf4a5d6e5e6 (patch)
tree2dbc80310c8bc6317339c4c0904f1f41e7b32a47 /src/java/org/apache/fop/afp/util
parentc219edf458b679f2e621cb70bdfe9d2b5b5f6f74 (diff)
downloadxmlgraphics-fop-1914db175fda0d8776f686cf8409ddf4a5d6e5e6.tar.gz
xmlgraphics-fop-1914db175fda0d8776f686cf8409ddf4a5d6e5e6.zip
Added full URI resolution support for AFP fonts.
git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@752133 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/java/org/apache/fop/afp/util')
-rw-r--r--src/java/org/apache/fop/afp/util/DefaultFOPResourceAccessor.java42
-rw-r--r--src/java/org/apache/fop/afp/util/SimpleResourceAccessor.java34
2 files changed, 60 insertions, 16 deletions
diff --git a/src/java/org/apache/fop/afp/util/DefaultFOPResourceAccessor.java b/src/java/org/apache/fop/afp/util/DefaultFOPResourceAccessor.java
index 55c8eab7d..97646542b 100644
--- a/src/java/org/apache/fop/afp/util/DefaultFOPResourceAccessor.java
+++ b/src/java/org/apache/fop/afp/util/DefaultFOPResourceAccessor.java
@@ -19,42 +19,68 @@
package org.apache.fop.afp.util;
+import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
+import java.net.URL;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
+import org.apache.commons.io.IOUtils;
+
import org.apache.fop.apps.FOUserAgent;
+import org.apache.fop.apps.FopFactory;
+import org.apache.fop.fonts.FontManager;
/**
* Default implementation of the {@link ResourceAccessor} interface for use inside FOP.
*/
-public class DefaultFOPResourceAccessor implements ResourceAccessor {
+public class DefaultFOPResourceAccessor extends SimpleResourceAccessor {
private FOUserAgent userAgent;
+ private String categoryBaseURI;
/**
- * Main constructor.
+ * Constructor for resource to be accessed via the {@link FOUserAgent}. This contructor
+ * can take two base URIs: the category base URI is the one to use when differentiating between
+ * normal resources (ex. images) and font resources. So, if fonts need to be accessed, you can
+ * set the {@link FontManager}'s base URI instead of the one on the {@link FopFactory}.
* @param userAgent the FO user agent
+ * @param categoryBaseURI the category base URI (may be null)
+ * @param baseURI the custom base URI to resolve relative URIs against (may be null)
*/
- public DefaultFOPResourceAccessor(FOUserAgent userAgent) {
+ public DefaultFOPResourceAccessor(FOUserAgent userAgent, String categoryBaseURI, URI baseURI) {
+ super(baseURI);
this.userAgent = userAgent;
+ this.categoryBaseURI = categoryBaseURI;
}
/** {@inheritDoc} */
public InputStream createInputStream(URI uri) throws IOException {
- Source src = userAgent.resolveURI(uri.toASCIIString());
+ //Step 1: resolve against local base URI --> URI
+ URI resolved = resolveAgainstBase(uri);
+
+ //Step 2: resolve against the user agent --> stream
+ Source src;
+ src = userAgent.resolveURI(resolved.toASCIIString(), this.categoryBaseURI);
+
if (src == null) {
- return null;
+ throw new FileNotFoundException("Resource not found: " + uri.toASCIIString());
} else if (src instanceof StreamSource) {
StreamSource ss = (StreamSource)src;
InputStream in = ss.getInputStream();
- return in;
- } else {
- return null;
+ if (in != null) {
+ return in;
+ }
+ if (ss.getReader() != null) {
+ //Don't support reader, retry using system ID below
+ IOUtils.closeQuietly(ss.getReader());
+ }
}
+ URL url = new URL(src.getSystemId());
+ return url.openStream();
}
}
diff --git a/src/java/org/apache/fop/afp/util/SimpleResourceAccessor.java b/src/java/org/apache/fop/afp/util/SimpleResourceAccessor.java
index 51772c253..7a963928b 100644
--- a/src/java/org/apache/fop/afp/util/SimpleResourceAccessor.java
+++ b/src/java/org/apache/fop/afp/util/SimpleResourceAccessor.java
@@ -26,7 +26,8 @@ import java.net.URI;
import java.net.URL;
/**
- * Simple implementation of the {@link ResourceAccessor} interface for access via files.
+ * Simple implementation of the {@link ResourceAccessor} interface for access relative to a
+ * base URI.
*/
public class SimpleResourceAccessor implements ResourceAccessor {
@@ -34,23 +35,40 @@ public class SimpleResourceAccessor implements ResourceAccessor {
/**
* Creates a new simple resource accessor.
- * @param basePath the base path to resolve relative URIs to
+ * @param baseURI the base URI to resolve relative URIs against (may be null)
*/
- public SimpleResourceAccessor(File basePath) {
- this.baseURI = basePath.toURI();
+ public SimpleResourceAccessor(URI baseURI) {
+ this.baseURI = baseURI;
}
/**
* Creates a new simple resource accessor.
- * @param basePath the base path to resolve relative URIs to
+ * @param baseDir the base directory to resolve relative filenames against (may be null)
*/
- public SimpleResourceAccessor(String basePath) {
- this(new File(basePath));
+ public SimpleResourceAccessor(File baseDir) {
+ this(baseDir != null ? baseDir.toURI() : null);
+ }
+
+ /**
+ * Returns the base URI.
+ * @return the base URI (or null if no base URI was set)
+ */
+ public URI getBaseURI() {
+ return this.baseURI;
+ }
+
+ /**
+ * Resolve the given URI against the baseURI.
+ * @param uri the URI to resolve
+ * @return the resolved URI
+ */
+ protected URI resolveAgainstBase(URI uri) {
+ return (getBaseURI() != null ? getBaseURI().resolve(uri) : uri);
}
/** {@inheritDoc} */
public InputStream createInputStream(URI uri) throws IOException {
- URI resolved = this.baseURI.resolve(uri);
+ URI resolved = resolveAgainstBase(uri);
URL url = resolved.toURL();
return url.openStream();
}