View Javadoc

1   /*
2    * Copyright 1999-2006 Faculty of Mathematics, Physics
3    * and Informatics, Comenius University, Bratislava. This file is protected by
4    * the Mozilla Public License version 1.1 (the License); you may not use this
5    * file except in compliance with the License. You may obtain a copy of the
6    * License at http://euromath2.sourceforge.net/license.html Unless required by
7    * applicable law or agreed to in writing, software distributed under the
8    * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
9    * OF ANY KIND, either express or implied. See the License for the specific
10   * language governing permissions and limitations under the License.
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           * (non-Javadoc)
64           * 
65           * @see sk.uniba.euromath.foRenderer.fop.AbstractFOPLauncher#createFop()
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                  // hier it is -> ContentHandler
82                  transformer.transform(inputSource,
83                  // new SAXResult(new SaxDebuggerPassive(getContentHandler(),new
84                  // PrintWriter(System.out,true),2,true)));
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 }