001: /*
002: * Copyright 2004 by Paulo Soares.
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, 2000, 2001, 2002 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, 2001, 2002 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;
048:
049: import java.io.IOException;
050: import java.util.ArrayList;
051: import java.util.Arrays;
052: import java.util.HashMap;
053: import java.util.LinkedHashMap;
054:
055: /**
056: * Creates a name tree.
057: * @author Paulo Soares (psoares@consiste.pt)
058: */
059: public class PdfNameTree {
060:
061: private static final int leafSize = 64;
062:
063: /**
064: * Creates a name tree.
065: * @param items the item of the name tree. The key is a <CODE>String</CODE>
066: * and the value is a <CODE>PdfObject</CODE>. Note that although the
067: * keys are strings only the lower byte is used and no check is made for chars
068: * with the same lower byte and different upper byte. This will generate a wrong
069: * tree name.
070: * @param writer the writer
071: * @throws IOException on error
072: * @return the dictionary with the name tree. This dictionary is the one
073: * generally pointed to by the key /Dests, for example
074: */
075: public static PdfDictionary writeTree(HashMap items,
076: PdfWriter writer) throws IOException {
077: if (items.isEmpty())
078: return null;
079: String names[] = new String[items.size()];
080: names = (String[]) items.keySet().toArray(names);
081: if (!(items instanceof LinkedHashMap))
082: Arrays.sort(names);
083: if (names.length <= leafSize) {
084: PdfDictionary dic = new PdfDictionary();
085: PdfArray ar = new PdfArray();
086: for (int k = 0; k < names.length; ++k) {
087: ar.add(new PdfString(names[k], null));
088: ar.add((PdfObject) items.get(names[k]));
089: }
090: dic.put(PdfName.NAMES, ar);
091: return dic;
092: }
093: int skip = leafSize;
094: PdfIndirectReference kids[] = new PdfIndirectReference[(names.length
095: + leafSize - 1)
096: / leafSize];
097: for (int k = 0; k < kids.length; ++k) {
098: int offset = k * leafSize;
099: int end = Math.min(offset + leafSize, names.length);
100: PdfDictionary dic = new PdfDictionary();
101: PdfArray arr = new PdfArray();
102: arr.add(new PdfString(names[offset], null));
103: arr.add(new PdfString(names[end - 1], null));
104: dic.put(PdfName.LIMITS, arr);
105: arr = new PdfArray();
106: for (; offset < end; ++offset) {
107: arr.add(new PdfString(names[offset], null));
108: arr.add((PdfObject) items.get(names[offset]));
109: }
110: dic.put(PdfName.NAMES, arr);
111: kids[k] = writer.addToBody(dic).getIndirectReference();
112: }
113: int top = kids.length;
114: while (true) {
115: if (top <= leafSize) {
116: PdfArray arr = new PdfArray();
117: for (int k = 0; k < top; ++k)
118: arr.add(kids[k]);
119: PdfDictionary dic = new PdfDictionary();
120: dic.put(PdfName.KIDS, arr);
121: return dic;
122: }
123: skip *= leafSize;
124: int tt = (names.length + skip - 1) / skip;
125: for (int k = 0; k < tt; ++k) {
126: int offset = k * leafSize;
127: int end = Math.min(offset + leafSize, top);
128: PdfDictionary dic = new PdfDictionary();
129: PdfArray arr = new PdfArray();
130: arr.add(new PdfString(names[k * skip], null));
131: arr.add(new PdfString(names[Math.min((k + 1) * skip,
132: names.length) - 1], null));
133: dic.put(PdfName.LIMITS, arr);
134: arr = new PdfArray();
135: for (; offset < end; ++offset) {
136: arr.add(kids[offset]);
137: }
138: dic.put(PdfName.KIDS, arr);
139: kids[k] = writer.addToBody(dic).getIndirectReference();
140: }
141: top = tt;
142: }
143: }
144:
145: private static void iterateItems(PdfDictionary dic, HashMap items) {
146: PdfArray nn = (PdfArray) PdfReader.getPdfObjectRelease(dic
147: .get(PdfName.NAMES));
148: if (nn != null) {
149: ArrayList arr = nn.getArrayList();
150: for (int k = 0; k < arr.size(); ++k) {
151: PdfString s = (PdfString) PdfReader
152: .getPdfObjectRelease((PdfObject) arr.get(k++));
153: items.put(PdfEncodings.convertToString(s.getBytes(),
154: null), arr.get(k));
155: }
156: } else if ((nn = (PdfArray) PdfReader.getPdfObjectRelease(dic
157: .get(PdfName.KIDS))) != null) {
158: ArrayList arr = nn.getArrayList();
159: for (int k = 0; k < arr.size(); ++k) {
160: PdfDictionary kid = (PdfDictionary) PdfReader
161: .getPdfObjectRelease((PdfObject) arr.get(k));
162: iterateItems(kid, items);
163: }
164: }
165: }
166:
167: public static HashMap readTree(PdfDictionary dic) {
168: HashMap items = new HashMap();
169: if (dic != null)
170: iterateItems(dic, items);
171: return items;
172: }
173: }
|