001: /*
002: * $Id: RtfListTable.java 2776 2007-05-23 20:01:40Z hallm $
003: * $Name$
004: *
005: * Copyright 2001, 2002, 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.list;
052:
053: import java.io.ByteArrayOutputStream;
054: import java.io.IOException;
055: import java.io.OutputStream;
056: import java.util.ArrayList;
057:
058: import com.lowagie.text.rtf.RtfElement;
059: import com.lowagie.text.rtf.RtfExtendedElement;
060: import com.lowagie.text.rtf.document.RtfDocument;
061:
062: /**
063: * The RtfListTable manages all RtfLists in one RtfDocument. It also generates
064: * the list and list override tables in the document header.
065: *
066: * @version $Id: RtfListTable.java 2776 2007-05-23 20:01:40Z hallm $
067: * @author Mark Hall (mhall@edu.uni-klu.ac.at)
068: * @author Thomas Bickel (tmb99@inode.at)
069: */
070: public class RtfListTable extends RtfElement implements
071: RtfExtendedElement {
072:
073: /**
074: * Constant for the list number
075: */
076: protected static final byte[] LIST_NUMBER = "\\ls".getBytes();
077: /**
078: * Constant for the list table
079: */
080: private static final byte[] LIST_TABLE = "\\*\\listtable"
081: .getBytes();
082: /**
083: * Constant for the list
084: */
085: private static final byte[] LIST = "\\list".getBytes();
086: /**
087: * Constant for the list template id
088: */
089: private static final byte[] LIST_TEMPLATE_ID = "\\listtemplateid"
090: .getBytes();
091: /**
092: * Constant for the hybrid list
093: */
094: private static final byte[] LIST_HYBRID = "\\hybrid".getBytes();
095: /**
096: * Constant for the list id
097: */
098: private static final byte[] LIST_ID = "\\listid".getBytes();
099: /**
100: * Constant for the list override table
101: */
102: private static final byte[] LIST_OVERRIDE_TABLE = "\\*\\listoverridetable"
103: .getBytes();
104: /**
105: * Constant for the list override
106: */
107: private static final byte[] LIST_OVERRIDE = "\\listoverride"
108: .getBytes();
109: /**
110: * Constant for the list override count
111: */
112: private static final byte[] LIST_OVERRIDE_COUNT = "\\listoverridecount"
113: .getBytes();
114:
115: /**
116: * The RtfLists managed by this RtfListTable
117: */
118: private ArrayList lists;
119:
120: /**
121: * Constructs a RtfListTable for a RtfDocument
122: *
123: * @param doc The RtfDocument this RtfListTable belongs to
124: */
125: public RtfListTable(RtfDocument doc) {
126: super (doc);
127:
128: this .lists = new ArrayList();
129: }
130:
131: /**
132: * unused
133: * @deprecated replaced by {@link #writeContent(OutputStream)}
134: */
135: public byte[] write() {
136: return (new byte[0]);
137: }
138:
139: /**
140: * unused
141: */
142: public void writeContent(final OutputStream out) throws IOException {
143: }
144:
145: /**
146: * Writes the list and list override tables.
147: *
148: * @return A byte array with the list and list override tables.
149: * @deprecated replaced by {@link #writeDefinition(OutputStream)}
150: */
151: public byte[] writeDefinition() {
152: ByteArrayOutputStream result = new ByteArrayOutputStream();
153: try {
154: writeDefinition(result);
155: } catch (IOException ioe) {
156: ioe.printStackTrace();
157: }
158: return result.toByteArray();
159: }
160:
161: /**
162: * Writes the list and list override tables.
163: */
164: public void writeDefinition(final OutputStream result)
165: throws IOException {
166: final int[] listIds = new int[lists.size()];
167: result.write(OPEN_GROUP);
168: result.write(LIST_TABLE);
169: result.write("\n".getBytes());
170: for (int i = 0; i < lists.size(); i++) {
171: result.write(OPEN_GROUP);
172: result.write(LIST);
173: result.write(LIST_TEMPLATE_ID);
174: result.write(intToByteArray(document.getRandomInt()));
175: result.write(LIST_HYBRID);
176: result.write("\n".getBytes());
177: final RtfList rList = (RtfList) lists.get(i);
178: //.result.write(rList.writeDefinition());
179: rList.writeDefinition(result);
180: result.write(LIST_ID);
181: listIds[i] = document.getRandomInt();
182: result.write(intToByteArray(listIds[i]));
183: result.write(CLOSE_GROUP);
184: result.write("\n".getBytes());
185: }
186: result.write(CLOSE_GROUP);
187: result.write("\n".getBytes());
188: result.write(OPEN_GROUP);
189: result.write(LIST_OVERRIDE_TABLE);
190: result.write("\n".getBytes());
191: for (int i = 0; i < lists.size(); i++) {
192: result.write(OPEN_GROUP);
193: result.write(LIST_OVERRIDE);
194: result.write(LIST_ID);
195: result.write(intToByteArray(listIds[i]));
196: result.write(LIST_OVERRIDE_COUNT);
197: result.write(intToByteArray(0));
198: result.write(LIST_NUMBER);
199: result.write(intToByteArray(((RtfList) lists.get(i))
200: .getListNumber()));
201: result.write(CLOSE_GROUP);
202: result.write("\n".getBytes());
203: }
204: result.write(CLOSE_GROUP);
205: result.write("\n".getBytes());
206: }
207:
208: /**
209: * Gets the id of the specified RtfList. If the RtfList is not yet in the
210: * list of RtfLists, then it is added.
211: *
212: * @param list The RtfList for which to get the id.
213: * @return The id of the RtfList.
214: */
215: public int getListNumber(RtfList list) {
216: if (lists.contains(list)) {
217: return lists.indexOf(list);
218: } else {
219: lists.add(list);
220: return lists.size();
221: }
222: }
223:
224: /**
225: * Remove a RtfList from the list of RtfLists
226: *
227: * @param list The RtfList to remove.
228: */
229: public void freeListNumber(RtfList list) {
230: int i = lists.indexOf(list);
231: if (i >= 0) {
232: lists.remove(i);
233: }
234: }
235: }
|