KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icl > saxon > fop > FOPEmitter


1 package com.icl.saxon.fop;
2 import com.icl.saxon.output.ContentHandlerProxy;
3 import org.xml.sax.Attributes JavaDoc;
4 import org.xml.sax.ContentHandler JavaDoc;
5 import javax.xml.transform.TransformerException JavaDoc;
6 import org.apache.fop.apps.Driver;
7 import org.apache.fop.apps.Options;
8 import org.apache.fop.apps.FOPException;
9 import java.util.Properties JavaDoc;
10 import java.io.File JavaDoc;
11
12 /**
13   * FOPEmitter: This class acts as a SAXON output emitter that feeds data into Apache
14   * FOP (see http://xml.apache.org/fop).
15   * March 2001: repackaged into a new package, com.icl.saxon.fop, and excluded from the
16   * JAR file, to avoid problems with external references. The class is now loaded
17   * dynamically when required.
18   * Sept 2001: changed to cope with the latest FOP API changes in FOP 0.20.1
19   */

20   
21 public class FOPEmitter extends ContentHandlerProxy
22 {
23     public static String JavaDoc FOP_RENDERER = "{http://icl.com/saxon/fop}renderer";
24     public static String JavaDoc FOP_CONFIGURATION = "{http://icl.com/saxon/fop}configuration";
25
26     private Driver fop;
27
28     /**
29     * Determine whether the Emitter wants a Writer for character output or
30     * an OutputStream for binary output: FOPEmitter wants an OutputStream
31     */

32     
33     public boolean usesWriter() {
34         return false;
35     }
36
37     /**
38     * Start of document processing
39     */

40
41     public void startDocument() throws TransformerException JavaDoc {
42         fop = new Driver();
43
44         ContentHandler JavaDoc ch = fop.getContentHandler();
45         
46         Properties JavaDoc props = getOutputProperties();
47         String JavaDoc renderer = props.getProperty(FOP_RENDERER);
48         if (renderer == null) {
49             fop.setRenderer(Driver.RENDER_PDF);
50         } else {
51             fop.setRenderer(renderer);
52         }
53         // next statement needed by FOP 0.20.1
54
((org.apache.fop.fo.FOTreeBuilder)ch).setStreamRenderer(
55             new org.apache.fop.apps.StreamRenderer(outputStream, fop.getRenderer()));
56         
57         String JavaDoc options = props.getProperty(FOP_CONFIGURATION);
58         if (options != null) {
59             try {
60                 System.setProperty("org.xml.sax.parser", "com.icl.saxon.aelfred.SAXDriver");
61                 Options fopOptions = new Options(new File JavaDoc(options));
62                 // instantiating this class updates FOP's static configuration data
63
} catch (org.apache.fop.apps.FOPException err) {
64                 throw new TransformerException JavaDoc(err);
65             }
66         }
67
68         setUnderlyingContentHandler(ch);
69         super.startDocument();
70     }
71
72     /**
73     * End of document processing
74     */

75
76     public void endDocument() throws TransformerException JavaDoc {
77         super.endDocument();
78         // following code no longer needed: FOP 0.20.1 deals with endDocument() automatically.
79
//try {
80
// fop.setOutputStream(outputStream);
81
// fop.format();
82
// fop.render();
83
//} catch (FOPException err) {
84
// throw new TransformerException("Error from FOP: ", err);
85
//} catch (java.io.IOException err) {
86
// throw new TransformerException("I/O Error from FOP: ", err);
87
//} catch (Throwable err) {
88
// throw new TransformerException("FOP processing error: " + err.getMessage());
89
//}
90
}
91
92 }
93
94 //
95
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
96
// you may not use this file except in compliance with the License. You may obtain a copy of the
97
// License at http://www.mozilla.org/MPL/
98
//
99
// Software distributed under the License is distributed on an "AS IS" basis,
100
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
101
// See the License for the specific language governing rights and limitations under the License.
102
//
103
// The Original Code is: all this file.
104
//
105
// The Initial Developer of the Original Code is
106
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
107
//
108
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
109
//
110
// Contributor(s): none.
111
//
112
Popular Tags