PDFCollectionFolderComparator.java
7.29 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.agl.text.Collator
* com.adobe.internal.pdftoolkit.core.exceptions.PDFIOException
* com.adobe.internal.pdftoolkit.core.exceptions.PDFInvalidDocumentException
* 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
*/
package com.adobe.internal.pdftoolkit.pdf.interactive.navigation.collection;
import com.adobe.agl.text.Collator;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFIOException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFInvalidDocumentException;
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.pdf.document.PDFCatalog;
import com.adobe.internal.pdftoolkit.pdf.document.PDFDocument;
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.PDFCollectionItem;
import com.adobe.internal.pdftoolkit.pdf.interactive.navigation.collection.PDFCollectionItemComparator;
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.PDFCollectionSort;
import com.adobe.internal.pdftoolkit.pdf.interactive.navigation.collection.PDFCollectionSortItem;
import com.adobe.internal.pdftoolkit.pdf.interactive.navigation.collection.PDFCollectionSortIterator;
import com.adobe.internal.pdftoolkit.pdf.interactive.navigation.collection.PDFPortableCollection;
import java.util.Comparator;
public class PDFCollectionFolderComparator
implements Comparator {
private PDFCollectionSortItem[] mSortItems;
private PDFCollectionSchema mSchema = null;
private Collator mCollator;
public PDFCollectionFolderComparator(PDFCollectionSort sort, Collator collator) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
int sortInd = 0;
this.mCollator = collator;
PDFCollectionSortIterator nameIter = sort.getSortIterator();
this.mSortItems = new PDFCollectionSortItem[nameIter.size()];
while (nameIter.hasNext()) {
this.mSortItems[sortInd++] = nameIter.nextItem();
}
PDFPortableCollection collection = sort.getPDFDocument().requireCatalog().getCollection();
if (collection == null) {
throw new PDFInvalidDocumentException("Collection is not defined");
}
this.mSchema = collection.getSchema();
if (this.mSchema == null) {
throw new PDFInvalidDocumentException("Collection Schema is not defined");
}
}
public PDFCollectionFolderComparator(PDFCollectionSort sort) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
this(sort, null);
}
public int compare(Object arg0, Object arg1) {
PDFCollectionFolder first = (PDFCollectionFolder)arg0;
PDFCollectionFolder second = (PDFCollectionFolder)arg1;
int compared = 0;
int sortInd = 0;
while (sortInd < this.mSortItems.length && compared == 0) {
try {
compared = this.compareFolders(first, second, sortInd++);
continue;
}
catch (Exception e) {
throw new RuntimeException(e);
}
}
return compared;
}
private int compareFolders(PDFCollectionFolder first, PDFCollectionFolder second, int sortInd) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException, PDFParseException {
int compared = 0;
PDFCollectionSortItem sortItem = this.mSortItems[sortInd];
int direction = sortItem.getOrder() ? 1 : -1;
ASName key = sortItem.getName();
PDFCollectionField field = this.mSchema.get(key);
if (field == null) {
return - direction;
}
PDFCollectionFieldType fieldType = field.getFieldType();
if (fieldType.isInCollectionItem()) {
PDFCollectionItem firstItem = first.getCollectionItem();
PDFCollectionItem secondItem = second.getCollectionItem();
if (firstItem == null) {
if (secondItem == null) {
return 0;
}
return -1 * direction;
}
if (secondItem == null) {
return 1 * direction;
}
if (firstItem.containsKey((Object)key) && !secondItem.containsKey((Object)key)) {
return 1 * direction;
}
if (!firstItem.containsKey((Object)key) && secondItem.containsKey((Object)key)) {
return -1 * direction;
}
if (!firstItem.containsKey((Object)key) && !secondItem.containsKey((Object)key)) {
return 0;
}
if (fieldType.equal(PDFCollectionFieldType.date)) {
compared = PDFCollectionItemComparator.compareDates(firstItem.getItemData(key).getDate(), secondItem.getItemData(key).getDate());
} else if (fieldType.equal(PDFCollectionFieldType.number)) {
compared = PDFCollectionItemComparator.compareNumbers(firstItem.getItemData(key).getNumber(), secondItem.getItemData(key).getNumber());
} else if (fieldType.equal(PDFCollectionFieldType.text)) {
String firstText = firstItem.getItemData(key).getText();
String secondText = secondItem.getItemData(key).getText();
compared = PDFCollectionItemComparator.compareText(firstText, secondText, this.mCollator);
}
} else if (fieldType.isInFileSpec()) {
if (fieldType.equal(PDFCollectionFieldType.file)) {
String text1 = first.getName();
String text2 = second.getName();
if (text1 != null && text2 != null) {
compared = PDFCollectionItemComparator.compareText(text1, text2, null);
}
} else if (fieldType.equal(PDFCollectionFieldType.Desc)) {
String desc1 = first.getDescription();
String desc2 = second.getDescription();
compared = PDFCollectionItemComparator.compareText(desc1, desc2, null);
}
} else if (fieldType.isInEmbeddedFile()) {
if (fieldType.equal(PDFCollectionFieldType.CreationDate)) {
compared = PDFCollectionItemComparator.compareDates(first.getCreationDate(), second.getCreationDate());
} else if (fieldType.equal(PDFCollectionFieldType.ModDate)) {
compared = PDFCollectionItemComparator.compareDates(first.getModificationDate(), second.getModificationDate());
}
}
return compared * direction;
}
}