001: /*
002: * $Id: RtfDocumentHeader.java 2776 2007-05-23 20:01:40Z hallm $
003: * $Name$
004: *
005: * Copyright 2003, 2004 by Mark Hall
006: *
007: * The contents of this file are subject to the Mozilla Public License Version 1.1
008: * (the "License"); you may not use this file except in compliance with the License.
009: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
010: *
011: * Software distributed under the License is distributed on an "AS IS" basis,
012: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
013: * for the specific language governing rights and limitations under the License.
014: *
015: * The Original Code is 'iText, a free JAVA-PDF library'.
016: *
017: * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
018: * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
019: * All Rights Reserved.
020: * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
021: * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
022: *
023: * Contributor(s): all the names of the contributors are added in the source code
024: * where applicable.
025: *
026: * Alternatively, the contents of this file may be used under the terms of the
027: * LGPL license (the ?GNU LIBRARY GENERAL PUBLIC LICENSE?), in which case the
028: * provisions of LGPL are applicable instead of those above. If you wish to
029: * allow use of your version of this file only under the terms of the LGPL
030: * License and not to allow others to use your version of this file under
031: * the MPL, indicate your decision by deleting the provisions above and
032: * replace them with the notice and other provisions required by the LGPL.
033: * If you do not delete the provisions above, a recipient may use your version
034: * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
035: *
036: * This library is free software; you can redistribute it and/or modify it
037: * under the terms of the MPL as stated above or under the terms of the GNU
038: * Library General Public License as published by the Free Software Foundation;
039: * either version 2 of the License, or any later version.
040: *
041: * This library is distributed in the hope that it will be useful, but WITHOUT
042: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
043: * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
044: * details.
045: *
046: * If you didn't download this code from the following link, you should check if
047: * you aren't using an obsolete version:
048: * http://www.lowagie.com/iText/
049: */
050:
051: package com.lowagie.text.rtf.document;
052:
053: import java.io.ByteArrayOutputStream;
054: import java.io.IOException;
055: import java.io.OutputStream;
056:
057: import com.lowagie.text.HeaderFooter;
058: import com.lowagie.text.rtf.RtfElement;
059: import com.lowagie.text.rtf.document.output.RtfNilOutputStream;
060: import com.lowagie.text.rtf.headerfooter.RtfHeaderFooter;
061: import com.lowagie.text.rtf.headerfooter.RtfHeaderFooterGroup;
062: import com.lowagie.text.rtf.list.RtfList;
063: import com.lowagie.text.rtf.list.RtfListTable;
064: import com.lowagie.text.rtf.style.RtfColor;
065: import com.lowagie.text.rtf.style.RtfColorList;
066: import com.lowagie.text.rtf.style.RtfFont;
067: import com.lowagie.text.rtf.style.RtfFontList;
068: import com.lowagie.text.rtf.style.RtfParagraphStyle;
069: import com.lowagie.text.rtf.style.RtfStylesheetList;
070:
071: /**
072: * The RtfDocumentHeader contains all classes required for the generation of
073: * the document header area.
074: *
075: * @version $Id: RtfDocumentHeader.java 2776 2007-05-23 20:01:40Z hallm $
076: * @author Mark Hall (mhall@edu.uni-klu.ac.at)
077: * @author Thomas Bickel (tmb99@inode.at)
078: */
079: public class RtfDocumentHeader extends RtfElement {
080: /**
081: * Constant for the title page
082: */
083: private static final byte[] TITLE_PAGE = "\\titlepg".getBytes();
084: /**
085: * Constant for facing pages
086: */
087: private static final byte[] FACING_PAGES = "\\facingp".getBytes();
088:
089: /**
090: * The code page to use
091: */
092: private RtfCodePage codePage = null;
093: /**
094: * Stores all the colors used in the document
095: */
096: private RtfColorList colorList = null;
097: /**
098: * Stores all the fonts used in the document
099: */
100: private RtfFontList fontList = null;
101: /**
102: * Manages List tables
103: */
104: private RtfListTable listTable = null;
105: /**
106: * Stores all paragraph styles used in the document.
107: */
108: private RtfStylesheetList stylesheetList = null;
109: /**
110: * The information group with author/subject/keywords/title/producer/creationdate data
111: */
112: private RtfInfoGroup infoGroup = null;
113: /**
114: * The page settings
115: */
116: private RtfPageSetting pageSetting = null;
117: /**
118: * The current RtfHeaderFooterGroup for the header
119: */
120: private HeaderFooter header = null;
121: /**
122: * The current RtfHeaderFooterGroup for the footer
123: */
124: private HeaderFooter footer = null;
125:
126: /**
127: * Constructs a RtfDocumentHeader for a RtfDocument
128: *
129: * @param doc The RtfDocument this RtfDocumentHeader belongs to
130: */
131: protected RtfDocumentHeader(RtfDocument doc) {
132: super (doc);
133: }
134:
135: /**
136: * Initialises the RtfDocumentHeader.
137: */
138: protected void init() {
139: this .codePage = new RtfCodePage(this .document);
140: this .colorList = new RtfColorList(this .document);
141: this .fontList = new RtfFontList(this .document);
142: this .listTable = new RtfListTable(this .document);
143: this .stylesheetList = new RtfStylesheetList(this .document);
144: this .infoGroup = new RtfInfoGroup(this .document);
145: this .pageSetting = new RtfPageSetting(this .document);
146: this .header = new RtfHeaderFooterGroup(this .document,
147: RtfHeaderFooter.TYPE_HEADER);
148: this .footer = new RtfHeaderFooterGroup(this .document,
149: RtfHeaderFooter.TYPE_FOOTER);
150: }
151:
152: /**
153: * Write the contents of the document header area.
154: *
155: * @return A byte array with the contents of the document header area
156: * @deprecated replaced by {@link #writeContent(OutputStream)}
157: */
158: public byte[] write() {
159: ByteArrayOutputStream result = new ByteArrayOutputStream();
160: try {
161: writeContent(result);
162: } catch (IOException ioe) {
163: ioe.printStackTrace();
164: }
165: return result.toByteArray();
166: }
167:
168: /**
169: * Writes the element content to the given output stream.
170: */
171: public void writeContent(final OutputStream result)
172: throws IOException {
173: try {
174: // This is so that all colour, font and similar information is processed once, before
175: // the header section is written.
176: //.writeSectionDefinition();
177: writeSectionDefinition(new RtfNilOutputStream());
178:
179: // result.write(this.codePage.writeDefinition());
180: // result.write(this.fontList.writeDefinition());
181: // result.write(this.colorList.writeDefinition());
182: // result.write(this.stylesheetList.writeDefinition());
183: // result.write(this.listTable.writeDefinition());
184: // result.write(this.infoGroup.write());
185: // result.write(this.pageSetting.writeDefinition());
186:
187: this .codePage.writeDefinition(result);
188: this .fontList.writeDefinition(result);
189: this .colorList.writeDefinition(result);
190: this .stylesheetList.writeDefinition(result);
191: this .listTable.writeDefinition(result);
192: this .infoGroup.writeContent(result);
193: this .pageSetting.writeDefinition(result);
194:
195: //.result.write(writeSectionDefinition());
196: writeSectionDefinition(result);
197: } catch (IOException ioe) {
198: ioe.printStackTrace();
199: }
200: }
201:
202: /**
203: * Writes the section definition data
204: *
205: * @return A byte array with the section definition data
206: * @deprecated
207: */
208: public byte[] writeSectionDefinition() {
209: ByteArrayOutputStream result = new ByteArrayOutputStream();
210: writeSectionDefinition(result);
211: return result.toByteArray();
212: }
213:
214: /**
215: * Writes the section definition data
216: * @param result
217: */
218: public void writeSectionDefinition(final OutputStream result) {
219: try {
220: RtfHeaderFooterGroup header = convertHeaderFooter(
221: this .header, RtfHeaderFooter.TYPE_HEADER);
222: RtfHeaderFooterGroup footer = convertHeaderFooter(
223: this .footer, RtfHeaderFooter.TYPE_FOOTER);
224: if (header.hasTitlePage() || footer.hasTitlePage()) {
225: result.write(TITLE_PAGE);
226: header.setHasTitlePage();
227: footer.setHasTitlePage();
228: }
229: if (header.hasFacingPages() || footer.hasFacingPages()) {
230: result.write(FACING_PAGES);
231: header.setHasFacingPages();
232: footer.setHasFacingPages();
233: }
234: //.result.write(footer.write());
235: footer.writeContent(result);
236: //.result.write(header.write());
237: header.writeContent(result);
238: //.result.write(pageSetting.writeSectionDefinition());
239: pageSetting.writeSectionDefinition(result);
240: } catch (IOException ioe) {
241: ioe.printStackTrace();
242: }
243: }
244:
245: /**
246: * Gets the number of the specified RtfFont
247: *
248: * @param font The RtfFont for which to get the number
249: * @return The number of the font
250: */
251: public int getFontNumber(RtfFont font) {
252: return this .fontList.getFontNumber(font);
253: }
254:
255: /**
256: * Gets the number of the specified RtfColor
257: *
258: * @param color The RtfColor for which to get the number
259: * @return The number of the color
260: */
261: public int getColorNumber(RtfColor color) {
262: return this .colorList.getColorNumber(color);
263: }
264:
265: /**
266: * Gets the number of the specified RtfList
267: *
268: * @param list The RtfList for which to get the number
269: * @return The number of the list
270: */
271: public int getListNumber(RtfList list) {
272: return this .listTable.getListNumber(list);
273: }
274:
275: /**
276: * Gets the RtfParagraphStyle with the given style name.
277: *
278: * @param styleName The style name of the RtfParagraphStyle to get.
279: * @return The RtfParagraphStyle with the given style name or null.
280: */
281: public RtfParagraphStyle getRtfParagraphStyle(String styleName) {
282: return this .stylesheetList.getRtfParagraphStyle(styleName);
283: }
284:
285: /**
286: * Removes a RtfList from the list table
287: *
288: * @param list The RtfList to remove
289: */
290: public void freeListNumber(RtfList list) {
291: this .listTable.freeListNumber(list);
292: }
293:
294: /**
295: * Gets the RtfPageSetting object of this RtfDocument
296: *
297: * @return The RtfPageSetting object
298: */
299: public RtfPageSetting getPageSetting() {
300: return this .pageSetting;
301: }
302:
303: /**
304: * Adds an RtfInfoElement to the list of RtfInfoElements
305: *
306: * @param rtfInfoElement The RtfInfoElement to add
307: */
308: public void addInfoElement(RtfInfoElement rtfInfoElement) {
309: this .infoGroup.add(rtfInfoElement);
310: }
311:
312: /**
313: * Sets the current header to use
314: *
315: * @param header The HeaderFooter to use as header
316: */
317: public void setHeader(HeaderFooter header) {
318: this .header = header;
319: }
320:
321: /**
322: * Sets the current footer to use
323: *
324: * @param footer The HeaderFooter to use as footer
325: */
326: public void setFooter(HeaderFooter footer) {
327: this .footer = footer;
328: }
329:
330: /**
331: * Registers the RtfParagraphStyle for further use in the document.
332: *
333: * @param rtfParagraphStyle The RtfParagraphStyle to register.
334: */
335: public void registerParagraphStyle(
336: RtfParagraphStyle rtfParagraphStyle) {
337: this .stylesheetList.registerParagraphStyle(rtfParagraphStyle);
338: }
339:
340: /**
341: * Converts a HeaderFooter into a RtfHeaderFooterGroup. Depending on which class
342: * the HeaderFooter is, the correct RtfHeaderFooterGroup is created.
343: *
344: * @param hf The HeaderFooter to convert.
345: * @param type Whether the conversion is being done on a footer or header
346: * @return The converted RtfHeaderFooterGroup.
347: * @see com.lowagie.text.rtf.headerfooter.RtfHeaderFooter
348: * @see com.lowagie.text.rtf.headerfooter.RtfHeaderFooterGroup
349: */
350: private RtfHeaderFooterGroup convertHeaderFooter(HeaderFooter hf,
351: int type) {
352: if (hf != null) {
353: if (hf instanceof RtfHeaderFooterGroup) {
354: return new RtfHeaderFooterGroup(this .document,
355: (RtfHeaderFooterGroup) hf, type);
356: } else if (hf instanceof RtfHeaderFooter) {
357: return new RtfHeaderFooterGroup(this .document,
358: (RtfHeaderFooter) hf, type);
359: } else {
360: return new RtfHeaderFooterGroup(this .document, hf, type);
361: }
362: } else {
363: return new RtfHeaderFooterGroup(this.document, type);
364: }
365: }
366: }
|