KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > render > mif > MIFHandler


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 /* $Id: MIFHandler.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.render.mif;
21
22 // Java
23
import java.io.IOException JavaDoc;
24 import java.io.OutputStream JavaDoc;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.apache.fop.apps.FOUserAgent;
29 import org.apache.fop.fo.FOEventHandler;
30 import org.apache.fop.fo.flow.BasicLink;
31 import org.apache.fop.fo.flow.Block;
32 import org.apache.fop.fo.flow.ExternalGraphic;
33 import org.apache.fop.fo.flow.Footnote;
34 import org.apache.fop.fo.flow.FootnoteBody;
35 import org.apache.fop.fo.flow.InstreamForeignObject;
36 import org.apache.fop.fo.flow.Inline;
37 import org.apache.fop.fo.flow.Leader;
38 import org.apache.fop.fo.flow.ListBlock;
39 import org.apache.fop.fo.flow.ListItem;
40 import org.apache.fop.fo.flow.PageNumber;
41 import org.apache.fop.fo.flow.Table;
42 import org.apache.fop.fo.flow.TableBody;
43 import org.apache.fop.fo.flow.TableCell;
44 import org.apache.fop.fo.flow.TableColumn;
45 import org.apache.fop.fo.flow.TableRow;
46 import org.apache.fop.fo.pagination.Flow;
47 import org.apache.fop.fo.pagination.PageSequence;
48 import org.apache.fop.fo.pagination.PageSequenceMaster;
49 import org.apache.fop.fo.pagination.SimplePageMaster;
50 import org.apache.fop.fonts.FontSetup;
51 import org.apache.fop.render.DefaultFontResolver;
52 import org.xml.sax.SAXException JavaDoc;
53
54 // TODO: do we really want every method throwing a SAXException
55

56 /**
57  * The MIF Handler.
58  * This generates MIF output using the structure events from
59  * the FO Tree sent to this structure handler.
60  * This builds an MIF file and writes it to the output.
61  */

62 public class MIFHandler extends FOEventHandler {
63
64     /** Logger */
65     private static Log log = LogFactory.getLog(MIFHandler.class);
66
67     /** the MIFFile instance */
68     protected MIFFile mifFile;
69     
70     /** the OutputStream to write to */
71     protected OutputStream JavaDoc outStream;
72
73     // current state elements
74
private MIFElement textFlow;
75     private MIFElement para;
76
77     /**
78      * Creates a new MIF handler on a given OutputStream.
79      * @param ua FOUserAgent instance for this process
80      * @param os OutputStream to write to
81      */

82     public MIFHandler(FOUserAgent ua, OutputStream JavaDoc os) {
83         super(ua);
84         outStream = os;
85         FontSetup.setup(fontInfo, null, new DefaultFontResolver(ua));
86     }
87
88     /**
89      * @see org.apache.fop.fo.FOEventHandler#startDocument()
90      */

91     public void startDocument() throws SAXException JavaDoc {
92         log.fatal("The MIF Handler is non-functional at this time. Please help resurrect it!");
93         mifFile = new MIFFile();
94         try {
95             mifFile.output(outStream);
96         } catch (IOException JavaDoc ioe) {
97             throw new SAXException JavaDoc(ioe);
98         }
99     }
100
101     /**
102      * @see org.apache.fop.fo.FOEventHandler#endDocument()
103      */

104     public void endDocument() throws SAXException JavaDoc {
105         // finish all open elements
106
mifFile.finish(true);
107         try {
108             mifFile.output(outStream);
109             outStream.flush();
110         } catch (IOException JavaDoc ioe) {
111             throw new SAXException JavaDoc(ioe);
112         }
113     }
114
115     /**
116      * Start the page sequence.
117      * This creates the pages in the MIF document that will be used
118      * by the following flows and static areas.
119      * @see org.apache.fop.fo.FOEventHandler
120      */

121     public void startPageSequence(PageSequence pageSeq) {
122         // get the layout master set
123
// setup the pages for this sequence
124
String JavaDoc name = pageSeq.getMasterReference();
125         SimplePageMaster spm = pageSeq.getRoot().getLayoutMasterSet().getSimplePageMaster(name);
126         if (spm == null) {
127             PageSequenceMaster psm = pageSeq.getRoot().getLayoutMasterSet().getPageSequenceMaster(name);
128         } else {
129             // create simple master with regions
130
MIFElement prop = new MIFElement("PageType");
131             prop.setValue("BodyPage");
132
133            MIFElement page = new MIFElement("Page");
134            page.addElement(prop);
135
136            prop = new MIFElement("PageBackground");
137            prop.setValue("'Default'");
138            page.addElement(prop);
139
140            // build regions
141
MIFElement textRect = new MIFElement("TextRect");
142            prop = new MIFElement("ID");
143            prop.setValue("1");
144            textRect.addElement(prop);
145            prop = new MIFElement("ShapeRect");
146            prop.setValue("0.0 841.889 453.543 0.0");
147            textRect.addElement(prop);
148            page.addElement(textRect);
149
150            textRect = new MIFElement("TextRect");
151            prop = new MIFElement("ID");
152            prop.setValue("2");
153            textRect.addElement(prop);
154            prop = new MIFElement("ShapeRect");
155            prop.setValue("0.0 841.889 453.543 187.65");
156            textRect.addElement(prop);
157            page.addElement(textRect);
158
159            mifFile.addPage(page);
160         }
161     }
162
163     /**
164      * @see org.apache.fop.fo.FOEventHandler#endPageSequence(PageSequence)
165      */

166     public void endPageSequence(PageSequence pageSeq) {
167     }
168
169     /**
170      * @see org.apache.fop.fo.FOEventHandler#startFlow(Flow)
171      */

172     public void startFlow(Flow fl) {
173         // start text flow in body region
174
textFlow = new MIFElement("TextFlow");
175     }
176
177     /**
178      * @see org.apache.fop.fo.FOEventHandler#endFlow(Flow)
179      */

180     public void endFlow(Flow fl) {
181         textFlow.finish(true);
182         mifFile.addElement(textFlow);
183         textFlow = null;
184     }
185
186     /**
187      * @see org.apache.fop.fo.FOEventHandler#startBlock(Block)
188      */

189     public void startBlock(Block bl) {
190         para = new MIFElement("Para");
191         // get font
192
textFlow.addElement(para);
193     }
194
195     /**
196      * @see org.apache.fop.fo.FOEventHandler#endBlock(Block)
197      */

198     public void endBlock(Block bl) {
199         para.finish(true);
200         para = null;
201     }
202
203     /**
204      *
205      * @param inl Inline that is starting.
206      */

207     public void startInline(Inline inl){
208     }
209
210     /**
211      *
212      * @param inl Inline that is ending.
213      */

214     public void endInline(Inline inl){
215     }
216
217     /**
218      * @see org.apache.fop.fo.FOEventHandler#startTable(Table)
219      */

220     public void startTable(Table tbl) {
221     }
222
223     /**
224      * @see org.apache.fop.fo.FOEventHandler#endTable(Table)
225      */

226     public void endTable(Table tbl) {
227     }
228
229     /**
230      *
231      * @param tc TableColumn that is starting;
232      */

233     public void startColumn(TableColumn tc) {
234     }
235
236     /**
237      *
238      * @param tc TableColumn that is ending;
239      */

240     public void endColumn(TableColumn tc) {
241     }
242
243     /**
244      * @see org.apache.fop.fo.FOEventHandler#startHeader(TableBody)
245      */

246     public void startHeader(TableBody th) {
247     }
248
249     /**
250      * @see org.apache.fop.fo.FOEventHandler#endHeader(TableBody)
251      */

252     public void endHeader(TableBody th) {
253     }
254
255     /**
256      * @see org.apache.fop.fo.FOEventHandler#startFooter(TableBody)
257      */

258     public void startFooter(TableBody tf) {
259     }
260
261     /**
262      * @see org.apache.fop.fo.FOEventHandler#endFooter(TableBody)
263      */

264     public void endFooter(TableBody tf) {
265     }
266
267     /**
268      * @see org.apache.fop.fo.FOEventHandler#startBody(TableBody)
269      */

270     public void startBody(TableBody tb) {
271     }
272
273     /**
274      * @see org.apache.fop.fo.FOEventHandler#endBody(TableBody)
275      */

276     public void endBody(TableBody tb) {
277     }
278
279     /**
280      * @see org.apache.fop.fo.FOEventHandler#startRow(TableRow)
281      */

282     public void startRow(TableRow tr) {
283     }
284
285     /**
286      * @see org.apache.fop.fo.FOEventHandler#endRow(TableRow)
287      */

288     public void endRow(TableRow tr) {
289     }
290
291     /**
292      * @see org.apache.fop.fo.FOEventHandler#startCell(TableCell)
293      */

294     public void startCell(TableCell tc) {
295     }
296
297     /**
298      * @see org.apache.fop.fo.FOEventHandler#endCell(TableCell)
299      */

300     public void endCell(TableCell tc) {
301     }
302
303     // Lists
304
/**
305      * @see org.apache.fop.fo.FOEventHandler#startList(ListBlock)
306      */

307     public void startList(ListBlock lb) {
308     }
309
310     /**
311      * @see org.apache.fop.fo.FOEventHandler#endList(ListBlock)
312      */

313     public void endList(ListBlock lb) {
314     }
315
316     /**
317      * @see org.apache.fop.fo.FOEventHandler#startListItem(ListItem)
318      */

319     public void startListItem(ListItem li) {
320     }
321
322     /**
323      * @see org.apache.fop.fo.FOEventHandler#endListItem(ListItem)
324      */

325     public void endListItem(ListItem li) {
326     }
327
328     /**
329      * @see org.apache.fop.fo.FOEventHandler#startListLabel()
330      */

331     public void startListLabel() {
332     }
333
334     /**
335      * @see org.apache.fop.fo.FOEventHandler#endListLabel()
336      */

337     public void endListLabel() {
338     }
339
340     /**
341      * @see org.apache.fop.fo.FOEventHandler#startListBody()
342      */

343     public void startListBody() {
344     }
345
346     /**
347      * @see org.apache.fop.fo.FOEventHandler#endListBody()
348      */

349     public void endListBody() {
350     }
351
352     // Static Regions
353
/**
354      * @see org.apache.fop.fo.FOEventHandler#startStatic()
355      */

356     public void startStatic() {
357     }
358
359     /**
360      * @see org.apache.fop.fo.FOEventHandler#endStatic()
361      */

362     public void endStatic() {
363     }
364
365     /**
366      * @see org.apache.fop.fo.FOEventHandler#startMarkup()
367      */

368     public void startMarkup() {
369     }
370
371     /**
372      * @see org.apache.fop.fo.FOEventHandler#endMarkup()
373      */

374     public void endMarkup() {
375     }
376
377     /**
378      * @see org.apache.fop.fo.FOEventHandler#startLink(BasicLink basicLink)
379      */

380     public void startLink(BasicLink basicLink) {
381     }
382
383     /**
384      * @see org.apache.fop.fo.FOEventHandler#endLink()
385      */

386     public void endLink() {
387     }
388
389     /**
390      * @see org.apache.fop.fo.FOEventHandler#image(ExternalGraphic)
391      */

392     public void image(ExternalGraphic eg) {
393     }
394
395     /**
396      * @see org.apache.fop.fo.FOEventHandler#pageRef()
397      */

398     public void pageRef() {
399     }
400
401     /**
402      * @see org.apache.fop.fo.FOEventHandler#foreignObject(InstreamForeignObject)
403      */

404     public void foreignObject(InstreamForeignObject ifo) {
405     }
406
407     /**
408      * @see org.apache.fop.fo.FOEventHandler#startFootnote(Footnote)
409      */

410     public void startFootnote(Footnote footnote) {
411     }
412     
413     /**
414      * @see org.apache.fop.fo.FOEventHandler#endFootnote(Footnote)
415      */

416     public void endFootnote(Footnote footnote) {
417     }
418     
419     /**
420      * @see org.apache.fop.fo.FOEventHandler#startFootnoteBody(FootnoteBody)
421      */

422     public void startFootnoteBody(FootnoteBody body) {
423     }
424     
425     /**
426      * @see org.apache.fop.fo.FOEventHandler#endFootnoteBody(FootnoteBody)
427      */

428     public void endFootnoteBody(FootnoteBody body) {
429     }
430     
431     /**
432      * @see org.apache.fop.fo.FOEventHandler#leader(Leader)
433      */

434     public void leader(Leader l) {
435     }
436
437     /**
438      * @see org.apache.fop.fo.FOEventHandler#characters(char[], int, int)
439      */

440     public void characters(char data[], int start, int length) {
441         if (para != null) {
442             String JavaDoc str = new String JavaDoc(data, start, length);
443             str = str.trim();
444             // break into nice length chunks
445
if (str.length() == 0) {
446                 return;
447             }
448
449             MIFElement line = new MIFElement("ParaLine");
450             MIFElement prop = new MIFElement("TextRectID");
451             prop.setValue("2");
452             line.addElement(prop);
453             prop = new MIFElement("String");
454             prop.setValue("\"" + str + "\"");
455             line.addElement(prop);
456
457             para.addElement(line);
458         }
459     }
460
461     /**
462      *
463      * @param pagenum PageNumber that is starting.
464      */

465     public void startPageNumber(PageNumber pagenum) {
466     }
467
468     /**
469      *
470      * @param pagenum PageNumber that is ending.
471      */

472     public void endPageNumber(PageNumber pagenum) {
473     }
474 }
475
476
Popular Tags