PDFCollectionUtil.java
20.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
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.internal.pdftoolkit.core.cos.CosArray
* com.adobe.internal.pdftoolkit.core.cos.CosDocument
* com.adobe.internal.pdftoolkit.core.cos.CosNumeric
* 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.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.CosDocument;
import com.adobe.internal.pdftoolkit.core.cos.CosNumeric;
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.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.PDFCatalog;
import com.adobe.internal.pdftoolkit.pdf.document.PDFCosUtils;
import com.adobe.internal.pdftoolkit.pdf.document.PDFDocument;
import com.adobe.internal.pdftoolkit.pdf.document.PDFFileSpecification;
import com.adobe.internal.pdftoolkit.pdf.document.PDFNameDictionary;
import com.adobe.internal.pdftoolkit.pdf.document.PDFNamedEmbeddedFiles;
import com.adobe.internal.pdftoolkit.pdf.document.PDFText;
import com.adobe.internal.pdftoolkit.pdf.document.PDFTree;
import com.adobe.internal.pdftoolkit.pdf.interactive.navigation.collection.PDFCollectionColors;
import com.adobe.internal.pdftoolkit.pdf.interactive.navigation.collection.PDFCollectionField;
import com.adobe.internal.pdftoolkit.pdf.interactive.navigation.collection.PDFCollectionFieldType;
import com.adobe.internal.pdftoolkit.pdf.interactive.navigation.collection.PDFCollectionFolder;
import com.adobe.internal.pdftoolkit.pdf.interactive.navigation.collection.PDFCollectionItemData;
import com.adobe.internal.pdftoolkit.pdf.interactive.navigation.collection.PDFCollectionSchema;
import com.adobe.internal.pdftoolkit.pdf.interactive.navigation.collection.PDFCollectionSubItem;
import com.adobe.internal.pdftoolkit.pdf.interactive.navigation.collection.PDFPortableCollection;
import com.adobe.internal.pdftoolkit.pdf.utils.PDFUtil;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
/*
* This class specifies class file version 49.0 but uses Java 6 signatures. Assumed Java 6.
*/
public class PDFCollectionUtil {
static final long FOLDER_FREE_ID_MIN = 0;
static final long FOLDER_FREE_ID_MAX = Integer.MAX_VALUE;
static void setColor(double[] color, ASName name, PDFCollectionColors pdfCollectionColors) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException, PDFInvalidParameterException {
if (color == null) {
pdfCollectionColors.removeValue(name);
return;
}
if (color.length != 3) {
throw new PDFInvalidDocumentException(name.toString() + " is not an array of 3 numbers");
}
ArrayList<CosNumeric> l = new ArrayList<CosNumeric>(3);
for (int i = 0; i < 3; ++i) {
if (color[i] < 0.0 || color[i] > 1.0) {
throw new PDFInvalidDocumentException(name.toString() + " color needs to be in the range 0 to 1");
}
l.add(i, pdfCollectionColors.getCosObject().getDocument().createCosNumeric(color[i]));
}
pdfCollectionColors.setDictionaryValue(name, (CosObject)PDFCosUtils.newCosArray(pdfCollectionColors.getPDFDocument(), l));
}
static double[] getColor(ASName name, PDFCollectionColors pdfCollectionColors) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
if (pdfCollectionColors.dictionaryContains(name)) {
CosArray colorCosArray = pdfCollectionColors.getDictionaryArrayValue(name);
if (colorCosArray.size() != 3) {
throw new PDFInvalidDocumentException(name.toString() + " is not an array of 3 numbers");
}
double[] colorArray = colorCosArray.getArrayDouble();
for (int i = 0; i < 3; ++i) {
if (colorArray[i] >= 0.0 && colorArray[i] <= 1.0) continue;
throw new PDFInvalidDocumentException(name.toString() + " color needs to be in the range 0 to 1");
}
return colorArray;
}
return null;
}
static boolean checkPDFCollectionItemData(ASName name, PDFCollectionItemData collectionItemData) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
CosObject data = collectionItemData.getCosObject();
if (collectionItemData.mChecked) {
return collectionItemData.mIsValid;
}
if (name.equals((Object)ASName.k_Type)) {
return false;
}
boolean goodData = false;
PDFPortableCollection collection = collectionItemData.getPDFDocument().requireCatalog().getCollection();
if (collection == null) {
return goodData;
}
PDFCollectionSchema schema = collection.getSchema();
if (schema == null) {
return goodData;
}
PDFCollectionField field = schema.get(name);
if (field == null) {
return goodData;
}
PDFCollectionFieldType fieldType = field.getFieldType();
if (fieldType.equal(PDFCollectionFieldType.text) && data.getType() == 4) {
collectionItemData.mIsText = true;
goodData = true;
}
if (fieldType.equal(PDFCollectionFieldType.date) && data.getType() == 4) {
collectionItemData.mIsDate = true;
goodData = true;
}
if (fieldType.equal(PDFCollectionFieldType.number) && data.getType() == 1) {
collectionItemData.mIsNumber = true;
goodData = true;
}
collectionItemData.mIsValid = goodData;
collectionItemData.mChecked = true;
return goodData;
}
public static PDFCollectionItemData copyTo(PDFDocument targetDoc, PDFCollectionItemData srcData) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
PDFCollectionItemData targetData = null;
if (srcData.isDate()) {
targetData = PDFCollectionItemData.newInstance(targetDoc, srcData.getDate(), srcData.mFieldName);
} else if (srcData.isNumber()) {
targetData = PDFCollectionItemData.newInstance(targetDoc, srcData.getNumber(), srcData.mFieldName);
} else if (srcData.isText()) {
targetData = PDFCollectionItemData.newInstance(targetDoc, srcData.getText(), srcData.mFieldName);
}
return targetData;
}
public static PDFCollectionSubItem copyTo(PDFDocument targetDoc, PDFCollectionSubItem srcSubItem) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
PDFCollectionItemData srcData = srcSubItem.getData();
return PDFCollectionSubItem.newInstance(targetDoc, srcData != null ? PDFCollectionUtil.copyTo(targetDoc, srcData) : null, srcSubItem.getPrefix(), srcSubItem.mFieldKey);
}
public static List<PDFTree<ASString, PDFFileSpecification>> getEmbeddedFilesFromPDFDoc(PDFDocument pdfDoc) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
PDFNameDictionary names = pdfDoc.requireCatalog().getNameDictionary();
if (names == null) {
return null;
}
PDFNamedEmbeddedFiles embeddedFilesTree = names.getNamedEmbeddedFiles();
if (embeddedFilesTree == null) {
return null;
}
ArrayList<PDFTree<ASString, PDFFileSpecification>> embeddedFiles = new ArrayList<PDFTree<ASString, PDFFileSpecification>>();
Iterator filesIter = embeddedFilesTree.iterator();
while (filesIter.hasNext()) {
embeddedFiles.add(filesIter.next());
}
return embeddedFiles;
}
public static List<PDFTree<ASString, PDFFileSpecification>> sortEmbeddedFiles(List<PDFTree<ASString, PDFFileSpecification>> embeddedFiles, Comparator itemComparator) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
Collections.sort(embeddedFiles, itemComparator);
return embeddedFiles;
}
public static ArrayList sortFolders(ArrayList folders, Comparator itemComparator) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
Object[] foldersArray = folders.toArray();
Arrays.sort(foldersArray, itemComparator);
ArrayList<Object> sortedFolders = new ArrayList<Object>();
for (int i = 0; i < foldersArray.length; ++i) {
sortedFolders.add(foldersArray[i]);
}
return sortedFolders;
}
static ArrayList compileUsedIDList(PDFCollectionFolder root) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
ArrayList<Long> IDList = new ArrayList<Long>();
if (root == null) {
return IDList;
}
IDList.add(Long.valueOf(root.getID()));
ArrayList children = root.getChildren();
for (PDFCollectionFolder child : children) {
ArrayList childIDList = PDFCollectionUtil.compileUsedIDList(child);
IDList.addAll(childIDList);
}
return IDList;
}
static long getNewFolderID(PDFDocument pdfDoc) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
long ID = 1;
PDFPortableCollection coll = pdfDoc.requireCatalog().getCollection();
if (coll == null) {
throw new PDFInvalidDocumentException("Document does not have a Collections dictionary");
}
PDFCollectionFolder root = coll.getRootFolder();
if (root == null) {
throw new PDFInvalidDocumentException("Document does not have a Root Folder");
}
CosArray freeIDObj = root.getFree();
if (freeIDObj != null) {
long[] freeIDs = freeIDObj.getArrayLong();
if (freeIDs.length % 2 != 0) {
throw new PDFInvalidDocumentException("Free in Root Folder in Collection dictionary should have an even number of elements");
}
long min = freeIDs[0];
long max = freeIDs[1];
if (min <= max) {
ID = min++;
if (min > max) {
freeIDs[0] = -1;
freeIDs[1] = -1;
} else {
freeIDs[0] = min;
}
} else {
throw new PDFInvalidDocumentException("Free in Root folder has invalid values");
}
ArrayList<CosNumeric> newFreeList = new ArrayList<CosNumeric>();
for (int i = 0; i < freeIDs.length; ++i) {
if (freeIDs[i] < 0) continue;
newFreeList.add(pdfDoc.getCosDocument().createCosNumeric(freeIDs[i]));
}
CosArray newFreeObj = pdfDoc.getCosDocument().createCosArray(newFreeList);
root.setFree(newFreeObj);
} else {
ArrayList usedIDList = PDFCollectionUtil.compileUsedIDList(root);
for (long i = 0; i < Integer.MAX_VALUE; ++i) {
boolean used = false;
for (int j = 0; j < usedIDList.size() && !used; ++j) {
Long usedID = (Long)usedIDList.get(j);
if (usedID != i) continue;
used = true;
}
if (used) continue;
ID = i;
break;
}
}
if (ID < 0) {
throw new PDFInvalidDocumentException("Could not allocate a valid ID for the folder");
}
return ID;
}
public static boolean isRootFolder(PDFCollectionFolder folder) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
PDFPortableCollection coll;
CosObject rootFolderCosObj;
CosObject folderCosObj = folder.getCosObject();
return folderCosObj == (rootFolderCosObj = (coll = folder.getPDFDocument().requireCatalog().getCollection()).getRootFolder().getCosObject());
}
public static List<PDFTree<ASString, PDFFileSpecification>> getEmbeddedFiles(PDFCollectionFolder folder) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException, PDFParseException {
if (folder == null) {
return null;
}
long ID = folder.getID();
String IDStr = "<" + ID + ">";
PDFNameDictionary names = folder.getPDFDocument().requireCatalog().getNameDictionary();
if (names == null) {
return null;
}
PDFNamedEmbeddedFiles embeddedFilesTree = names.getNamedEmbeddedFiles();
if (embeddedFilesTree == null) {
return null;
}
ArrayList<PDFTree<ASString, PDFFileSpecification>> embeddedFiles = new ArrayList<PDFTree<ASString, PDFFileSpecification>>();
Iterator filesIter = embeddedFilesTree.iterator();
while (filesIter.hasNext()) {
PDFTree.Entry curEmbeddedFile = (PDFTree.Entry)((Object)filesIter.next());
ASString key = (ASString)curEmbeddedFile.getKey();
boolean isPDFTextStr = true;
String keyStr = null;
try {
keyStr = PDFDocEncoding.toUnicodeString((byte[])key.getBytes());
}
catch (PDFParseException e) {
isPDFTextStr = false;
}
if (PDFCollectionUtil.isRootFolder(folder)) {
if (isPDFTextStr && !keyStr.startsWith(IDStr) && PDFCollectionUtil.hasValidFolderID(keyStr)) continue;
embeddedFiles.add((PDFTree<ASString, PDFFileSpecification>)((Object)curEmbeddedFile));
continue;
}
if (keyStr == null || !keyStr.startsWith(IDStr)) continue;
embeddedFiles.add((PDFTree<ASString, PDFFileSpecification>)((Object)curEmbeddedFile));
}
return embeddedFiles;
}
static boolean hasValidFolderID(String key) {
String regex = "<[0-9]*>.*";
return key.matches(regex);
}
static PDFNamedEmbeddedFiles getNEFTree(PDFDocument pdfDocument) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
PDFNameDictionary nameDictionary = pdfDocument.requireCatalog().getNameDictionary();
if (nameDictionary != null) {
return nameDictionary.getNamedEmbeddedFiles();
}
return null;
}
static String makeNamedEmbeddedFileKey(String key, PDFCollectionFolder pdfCollectionFolder) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
return "<" + pdfCollectionFolder.getID() + ">" + key;
}
public static void addFileToPDFCollectionFolder(String key, PDFFileSpecification value, PDFCollectionFolder folder) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException, PDFInvalidParameterException {
PDFNameDictionary names = folder.getPDFDocument().requireCatalog().getNameDictionary();
PDFNamedEmbeddedFiles nef = null;
if (names == null) {
names = PDFNameDictionary.newInstance(folder.getPDFDocument());
folder.getPDFDocument().requireCatalog().setNameDictionary(names);
nef = PDFNamedEmbeddedFiles.newInstance(folder.getPDFDocument());
names.setNamedEmbeddedFiles(nef);
} else {
nef = names.getNamedEmbeddedFiles();
if (nef == null) {
nef = PDFNamedEmbeddedFiles.newInstance(folder.getPDFDocument());
names.setNamedEmbeddedFiles(nef);
}
}
nef.addEntry(new ASString(PDFCollectionUtil.makeNamedEmbeddedFileKey(key, folder)), value);
}
public static void deleteFile(String key, PDFDocument pdfDoc) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
PDFNameDictionary names = pdfDoc.requireCatalog().getNameDictionary();
if (names == null) {
return;
}
PDFNamedEmbeddedFiles embeddedFilesTree = names.getNamedEmbeddedFiles();
byte[] keyFound = embeddedFilesTree.findKey(key);
if (keyFound.length != 0) {
ASString asName = new ASString(keyFound);
embeddedFilesTree.removeEntry(asName);
}
pdfDoc.requireCatalog().getNameDictionary().setNamedEmbeddedFiles(embeddedFilesTree);
}
public static void deleteAllFilesInPDFCollectionFolder(PDFCollectionFolder collectionFolder) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException, PDFParseException {
PDFNameDictionary names = collectionFolder.getPDFDocument().requireCatalog().getNameDictionary();
if (names == null) {
return;
}
PDFNamedEmbeddedFiles embeddedFilesTree = names.getNamedEmbeddedFiles();
List<PDFTree<ASString, PDFFileSpecification>> embFiles = PDFCollectionUtil.getEmbeddedFiles(collectionFolder);
for (int i = 0; i < embFiles.size(); ++i) {
ASString asName = (ASString)((PDFTree.Entry)((Object)embFiles.get(i))).getKey();
embeddedFilesTree.removeEntry(asName);
}
collectionFolder.getPDFDocument().requireCatalog().getNameDictionary().setNamedEmbeddedFiles(embeddedFilesTree);
}
private static void deleteFilesRecursively(PDFCollectionFolder folder) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException, PDFParseException {
PDFCollectionUtil.deleteAllFilesInPDFCollectionFolder(folder);
ArrayList children = folder.getChildren();
Iterator iter = children.iterator();
while (iter.hasNext()) {
PDFCollectionUtil.deleteFilesRecursively((PDFCollectionFolder)iter.next());
}
}
public static void removePDFCollectionFolder(PDFCollectionFolder folder) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException, PDFParseException {
PDFCollectionUtil.deleteFilesRecursively(folder);
PDFPortableCollection collection = folder.getPDFDocument().requireCatalog().getCollection();
if (PDFCollectionUtil.isRootFolder(folder)) {
collection.setRootFolder(null);
} else {
PDFCollectionFolder parent = folder.getParent();
if (parent == null) {
throw new PDFInvalidDocumentException("parent of non-root folder cannot be null");
}
PDFCollectionFolder child = parent.getChild();
if (PDFUtil.isPDFCosObjectRefEqual(child, folder)) {
PDFCollectionFolder nextChild = child.getNext();
parent.setChild(nextChild);
} else {
PDFCollectionFolder prevChild = child;
for (child = child.getNext(); child != null; child = child.getNext()) {
if (child.equals(folder)) {
prevChild.setNext(child.getNext());
break;
}
prevChild = child;
}
}
}
}
public static void replace(String key, PDFFileSpecification value, PDFCollectionFolder collectionFolder) throws PDFInvalidDocumentException, PDFIOException, PDFInvalidParameterException, PDFSecurityException {
PDFNamedEmbeddedFiles nefTree = PDFCollectionUtil.getNEFTree(collectionFolder.getPDFDocument());
byte[] keyFound = nefTree.findKey(key);
if (PDFCollectionUtil.isRootFolder(collectionFolder) && keyFound.length != 0) {
PDFText pdfKey = PDFText.newInstance(key, true, collectionFolder.getPDFDocument());
nefTree.replaceEntry(new ASString(pdfKey.stringValue()), value);
return;
}
String keyStr = "<" + collectionFolder.getID() + ">" + key;
PDFText pdfKey = PDFText.newInstance(keyStr, true, collectionFolder.getPDFDocument());
nefTree.replaceEntry(new ASString(pdfKey.stringValue()), value);
}
}