KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > rtf > document > RtfDocumentHeader


1 /*
2  * $Id: RtfDocumentHeader.java 2776 2007-05-23 20:01:40Z hallm $
3  * $Name$
4  *
5  * Copyright 2003, 2004 by Mark Hall
6  *
7  * The contents of this file are subject to the Mozilla Public License Version 1.1
8  * (the "License"); you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the License.
14  *
15  * The Original Code is 'iText, a free JAVA-PDF library'.
16  *
17  * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
18  * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
19  * All Rights Reserved.
20  * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
21  * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
22  *
23  * Contributor(s): all the names of the contributors are added in the source code
24  * where applicable.
25  *
26  * Alternatively, the contents of this file may be used under the terms of the
27  * LGPL license (the ?GNU LIBRARY GENERAL PUBLIC LICENSE?), in which case the
28  * provisions of LGPL are applicable instead of those above. If you wish to
29  * allow use of your version of this file only under the terms of the LGPL
30  * License and not to allow others to use your version of this file under
31  * the MPL, indicate your decision by deleting the provisions above and
32  * replace them with the notice and other provisions required by the LGPL.
33  * If you do not delete the provisions above, a recipient may use your version
34  * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Library General Public License as published by the Free Software Foundation;
39  * either version 2 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
43  * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
44  * details.
45  *
46  * If you didn't download this code from the following link, you should check if
47  * you aren't using an obsolete version:
48  * http://www.lowagie.com/iText/
49  */

50
51 package com.lowagie.text.rtf.document;
52
53 import java.io.ByteArrayOutputStream JavaDoc;
54 import java.io.IOException JavaDoc;
55 import java.io.OutputStream JavaDoc;
56
57 import com.lowagie.text.HeaderFooter;
58 import com.lowagie.text.rtf.RtfElement;
59 import com.lowagie.text.rtf.document.output.RtfNilOutputStream;
60 import com.lowagie.text.rtf.headerfooter.RtfHeaderFooter;
61 import com.lowagie.text.rtf.headerfooter.RtfHeaderFooterGroup;
62 import com.lowagie.text.rtf.list.RtfList;
63 import com.lowagie.text.rtf.list.RtfListTable;
64 import com.lowagie.text.rtf.style.RtfColor;
65 import com.lowagie.text.rtf.style.RtfColorList;
66 import com.lowagie.text.rtf.style.RtfFont;
67 import com.lowagie.text.rtf.style.RtfFontList;
68 import com.lowagie.text.rtf.style.RtfParagraphStyle;
69 import com.lowagie.text.rtf.style.RtfStylesheetList;
70
71
72 /**
73  * The RtfDocumentHeader contains all classes required for the generation of
74  * the document header area.
75  *
76  * @version $Id: RtfDocumentHeader.java 2776 2007-05-23 20:01:40Z hallm $
77  * @author Mark Hall (mhall@edu.uni-klu.ac.at)
78  * @author Thomas Bickel (tmb99@inode.at)
79  */

80 public class RtfDocumentHeader extends RtfElement {
81     /**
82      * Constant for the title page
83      */

84     private static final byte[] TITLE_PAGE = "\\titlepg".getBytes();
85     /**
86      * Constant for facing pages
87      */

88     private static final byte[] FACING_PAGES = "\\facingp".getBytes();
89     
90     /**
91      * The code page to use
92      */

93     private RtfCodePage codePage = null;
94     /**
95      * Stores all the colors used in the document
96      */

97     private RtfColorList colorList = null;
98     /**
99      * Stores all the fonts used in the document
100      */

101     private RtfFontList fontList = null;
102     /**
103      * Manages List tables
104      */

105     private RtfListTable listTable = null;
106     /**
107      * Stores all paragraph styles used in the document.
108      */

109     private RtfStylesheetList stylesheetList = null;
110     /**
111      * The information group with author/subject/keywords/title/producer/creationdate data
112      */

113     private RtfInfoGroup infoGroup = null;
114     /**
115      * The page settings
116      */

117     private RtfPageSetting pageSetting = null;
118     /**
119      * The current RtfHeaderFooterGroup for the header
120      */

121     private HeaderFooter header = null;
122     /**
123      * The current RtfHeaderFooterGroup for the footer
124      */

125     private HeaderFooter footer = null;
126
127     /**
128      * Constructs a RtfDocumentHeader for a RtfDocument
129      *
130      * @param doc The RtfDocument this RtfDocumentHeader belongs to
131      */

132     protected RtfDocumentHeader(RtfDocument doc) {
133         super(doc);
134     }
135
136     /**
137      * Initialises the RtfDocumentHeader.
138      */

139     protected void init() {
140         this.codePage = new RtfCodePage(this.document);
141         this.colorList = new RtfColorList(this.document);
142         this.fontList = new RtfFontList(this.document);
143         this.listTable = new RtfListTable(this.document);
144         this.stylesheetList = new RtfStylesheetList(this.document);
145         this.infoGroup = new RtfInfoGroup(this.document);
146         this.pageSetting = new RtfPageSetting(this.document);
147         this.header = new RtfHeaderFooterGroup(this.document, RtfHeaderFooter.TYPE_HEADER);
148         this.footer = new RtfHeaderFooterGroup(this.document, RtfHeaderFooter.TYPE_FOOTER);
149     }
150     
151     /**
152      * Write the contents of the document header area.
153      *
154      * @return A byte array with the contents of the document header area
155      * @deprecated replaced by {@link #writeContent(OutputStream)}
156      */

157     public byte[] write() {
158         ByteArrayOutputStream JavaDoc result = new ByteArrayOutputStream JavaDoc();
159         try {
160             writeContent(result);
161         } catch(IOException JavaDoc ioe) {
162             ioe.printStackTrace();
163         }
164         return result.toByteArray();
165     }
166     /**
167      * Writes the element content to the given output stream.
168      */

169     public void writeContent(final OutputStream JavaDoc result) throws IOException JavaDoc
170     {
171         try {
172             // This is so that all colour, font and similar information is processed once, before
173
// the header section is written.
174
//.writeSectionDefinition();
175
writeSectionDefinition(new RtfNilOutputStream());
176             
177 // result.write(this.codePage.writeDefinition());
178
// result.write(this.fontList.writeDefinition());
179
// result.write(this.colorList.writeDefinition());
180
// result.write(this.stylesheetList.writeDefinition());
181
// result.write(this.listTable.writeDefinition());
182
// result.write(this.infoGroup.write());
183
// result.write(this.pageSetting.writeDefinition());
184

185             this.codePage.writeDefinition(result);
186             this.fontList.writeDefinition(result);
187             this.colorList.writeDefinition(result);
188             this.stylesheetList.writeDefinition(result);
189             this.listTable.writeDefinition(result);
190             this.infoGroup.writeContent(result);
191             this.pageSetting.writeDefinition(result);
192             
193             //.result.write(writeSectionDefinition());
194
writeSectionDefinition(result);
195         } catch(IOException JavaDoc ioe) {
196             ioe.printStackTrace();
197         }
198     }
199
200     /**
201      * Writes the section definition data
202      *
203      * @return A byte array with the section definition data
204      * @deprecated
205      */

206     public byte[] writeSectionDefinition()
207     {
208         ByteArrayOutputStream JavaDoc result = new ByteArrayOutputStream JavaDoc();
209         writeSectionDefinition(result);
210         return result.toByteArray();
211     }
212     /**
213      * Writes the section definition data
214      * @param result
215      */

216     public void writeSectionDefinition(final OutputStream JavaDoc result)
217     {
218         try {
219             RtfHeaderFooterGroup header = convertHeaderFooter(this.header, RtfHeaderFooter.TYPE_HEADER);
220             RtfHeaderFooterGroup footer = convertHeaderFooter(this.footer, RtfHeaderFooter.TYPE_FOOTER);
221             if(header.hasTitlePage() || footer.hasTitlePage()) {
222                 result.write(TITLE_PAGE);
223                 header.setHasTitlePage();
224                 footer.setHasTitlePage();
225             }
226             if(header.hasFacingPages() || footer.hasFacingPages()) {
227                 result.write(FACING_PAGES);
228                 header.setHasFacingPages();
229                 footer.setHasFacingPages();
230             }
231             //.result.write(footer.write());
232
footer.writeContent(result);
233             //.result.write(header.write());
234
header.writeContent(result);
235             //.result.write(pageSetting.writeSectionDefinition());
236
pageSetting.writeSectionDefinition(result);
237         } catch(IOException JavaDoc ioe) {
238             ioe.printStackTrace();
239         }
240     }
241     
242     /**
243      * Gets the number of the specified RtfFont
244      *
245      * @param font The RtfFont for which to get the number
246      * @return The number of the font
247      */

248     public int getFontNumber(RtfFont font) {
249         return this.fontList.getFontNumber(font);
250     }
251
252     /**
253      * Gets the number of the specified RtfColor
254      *
255      * @param color The RtfColor for which to get the number
256      * @return The number of the color
257      */

258     public int getColorNumber(RtfColor color) {
259         return this.colorList.getColorNumber(color);
260     }
261     
262     /**
263      * Gets the number of the specified RtfList
264      *
265      * @param list The RtfList for which to get the number
266      * @return The number of the list
267      */

268     public int getListNumber(RtfList list) {
269         return this.listTable.getListNumber(list);
270     }
271     
272     /**
273      * Gets the RtfParagraphStyle with the given style name.
274      *
275      * @param styleName The style name of the RtfParagraphStyle to get.
276      * @return The RtfParagraphStyle with the given style name or null.
277      */

278     public RtfParagraphStyle getRtfParagraphStyle(String JavaDoc styleName) {
279         return this.stylesheetList.getRtfParagraphStyle(styleName);
280     }
281     
282     /**
283      * Removes a RtfList from the list table
284      *
285      * @param list The RtfList to remove
286      */

287     public void freeListNumber(RtfList list) {
288         this.listTable.freeListNumber(list);
289     }
290     
291     /**
292      * Gets the RtfPageSetting object of this RtfDocument
293      *
294      * @return The RtfPageSetting object
295      */

296     public RtfPageSetting getPageSetting() {
297         return this.pageSetting;
298     }
299     
300     /**
301      * Adds an RtfInfoElement to the list of RtfInfoElements
302      *
303      * @param rtfInfoElement The RtfInfoElement to add
304      */

305     public void addInfoElement(RtfInfoElement rtfInfoElement) {
306         this.infoGroup.add(rtfInfoElement);
307     }
308     
309     /**
310      * Sets the current header to use
311      *
312      * @param header The HeaderFooter to use as header
313      */

314     public void setHeader(HeaderFooter header) {
315         this.header = header;
316     }
317     
318     /**
319      * Sets the current footer to use
320      *
321      * @param footer The HeaderFooter to use as footer
322      */

323     public void setFooter(HeaderFooter footer) {
324         this.footer = footer;
325     }
326     
327     /**
328      * Registers the RtfParagraphStyle for further use in the document.
329      *
330      * @param rtfParagraphStyle The RtfParagraphStyle to register.
331      */

332     public void registerParagraphStyle(RtfParagraphStyle rtfParagraphStyle) {
333         this.stylesheetList.registerParagraphStyle(rtfParagraphStyle);
334     }
335     
336     /**
337      * Converts a HeaderFooter into a RtfHeaderFooterGroup. Depending on which class
338      * the HeaderFooter is, the correct RtfHeaderFooterGroup is created.
339      *
340      * @param hf The HeaderFooter to convert.
341      * @param type Whether the conversion is being done on a footer or header
342      * @return The converted RtfHeaderFooterGroup.
343      * @see com.lowagie.text.rtf.headerfooter.RtfHeaderFooter
344      * @see com.lowagie.text.rtf.headerfooter.RtfHeaderFooterGroup
345      */

346     private RtfHeaderFooterGroup convertHeaderFooter(HeaderFooter hf, int type) {
347         if(hf != null) {
348             if(hf instanceof RtfHeaderFooterGroup) {
349                 return new RtfHeaderFooterGroup(this.document, (RtfHeaderFooterGroup) hf, type);
350             } else if(hf instanceof RtfHeaderFooter) {
351                 return new RtfHeaderFooterGroup(this.document, (RtfHeaderFooter) hf, type);
352             } else {
353                 return new RtfHeaderFooterGroup(this.document, hf, type);
354             }
355         } else {
356             return new RtfHeaderFooterGroup(this.document, type);
357         }
358     }
359 }
360
Popular Tags