Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

FopPrintServlet.java 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * Copyright 1999-2005 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* $Id$ */
  17. package org.apache.fop.servlet;
  18. import java.io.File;
  19. import java.io.InputStream;
  20. import java.io.PrintWriter;
  21. // JAXP
  22. import javax.servlet.ServletException;
  23. import javax.servlet.http.HttpServlet;
  24. import javax.servlet.http.HttpServletRequest;
  25. import javax.servlet.http.HttpServletResponse;
  26. import javax.xml.transform.Transformer;
  27. import javax.xml.transform.TransformerFactory;
  28. import javax.xml.transform.Result;
  29. import javax.xml.transform.Source;
  30. import javax.xml.transform.sax.SAXResult;
  31. import javax.xml.transform.stream.StreamSource;
  32. // XML
  33. import org.apache.commons.logging.impl.SimpleLog;
  34. import org.apache.fop.apps.Fop;
  35. import org.apache.fop.apps.MimeConstants;
  36. /**
  37. * Example servlet to generate a fop printout from a servlet.
  38. * Printing goes to the default printer on host where the servlet executes.
  39. * Servlet param is:
  40. * <ul>
  41. * <li>fo: the path to a XSL-FO file to render
  42. * </ul>
  43. * or
  44. * <ul>
  45. * <li>xml: the path to an XML file to render</li>
  46. * <li>xslt: the path to an XSLT file that can transform the above XML to XSL-FO</li>
  47. * </ul>
  48. * <br/>
  49. * Example URL: http://servername/fop/servlet/FopPrintServlet?fo=readme.fo
  50. * <br/>
  51. * Example URL: http://servername/fop/servlet/FopPrintServlet?xml=data.xml&xsl=format.xsl
  52. *
  53. * @author <a href="mailto:fop-dev@xml.apache.org">Apache XML FOP Development Team</a>
  54. * @version $Id$
  55. * (todo) Doesn't work since there's no AWTRenderer at the moment. Revisit when
  56. * available.
  57. * (todo) Ev. add caching mechanism for Templates objects
  58. */
  59. public class FopPrintServlet extends HttpServlet {
  60. /** Name of the parameter used for the XSL-FO file */
  61. protected static final String FO_REQUEST_PARAM = "fo";
  62. /** Name of the parameter used for the XML file */
  63. protected static final String XML_REQUEST_PARAM = "xml";
  64. /** Name of the parameter used for the XSLT file */
  65. protected static final String XSLT_REQUEST_PARAM = "xslt";
  66. /** Logger to give to FOP */
  67. protected SimpleLog log = null;
  68. /** The TransformerFactory to use to create Transformer instances */
  69. protected TransformerFactory transFactory = null;
  70. /**
  71. * @see javax.servlet.GenericServlet#init()
  72. */
  73. public void init() throws ServletException {
  74. this.log = new SimpleLog("FOP/Print Servlet");
  75. log.setLevel(SimpleLog.LOG_LEVEL_WARN);
  76. this.transFactory = TransformerFactory.newInstance();
  77. }
  78. /**
  79. * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest, HttpServletResponse)
  80. */
  81. public void doGet(HttpServletRequest request,
  82. HttpServletResponse response) throws ServletException {
  83. if (log == null) {
  84. log = new SimpleLog("FOP/Print Servlet");
  85. log.setLevel(SimpleLog.LOG_LEVEL_WARN);
  86. }
  87. try {
  88. String foParam = request.getParameter(FO_REQUEST_PARAM);
  89. String xmlParam = request.getParameter(XML_REQUEST_PARAM);
  90. String xsltParam = request.getParameter(XSLT_REQUEST_PARAM);
  91. if (foParam != null) {
  92. InputStream file = new java.io.FileInputStream(foParam);
  93. renderFO(file, response);
  94. } else if ((xmlParam != null) && (xsltParam != null)) {
  95. renderXML(new File(xmlParam), new File(xsltParam), response);
  96. } else {
  97. response.setContentType("text/html");
  98. PrintWriter out = response.getWriter();
  99. out.println("<html><title>Error</title>\n"
  100. + "<body><h1>FopServlet Error</h1>\n"
  101. + "<h3>No 'fo' or 'xml/xsl' "
  102. + "request param given.</h3></body>\n</html>");
  103. }
  104. } catch (ServletException ex) {
  105. throw ex;
  106. } catch (Exception ex) {
  107. throw new ServletException(ex);
  108. }
  109. }
  110. /**
  111. * Renders an FO inputsource to the default printer.
  112. * @param foFile The XSL-FO file
  113. * @param response Response to write to
  114. * @throws ServletException In case of a problem
  115. */
  116. public void renderFO(InputStream foFile,
  117. HttpServletResponse response) throws ServletException {
  118. try {
  119. Fop fop = new Fop(MimeConstants.MIME_FOP_PRINT);
  120. // Setup JAXP
  121. TransformerFactory factory = TransformerFactory.newInstance();
  122. Transformer transformer = factory.newTransformer(); //identity transformer
  123. // Setup input for XSLT transformation
  124. Source src = new StreamSource(foFile);
  125. // Resulting SAX events (the generated FO) must be piped through to FOP
  126. Result res = new SAXResult(fop.getDefaultHandler());
  127. // Start XSLT transformation and FOP processing
  128. transformer.transform(src, res);
  129. reportOK (response);
  130. } catch (Exception ex) {
  131. throw new ServletException(ex);
  132. }
  133. }
  134. /**
  135. * Renders an FO generated using an XML and a stylesheet to the default printer.
  136. * @param xmlfile XML file object
  137. * @param xsltfile XSLT stylesheet
  138. * @param response HTTP response object
  139. * @throws ServletException In case of a problem
  140. */
  141. public void renderXML(File xmlfile, File xsltfile,
  142. HttpServletResponse response) throws ServletException {
  143. try {
  144. Fop fop = new Fop(MimeConstants.MIME_FOP_PRINT);
  145. // Setup XSLT
  146. TransformerFactory factory = TransformerFactory.newInstance();
  147. Transformer transformer = factory.newTransformer(new StreamSource(xsltfile));
  148. // Setup input for XSLT transformation
  149. Source src = new StreamSource(xmlfile);
  150. // Resulting SAX events (the generated FO) must be piped through to FOP
  151. Result res = new SAXResult(fop.getDefaultHandler());
  152. // Start XSLT transformation and FOP processing
  153. transformer.transform(src, res);
  154. reportOK (response);
  155. } catch (Exception ex) {
  156. throw new ServletException(ex);
  157. }
  158. }
  159. // private helper, tell (browser) user that file printed
  160. private void reportOK(HttpServletResponse response)
  161. throws ServletException {
  162. String sMsg = "<html><title>Success</title>\n"
  163. + "<body><h1>FopPrintServlet: </h1>"
  164. + "<h3>The requested data was printed</h3></body></html>";
  165. response.setContentType("text/html");
  166. response.setContentLength(sMsg.length());
  167. try {
  168. PrintWriter out = response.getWriter();
  169. out.println(sMsg);
  170. out.flush();
  171. } catch (Exception ex) {
  172. throw new ServletException(ex);
  173. }
  174. }
  175. }