KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > servlet > FopPrintServlet


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 /* $Id: FopPrintServlet.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.servlet;
21
22 import java.io.IOException JavaDoc;
23 import java.io.PrintWriter JavaDoc;
24
25 import javax.servlet.http.HttpServletResponse JavaDoc;
26
27 import javax.xml.transform.Transformer JavaDoc;
28 import javax.xml.transform.TransformerException JavaDoc;
29 import javax.xml.transform.Result JavaDoc;
30 import javax.xml.transform.Source JavaDoc;
31 import javax.xml.transform.sax.SAXResult JavaDoc;
32
33 import org.apache.fop.apps.FOPException;
34 import org.apache.fop.apps.FOUserAgent;
35 import org.apache.fop.apps.Fop;
36 import org.apache.fop.apps.MimeConstants;
37
38
39 /**
40  * Example servlet to generate a fop printout from a servlet.
41  * Printing goes to the default printer on host where the servlet executes.
42  * Servlet param is:
43  * <ul>
44  * <li>fo: the path to a XSL-FO file to render
45  * </ul>
46  * or
47  * <ul>
48  * <li>xml: the path to an XML file to render</li>
49  * <li>xslt: the path to an XSLT file that can transform the above XML to XSL-FO</li>
50  * </ul>
51  * <br/>
52  * Example URL: http://servername/fop/servlet/FopPrintServlet?fo=readme.fo
53  * <br/>
54  * Example URL: http://servername/fop/servlet/FopPrintServlet?xml=data.xml&xsl=format.xsl
55  * <br/>
56  * <b>Note:</b> This servlet is derived from FopServlet. Most methods are inherited from the
57  * superclass. Only the differences to the base class are necessary.
58  *
59  * @author <a HREF="mailto:fop-dev@xmlgraphics.apache.org">Apache FOP Development Team</a>
60  * @version $Id: FopPrintServlet.java 426576 2006-07-28 15:44:37Z jeremias $
61  */

62 public class FopPrintServlet extends FopServlet {
63
64     /**
65      * @see org.apache.fop.servlet.FopServlet#render(javax.xml.transform.Source,
66      * javax.xml.transform.Transformer, javax.servlet.http.HttpServletResponse)
67      */

68     protected void render(Source JavaDoc src, Transformer JavaDoc transformer, HttpServletResponse JavaDoc response)
69             throws FOPException, TransformerException JavaDoc, IOException JavaDoc {
70
71         FOUserAgent foUserAgent = getFOUserAgent();
72         
73         //Setup FOP
74
Fop fop = fopFactory.newFop(MimeConstants.MIME_FOP_PRINT, foUserAgent);
75         
76         //Make sure the XSL transformation's result is piped through to FOP
77
Result JavaDoc res = new SAXResult JavaDoc(fop.getDefaultHandler());
78         
79         //Start the transformation and rendering process
80
transformer.transform(src, res);
81         
82         //Return the result
83
reportOK(response);
84     }
85
86     // private helper, tell (browser) user that file printed
87
private void reportOK(HttpServletResponse JavaDoc response) throws IOException JavaDoc {
88         String JavaDoc sMsg = "<html><title>Success</title>\n"
89                 + "<body><h1>FopPrintServlet: </h1>"
90                 + "<h3>The requested data was printed to the default printer.</h3></body></html>";
91
92         response.setContentType("text/html");
93         response.setContentLength(sMsg.length());
94
95         PrintWriter JavaDoc out = response.getWriter();
96         out.println(sMsg);
97         out.flush();
98     }
99
100 }
101
102
Popular Tags