KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > rtf > field > RtfTOCEntry


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

52
53 package com.lowagie.text.rtf.field;
54
55 import java.io.ByteArrayOutputStream JavaDoc;
56 import java.io.IOException JavaDoc;
57 import java.io.OutputStream JavaDoc;
58
59 import com.lowagie.text.Font;
60
61
62 /**
63  * The RtfTOCEntry is used together with the RtfTableOfContents to generate a table of
64  * contents. Add the RtfTOCEntry in those locations in the document where table of
65  * contents entries should link to
66  *
67  * @version $Id: RtfTOCEntry.java 2784 2007-05-24 15:43:40Z hallm $
68  * @author Mark Hall (mhall@edu.uni-klu.ac.at)
69  * @author Steffen.Stundzig (Steffen.Stundzig@smb-tec.com)
70  * @author Thomas Bickel (tmb99@inode.at)
71  */

72 public class RtfTOCEntry extends RtfField {
73
74     /**
75      * Constant for the beginning of hidden text
76      */

77     private static final byte[] TEXT_HIDDEN_ON = "\\v".getBytes();
78     /**
79      * Constant for the end of hidden text
80      */

81     private static final byte[] TEXT_HIDDEN_OFF = "\\v0".getBytes();
82     /**
83      * Constant for a TOC entry with page numbers
84      */

85     private static final byte[] TOC_ENTRY_PAGE_NUMBER = "\\tc".getBytes();
86     /**
87      * Constant for a TOC entry without page numbers
88      */

89     private static final byte[] TOC_ENTRY_NO_PAGE_NUMBER = "\\tcn".getBytes();
90     
91     /**
92      * The entry text of this RtfTOCEntry
93      */

94     private String JavaDoc entry = "";
95     /**
96      * Whether to show page numbers in the table of contents
97      */

98     private boolean showPageNumber = true;
99     
100     /**
101      * Constructs a RtfTOCEntry with a certain entry text.
102      *
103      * @param entry The entry text to display
104      */

105     public RtfTOCEntry(String JavaDoc entry) {
106         super(null, new Font());
107         if(entry != null) {
108             this.entry = entry;
109         }
110     }
111     
112     /**
113      * Writes the content of the RtfTOCEntry
114      *
115      * @return A byte array with the contents of the RtfTOCEntry
116      * @deprecated replaced by {@link #writeContent(OutputStream)}
117      */

118     public byte[] write() {
119         ByteArrayOutputStream JavaDoc result = new ByteArrayOutputStream JavaDoc();
120         try {
121             writeContent(result);
122         } catch(IOException JavaDoc ioe) {
123             ioe.printStackTrace();
124         }
125         return result.toByteArray();
126     }
127     /**
128      * Writes the content of the RtfTOCEntry
129      */

130     public void writeContent(final OutputStream JavaDoc result) throws IOException JavaDoc
131     {
132         result.write(TEXT_HIDDEN_ON);
133         result.write(OPEN_GROUP);
134         if(this.showPageNumber) {
135             result.write(TOC_ENTRY_PAGE_NUMBER);
136         } else {
137             result.write(TOC_ENTRY_NO_PAGE_NUMBER);
138         }
139         result.write(DELIMITER);
140         //.result.write(this.document.filterSpecialChar(this.entry, true, false).getBytes());
141
this.document.filterSpecialChar(result, this.entry, true, false);
142         result.write(CLOSE_GROUP);
143         result.write(TEXT_HIDDEN_OFF);
144     }
145     
146     /**
147      * Sets whether to display a page number in the table of contents, or not
148      *
149      * @param showPageNumber Whether to display a page number or not
150      */

151     public void setShowPageNumber(boolean showPageNumber) {
152         this.showPageNumber = showPageNumber;
153     }
154     
155     /**
156      * UNUSED
157      * @return null
158      * @deprecated
159      * @throws IOException never thrown
160      */

161     protected byte[] writeFieldInstContent() throws IOException JavaDoc
162     {
163         return null;
164     }
165     /**
166      * unused
167      */

168     protected void writeFieldInstContent(OutputStream JavaDoc out) throws IOException JavaDoc
169     {
170     }
171
172     /**
173      * UNUSED
174      * @return null
175      * @deprecated
176      * @throws IOException never thrown
177      */

178     protected byte[] writeFieldResultContent() throws IOException JavaDoc {
179         return null;
180     }
181     
182     /*
183      * unused
184      * @see com.lowagie.text.rtf.field.RtfField#writeFieldResultContent(java.io.OutputStream)
185      */

186     protected void writeFieldResultContent(OutputStream JavaDoc out) throws IOException JavaDoc
187     {
188     }
189
190 }
191
Popular Tags