1
2
3
4
5
6
7
8
9
10
11
12 package sk.uniba.euromath.foRenderer.fop;
13
14 import java.io.InputStream;
15
16 import javax.xml.transform.Source;
17 import javax.xml.transform.Transformer;
18 import javax.xml.transform.TransformerException;
19 import javax.xml.transform.dom.DOMSource;
20 import javax.xml.transform.sax.SAXResult;
21 import javax.xml.transform.stream.StreamSource;
22
23 import org.apache.fop.apps.FOPException;
24 import org.apache.fop.apps.FOUserAgent;
25 import org.apache.fop.apps.Fop;
26 import org.apache.fop.apps.FopFactory;
27 import org.apache.fop.render.Renderer;
28 import org.w3c.dom.Document;
29 import org.xml.sax.ContentHandler;
30
31 import sk.baka.ikslibs.TransformUtils;
32 import sk.uniba.euromath.foRenderer.Draw2dRenderer;
33 import sk.uniba.euromath.foRenderer.FORenderer;
34 import sk.uniba.euromath.foRenderer.MarkElementMapping;
35
36 /***
37 * @author TV Created on 14.7.2003
38 *
39 * FOP Wrapper. The only access point usable to run the FOP library in the
40 * EuroMath application. FOP is configured in specific way allowing it to be
41 * used in our application.
42 */
43
44 public class AbstractFOPLauncher {
45
46 protected Fop fop;
47
48 private Source inputSource;
49
50 private final Transformer transformer;
51
52 public AbstractFOPLauncher(FORenderer renderer) throws FOPException {
53 super();
54 fop = createFop(renderer);
55 transformer = TransformUtils.newIdentity();
56 }
57
58 public void reinit(FORenderer renderer) throws FOPException{
59 fop = createFop(renderer);
60 }
61
62
63
64
65
66
67 protected Fop createFop(FORenderer renderer) throws FOPException {
68 final FopFactory fopFactory = FopFactory.newInstance();
69 final FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
70 foUserAgent.setRendererOverride(new Draw2dRenderer(renderer));
71 fopFactory.addElementMapping(new MarkElementMapping());
72 return fopFactory.newFop(foUserAgent);
73 }
74
75 public void run() throws TransformerException, FOPException {
76 if (inputSource == null) {
77 throw new IllegalStateException(
78 "Cannot run FOP with no input data!");
79 }
80
81
82 transformer.transform(inputSource,
83
84
85 new SAXResult(getContentHandler()));
86 }
87
88 public void setInput(Document input) {
89 if (input != null)
90 setInput(new DOMSource(input));
91 else
92 setInput((Source) null);
93 }
94
95 public void setInput(InputStream stream) {
96 if (stream != null)
97 setInput(new StreamSource(stream));
98 else
99 setInput((Source) null);
100 }
101
102 public void setInput(Source source) {
103 this.inputSource = source;
104 }
105
106 public ContentHandler getContentHandler() throws FOPException {
107 return fop.getDefaultHandler();
108 }
109
110 public FOUserAgent getUserAgent() {
111 return this.fop.getUserAgent();
112 }
113
114 public Source getInput() {
115 return inputSource;
116 }
117
118 public int getPageCount() {
119 return ((Draw2dRenderer) getRenderer()).getPages().size();
120 }
121
122 public Renderer getRenderer() {
123 return fop.getUserAgent().getRendererOverride();
124 }
125
126 }