KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > render > rtf > rtflib > rtfdoc > RtfFile


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: RtfFile.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.render.rtf.rtflib.rtfdoc;
21
22 /*
23  * This file is part of the RTF library of the FOP project, which was originally
24  * created by Bertrand Delacretaz <bdelacretaz@codeconsult.ch> and by other
25  * contributors to the jfor project (www.jfor.org), who agreed to donate jfor to
26  * the FOP project.
27  */

28
29 import org.apache.fop.render.rtf.rtflib.exceptions.RtfStructureException;
30 import java.io.Writer JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.io.BufferedWriter JavaDoc;
33 import java.io.FileWriter JavaDoc;
34 import java.io.OutputStreamWriter JavaDoc;
35
36 /**
37  * Models the top-level structure of an RTF file.
38  * @author Bertrand Delacretaz bdelacretaz@codeconsult.ch
39  * @author Andreas Putz a.putz@skynamics.com
40  * @author Christopher Scott scottc@westinghouse.com
41  */

42
43 public class RtfFile
44 extends RtfContainer {
45     private RtfHeader header;
46     private RtfPageArea pageArea;
47     private RtfListTable listTable;
48     private RtfDocumentArea docArea;
49 // private ConverterLogChannel m_log;
50
private RtfContainer listTableContainer;
51     private int listNum = 0;
52
53     /**
54      * Create an RTF file that outputs to the given Writer
55      * @param w the Writer to write to
56      * @throws IOException for I/O problems
57      */

58     public RtfFile(Writer JavaDoc w) throws IOException JavaDoc {
59         super(null, w);
60     }
61
62     /** optional log channel */
63 // public void setLogChannel(ConverterLogChannel log)
64
// {
65
// m_log = log;
66
// }
67

68     /**
69      * Gets the log channel.
70      * If logchannel not set, it will return a empty log channel.
71      * @return our log channel, it is never null */

72 // ConverterLogChannel getLog()
73
// {
74
// if (m_log == null)
75
// m_log = new ConverterLogChannel (null);
76
// return m_log;
77
// }
78

79     /**
80      * If called, must be called before startDocumentArea
81      * @return the new RtfHeader
82      * @throws IOException for I/O problems
83      */

84     public RtfHeader startHeader()
85     throws IOException JavaDoc {
86         if (header != null) {
87             throw new RtfStructureException("startHeader called more than once");
88         }
89         header = new RtfHeader(this, writer);
90         listTableContainer = new RtfContainer(this, writer);
91         return header;
92     }
93
94     /**
95      * Creates the list table.
96      * @param attr attributes for the RtfListTable
97      * @return the new RtfListTable
98      * @throws IOException for I/O problems
99      */

100     public RtfListTable startListTable(RtfAttributes attr)
101     throws IOException JavaDoc {
102         listNum++;
103         if (listTable != null) {
104             return listTable;
105         } else {
106             listTable = new RtfListTable(this, writer, new Integer JavaDoc(listNum), attr);
107             listTableContainer.addChild(listTable);
108         }
109
110         return listTable;
111     }
112     
113     /**
114      * Get the list table.
115      * @return the RtfListTable
116      */

117     public RtfListTable getListTable() {
118         return listTable;
119     }
120
121     /**
122      * Closes the RtfHeader if not done yet, and starts the docment area.
123      * Like startDocumentArea, is only called once. This is not optimal,
124      * must be able to have multiple page definition, and corresponding
125      * Document areas
126      * @return the RtfPageArea
127      * @throws IOException for I/O problems
128      * @throws RtfStructureException for illegal RTF structure
129      */

130     public RtfPageArea startPageArea()
131     throws IOException JavaDoc {
132         if (pageArea != null) {
133             throw new RtfStructureException("startPageArea called more than once");
134         }
135         // create an empty header if there was none
136
if (header == null) {
137             startHeader();
138         }
139         header.close();
140         pageArea = new RtfPageArea(this, writer);
141         addChild(pageArea);
142         return pageArea;
143     }
144
145     /**
146      * Call startPageArea if needed and return the page area object.
147      * @return the RtfPageArea
148      * @throws IOException for I/O problems
149      * @throws RtfStructureException for illegal RTF structure
150      */

151     public RtfPageArea getPageArea()
152     throws IOException JavaDoc {
153         if (pageArea == null) {
154             return startPageArea();
155         }
156         return pageArea;
157     }
158
159     /**
160      * Closes the RtfHeader if not done yet, and starts the document area.
161      * Must be called once only.
162      * @return the RtfDocumentArea
163      * @throws IOException for I/O problems
164      * @throws RtfStructureException for illegal RTF structure
165      */

166     public RtfDocumentArea startDocumentArea()
167     throws IOException JavaDoc {
168         if (docArea != null) {
169             throw new RtfStructureException("startDocumentArea called more than once");
170         }
171         // create an empty header if there was none
172
if (header == null) {
173             startHeader();
174         }
175         header.close();
176         docArea = new RtfDocumentArea(this, writer);
177         addChild(docArea);
178         return docArea;
179     }
180
181
182
183     /**
184      * Call startDocumentArea if needed and return the document area object.
185      * @return the RtfDocumentArea
186      * @throws IOException for I/O problems
187      * @throws RtfStructureException for illegal RTF structure
188      */

189     public RtfDocumentArea getDocumentArea()
190     throws IOException JavaDoc {
191         if (docArea == null) {
192             return startDocumentArea();
193         }
194         return docArea;
195     }
196
197     /**
198      * overridden to write RTF prefix code, what comes before our children
199      * @throws IOException for I/O problems
200      */

201     protected void writeRtfPrefix() throws IOException JavaDoc {
202         writeGroupMark(true);
203         writeControlWord("rtf1");
204     }
205
206     /**
207      * overridden to write RTF suffix code, what comes after our children
208      * @throws IOException for I/O problems
209      */

210     protected void writeRtfSuffix() throws IOException JavaDoc {
211         writeGroupMark(false);
212     }
213
214     /**
215      * must be called when done creating the document
216      * @throws IOException for I/O problems
217      */

218     public synchronized void flush() throws IOException JavaDoc {
219         writeRtf();
220         writer.flush();
221     }
222
223     /**
224      * minimal test and usage example
225      * @param args command-line arguments
226      * @throws Exception for problems
227      */

228     public static void main(String JavaDoc[] args)
229     throws Exception JavaDoc {
230         Writer JavaDoc w = null;
231         if (args.length != 0) {
232             final String JavaDoc outFile = args[0];
233             System.err.println("Outputting RTF to file '" + outFile + "'");
234             w = new BufferedWriter JavaDoc(new FileWriter JavaDoc(outFile));
235         } else {
236             System.err.println("Outputting RTF code to standard output");
237             w = new BufferedWriter JavaDoc(new OutputStreamWriter JavaDoc(System.out));
238         }
239
240         final RtfFile f = new RtfFile(w);
241         final RtfSection sect = f.startDocumentArea().newSection();
242
243         final RtfParagraph p = sect.newParagraph();
244         p.newText("Hello, RTF world.\n", null);
245         final RtfAttributes attr = new RtfAttributes();
246         attr.set(RtfText.ATTR_BOLD);
247         attr.set(RtfText.ATTR_ITALIC);
248         attr.set(RtfText.ATTR_FONT_SIZE, 36);
249         p.newText("This is bold, italic, 36 points", attr);
250
251         f.flush();
252         System.err.println("RtfFile test: all done.");
253     }
254 }
255
Popular Tags