PDFTreeNode.java
14.1 KB
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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.internal.pdftoolkit.core.cos.CosArray
* com.adobe.internal.pdftoolkit.core.cos.CosDictionary
* com.adobe.internal.pdftoolkit.core.cos.CosObject
* com.adobe.internal.pdftoolkit.core.cos.CosScalar
* com.adobe.internal.pdftoolkit.core.cos.CosScalarOps
* com.adobe.internal.pdftoolkit.core.exceptions.PDFException
* com.adobe.internal.pdftoolkit.core.exceptions.PDFIOException
* com.adobe.internal.pdftoolkit.core.exceptions.PDFInvalidDocumentException
* com.adobe.internal.pdftoolkit.core.exceptions.PDFInvalidParameterException
* com.adobe.internal.pdftoolkit.core.exceptions.PDFSecurityException
* com.adobe.internal.pdftoolkit.core.types.ASName
*/
package com.adobe.internal.pdftoolkit.pdf.document;
import com.adobe.internal.pdftoolkit.core.cos.CosArray;
import com.adobe.internal.pdftoolkit.core.cos.CosDictionary;
import com.adobe.internal.pdftoolkit.core.cos.CosObject;
import com.adobe.internal.pdftoolkit.core.cos.CosScalar;
import com.adobe.internal.pdftoolkit.core.cos.CosScalarOps;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFIOException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFInvalidDocumentException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFInvalidParameterException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFSecurityException;
import com.adobe.internal.pdftoolkit.core.types.ASName;
import com.adobe.internal.pdftoolkit.pdf.document.PDFCosDictionary;
import com.adobe.internal.pdftoolkit.pdf.document.PDFCosObject;
import com.adobe.internal.pdftoolkit.pdf.document.PDFDocument;
import com.adobe.internal.pdftoolkit.pdf.document.PDFTreeList;
import java.util.ListIterator;
/*
* This class specifies class file version 49.0 but uses Java 6 signatures. Assumed Java 6.
*/
class PDFTreeNode<K, V>
extends PDFCosDictionary {
protected static final int SPLIT_NUMBER = 64;
private final ASName nameDictionaryKey;
protected PDFTreeNode<K, V> mParentNode = null;
protected PDFTreeNode(CosObject cosObject, ASName nameDictionaryKey) throws PDFInvalidDocumentException {
super(cosObject);
this.nameDictionaryKey = nameDictionaryKey;
}
protected static <K, V> PDFTreeNode<K, V> getInstance(CosObject cosObject, PDFTreeNode<K, V> parent, ASName nameDictionaryKey) throws PDFInvalidDocumentException {
if (PDFCosObject.checkNullCosObject(cosObject) == null) {
return null;
}
PDFTreeNode<K, V> pdfTreeNode = new PDFTreeNode<K, V>(cosObject, nameDictionaryKey);
pdfTreeNode.setParent(parent);
return pdfTreeNode;
}
protected static <K, V> PDFTreeNode<K, V> newInstance(PDFDocument pdfDocument, ASName nameDictionaryKey, PDFTreeList<K, V> kids) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
CosDictionary cosObject = PDFCosObject.newCosDictionary(pdfDocument);
PDFTreeNode<K, V> pdfTreeNode = new PDFTreeNode<K, V>((CosObject)cosObject, nameDictionaryKey);
pdfTreeNode.setParent(kids.getParent());
pdfTreeNode.getCosDictionary().put(ASName.k_Kids, kids.getCosObject());
kids.setParent(pdfTreeNode);
pdfTreeNode.resetLimits();
return pdfTreeNode;
}
protected ASName getNameDictionaryKey() {
return this.nameDictionaryKey;
}
protected PDFTreeList<K, V> getChildren() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
return PDFTreeList.getInstance(this.getDictionaryCosObjectValue(ASName.k_Kids), this, this.getNameDictionaryKey());
}
protected boolean hasChildren() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
return this.containsKey(ASName.k_Kids);
}
protected PDFTreeNode<K, V> getParent() {
return this.mParentNode;
}
protected void setParent(PDFTreeNode<K, V> parent) {
this.mParentNode = parent;
}
protected void resetLimits() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
PDFTreeList<K, V> list = this.getChildren();
if (list != null) {
PDFTreeNode kid = (PDFTreeNode)list.get(0);
this.setLimitLower(kid.getLimitLower());
kid = (PDFTreeNode)list.get(list.size() - 1);
this.setLimitUpper(kid.getLimitUpper());
this.mParentNode.resetLimits();
}
}
protected CosScalar getLimitLower() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
CosArray limits = this.getDictionaryArrayValue(ASName.k_Limits);
if (limits == null) {
return null;
}
return (CosScalar)limits.get(0);
}
protected CosScalar getLimitUpper() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
CosArray limits = this.getDictionaryArrayValue(ASName.k_Limits);
if (limits == null) {
return null;
}
return (CosScalar)limits.get(1);
}
protected void setLimits(CosScalar lower, CosScalar upper) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
this.setLimitLower(lower);
this.setLimitUpper(upper);
}
protected void setLimitLower(CosScalar lower) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
CosArray limits = this.getDictionaryArrayValue(ASName.k_Limits);
if (limits != null) {
limits.set(0, (CosObject)lower);
return;
}
limits = PDFCosObject.newCosArray(this.getPDFDocument());
limits.add((CosObject)lower);
limits.add((CosObject)lower);
this.setDictionaryValue(ASName.k_Limits, (CosObject)limits);
}
protected void setLimitUpper(CosScalar upper) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
CosArray limits = this.getDictionaryArrayValue(ASName.k_Limits);
if (limits != null) {
limits.set(1, (CosObject)upper);
return;
}
limits = PDFCosObject.newCosArray(this.getPDFDocument());
limits.set(0, (CosObject)upper);
limits.set(1, (CosObject)upper);
this.setDictionaryValue(ASName.k_Limits, (CosObject)limits);
}
protected int inRange(CosScalar name) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
CosScalar lower = this.getLimitLower();
if (lower != null && CosScalarOps.compareTo((CosScalar)name, (CosScalar)lower) < 0) {
return -1;
}
CosScalar upper = this.getLimitUpper();
if (upper != null && CosScalarOps.compareTo((CosScalar)name, (CosScalar)upper) > 0) {
return 1;
}
return 0;
}
protected CosObject keyValue(CosScalar name) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
for (PDFTreeNode kid : this.getChildren()) {
if (kid.inRange(name) != 0) continue;
return kid.keyValue(name);
}
return null;
}
protected CosScalar previousKey(CosScalar name) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
CosScalar prevKey = null;
for (PDFTreeNode kid : this.getChildren()) {
int rangeSign = kid.inRange(name);
if (rangeSign == 0) {
return kid.previousKey(name);
}
if (rangeSign <= 0) continue;
prevKey = kid.getLimitUpper();
}
return prevKey;
}
protected boolean removeKid(PDFTreeNode<K, V> kid) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
PDFTreeList<K, V> kids = this.getChildren();
if (kids == null) {
return false;
}
return kids.remove(kid);
}
@Override
public boolean isEmpty() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
PDFTreeList<K, V> kids = this.getChildren();
if (kids == null) {
return true;
}
return kids.isEmpty();
}
protected boolean deleteValue(CosScalar name) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
PDFTreeList<K, V> kids = this.getChildren();
if (kids == null) {
return false;
}
for (PDFTreeNode kid : kids) {
if (kid.inRange(name) != 0 || !kid.deleteValue(name)) continue;
if (kid.isEmpty()) {
this.removeKid(kid);
if (!this.isEmpty()) {
this.resetLimits();
}
}
return true;
}
return false;
}
protected PDFTreeNode<K, V> putValue(CosScalar name, CosObject value, boolean replace) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException, PDFInvalidParameterException {
PDFTreeList<K, V> newKids = this.getChildren().putValue(name, value, replace);
this.resetLimits();
if (newKids == null) {
return null;
}
PDFTreeNode<K, V> newNode = PDFTreeNode.newInstance(this.getPDFDocument(), this.getNameDictionaryKey(), newKids);
this.getChildren().add(newNode);
this.resetLimits();
return null;
}
protected ListIterator<PDFTreeNode<K, V>> treeNodeListIterator() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
return new TreeNodeIterator(this.getChildren());
}
/*
* This class specifies class file version 49.0 but uses Java 6 signatures. Assumed Java 6.
*/
private class TreeNodeIterator
implements ListIterator<PDFTreeNode<K, V>> {
private ListIterator<PDFTreeNode<K, V>> kidIterator;
private PDFTreeNode<K, V> currentKid;
private ListIterator<PDFTreeNode<K, V>> grandKidIterator;
TreeNodeIterator(PDFTreeList<K, V> kids) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
if (kids == null) {
return;
}
this.currentKid = null;
this.kidIterator = kids.listIterator();
if (this.kidIterator.hasNext()) {
this.currentKid = this.kidIterator.next();
this.grandKidIterator = this.currentKid.treeNodeListIterator();
}
}
@Override
public boolean hasNext() {
if (this.currentKid == null) {
return false;
}
if (this.kidIterator.hasNext()) {
return true;
}
return this.grandKidIterator.hasNext();
}
@Override
public PDFTreeNode<K, V> next() {
try {
if (this.grandKidIterator.hasNext()) {
return (InternalEntry)((Object)this.grandKidIterator.next());
}
if (this.kidIterator.hasNext()) {
this.currentKid = this.kidIterator.next();
this.grandKidIterator = this.currentKid.treeNodeListIterator();
return (InternalEntry)((Object)this.grandKidIterator.next());
}
}
catch (PDFException e) {
// empty catch block
}
return null;
}
@Override
public void remove() {
try {
this.grandKidIterator.remove();
if (this.currentKid.isEmpty()) {
this.kidIterator.remove();
if (!PDFTreeNode.this.isEmpty()) {
PDFTreeNode.this.resetLimits();
} else {
CosDictionary cosDict = (CosDictionary)PDFTreeNode.this.getCosObject();
cosDict.remove(ASName.k_Kids);
cosDict.remove(ASName.k_Limits);
}
}
}
catch (PDFException e) {
IllegalStateException newException = new IllegalStateException("Error during PDFTreeNodeIterator.remove().");
newException.initCause((Throwable)e);
throw newException;
}
}
@Override
public int nextIndex() {
throw new UnsupportedOperationException();
}
@Override
public int previousIndex() {
throw new UnsupportedOperationException();
}
@Override
public boolean hasPrevious() {
if (this.currentKid == null) {
return false;
}
if (this.kidIterator.hasPrevious()) {
return true;
}
return this.grandKidIterator.hasPrevious();
}
@Override
public PDFTreeNode<K, V> previous() {
try {
if (this.grandKidIterator.hasPrevious()) {
return (InternalEntry)((Object)this.grandKidIterator.previous());
}
if (this.kidIterator.hasPrevious()) {
this.currentKid = this.kidIterator.previous();
this.grandKidIterator = this.currentKid.treeNodeListIterator();
InternalEntry grandChild = null;
while (this.grandKidIterator.hasNext()) {
grandChild = (InternalEntry)((Object)this.grandKidIterator.next());
}
return grandChild;
}
}
catch (PDFException e) {
// empty catch block
}
return null;
}
@Override
public void add(PDFTreeNode<K, V> o) {
throw new UnsupportedOperationException();
}
@Override
public void set(PDFTreeNode<K, V> o) {
throw new UnsupportedOperationException();
}
}
public class InternalEntry {
private final CosScalar key;
private final CosObject value;
protected InternalEntry(CosScalar key, CosObject value) {
this.key = key;
this.value = value;
}
public CosScalar getKey() {
return this.key;
}
public CosObject getValue() {
return this.value;
}
}
}