PDFCollectionFolder.java
16.6 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
/*
* 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.exceptions.PDFIOException
* com.adobe.internal.pdftoolkit.core.exceptions.PDFInvalidDocumentException
* com.adobe.internal.pdftoolkit.core.exceptions.PDFInvalidParameterException
* com.adobe.internal.pdftoolkit.core.exceptions.PDFParseException
* com.adobe.internal.pdftoolkit.core.exceptions.PDFSecurityException
* com.adobe.internal.pdftoolkit.core.exceptions.PDFUnsupportedFeatureException
* com.adobe.internal.pdftoolkit.core.types.ASDate
* com.adobe.internal.pdftoolkit.core.types.ASName
* com.adobe.internal.pdftoolkit.core.types.ASString
* com.adobe.internal.pdftoolkit.core.util.PDFDocEncoding
*/
package com.adobe.internal.pdftoolkit.pdf.interactive.navigation.collection;
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.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.PDFParseException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFSecurityException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFUnsupportedFeatureException;
import com.adobe.internal.pdftoolkit.core.types.ASDate;
import com.adobe.internal.pdftoolkit.core.types.ASName;
import com.adobe.internal.pdftoolkit.core.types.ASString;
import com.adobe.internal.pdftoolkit.core.util.PDFDocEncoding;
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.PDFFileSpecification;
import com.adobe.internal.pdftoolkit.pdf.document.PDFNamedEmbeddedFiles;
import com.adobe.internal.pdftoolkit.pdf.document.PDFText;
import com.adobe.internal.pdftoolkit.pdf.graphics.xobject.PDFXObject;
import com.adobe.internal.pdftoolkit.pdf.graphics.xobject.PDFXObjectFactory;
import com.adobe.internal.pdftoolkit.pdf.interactive.navigation.collection.PDFCollectionItem;
import com.adobe.internal.pdftoolkit.pdf.interactive.navigation.collection.PDFCollectionUtil;
import java.util.ArrayList;
public class PDFCollectionFolder
extends PDFCosDictionary {
public static final ASName k_Name = ASName.create((String)"Name");
public static final ASName k_Desc = ASName.create((String)"Desc");
public static final ASName k_ModDate = ASName.create((String)"ModDate");
public static final ASName k_CreationDate = ASName.create((String)"CreationDate");
public static final ASName k_ID = ASName.create((String)"ID");
public static final ASName k_Parent = ASName.create((String)"Parent");
public static final ASName k_Next = ASName.create((String)"Next");
public static final ASName k_Child = ASName.create((String)"Child");
public static final ASName k_CI = ASName.create((String)"CI");
public static final ASName k_Thumb = ASName.create((String)"Thumb");
public static final ASName k_Free = ASName.create((String)"Free");
public static final ASName k_Folder = ASName.create((String)"Folder");
private PDFCollectionFolder(CosObject cosObject) throws PDFInvalidDocumentException {
super(cosObject);
}
private PDFCollectionFolder(PDFDocument pdfDoc) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
super((CosObject)PDFCosObject.newCosDictionary(pdfDoc));
this.setDictionaryNameValue(ASName.k_Type, k_Folder);
}
public static PDFCollectionFolder getInstance(CosObject cosObject) throws PDFInvalidDocumentException {
if (PDFCosObject.checkNullCosObject(cosObject) == null) {
return null;
}
PDFCollectionFolder pdfObject = PDFCosObject.getCachedInstance(cosObject, PDFCollectionFolder.class);
if (pdfObject == null) {
pdfObject = new PDFCollectionFolder(cosObject);
}
return pdfObject;
}
protected static PDFCollectionFolder newInstanceFolderRoot(PDFDocument pdfDocument, String name, long ID) throws PDFInvalidDocumentException, PDFIOException, PDFUnsupportedFeatureException, PDFSecurityException {
PDFCollectionFolder pdfObject = new PDFCollectionFolder(pdfDocument);
pdfObject.setParent(null);
pdfObject.setName(null, name);
pdfObject.setID(ID);
return pdfObject;
}
public static PDFCollectionFolder newInstance(PDFCollectionFolder parent, String name) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
if (parent == null) {
throw new PDFInvalidDocumentException("parent folder cannot be null");
}
long newID = PDFCollectionUtil.getNewFolderID(parent.getPDFDocument());
PDFCollectionFolder folder = new PDFCollectionFolder(parent.getPDFDocument());
folder.setName(parent, name);
folder.setParent(parent);
folder.setID(newID);
return folder;
}
public static PDFCollectionFolder newInstance(PDFCollectionFolder parent, String name, String desc, ASDate creationDate, ASDate modDate, PDFXObject thumbnail, PDFCollectionItem ci) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
PDFCollectionFolder folder = PDFCollectionFolder.newInstance(parent, name);
folder.setDescription(desc);
folder.setModificationDate(modDate);
folder.setCreationDate(creationDate);
folder.setThumb(thumbnail);
folder.setCollectionItem(ci);
return folder;
}
public ArrayList getChildren() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
ArrayList<PDFCollectionFolder> list = new ArrayList<PDFCollectionFolder>();
for (PDFCollectionFolder kid = this.getChild(); kid != null; kid = kid.getNext()) {
list.add(kid);
}
return list;
}
public ArrayList getSiblings() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
ArrayList<PDFCollectionFolder> list = new ArrayList<PDFCollectionFolder>();
PDFCollectionFolder parent = this.getParent();
if (parent == null) {
list.add(parent);
return list;
}
for (PDFCollectionFolder sibling = parent.getChild(); sibling != null; sibling = sibling.getNext()) {
list.add(sibling);
}
return list;
}
public PDFCollectionFolder getChildFolder(String name) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
ArrayList children = this.getChildren();
for (PDFCollectionFolder child : children) {
if (!child.getName().equals(name)) continue;
return child;
}
return null;
}
public PDFCollectionFolder getSiblingFolder(String name) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
ArrayList siblings = this.getSiblings();
for (PDFCollectionFolder sibling : siblings) {
if (!sibling.getName().equals(name)) continue;
return sibling;
}
return null;
}
public void setName(PDFCollectionFolder parent, String name) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
if (name == null) {
throw new PDFInvalidDocumentException("Name is null - it is a required key in Folder");
}
if (!this.uniqueName(parent, name)) {
throw new PDFInvalidDocumentException("Name of folder is not unique");
}
PDFText textValue = PDFText.newInstance(this.getPDFDocument(), name);
((CosDictionary)this.getCosObject()).put(ASName.k_Name, textValue.getCosObject());
}
public String getName() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
PDFText textValue = PDFText.getInstance(((CosDictionary)this.getCosObject()).get(ASName.k_Name));
if (textValue != null) {
return textValue.stringValue();
}
throw new PDFInvalidDocumentException("Name does not exist - it is a required key in Folder");
}
public void setDescription(String desc) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
if (desc == null) {
this.removeValue(k_Desc);
} else {
PDFText textValue = PDFText.newInstance(this.getPDFDocument(), desc);
((CosDictionary)this.getCosObject()).put(ASName.k_Desc, textValue.getCosObject());
}
}
public String getDescription() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
PDFText textValue = PDFText.getInstance(((CosDictionary)this.getCosObject()).get(ASName.k_Desc));
if (textValue != null) {
return textValue.stringValue();
}
return null;
}
public void setModificationDate(ASDate modDate) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
if (modDate == null) {
this.removeValue(k_ModDate);
} else {
String dateStr = modDate.asString();
this.setDictionaryStringValue(k_ModDate, dateStr);
}
}
public ASDate getModificationDate() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException, PDFParseException {
if (this.dictionaryContains(k_ModDate)) {
return new ASDate(this.getDictionaryStringValue(k_ModDate));
}
return null;
}
public void setCreationDate(ASDate creationDate) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
if (creationDate == null) {
this.removeValue(k_CreationDate);
} else {
String dateStr = creationDate.asString();
this.setDictionaryStringValue(k_CreationDate, dateStr);
}
}
public ASDate getCreationDate() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException, PDFParseException {
if (this.dictionaryContains(k_CreationDate)) {
return new ASDate(this.getDictionaryStringValue(k_CreationDate));
}
return null;
}
public void setCollectionItem(PDFCollectionItem ci) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
if (ci == null) {
this.removeValue(k_CI);
} else {
this.setDictionaryValue(k_CI, ci);
}
}
public PDFCollectionItem getCollectionItem() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
if (this.dictionaryContains(k_CI)) {
return PDFCollectionItem.getInstance(this.getDictionaryValue(k_CI));
}
return null;
}
public void setThumb(PDFXObject thumb) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
if (thumb == null) {
this.removeValue(k_Thumb);
} else {
this.setDictionaryValue(k_Thumb, thumb);
}
}
public PDFXObject getThumb() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
if (this.dictionaryContains(k_Thumb)) {
return PDFXObjectFactory.getInstance(this.getDictionaryValue(k_Thumb));
}
return null;
}
public PDFFileSpecification get(String key) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
ASString asKey;
String keyStr;
PDFNamedEmbeddedFiles nefTree = PDFCollectionUtil.getNEFTree(this.getPDFDocument());
PDFFileSpecification fileSpec = nefTree.getEntry(asKey = new ASString(PDFDocEncoding.fromUnicodeString((String)(keyStr = "<" + this.getID() + ">" + key), (boolean)true)));
if (fileSpec != null) {
return fileSpec;
}
if (PDFCollectionUtil.isRootFolder(this)) {
asKey = new ASString(PDFDocEncoding.fromUnicodeString((String)key, (boolean)true));
return nefTree.getEntry(asKey);
}
return null;
}
public void add(PDFCollectionFolder folder, String key, PDFFileSpecification value) throws PDFInvalidDocumentException, PDFIOException, PDFInvalidParameterException, PDFSecurityException {
PDFNamedEmbeddedFiles nefTree = PDFCollectionUtil.getNEFTree(this.getPDFDocument());
String keyStr = "<" + folder.getID() + ">" + key;
PDFText pdfKey = PDFText.newInstance(keyStr, true, this.getPDFDocument());
nefTree.addEntry(new ASString(pdfKey.stringValue()), value);
}
public byte[] findKey(String key) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
PDFNamedEmbeddedFiles nefTree = PDFCollectionUtil.getNEFTree(this.getPDFDocument());
byte[] notFound = new byte[]{};
String folderKey = "<" + this.getID() + ">" + key;
byte[] keyFound = nefTree.findKey(folderKey);
if (keyFound.length != 0) {
return keyFound;
}
if (PDFCollectionUtil.isRootFolder(this)) {
return nefTree.findKey(key);
}
return notFound;
}
protected void setID(long ID) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
this.setDictionaryIntValue(k_ID, ID);
}
public int getID() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
if (this.dictionaryContains(k_ID)) {
return this.getDictionaryIntValue(k_ID);
}
throw new PDFInvalidDocumentException("ID is a required key in Folder");
}
private void setParent(PDFCollectionFolder parent) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
if (parent != null) {
this.setDictionaryValue(k_Parent, parent);
ArrayList children = parent.getChildren();
if (children.isEmpty()) {
parent.setDictionaryValue(k_Child, this);
} else {
PDFCollectionFolder lastChild = (PDFCollectionFolder)children.get(children.size() - 1);
lastChild.setDictionaryValue(k_Next, this);
}
}
}
public PDFCollectionFolder getParent() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
if (this.dictionaryContains(k_Parent)) {
return PDFCollectionFolder.getInstance(this.getDictionaryValue(k_Parent));
}
return null;
}
public PDFCollectionFolder getChild() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
if (this.dictionaryContains(k_Child)) {
return PDFCollectionFolder.getInstance(this.getDictionaryValue(k_Child));
}
return null;
}
public void setChild(PDFCollectionFolder child) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
if (child == null) {
this.removeValue(k_Child);
} else {
this.setDictionaryValue(k_Child, child);
}
}
public PDFCollectionFolder getNext() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
if (this.dictionaryContains(k_Next)) {
return PDFCollectionFolder.getInstance(this.getDictionaryValue(k_Next));
}
return null;
}
public void setNext(PDFCollectionFolder next) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
if (next == null) {
this.removeValue(k_Next);
} else {
this.setDictionaryValue(k_Next, next);
}
}
protected CosArray getFree() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
if (this.dictionaryContains(k_Free)) {
return this.getDictionaryArrayValue(k_Free);
}
return null;
}
protected void setFree(CosArray free) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
if (free == null) {
this.removeValue(k_Free);
} else {
this.setDictionaryArrayValue(k_Free, free);
}
}
private boolean uniqueName(PDFCollectionFolder parent, String name) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
if (parent == null) {
return true;
}
ArrayList siblings = parent.getChildren();
for (PDFCollectionFolder sibling : siblings) {
if (!sibling.getName().equals(name)) continue;
return false;
}
return true;
}
}