aboutsummaryrefslogtreecommitdiffstats
path: root/src/java/org
diff options
context:
space:
mode:
authorJeremias Maerki <jeremias@apache.org>2006-02-22 16:35:14 +0000
committerJeremias Maerki <jeremias@apache.org>2006-02-22 16:35:14 +0000
commitfdda3a8d464b292c5cdbd2f19de3f2c49d7cebe3 (patch)
treef64f16b5584b5c5e7976fead5fa3e5e3b34910c2 /src/java/org
parent6b07665badb08b36d61bede31f5d6b4ffc264bbf (diff)
downloadxmlgraphics-fop-fdda3a8d464b292c5cdbd2f19de3f2c49d7cebe3.tar.gz
xmlgraphics-fop-fdda3a8d464b292c5cdbd2f19de3f2c49d7cebe3.zip
Added a convenience URIResolver that uses the ServletContext in a servlet to access resources packed into a web application.
The URIResolver listens to the "servlet-context:" protocol. Integrated the new URIResolver into out two servlets at all levels (TransformerFactory, Transformer and FOP). git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@379810 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/java/org')
-rw-r--r--src/java/org/apache/fop/servlet/FopPrintServlet.java20
-rw-r--r--src/java/org/apache/fop/servlet/FopServlet.java19
-rw-r--r--src/java/org/apache/fop/servlet/ServletContextURIResolver.java84
3 files changed, 119 insertions, 4 deletions
diff --git a/src/java/org/apache/fop/servlet/FopPrintServlet.java b/src/java/org/apache/fop/servlet/FopPrintServlet.java
index 3b4f92f60..8ac980ccd 100644
--- a/src/java/org/apache/fop/servlet/FopPrintServlet.java
+++ b/src/java/org/apache/fop/servlet/FopPrintServlet.java
@@ -31,11 +31,13 @@ import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
+import javax.xml.transform.URIResolver;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.stream.StreamSource;
// XML
import org.apache.commons.logging.impl.SimpleLog;
+import org.apache.fop.apps.FOUserAgent;
import org.apache.fop.apps.Fop;
import org.apache.fop.apps.MimeConstants;
@@ -77,6 +79,8 @@ public class FopPrintServlet extends HttpServlet {
/** The TransformerFactory to use to create Transformer instances */
protected TransformerFactory transFactory = null;
+ /** URIResolver for use by this servlet */
+ protected URIResolver uriResolver;
/**
* @see javax.servlet.GenericServlet#init()
@@ -84,7 +88,9 @@ public class FopPrintServlet extends HttpServlet {
public void init() throws ServletException {
this.log = new SimpleLog("FOP/Print Servlet");
log.setLevel(SimpleLog.LOG_LEVEL_WARN);
+ this.uriResolver = new ServletContextURIResolver(getServletContext());
this.transFactory = TransformerFactory.newInstance();
+ this.transFactory.setURIResolver(this.uriResolver);
}
/**
@@ -132,11 +138,12 @@ public class FopPrintServlet extends HttpServlet {
public void renderFO(InputStream foFile,
HttpServletResponse response) throws ServletException {
try {
- Fop fop = new Fop(MimeConstants.MIME_FOP_PRINT);
+ Fop fop = new Fop(MimeConstants.MIME_FOP_PRINT, getFOUserAgent());
// Setup JAXP
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(); //identity transformer
+ transformer.setURIResolver(this.uriResolver);
// Setup input for XSLT transformation
Source src = new StreamSource(foFile);
@@ -163,11 +170,12 @@ public class FopPrintServlet extends HttpServlet {
public void renderXML(File xmlfile, File xsltfile,
HttpServletResponse response) throws ServletException {
try {
- Fop fop = new Fop(MimeConstants.MIME_FOP_PRINT);
+ Fop fop = new Fop(MimeConstants.MIME_FOP_PRINT, getFOUserAgent());
// Setup XSLT
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(new StreamSource(xsltfile));
+ transformer.setURIResolver(this.uriResolver);
// Setup input for XSLT transformation
Source src = new StreamSource(xmlfile);
@@ -202,5 +210,13 @@ public class FopPrintServlet extends HttpServlet {
throw new ServletException(ex);
}
}
+
+ /** @return a new FOUserAgent for FOP */
+ protected FOUserAgent getFOUserAgent() {
+ FOUserAgent userAgent = new FOUserAgent();
+ userAgent.setURIResolver(this.uriResolver);
+ return userAgent;
+ }
+
}
diff --git a/src/java/org/apache/fop/servlet/FopServlet.java b/src/java/org/apache/fop/servlet/FopServlet.java
index a5d8612d7..cd1d23014 100644
--- a/src/java/org/apache/fop/servlet/FopServlet.java
+++ b/src/java/org/apache/fop/servlet/FopServlet.java
@@ -1,5 +1,5 @@
/*
- * Copyright 1999-2005 The Apache Software Foundation.
+ * Copyright 1999-2006 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -30,6 +30,7 @@ import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.URIResolver;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.stream.StreamSource;
@@ -37,6 +38,7 @@ import org.apache.commons.io.output.ByteArrayOutputStream;
import org.apache.commons.logging.impl.SimpleLog;
//FOP
+import org.apache.fop.apps.FOUserAgent;
import org.apache.fop.apps.Fop;
import org.apache.fop.apps.FOPException;
import org.apache.fop.apps.MimeConstants;
@@ -78,6 +80,8 @@ public class FopServlet extends HttpServlet {
protected SimpleLog log = null;
/** The TransformerFactory to use to create Transformer instances */
protected TransformerFactory transFactory = null;
+ /** URIResolver for use by this servlet */
+ protected URIResolver uriResolver;
/**
* @see javax.servlet.GenericServlet#init()
@@ -85,7 +89,9 @@ public class FopServlet extends HttpServlet {
public void init() throws ServletException {
this.log = new SimpleLog("FOP/Servlet");
log.setLevel(SimpleLog.LOG_LEVEL_WARN);
+ this.uriResolver = new ServletContextURIResolver(getServletContext());
this.transFactory = TransformerFactory.newInstance();
+ this.transFactory.setURIResolver(this.uriResolver);
}
/**
@@ -152,6 +158,7 @@ public class FopServlet extends HttpServlet {
//Setup the identity transformation
Transformer transformer = this.transFactory.newTransformer();
+ transformer.setURIResolver(this.uriResolver);
//Start transformation and rendering process
return render(foSrc, transformer);
@@ -178,6 +185,7 @@ public class FopServlet extends HttpServlet {
//Setup the XSL transformation
Transformer transformer = this.transFactory.newTransformer(xsltSrc);
+ transformer.setURIResolver(this.uriResolver);
//Start transformation and rendering process
return render(xmlSrc, transformer);
@@ -201,7 +209,7 @@ public class FopServlet extends HttpServlet {
throws FOPException, TransformerException {
//Setup FOP
- Fop fop = new Fop(MimeConstants.MIME_PDF);
+ Fop fop = new Fop(MimeConstants.MIME_PDF, getFOUserAgent());
//Setup output
ByteArrayOutputStream out = new ByteArrayOutputStream();
@@ -216,5 +224,12 @@ public class FopServlet extends HttpServlet {
//Return the result
return out.toByteArray();
}
+
+ /** @return a new FOUserAgent for FOP */
+ protected FOUserAgent getFOUserAgent() {
+ FOUserAgent userAgent = new FOUserAgent();
+ userAgent.setURIResolver(this.uriResolver);
+ return userAgent;
+ }
}
diff --git a/src/java/org/apache/fop/servlet/ServletContextURIResolver.java b/src/java/org/apache/fop/servlet/ServletContextURIResolver.java
new file mode 100644
index 000000000..ae8b4eba4
--- /dev/null
+++ b/src/java/org/apache/fop/servlet/ServletContextURIResolver.java
@@ -0,0 +1,84 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed 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.servlet;
+
+import java.io.InputStream;
+import java.net.MalformedURLException;
+import java.net.URL;
+
+import javax.servlet.ServletContext;
+import javax.xml.transform.Source;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.URIResolver;
+import javax.xml.transform.stream.StreamSource;
+
+/**
+ * This class is a URIResolver implementation that provides access to resources in the WEB-INF
+ * directory of a web application using "servlet-content:" URIs.
+ */
+public class ServletContextURIResolver implements URIResolver {
+
+ /** The protocol name for the servlet context URIs. */
+ public static final String SERVLET_CONTEXT_PROTOCOL = "servlet-context:";
+
+ private ServletContext servletContext;
+
+ /**
+ * Main constructor
+ * @param servletContext the servlet context to access the resources through
+ */
+ public ServletContextURIResolver(ServletContext servletContext) {
+ this.servletContext = servletContext;
+ }
+
+ /** @see javax.xml.transform.URIResolver#resolve(java.lang.String, java.lang.String) */
+ public Source resolve(String href, String base) throws TransformerException {
+ if (href.startsWith(SERVLET_CONTEXT_PROTOCOL)) {
+ return resolveServletContextURI(href.substring(SERVLET_CONTEXT_PROTOCOL.length()));
+ } else {
+ return null;
+ }
+ }
+
+ /**
+ * Resolves the "servlet-context:" URI.
+ * @param path the path part after the protocol (should start with a "/")
+ * @return the resolved Source or null if the resource was not found
+ * @throws TransformerException if no URL can be constructed from the path
+ */
+ protected Source resolveServletContextURI(String path) throws TransformerException {
+ try {
+ URL url = this.servletContext.getResource(path);
+ InputStream in = this.servletContext.getResourceAsStream(path);
+ if (in != null) {
+ if (url != null) {
+ return new StreamSource(in, url.toExternalForm());
+ } else {
+ return new StreamSource(in);
+ }
+ } else {
+ throw new TransformerException("Resource does not exist. \"" + path
+ + "\" is not accessible through the servlet context.");
+ }
+ } catch (MalformedURLException mfue) {
+ throw new TransformerException(
+ "Error accessing resource using servlet context: " + path, mfue);
+ }
+ }
+}