KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fops > business > RenderManagerImpl


1 package fops.business;
2
3 import fops.spec.RenderManager;
4
5 import org.apache.fop.apps.XSLTInputHandler;
6 import org.apache.avalon.framework.logger.Logger;
7 import org.apache.fop.apps.Driver;
8 import org.apache.fop.apps.Options;
9
10 import org.xml.sax.InputSource JavaDoc;
11 import org.xml.sax.XMLReader JavaDoc;
12
13 import java.io.*;
14
15 public class RenderManagerImpl implements RenderManager{
16
17  /**
18      * renders a HTML fields and Applet contents inputsource
19      * into a PDF file which is rendered directly to the response
20      * object's OutputStream
21      */

22   public byte[] renderFO(boolean noParams,String JavaDoc DateHTML,String JavaDoc NameHTML,String JavaDoc SubjHTML,String JavaDoc AppletHTML,Logger log) throws Exception JavaDoc {
23          
24         ByteArrayOutputStream out = new ByteArrayOutputStream();
25         
26                 
27         
28         StringReader str;
29         if(noParams)
30             str = defaultText();
31         else
32             str = inputText(DateHTML,NameHTML,SubjHTML,AppletHTML);
33         Driver driver = new Driver(new InputSource JavaDoc(str), out);
34         driver.setLogger(log);
35         driver.setRenderer(Driver.RENDER_PDF);
36
37         driver.run();
38
39         byte[] content = out.toByteArray();
40         
41         return content;
42         
43            }
44
45   public byte[] renderXML(String JavaDoc xmlParam,String JavaDoc xslParam,Logger log) throws Exception JavaDoc {
46         
47         XSLTInputHandler input = new XSLTInputHandler(new File(xmlParam), new File(xslParam));
48         ByteArrayOutputStream out = new ByteArrayOutputStream();
49
50         
51         Driver driver = new Driver();
52         driver.setLogger(log);
53         driver.setRenderer(Driver.RENDER_PDF);
54         driver.setOutputStream(out);
55         driver.render(input.getParser(), input.getInputSource());
56
57         byte[] content = out.toByteArray();
58         
59         return content;
60         
61         
62    }
63
64    /**
65     * renders an FO inputsource into a PDF file which is rendered
66     * directly to the response object's OutputStream
67     */

68    public byte[] renderFOFile(String JavaDoc foParam,Logger log) throws Exception JavaDoc {
69         ByteArrayOutputStream out = new ByteArrayOutputStream();
70         FileInputStream foFile = new FileInputStream(foParam);
71         
72         Driver driver = new Driver();
73         driver.setLogger(log);
74         driver.setRenderer(Driver.RENDER_PDF);
75         driver.setInputSource(new InputSource JavaDoc(foFile));
76         driver.setOutputStream(out);
77         driver.run();
78
79         byte[] content = out.toByteArray();
80         
81         return content;
82         
83         
84    }
85
86     /**
87      * creates a SAX parser, using the value of org.xml.sax.parser
88      * defaulting to org.apache.xerces.parsers.SAXParser
89      *
90      * @return the created SAX parser
91      */

92    static XMLReader JavaDoc createParser() throws Exception JavaDoc {
93         String JavaDoc parserClassName = System.getProperty("org.xml.sax.parser");
94
95         if (parserClassName == null) {
96             parserClassName = "org.apache.xerces.parsers.SAXParser";
97         }
98         return (XMLReader JavaDoc) Class.forName(parserClassName).newInstance();
99    }
100
101     /**
102      * Method inputText
103      * Creates a XSLFO file with inserted parameters from HTML.
104      * Noncomptibility: FOP 0.19 - <fo:page-sequence master-name="simple">
105      * FOP 0.20.4 - <fo:page-sequence master-reference="simple">
106      * @return a StringReader
107      *
108      */

109     public StringReader inputText(String JavaDoc DateHTML,String JavaDoc NameHTML,String JavaDoc SubjHTML,String JavaDoc AppletHTML) {
110       String JavaDoc text = new String JavaDoc();
111       text += "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
112       text += "<fo:root xmlns:fo=\"http://www.w3.org/1999/XSL/Format\">\n";
113       text += " <fo:layout-master-set>\n";
114       text += " <fo:simple-page-master master-name=\"simple\" page-height=\"29.7cm\" page-width=\"21cm\" " + "margin-top=\"1cm\" margin-bottom=\"2cm\" margin-left=\"2.5cm\" margin-right=\"2.5cm\">\n";
115       text += " <fo:region-body margin-top=\"3cm\"/>\n";
116       text += " <fo:region-before extent=\"3cm\"/>\n";
117       text += " <fo:region-after extent=\"1.5cm\"/>\n";
118       text += " </fo:simple-page-master>\n";
119       text += " </fo:layout-master-set>\n";
120       text += " <fo:page-sequence master-reference=\"simple\">\n";
121       text += " <fo:flow flow-name=\"xsl-region-body\">\n";
122       text += " <fo:block font-size=\"18pt\" font-family=\"sans-serif\" line-height=\"24pt\" " + "space-after.optimum=\"15pt\" background-color=\"blue\" color=\"white\" " + "text-align=\"center\" padding-top=\"3pt\">\n";
123       text += " Demo PDF file from applet \n";
124       text += " </fo:block>\n";
125       text += " <fo:table font-family=\"Helvetica\" space-after.optimum=\"20pt\">\n";
126       text += " <fo:table-column column-width=\"20mm\" />\n";
127       text += " <fo:table-column column-width=\"150mm\" />\n";
128       text += " <fo:table-body>\n";
129       text += " <fo:table-row>\n";
130       text += " <fo:table-cell>\n";
131       text += " <fo:block color=\"blue\" font-weight=\"bold\">Date:</fo:block>\n";
132       text += " </fo:table-cell>\n";
133       text += " <fo:table-cell>\n";
134       text += " <fo:block>" + DateHTML + "</fo:block>\n";
135       text += " </fo:table-cell>\n";
136       text += " </fo:table-row>\n";
137       text += " <fo:table-row>\n";
138       text += " <fo:table-cell>\n";
139       text += " <fo:block color=\"blue\" font-weight=\"bold\">Name:</fo:block>\n";
140       text += " </fo:table-cell>\n";
141       text += " <fo:table-cell>\n";
142       text += " <fo:block>" + NameHTML + "</fo:block>\n";
143       text += " </fo:table-cell>\n";
144       text += " </fo:table-row>\n";
145       text += " <fo:table-row>\n";
146       text += " <fo:table-cell>\n";
147       text += " <fo:block color=\"blue\" font-weight=\"bold\">Subject:</fo:block>\n";
148       text += " </fo:table-cell>\n";
149       text += " <fo:table-cell>\n";
150       text += " <fo:block>" + SubjHTML + "</fo:block>\n";
151       text += " </fo:table-cell>\n";
152       text += " </fo:table-row>\n";
153       text += " </fo:table-body>\n";
154       text += " </fo:table>\n";
155       text += AppletHTML;
156       text += " </fo:flow>\n";
157       text += " </fo:page-sequence>\n";
158       text += "</fo:root>\n";
159
160       StringReader foText = new StringReader(text);
161
162       return foText;
163    }
164    
165    public StringReader defaultText(){
166       String JavaDoc text = new String JavaDoc();
167       text += "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
168       text += "<fo:root xmlns:fo=\"http://www.w3.org/1999/XSL/Format\">\n";
169       text += " <fo:layout-master-set>\n";
170       text += " <fo:simple-page-master master-name=\"simple\" page-height=\"29.7cm\" page-width=\"21cm\" " + "margin-top=\"1cm\" margin-bottom=\"2cm\" margin-left=\"2.5cm\" margin-right=\"2.5cm\">\n";
171       text += " <fo:region-body margin-top=\"3cm\"/>\n";
172       text += " <fo:region-before extent=\"3cm\"/>\n";
173       text += " <fo:region-after extent=\"1.5cm\"/>\n";
174       text += " </fo:simple-page-master>\n";
175       text += " </fo:layout-master-set>\n";
176       text += " <fo:page-sequence master-reference=\"simple\">\n";
177       text += " <fo:flow flow-name=\"xsl-region-body\">\n";
178       text += " <fo:block font-size=\"18pt\" font-family=\"sans-serif\" line-height=\"24pt\" " + "space-after.optimum=\"15pt\" background-color=\"blue\" color=\"white\" " + "text-align=\"center\" padding-top=\"3pt\">\n";
179       text += " Demo PDF file \n";
180       text += " </fo:block>\n";
181       text += " <fo:block font-size=\"12pt\" font-family=\"sans-serif\" line-height=\"18pt\" space-after.optimum=\"3pt\" text-align=\"center\" white-space-collapse=\"false\" language=\"en_US\" hyphenate=\"true\">\n";
182       text += " <fo:inline font-family=\"sans-serif\" font-size=\"18pt\" font-weight=\"bold\" >There is no any request parameter given!!!</fo:inline>\n";
183       text += " </fo:block>\n";
184       text += " <fo:block font-size=\"12pt\" font-family=\"sans-serif\" line-height=\"18pt\" space-after.optimum=\"3pt\" text-align=\"center\" white-space-collapse=\"false\" language=\"en_US\" hyphenate=\"true\">\n";
185       text += " <fo:inline font-family=\"sans-serif\" font-size=\"16pt\" ></fo:inline>\n";
186       text += " </fo:block>\n";
187       text += " <fo:block font-size=\"12pt\" font-family=\"sans-serif\" line-height=\"18pt\" space-after.optimum=\"3pt\" text-align=\"center\" white-space-collapse=\"false\" language=\"en_US\" hyphenate=\"true\">\n";
188       text += " <fo:inline font-family=\"sans-serif\" font-size=\"12pt\" >Please, see documentation for details.</fo:inline>\n";
189       text += " </fo:block>\n";
190       text += " </fo:flow>\n";
191       text += " </fo:page-sequence>\n";
192       text += "</fo:root>\n";
193     
194       StringReader defaultText = new StringReader(text);
195
196       return defaultText;
197    }
198 }
Popular Tags