001: /*
002: * Copyright 2005 by Michael Niedermair.
003: *
004: * The contents of this file are subject to the Mozilla Public License Version 1.1
005: * (the "License"); you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
007: *
008: * Software distributed under the License is distributed on an "AS IS" basis,
009: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
010: * for the specific language governing rights and limitations under the License.
011: *
012: * The Original Code is 'iText, a free JAVA-PDF library'.
013: *
014: * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
015: * the Initial Developer are Copyright (C) 1999-2005 by Bruno Lowagie.
016: * All Rights Reserved.
017: * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
018: * are Copyright (C) 2000-2005 by Paulo Soares. All Rights Reserved.
019: *
020: * Contributor(s): all the names of the contributors are added in the source code
021: * where applicable.
022: *
023: * Alternatively, the contents of this file may be used under the terms of the
024: * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
025: * provisions of LGPL are applicable instead of those above. If you wish to
026: * allow use of your version of this file only under the terms of the LGPL
027: * License and not to allow others to use your version of this file under
028: * the MPL, indicate your decision by deleting the provisions above and
029: * replace them with the notice and other provisions required by the LGPL.
030: * If you do not delete the provisions above, a recipient may use your version
031: * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
032: *
033: * This library is free software; you can redistribute it and/or modify it
034: * under the terms of the MPL as stated above or under the terms of the GNU
035: * Library General Public License as published by the Free Software Foundation;
036: * either version 2 of the License, or any later version.
037: *
038: * This library is distributed in the hope that it will be useful, but WITHOUT
039: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
040: * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
041: * details.
042: *
043: * If you didn't download this code from the following link, you should check if
044: * you aren't using an obsolete version:
045: * http://www.lowagie.com/iText/
046: */
047: package com.lowagie.text.pdf.events;
048:
049: import java.util.ArrayList;
050: import java.util.Collections;
051: import java.util.Comparator;
052: import java.util.HashMap;
053: import java.util.List;
054: import java.util.Map;
055: import java.util.TreeMap;
056:
057: import com.lowagie.text.Chunk;
058: import com.lowagie.text.Document;
059: import com.lowagie.text.Rectangle;
060: import com.lowagie.text.pdf.PdfPageEventHelper;
061: import com.lowagie.text.pdf.PdfWriter;
062:
063: /**
064: * Class for an index.
065: *
066: * @author Michael Niedermair
067: */
068: public class IndexEvents extends PdfPageEventHelper {
069:
070: /**
071: * keeps the indextag with the pagenumber
072: */
073: private Map indextag = new TreeMap();
074:
075: /**
076: * All the text that is passed to this event, gets registered in the indexentry.
077: *
078: * @see com.lowagie.text.pdf.PdfPageEventHelper#onGenericTag(
079: * com.lowagie.text.pdf.PdfWriter, com.lowagie.text.Document,
080: * com.lowagie.text.Rectangle, java.lang.String)
081: */
082: public void onGenericTag(PdfWriter writer, Document document,
083: Rectangle rect, String text) {
084: indextag.put(text, new Integer(writer.getPageNumber()));
085: }
086:
087: // --------------------------------------------------------------------
088: /**
089: * indexcounter
090: */
091: private long indexcounter = 0;
092:
093: /**
094: * the list for the index entry
095: */
096: private List indexentry = new ArrayList();
097:
098: /**
099: * Create an index entry.
100: *
101: * @param text The text for the Chunk.
102: * @param in1 The first level.
103: * @param in2 The second level.
104: * @param in3 The third level.
105: * @return Returns the Chunk.
106: */
107: public Chunk create(final String text, final String in1,
108: final String in2, final String in3) {
109:
110: Chunk chunk = new Chunk(text);
111: String tag = "idx_" + (indexcounter++);
112: chunk.setGenericTag(tag);
113: chunk.setLocalDestination(tag);
114: Entry entry = new Entry(in1, in2, in3, tag);
115: indexentry.add(entry);
116: return chunk;
117: }
118:
119: /**
120: * Create an index entry.
121: *
122: * @param text The text for the Chunk.
123: * @param in1 The first level.
124: * @return Returns the Chunk.
125: */
126: public Chunk create(final String text, final String in1) {
127: return create(text, in1, "", "");
128: }
129:
130: /**
131: * Create an index entry.
132: *
133: * @param text The text for the Chunk.
134: * @param in1 The first level.
135: * @param in2 The second level.
136: * @return Returns the Chunk.
137: */
138: public Chunk create(final String text, final String in1,
139: final String in2) {
140: return create(text, in1, in2, "");
141: }
142:
143: /**
144: * Create an index entry.
145: *
146: * @param text The text.
147: * @param in1 The first level.
148: * @param in2 The second level.
149: * @param in3 The third level.
150: */
151: public void create(final Chunk text, final String in1,
152: final String in2, final String in3) {
153:
154: String tag = "idx_" + (indexcounter++);
155: text.setGenericTag(tag);
156: text.setLocalDestination(tag);
157: Entry entry = new Entry(in1, in2, in3, tag);
158: indexentry.add(entry);
159: }
160:
161: /**
162: * Create an index entry.
163: *
164: * @param text The text.
165: * @param in1 The first level.
166: */
167: public void create(final Chunk text, final String in1) {
168: create(text, in1, "", "");
169: }
170:
171: /**
172: * Create an index entry.
173: *
174: * @param text The text.
175: * @param in1 The first level.
176: * @param in2 The second level.
177: */
178: public void create(final Chunk text, final String in1,
179: final String in2) {
180: create(text, in1, in2, "");
181: }
182:
183: /**
184: * Comparator for sorting the index
185: */
186: private Comparator comparator = new Comparator() {
187:
188: public int compare(Object arg0, Object arg1) {
189: Entry en1 = (Entry) arg0;
190: Entry en2 = (Entry) arg1;
191:
192: int rt = 0;
193: if (en1.getIn1() != null && en2.getIn1() != null) {
194: if ((rt = en1.getIn1()
195: .compareToIgnoreCase(en2.getIn1())) == 0) {
196: // in1 equals
197: if (en1.getIn2() != null && en2.getIn2() != null) {
198: if ((rt = en1.getIn2().compareToIgnoreCase(
199: en2.getIn2())) == 0) {
200: // in2 equals
201: if (en1.getIn3() != null
202: && en2.getIn3() != null) {
203: rt = en1.getIn3().compareToIgnoreCase(
204: en2.getIn3());
205: }
206: }
207: }
208: }
209: }
210: return rt;
211: }
212: };
213:
214: /**
215: * Set the comparator.
216: * @param aComparator The comparator to set.
217: */
218: public void setComparator(Comparator aComparator) {
219: comparator = aComparator;
220: }
221:
222: /**
223: * Returns the sorted list with the entries and the collected page numbers.
224: * @return Returns the sorted list with the entries and teh collected page numbers.
225: */
226: public List getSortedEntries() {
227:
228: Map grouped = new HashMap();
229:
230: for (int i = 0; i < indexentry.size(); i++) {
231: Entry e = (Entry) indexentry.get(i);
232: String key = e.getKey();
233:
234: Entry master = (Entry) grouped.get(key);
235: if (master != null) {
236: master.addPageNumberAndTag(e.getPageNumber(), e
237: .getTag());
238: } else {
239: e.addPageNumberAndTag(e.getPageNumber(), e.getTag());
240: grouped.put(key, e);
241: }
242: }
243:
244: // copy to a list and sort it
245: List sorted = new ArrayList(grouped.values());
246: Collections.sort(sorted, comparator);
247: return sorted;
248: }
249:
250: // --------------------------------------------------------------------
251: /**
252: * Class for an index entry.
253: * <p>
254: * In the first step, only in1, in2,in3 and tag are used.
255: * After the collections of the index entries, pagenumbers are used.
256: * </p>
257: */
258: public class Entry {
259:
260: /**
261: * first level
262: */
263: private String in1;
264:
265: /**
266: * second level
267: */
268: private String in2;
269:
270: /**
271: * third level
272: */
273: private String in3;
274:
275: /**
276: * the tag
277: */
278: private String tag;
279:
280: /**
281: * the lsit of all page numbers.
282: */
283: private List pagenumbers = new ArrayList();
284:
285: /**
286: * the lsit of all tags.
287: */
288: private List tags = new ArrayList();
289:
290: /**
291: * Create a new object.
292: * @param aIn1 The first level.
293: * @param aIn2 The second level.
294: * @param aIn3 The third level.
295: * @param aTag The tag.
296: */
297: public Entry(final String aIn1, final String aIn2,
298: final String aIn3, final String aTag) {
299: in1 = aIn1;
300: in2 = aIn2;
301: in3 = aIn3;
302: tag = aTag;
303: }
304:
305: /**
306: * Returns the in1.
307: * @return Returns the in1.
308: */
309: public String getIn1() {
310: return in1;
311: }
312:
313: /**
314: * Returns the in2.
315: * @return Returns the in2.
316: */
317: public String getIn2() {
318: return in2;
319: }
320:
321: /**
322: * Returns the in3.
323: * @return Returns the in3.
324: */
325: public String getIn3() {
326: return in3;
327: }
328:
329: /**
330: * Returns the tag.
331: * @return Returns the tag.
332: */
333: public String getTag() {
334: return tag;
335: }
336:
337: /**
338: * Returns the pagenumer for this entry.
339: * @return Returns the pagenumer for this entry.
340: */
341: public int getPageNumber() {
342: int rt = -1;
343: Integer i = (Integer) indextag.get(tag);
344: if (i != null) {
345: rt = i.intValue();
346: }
347: return rt;
348: }
349:
350: /**
351: * Add a pagenumber.
352: * @param number The page number.
353: * @param tag
354: */
355: public void addPageNumberAndTag(final int number,
356: final String tag) {
357: pagenumbers.add(new Integer(number));
358: tags.add(tag);
359: }
360:
361: /**
362: * Returns the key for the map-entry.
363: * @return Returns the key for the map-entry.
364: */
365: public String getKey() {
366: return in1 + "!" + in2 + "!" + in3;
367: }
368:
369: /**
370: * Returns the pagenumbers.
371: * @return Returns the pagenumbers.
372: */
373: public List getPagenumbers() {
374: return pagenumbers;
375: }
376:
377: /**
378: * Returns the tags.
379: * @return Returns the tags.
380: */
381: public List getTags() {
382: return tags;
383: }
384:
385: /**
386: * print the entry (only for test)
387: * @return the toString implementation of the entry
388: */
389: public String toString() {
390: StringBuffer buf = new StringBuffer();
391: buf.append(in1).append(' ');
392: buf.append(in2).append(' ');
393: buf.append(in3).append(' ');
394: for (int i = 0; i < pagenumbers.size(); i++) {
395: buf.append(pagenumbers.get(i)).append(' ');
396: }
397: return buf.toString();
398: }
399: }
400: }
|