1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
|
/*
* Copyright 2005 by Michael Niedermair.
*
* The contents of this file are subject to the Mozilla Public License Version 1.1
* (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the License.
*
* The Original Code is 'iText, a free JAVA-PDF library'.
*
* The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
* the Initial Developer are Copyright (C) 1999-2005 by Bruno Lowagie.
* All Rights Reserved.
* Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
* are Copyright (C) 2000-2005 by Paulo Soares. All Rights Reserved.
*
* Contributor(s): all the names of the contributors are added in the source code
* where applicable.
*
* Alternatively, the contents of this file may be used under the terms of the
* LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
* provisions of LGPL are applicable instead of those above. If you wish to
* allow use of your version of this file only under the terms of the LGPL
* License and not to allow others to use your version of this file under
* the MPL, indicate your decision by deleting the provisions above and
* replace them with the notice and other provisions required by the LGPL.
* If you do not delete the provisions above, a recipient may use your version
* of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the MPL as stated above or under the terms of the GNU
* Library General Public License as published by the Free Software Foundation;
* either version 2 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
* details.
*
* If you didn't download this code from the following link, you should check if
* you aren't using an obsolete version:
* http://www.lowagie.com/iText/
*/
package com.lowagie.text.pdf.events;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfPageEventHelper;
import com.lowagie.text.pdf.PdfWriter;
/**
* Class for an index.
*
* @author Michael Niedermair
*/
public class IndexEvents extends PdfPageEventHelper {
/**
* keeps the indextag with the pagenumber
*/
private Map indextag = new TreeMap();
/**
* All the text that is passed to this event, gets registered in the indexentry.
*
* @see com.lowagie.text.pdf.PdfPageEventHelper#onGenericTag(
* com.lowagie.text.pdf.PdfWriter, com.lowagie.text.Document,
* com.lowagie.text.Rectangle, java.lang.String)
*/
public void onGenericTag(PdfWriter writer, Document document,
Rectangle rect, String text) {
indextag.put(text, new Integer(writer.getPageNumber()));
}
// --------------------------------------------------------------------
/**
* indexcounter
*/
private long indexcounter = 0;
/**
* the list for the index entry
*/
private List indexentry = new ArrayList();
/**
* Create an index entry.
*
* @param text The text for the Chunk.
* @param in1 The first level.
* @param in2 The second level.
* @param in3 The third level.
* @return Returns the Chunk.
*/
public Chunk create(final String text, final String in1, final String in2,
final String in3) {
Chunk chunk = new Chunk(text);
String tag = "idx_" + String.valueOf(indexcounter++);
chunk.setGenericTag(tag);
chunk.setLocalDestination(tag);
Entry entry = new Entry(in1, in2, in3, tag);
indexentry.add(entry);
return chunk;
}
/**
* Create an index entry.
*
* @param text The text for the Chunk.
* @param in1 The first level.
* @return Returns the Chunk.
*/
public Chunk create(final String text, final String in1) {
return create(text, in1, "", "");
}
/**
* Create an index entry.
*
* @param text The text for the Chunk.
* @param in1 The first level.
* @param in2 The second level.
* @return Returns the Chunk.
*/
public Chunk create(final String text, final String in1, final String in2) {
return create(text, in1, in2, "");
}
/**
* Create an index entry.
*
* @param text The text.
* @param in1 The first level.
* @param in2 The second level.
* @param in3 The third level.
*/
public void create(final Chunk text, final String in1, final String in2,
final String in3) {
String tag = "idx_" + String.valueOf(indexcounter++);
text.setGenericTag(tag);
text.setLocalDestination(tag);
Entry entry = new Entry(in1, in2, in3, tag);
indexentry.add(entry);
}
/**
* Create an index entry.
*
* @param text The text.
* @param in1 The first level.
*/
public void create(final Chunk text, final String in1) {
create(text, in1, "", "");
}
/**
* Create an index entry.
*
* @param text The text.
* @param in1 The first level.
* @param in2 The second level.
*/
public void create(final Chunk text, final String in1, final String in2) {
create(text, in1, in2, "");
}
/**
* Comparator for sorting the index
*/
private Comparator comparator = new Comparator() {
public int compare(Object arg0, Object arg1) {
Entry en1 = (Entry) arg0;
Entry en2 = (Entry) arg1;
int rt = 0;
if (en1.getIn1() != null && en2.getIn1() != null) {
if ((rt = en1.getIn1().compareToIgnoreCase(en2.getIn1())) == 0) {
// in1 equals
if (en1.getIn2() != null && en2.getIn2() != null) {
if ((rt = en1.getIn2()
.compareToIgnoreCase(en2.getIn2())) == 0) {
// in2 equals
if (en1.getIn3() != null && en2.getIn3() != null) {
rt = en1.getIn3().compareToIgnoreCase(
en2.getIn3());
}
}
}
}
}
return rt;
}
};
/**
* Set the comparator.
* @param aComparator The comparator to set.
*/
public void setComparator(Comparator aComparator) {
comparator = aComparator;
}
/**
* Returns the sorted list with the entries and the collected page numbers.
* @return Returns the sorted list with the entries and teh collected page numbers.
*/
public List getSortedEntries() {
Map grouped = new HashMap();
for (int i = 0; i < indexentry.size(); i++) {
Entry e = (Entry) indexentry.get(i);
String key = e.getKey();
Entry master = (Entry) grouped.get(key);
if (master != null) {
master.addPageNumberAndTag(e.getPageNumber(), e.getTag());
} else {
e.addPageNumberAndTag(e.getPageNumber(), e.getTag());
grouped.put(key, e);
}
}
// copy to a list and sort it
List sorted = new ArrayList(grouped.size());
Iterator it = grouped.keySet().iterator();
while (it.hasNext()) {
String key = (String) it.next();
Entry e = (Entry) grouped.get(key);
sorted.add(e);
}
Collections.sort(sorted, comparator);
return sorted;
}
// --------------------------------------------------------------------
/**
* Class for an index entry.
* <p>
* In the first step, only in1, in2,in3 and tag are used.
* After the collections of the index entries, pagenumbers are used.
* </p>
*/
public class Entry {
/**
* first level
*/
private String in1;
/**
* second level
*/
private String in2;
/**
* third level
*/
private String in3;
/**
* the tag
*/
private String tag;
/**
* the lsit of all page numbers.
*/
private List pagenumbers = new ArrayList();
/**
* the lsit of all tags.
*/
private List tags = new ArrayList();
/**
* Create a new object.
* @param aIn1 The first level.
* @param aIn2 The second level.
* @param aIn3 The third level.
* @param aTag The tag.
*/
public Entry(final String aIn1, final String aIn2, final String aIn3,
final String aTag) {
in1 = aIn1;
in2 = aIn2;
in3 = aIn3;
tag = aTag;
}
/**
* Returns the in1.
* @return Returns the in1.
*/
public String getIn1() {
return in1;
}
/**
* Returns the in2.
* @return Returns the in2.
*/
public String getIn2() {
return in2;
}
/**
* Returns the in3.
* @return Returns the in3.
*/
public String getIn3() {
return in3;
}
/**
* Returns the tag.
* @return Returns the tag.
*/
public String getTag() {
return tag;
}
/**
* Returns the pagenumer for this entry.
* @return Returns the pagenumer for this entry.
*/
public int getPageNumber() {
int rt = -1;
Integer i = (Integer) indextag.get(tag);
if (i != null) {
rt = i.intValue();
}
return rt;
}
/**
* Add a pagenumber.
* @param number The page number.
* @param tag
*/
public void addPageNumberAndTag(final int number, final String tag) {
pagenumbers.add(new Integer(number));
tags.add(tag);
}
/**
* Returns the key for the map-entry.
* @return Returns the key for the map-entry.
*/
public String getKey() {
return in1 + "!" + in2 + "!" + in3;
}
/**
* Returns the pagenumbers.
* @return Returns the pagenumbers.
*/
public List getPagenumbers() {
return pagenumbers;
}
/**
* Returns the tags.
* @return Returns the tags.
*/
public List getTags() {
return tags;
}
/**
* print the entry (only for test)
* @return the toString implementation of the entry
*/
public String toString() {
StringBuffer buf = new StringBuffer();
buf.append(in1).append(" ");
buf.append(in2).append(" ");
buf.append(in3).append(" ");
for (int i = 0; i < pagenumbers.size(); i++) {
buf.append(pagenumbers.get(i)).append(" ");
}
return buf.toString();
}
}
}
|