KickJava   Java API By Example, From Geeks To Geeks.

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


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: RtfElement.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 java.io.Writer JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.util.Iterator JavaDoc;
32 //import org.apache.fop.render.rtf.rtflib.jfor.main.JForVersionInfo;
33

34 /** Base class for all elements of an RTF file.
35  * @author Bertrand Delacretaz bdelacretaz@codeconsult.ch
36  * @author Andreas Putz a.putz@skynamics.com
37  */

38 public abstract class RtfElement {
39     /** Writer to be used */
40     protected final Writer JavaDoc writer;
41     /** parent element */
42     protected final RtfContainer parent;
43     /** attributes of the element */
44     protected final RtfAttributes attrib;
45     private boolean written;
46     private boolean closed;
47     private final int id;
48     private static int idCounter;
49
50     /** Create an RTF element as a child of given container */
51     RtfElement(RtfContainer parent, Writer JavaDoc w) throws IOException JavaDoc {
52         this(parent, w, null);
53     }
54
55     /** Create an RTF element as a child of given container with given attributes */
56     RtfElement(RtfContainer parent, Writer JavaDoc w, RtfAttributes attr) throws IOException JavaDoc {
57
58         id = idCounter++;
59         this.parent = parent;
60         attrib = (attr != null ? attr : new RtfAttributes());
61         if (this.parent != null) {
62             this.parent.addChild(this);
63         }
64         writer = w;
65         written = false;
66     }
67
68     /**
69      * Does nothing, meant to allow elements to write themselves without waiting
70      * for write(), but not implemented yet
71      * @throws IOException for I/O problems
72      */

73     public final void close() throws IOException JavaDoc {
74         closed = true;
75     }
76
77     /**
78      * Write the RTF code of this element to our Writer
79      * @throws IOException for I/O problems
80      */

81     public final void writeRtf() throws IOException JavaDoc {
82         if (!written) {
83             written = true;
84             if (okToWriteRtf()) {
85                 writeRtfPrefix();
86                 writeRtfContent();
87                 writeRtfSuffix();
88             }
89         }
90     }
91
92     /**
93      * Starts a new line in the RTF file being written. This is only to format
94      * the RTF file itself (for easier debugging), not its content.
95      * @throws IOException in case of an I/O problem
96      */

97     public void newLine() throws IOException JavaDoc {
98         writer.write("\n");
99     }
100     
101     /**
102      * Write an RTF control word to our Writer
103      * @param word RTF control word to write
104      * @throws IOException for I/O problems
105      */

106     protected final void writeControlWord(String JavaDoc word)
107     throws IOException JavaDoc {
108         writer.write('\\');
109         writer.write(word);
110         writer.write(' ');
111     }
112
113     /**
114      * Write an RTF control word to our Writer, preceeded by a star '*'
115      * meaning "ignore this if you don't know what it means"
116      * @param word RTF control word to write
117      * @throws IOException for I/O problems
118      */

119     protected final void writeStarControlWord(String JavaDoc word)
120     throws IOException JavaDoc {
121         writer.write("\\*\\");
122         writer.write(word);
123         writer.write(' ');
124     }
125
126     /**
127      * Same as writeStarControlWord(String word), except with no space behind it
128      * @param word RTF control word to write
129      * @throws IOException for I/O problems
130      */

131     protected final void writeStarControlWordNS(String JavaDoc word)
132     throws IOException JavaDoc {
133         writer.write("\\*\\");
134         writer.write(word);
135     }
136
137     /**
138      * Write rtf control word without the space behind it
139      * @param word RTF control word to write
140      * @throws IOException for I/O problems
141      */

142     protected final void writeControlWordNS(String JavaDoc word)
143     throws IOException JavaDoc {
144         writer.write('\\');
145         writer.write(word);
146     }
147
148     /**
149      * Called before writeRtfContent()
150      * @throws IOException for I/O problems
151      */

152     protected void writeRtfPrefix() throws IOException JavaDoc {
153     }
154
155     /**
156      * Must be implemented to write RTF content to m_writer
157      * @throws IOException for I/O problems
158      */

159     protected abstract void writeRtfContent() throws IOException JavaDoc;
160
161     /**
162      * Called after writeRtfContent()
163      * @throws IOException for I/O problems
164      */

165     protected void writeRtfSuffix() throws IOException JavaDoc {
166     }
167
168     /**
169      * Write a start or end group mark
170      * @param isStart set to true if this is a start mark
171      * @throws IOException for I/O problems
172      */

173     protected final void writeGroupMark(boolean isStart)
174     throws IOException JavaDoc {
175         writer.write(isStart ? "{" : "}");
176     }
177
178     /**
179      * Write given attribute values to our Writer
180      * @param attr RtfAttributes to be written
181      * @param nameList if given, only attribute names from this list are considered
182      * @throws IOException for I/O problems
183      */

184     protected void writeAttributes(RtfAttributes attr, String JavaDoc [] nameList)
185     throws IOException JavaDoc {
186         if (attr == null) {
187             return;
188         }
189
190         if (nameList != null) {
191             // process only given attribute names
192
for (int i = 0; i < nameList.length; i++) {
193                 final String JavaDoc name = nameList[i];
194                 if (attr.isSet(name)) {
195                     writeOneAttribute(name, attr.getValue(name));
196                 }
197             }
198         } else {
199             // process all defined attributes
200
for (Iterator JavaDoc it = attr.nameIterator(); it.hasNext();) {
201                 final String JavaDoc name = (String JavaDoc)it.next();
202                 if (attr.isSet(name)) {
203                     writeOneAttribute(name, attr.getValue(name));
204                 }
205             }
206         }
207     }
208
209     /**
210      * Write one attribute to our Writer
211      * @param name name of attribute to write
212      * @param value value of attribute to be written
213      * @throws IOException for I/O problems
214      */

215     protected void writeOneAttribute(String JavaDoc name, Object JavaDoc value)
216     throws IOException JavaDoc {
217         String JavaDoc cw = name;
218         if (value instanceof Integer JavaDoc) {
219             // attribute has integer value, must write control word + value
220
cw += value;
221         } else if (value instanceof String JavaDoc) {
222             cw += value;
223         } else if (value instanceof RtfAttributes) {
224             writeControlWord(cw);
225             writeAttributes((RtfAttributes) value, null);
226             return;
227         }
228         writeControlWord(cw);
229     }
230
231     /**
232      * Write one attribute to our Writer without a space
233      * @param name name of attribute to write
234      * @param value value of attribute to be written
235      * @throws IOException for I/O problems
236      */

237     protected void writeOneAttributeNS(String JavaDoc name, Object JavaDoc value)
238     throws IOException JavaDoc {
239         String JavaDoc cw = name;
240         if (value instanceof Integer JavaDoc) {
241             // attribute has integer value, must write control word + value
242
cw += value;
243         } else if (value instanceof String JavaDoc) {
244             cw += value;
245         } else if (value instanceof RtfAttributes) {
246             writeControlWord(cw);
247             writeAttributes((RtfAttributes) value, null);
248             return;
249         }
250         writeControlWordNS(cw);
251     }
252
253     /**
254      * can be overridden to suppress all RTF output
255      * @return true if this object can be written into the RTF
256      */

257     protected boolean okToWriteRtf() {
258         return true;
259     }
260
261     /** debugging to given PrintWriter */
262     void dump(Writer JavaDoc w, int indent)
263     throws IOException JavaDoc {
264         for (int i = 0; i < indent; i++) {
265             w.write(' ');
266         }
267         w.write(this.toString());
268         w.write('\n');
269         w.flush();
270     }
271
272     /**
273      * minimal debugging display
274      * @return String representation of object
275      */

276     public String JavaDoc toString() {
277         return (this == null) ? "null" : (this.getClass().getName() + " #" + id);
278     }
279
280     /** true if close() has been called */
281     boolean isClosed() {
282         return closed;
283     }
284
285     /** access our RtfFile, which is always the topmost parent */
286     RtfFile getRtfFile() {
287         // go up the chain of parents until we find the topmost one
288
RtfElement result = this;
289         while (result.parent != null) {
290             result = result.parent;
291         }
292
293         // topmost parent must be an RtfFile
294
// a ClassCastException here would mean that the parent-child structure is not as expected
295
return (RtfFile)result;
296     }
297
298     /** find the first parent where c.isAssignableFrom(parent.getClass()) is true
299      * @return null if not found
300      */

301     RtfElement getParentOfClass(Class JavaDoc c) {
302         RtfElement result = null;
303         RtfElement current = this;
304         while (current.parent != null) {
305             current = current.parent;
306             if (c.isAssignableFrom(current.getClass())) {
307                 result = current;
308                 break;
309             }
310         }
311         return result;
312     }
313
314     /**
315      * @return true if this element would generate no "useful" RTF content
316      */

317     public abstract boolean isEmpty();
318
319     /**
320      * Make a visible entry in the RTF for an exception
321      * @param ie Exception to flag
322      * @throws IOException for I/O problems
323      */

324     protected void writeExceptionInRtf(Exception JavaDoc ie)
325     throws IOException JavaDoc {
326         writeGroupMark(true);
327         writeControlWord("par");
328
329         // make the exception message stand out so that the problem is visible
330
writeControlWord("fs48");
331 // RtfStringConverter.getInstance().writeRtfString(m_writer,
332
// JForVersionInfo.getShortVersionInfo() + ": ");
333
RtfStringConverter.getInstance().writeRtfString(writer, ie.getClass().getName());
334
335         writeControlWord("fs20");
336         RtfStringConverter.getInstance().writeRtfString(writer, " " + ie.toString());
337
338         writeControlWord("par");
339         writeGroupMark(false);
340     }
341
342     /**
343      * Added by Normand Masse
344      * Used for attribute inheritance
345      * @return RtfAttributes
346      */

347     public RtfAttributes getRtfAttributes() {
348         return attrib;
349     }
350 }
Popular Tags