blob: aadba4b2f6675b4a6b01d8e6deb72b0e90b44683 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
package org.apache.fop.apps;
import org.xml.sax.*;
import com.jclark.xsl.sax.*;
import java.io.*;
// FOP
import org.apache.fop.fo.XTFOTreeBuilder;
import org.apache.fop.fo.XTElementMapping;
import org.apache.fop.layout.AreaTree;
import org.apache.fop.layout.FontInfo;
import org.apache.fop.render.Renderer;
import org.apache.fop.messaging.MessageHandler;
//////////////////////////////////////////////////////////////////////////////////////
/**
* A DocumentHandler that writes a PDF representation to an OutputStream.
*
* Use with James Clark's XT. Just put FOP on your class path and add
* <xsl:output method="fop:org.apache.fop.apps.PDFOutputHandler"
* xmlns:fop="http://www.jclark.com/xt/java"/>
* to your stylesheet. Now XT will automatically call FOP.
*
*/
public class PDFOutputHandler extends XTFOTreeBuilder implements OutputDocumentHandler {
/** the area tree that is the result of formatting the FO tree */
protected AreaTree areaTree;
/** the renderer to use to output the area tree */
protected Renderer renderer;
/** the PrintWriter to use to output the results of the renderer */
protected PrintWriter writer;
/** the stream to use to output the results of the renderer */
protected OutputStream stream;
private boolean keepOpen;
//////////////////////////////////////////////////////////////////////////////////////
/**
*/
public PDFOutputHandler() {
}
//////////////////////////////////////////////////////////////////////////////////////
/**
*/
public PDFOutputHandler(OutputStream out) {
this();
this.stream = out;
}
//////////////////////////////////////////////////////////////////////////////////////
/**
*/
public DocumentHandler init(Destination dest, AttributeList atts) throws IOException {
this.stream = dest.getOutputStream("application/pdf", null);
this.keepOpen = dest.keepOpen();
String version = org.apache.fop.apps.Version.getVersion();
setRenderer("org.apache.fop.render.pdf.PDFRenderer", version);
addElementMapping("org.apache.fop.fo.StandardElementMapping");
addElementMapping("org.apache.fop.svg.SVGElementMapping");
return this;
}
//////////////////////////////////////////////////////////////////////////////////////
/**
* set the class name of the Renderer to use as well as the
* producer string for those renderers that can make use of it
*/
public void setRenderer(String rendererClassName, String producer) {
this.renderer = createRenderer(rendererClassName);
this.renderer.setProducer(producer);
}
//////////////////////////////////////////////////////////////////////////////////////
/**
* SAX passthrough, finish rendering the document
*/
public void endDocument() throws SAXException {
super.endDocument();
try {
doFormat();
doRender();
} catch (IOException io) {
throw new SAXException(io);
} catch (FOPException fop) {
throw new SAXException(fop);
}
writer.flush();
}
//////////////////////////////////////////////////////////////////////////////////////
/**
* format the formatting object tree into an area tree
*/
public void doFormat()
throws FOPException {
FontInfo fontInfo = new FontInfo();
this.renderer.setupFontInfo(fontInfo);
this.areaTree = new AreaTree();
this.areaTree.setFontInfo(fontInfo);
format(areaTree);
}
//////////////////////////////////////////////////////////////////////////////////////
/**
* render the area tree to the output form
*/
public void doRender()
throws IOException,FOPException {
this.renderer.render(areaTree, this.stream);
}
//////////////////////////////////////////////////////////////////////////////////////
/**
* add the given element mapping.
*
* an element mapping maps element names to Java classes
*/
public void addElementMapping(XTElementMapping mapping) {
mapping.addToBuilder(this);
}
//////////////////////////////////////////////////////////////////////////////////////
/**
* add the element mapping with the given class name
*/
public void addElementMapping(String mappingClassName) {
createElementMapping(mappingClassName).addToBuilder(this);
}
//////////////////////////////////////////////////////////////////////////////////////
/**
* protected method used by addElementMapping(String) to
* instantiate element mapping class
*/
protected XTElementMapping createElementMapping(String mappingClassName) {
MessageHandler.logln("using element mapping " + mappingClassName);
try {
return (XTElementMapping)
Class.forName(mappingClassName).newInstance();
} catch (ClassNotFoundException e) {
MessageHandler.errorln("Could not find " + mappingClassName);
} catch (InstantiationException e) {
MessageHandler.errorln("Could not instantiate "
+ mappingClassName);
} catch (IllegalAccessException e) {
MessageHandler.errorln("Could not access " + mappingClassName);
} catch (ClassCastException e) {
MessageHandler.errorln(mappingClassName + " is not an element mapping");
}
return null;
}
//////////////////////////////////////////////////////////////////////////////////////
/**
* protected method used by setRenderer(String, String) to
* instantiate the Renderer class
*/
protected Renderer createRenderer(String rendererClassName) {
MessageHandler.logln("using renderer " + rendererClassName);
try {
return (Renderer)
Class.forName(rendererClassName).newInstance();
} catch (ClassNotFoundException e) {
MessageHandler.errorln("Could not find " + rendererClassName);
} catch (InstantiationException e) {
MessageHandler.errorln("Could not instantiate "
+ rendererClassName);
} catch (IllegalAccessException e) {
MessageHandler.errorln("Could not access " + rendererClassName);
} catch (ClassCastException e) {
MessageHandler.errorln(rendererClassName + " is not a renderer");
}
return null;
}
}
|