001: /*
002: * $Id: RtfTOCEntry.java 2784 2007-05-24 15:43:40Z hallm $
003: * $Name$
004: *
005: * Copyright 2004 by Mark Hall
006: * Uses code Copyright 2002
007: * Steffen.Stundzig (Steffen.Stundzig@smb-tec.com)
008: *
009: * The contents of this file are subject to the Mozilla Public License Version 1.1
010: * (the "License"); you may not use this file except in compliance with the License.
011: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
012: *
013: * Software distributed under the License is distributed on an "AS IS" basis,
014: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
015: * for the specific language governing rights and limitations under the License.
016: *
017: * The Original Code is 'iText, a free JAVA-PDF library'.
018: *
019: * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
020: * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
021: * All Rights Reserved.
022: * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
023: * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
024: *
025: * Contributor(s): all the names of the contributors are added in the source code
026: * where applicable.
027: *
028: * Alternatively, the contents of this file may be used under the terms of the
029: * LGPL license (the ?GNU LIBRARY GENERAL PUBLIC LICENSE?), in which case the
030: * provisions of LGPL are applicable instead of those above. If you wish to
031: * allow use of your version of this file only under the terms of the LGPL
032: * License and not to allow others to use your version of this file under
033: * the MPL, indicate your decision by deleting the provisions above and
034: * replace them with the notice and other provisions required by the LGPL.
035: * If you do not delete the provisions above, a recipient may use your version
036: * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
037: *
038: * This library is free software; you can redistribute it and/or modify it
039: * under the terms of the MPL as stated above or under the terms of the GNU
040: * Library General Public License as published by the Free Software Foundation;
041: * either version 2 of the License, or any later version.
042: *
043: * This library is distributed in the hope that it will be useful, but WITHOUT
044: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
045: * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
046: * details.
047: *
048: * If you didn't download this code from the following link, you should check if
049: * you aren't using an obsolete version:
050: * http://www.lowagie.com/iText/
051: */
052:
053: package com.lowagie.text.rtf.field;
054:
055: import java.io.ByteArrayOutputStream;
056: import java.io.IOException;
057: import java.io.OutputStream;
058:
059: import com.lowagie.text.Font;
060:
061: /**
062: * The RtfTOCEntry is used together with the RtfTableOfContents to generate a table of
063: * contents. Add the RtfTOCEntry in those locations in the document where table of
064: * contents entries should link to
065: *
066: * @version $Id: RtfTOCEntry.java 2784 2007-05-24 15:43:40Z hallm $
067: * @author Mark Hall (mhall@edu.uni-klu.ac.at)
068: * @author Steffen.Stundzig (Steffen.Stundzig@smb-tec.com)
069: * @author Thomas Bickel (tmb99@inode.at)
070: */
071: public class RtfTOCEntry extends RtfField {
072:
073: /**
074: * Constant for the beginning of hidden text
075: */
076: private static final byte[] TEXT_HIDDEN_ON = "\\v".getBytes();
077: /**
078: * Constant for the end of hidden text
079: */
080: private static final byte[] TEXT_HIDDEN_OFF = "\\v0".getBytes();
081: /**
082: * Constant for a TOC entry with page numbers
083: */
084: private static final byte[] TOC_ENTRY_PAGE_NUMBER = "\\tc"
085: .getBytes();
086: /**
087: * Constant for a TOC entry without page numbers
088: */
089: private static final byte[] TOC_ENTRY_NO_PAGE_NUMBER = "\\tcn"
090: .getBytes();
091:
092: /**
093: * The entry text of this RtfTOCEntry
094: */
095: private String entry = "";
096: /**
097: * Whether to show page numbers in the table of contents
098: */
099: private boolean showPageNumber = true;
100:
101: /**
102: * Constructs a RtfTOCEntry with a certain entry text.
103: *
104: * @param entry The entry text to display
105: */
106: public RtfTOCEntry(String entry) {
107: super (null, new Font());
108: if (entry != null) {
109: this .entry = entry;
110: }
111: }
112:
113: /**
114: * Writes the content of the RtfTOCEntry
115: *
116: * @return A byte array with the contents of the RtfTOCEntry
117: * @deprecated replaced by {@link #writeContent(OutputStream)}
118: */
119: public byte[] write() {
120: ByteArrayOutputStream result = new ByteArrayOutputStream();
121: try {
122: writeContent(result);
123: } catch (IOException ioe) {
124: ioe.printStackTrace();
125: }
126: return result.toByteArray();
127: }
128:
129: /**
130: * Writes the content of the RtfTOCEntry
131: */
132: public void writeContent(final OutputStream result)
133: throws IOException {
134: result.write(TEXT_HIDDEN_ON);
135: result.write(OPEN_GROUP);
136: if (this .showPageNumber) {
137: result.write(TOC_ENTRY_PAGE_NUMBER);
138: } else {
139: result.write(TOC_ENTRY_NO_PAGE_NUMBER);
140: }
141: result.write(DELIMITER);
142: //.result.write(this.document.filterSpecialChar(this.entry, true, false).getBytes());
143: this .document
144: .filterSpecialChar(result, this .entry, true, false);
145: result.write(CLOSE_GROUP);
146: result.write(TEXT_HIDDEN_OFF);
147: }
148:
149: /**
150: * Sets whether to display a page number in the table of contents, or not
151: *
152: * @param showPageNumber Whether to display a page number or not
153: */
154: public void setShowPageNumber(boolean showPageNumber) {
155: this .showPageNumber = showPageNumber;
156: }
157:
158: /**
159: * UNUSED
160: * @return null
161: * @deprecated
162: * @throws IOException never thrown
163: */
164: protected byte[] writeFieldInstContent() throws IOException {
165: return null;
166: }
167:
168: /**
169: * unused
170: */
171: protected void writeFieldInstContent(OutputStream out)
172: throws IOException {
173: }
174:
175: /**
176: * UNUSED
177: * @return null
178: * @deprecated
179: * @throws IOException never thrown
180: */
181: protected byte[] writeFieldResultContent() throws IOException {
182: return null;
183: }
184:
185: /*
186: * unused
187: * @see com.lowagie.text.rtf.field.RtfField#writeFieldResultContent(java.io.OutputStream)
188: */
189: protected void writeFieldResultContent(OutputStream out)
190: throws IOException {
191: }
192:
193: }
|