PDFTreeLeaf.java
13.7 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
/*
* 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.PDFCosParseException
* 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.PDFCosParseException;
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.PDFCosObject;
import com.adobe.internal.pdftoolkit.pdf.document.PDFDocument;
import com.adobe.internal.pdftoolkit.pdf.document.PDFTreeNode;
import java.util.ListIterator;
/*
* This class specifies class file version 49.0 but uses Java 6 signatures. Assumed Java 6.
*/
class PDFTreeLeaf<K, V>
extends PDFTreeNode<K, V> {
private PDFTreeLeaf(CosObject cosObject, ASName nameDictionaryKey) throws PDFInvalidDocumentException {
super(cosObject, nameDictionaryKey);
}
public static <K, V> PDFTreeLeaf<K, V> getInstance(CosObject cosObject, PDFTreeNode<K, V> parent, ASName keyName) throws PDFInvalidDocumentException {
if (PDFCosObject.checkNullCosObject(cosObject) == null) {
return null;
}
PDFTreeLeaf<K, V> pdfObject = PDFCosObject.getCachedInstance(cosObject, PDFTreeLeaf.class);
if (pdfObject == null) {
pdfObject = new PDFTreeLeaf<K, V>(cosObject, keyName);
}
pdfObject.setParent(parent);
return pdfObject;
}
static <K, V> PDFTreeLeaf<K, V> newInstance(PDFDocument pdfDocument, PDFTreeNode<K, V> parent, ASName nameDictionaryKey, CosScalar name, CosObject value) throws PDFInvalidDocumentException, PDFIOException, PDFInvalidParameterException, PDFSecurityException {
CosDictionary cosObject = PDFCosObject.newCosDictionary(pdfDocument);
PDFTreeLeaf<K, V> pdfObject = new PDFTreeLeaf<K, V>((CosObject)cosObject, nameDictionaryKey);
pdfObject.addValue(name, value);
pdfObject.setParent(parent);
return pdfObject;
}
static <K, V> PDFTreeLeaf<K, V> newInstance(PDFDocument pdfDocument, PDFTreeNode<K, V> parent, ASName nameDictionaryKey, CosArray namedValues) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
CosDictionary cosObject = PDFCosObject.newCosDictionary(pdfDocument);
PDFTreeLeaf<K, V> pdfObject = new PDFTreeLeaf<K, V>((CosObject)cosObject, nameDictionaryKey);
pdfObject.setParent(parent);
pdfObject.getCosDictionary().put(nameDictionaryKey, (CosObject)namedValues);
pdfObject.resetLimits();
return pdfObject;
}
@Override
protected int inRange(CosScalar name) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
int rslt = 0;
CosScalar lower = this.getLimitLower();
if (lower != null && (rslt = CosScalarOps.compareTo((CosScalar)name, (CosScalar)lower)) <= 0) {
return rslt;
}
CosScalar upper = this.getLimitUpper();
if (upper != null && (rslt = CosScalarOps.compareTo((CosScalar)name, (CosScalar)upper)) >= 0) {
return rslt;
}
return 0;
}
@Override
protected CosObject keyValue(CosScalar name) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
CosArray namedValues = this.getDictionaryArrayValue(this.getNameDictionaryKey());
if (namedValues != null) {
for (int i = 0; i < namedValues.size(); i += 2) {
CosScalar stringKey = (CosScalar)namedValues.get(i);
if (!CosScalarOps.equivalent((CosScalar)stringKey, (CosScalar)name)) continue;
return namedValues.get(i + 1);
}
}
return null;
}
@Override
protected CosScalar previousKey(CosScalar name) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
CosScalar lastKey = null;
CosArray namedValues = this.getDictionaryArrayValue(this.getNameDictionaryKey());
for (int i = 0; i < namedValues.size(); i += 2) {
CosScalar stringKey = (CosScalar)namedValues.get(i);
if (CosScalarOps.compareTo((CosScalar)stringKey, (CosScalar)name) >= 0) {
return lastKey;
}
lastKey = stringKey;
}
return lastKey;
}
@Override
public boolean isEmpty() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
CosArray namedValues = this.getDictionaryArrayValue(this.getNameDictionaryKey());
if (namedValues == null) {
return true;
}
return namedValues.isEmpty();
}
@Override
protected boolean deleteValue(CosScalar name) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
if (this.inRange(name) != 0) {
return false;
}
CosArray namedValues = this.getDictionaryArrayValue(this.getNameDictionaryKey());
int size = namedValues.size();
for (int index = 0; index < size; index += 2) {
CosScalar orgName = (CosScalar)namedValues.get(index);
if (CosScalarOps.compareTo((CosScalar)orgName, (CosScalar)name) != 0) continue;
namedValues.remove(index);
namedValues.remove(index);
if (namedValues.size() != 0) {
this.resetLimits();
}
return true;
}
return false;
}
PDFTreeNode<K, V> addValue(CosScalar name, CosObject value) throws PDFInvalidDocumentException, PDFIOException, PDFInvalidParameterException, PDFSecurityException {
return this.putValue(name, value, false);
}
PDFTreeNode<K, V> replaceValue(CosScalar name, CosObject value) throws PDFInvalidDocumentException, PDFIOException, PDFInvalidParameterException, PDFSecurityException {
return this.putValue(name, value, true);
}
@Override
protected PDFTreeNode<K, V> putValue(CosScalar name, CosObject value, boolean replaceOperation) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException, PDFInvalidParameterException {
CosArray namedVals = this.getDictionaryArrayValue(this.getNameDictionaryKey());
if (namedVals == null) {
CosArray newNamedValues = PDFCosObject.newCosArray(this.getPDFDocument());
newNamedValues.add((CosObject)name);
newNamedValues.add(value);
this.setDictionaryValue(this.getNameDictionaryKey(), (CosObject)newNamedValues);
this.setLimits(name, name);
return null;
}
boolean done = false;
int siz = namedVals.size();
for (int i = 0; i < siz && !done; i += 2) {
CosScalar curName = (CosScalar)namedVals.get(i);
int nameCompare = CosScalarOps.compareTo((CosScalar)curName, (CosScalar)name);
if (nameCompare < 0) continue;
if (!replaceOperation && nameCompare == 0) {
throw new PDFInvalidParameterException("Value already associated with this name, " + (Object)name);
}
if (replaceOperation && nameCompare == 0) {
namedVals.set(i, (CosObject)name);
namedVals.set(i + 1, value);
} else {
namedVals.add(i, value);
namedVals.add(i, (CosObject)name);
}
done = true;
}
if (!done) {
namedVals.add((CosObject)name);
namedVals.add(value);
}
this.resetLimits();
return this.split();
}
@Override
protected void resetLimits() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
if (this.mParentNode == null) {
return;
}
CosArray namedValues = this.getDictionaryArrayValue(this.getNameDictionaryKey());
if (namedValues == null) {
return;
}
this.setLimitLower((CosScalar)namedValues.get(0));
this.setLimitUpper((CosScalar)namedValues.get(namedValues.size() - 2));
this.mParentNode.resetLimits();
}
private CosArray split(CosArray orgNamedValues) throws PDFCosParseException, PDFIOException, PDFInvalidDocumentException, PDFSecurityException {
int nNames = orgNamedValues.size() >> 1;
if (nNames <= 64) {
return null;
}
nNames = nNames >> 1 << 1;
return orgNamedValues.splitBefore(nNames);
}
PDFTreeNode<K, V> split() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
CosArray orgNamedValues = this.getDictionaryArrayValue(this.getNameDictionaryKey());
CosArray newNamedValues = this.split(orgNamedValues);
if (newNamedValues == null) {
return null;
}
this.resetLimits();
return PDFTreeLeaf.newInstance(this.getPDFDocument(), this.getParent(), this.getNameDictionaryKey(), newNamedValues);
}
@Override
protected ListIterator<PDFTreeNode<K, V>> treeNodeListIterator() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
return new PDFTreeLeafIterator();
}
/*
* This class specifies class file version 49.0 but uses Java 6 signatures. Assumed Java 6.
*/
private class PDFTreeLeafIterator
implements ListIterator<PDFTreeNode<K, V>> {
private ListIterator<CosObject> mIterator;
boolean nextCalled;
boolean prevCalled;
PDFTreeLeafIterator() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
this.nextCalled = false;
this.prevCalled = false;
CosArray namedValues = PDFTreeLeaf.this.getDictionaryArrayValue(PDFTreeLeaf.this.getNameDictionaryKey());
if (namedValues != null) {
this.mIterator = namedValues.listIterator();
}
}
@Override
public boolean hasNext() {
if (this.mIterator == null) {
return false;
}
return this.mIterator.hasNext();
}
@Override
public PDFTreeNode<K, V> next() {
if (this.mIterator == null) {
return null;
}
CosScalar key = (CosScalar)this.mIterator.next();
CosObject value = this.mIterator.next();
this.nextCalled = true;
this.prevCalled = false;
return new PDFTreeNode.InternalEntry(PDFTreeLeaf.this, key, value);
}
@Override
public void remove() {
if (this.mIterator != null) {
if (!this.nextCalled && !this.prevCalled) {
throw new IllegalStateException("neither next nor previous have been called, or remove or add have been called after the last call to * next or previous.");
}
this.mIterator.remove();
if (this.nextCalled) {
this.mIterator.previous();
} else {
this.mIterator.next();
}
this.mIterator.remove();
this.prevCalled = false;
this.nextCalled = false;
}
try {
if (!PDFTreeLeaf.this.isEmpty()) {
PDFTreeLeaf.this.resetLimits();
}
}
catch (PDFException e) {
IllegalStateException newException = new IllegalStateException("Error during PDFTreeLeafIterator.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.mIterator == null) {
return false;
}
return this.mIterator.hasPrevious();
}
@Override
public PDFTreeNode<K, V> previous() {
CosObject value = this.mIterator.previous();
CosScalar key = (CosScalar)this.mIterator.previous();
this.nextCalled = false;
this.prevCalled = true;
return new PDFTreeNode.InternalEntry(PDFTreeLeaf.this, key, value);
}
@Override
public void add(PDFTreeNode<K, V> o) {
throw new UnsupportedOperationException();
}
@Override
public void set(PDFTreeNode<K, V> o) {
throw new UnsupportedOperationException();
}
}
}