PDFStructureNode.java
12.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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* 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.PDFSecurityException
* com.adobe.internal.pdftoolkit.core.types.ASName
*/
package com.adobe.internal.pdftoolkit.pdf.interchange.structure;
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.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.interchange.structure.PDFStructureContentArray;
import com.adobe.internal.pdftoolkit.pdf.interchange.structure.PDFStructureContentFactory;
import com.adobe.internal.pdftoolkit.pdf.interchange.structure.PDFStructureContentInterface;
import com.adobe.internal.pdftoolkit.pdf.interchange.structure.PDFStructureContentIterator;
import com.adobe.internal.pdftoolkit.pdf.interchange.structure.PDFStructureElement;
import com.adobe.internal.pdftoolkit.pdf.interchange.structure.PDFStructureRole;
import com.adobe.internal.pdftoolkit.pdf.interchange.structure.PDFStructureRoleType;
import com.adobe.internal.pdftoolkit.pdf.interchange.structure.PDFStructureType;
import com.adobe.internal.pdftoolkit.pdf.interchange.structure.PDFStructureUtils;
import com.adobe.internal.pdftoolkit.pdf.page.PDFPage;
import com.adobe.internal.pdftoolkit.pdf.utils.PDFUtil;
import java.util.List;
public abstract class PDFStructureNode
extends PDFCosDictionary {
protected PDFStructureNode(CosObject cosObj) throws PDFInvalidDocumentException {
super(cosObj);
}
protected PDFStructureNode(PDFDocument pdfDoc) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
super((CosObject)PDFCosObject.newCosDictionary(pdfDoc));
}
public abstract PDFStructureType getStructureType();
public void removeKids() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
this.removeValue(ASName.k_K);
}
public PDFStructureContentInterface getContent() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
return PDFStructureContentFactory.getInstance(this.getDictionaryValue(ASName.k_K));
}
public void addContent(PDFStructureContentInterface content) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
PDFStructureContentInterface rootContent = this.getContent();
CosObject dict = this.getCosObject();
if (dict instanceof CosDictionary) {
if (rootContent == null) {
this.setDictionaryValue(ASName.k_K, content.getCosObject());
} else {
if (!rootContent.getStructureType().equals(PDFStructureType.Array)) {
rootContent = PDFStructureContentArray.newInstance(this.getPDFDocument(), rootContent);
this.setDictionaryValue(ASName.k_K, rootContent.getCosObject());
}
PDFStructureContentIterator contentIterator = content.contentIterator();
while (contentIterator.hasNext()) {
((PDFStructureContentArray)rootContent).add(contentIterator.next());
}
}
}
}
public void addContent(int index, PDFStructureContentInterface content) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
PDFStructureContentInterface rootContent = this.getContent();
CosObject dict = this.getCosObject();
if (dict instanceof CosDictionary) {
if (rootContent == null) {
this.setDictionaryValue(ASName.k_K, content.getCosObject());
} else {
if (!rootContent.getStructureType().equals(PDFStructureType.Array)) {
rootContent = PDFStructureContentArray.newInstance(this.getPDFDocument(), rootContent);
this.setDictionaryValue(ASName.k_K, rootContent.getCosObject());
}
PDFStructureContentIterator contentIterator = content.contentIterator();
while (contentIterator.hasNext()) {
((PDFStructureContentArray)rootContent).add(index, contentIterator.next());
}
}
}
}
public int getContentIndex(PDFStructureContentInterface searchedElement, boolean searchLast) throws PDFIOException, PDFSecurityException, PDFInvalidDocumentException {
int idxSearchElement = -1;
boolean found = false;
int count = -1;
int idxNonElementTypeBeforeSearched = -1;
int idxNonElementTypeAfterSearched = -1;
if (searchedElement == null) {
return idxSearchElement;
}
PDFStructureContentInterface parentContent = this.getContent();
CosObject dict = this.getCosObject();
if (dict instanceof CosDictionary) {
if (parentContent == null) {
return idxSearchElement;
}
if (!parentContent.getStructureType().equals(PDFStructureType.Array)) {
return 0;
}
PDFStructureContentIterator contentIterator = parentContent.contentIterator();
while (contentIterator.hasNext()) {
++count;
PDFStructureContentInterface curElem = contentIterator.next();
if (curElem.getStructureType() != PDFStructureType.Element) {
idxNonElementTypeBeforeSearched = count;
if (found) {
idxNonElementTypeAfterSearched = count;
}
}
if (PDFUtil.isPDFCosObjectRefEqual(curElem.getPDFCosObject(), searchedElement.getPDFCosObject())) {
idxSearchElement = count;
found = true;
if (!searchLast) break;
idxNonElementTypeAfterSearched = -1;
continue;
}
if (curElem.getStructureType() != PDFStructureType.Element) continue;
idxNonElementTypeBeforeSearched = -1;
if (!found) continue;
break;
}
}
if (idxNonElementTypeBeforeSearched != -1 && !searchLast && idxNonElementTypeBeforeSearched < idxSearchElement) {
idxSearchElement = idxNonElementTypeBeforeSearched;
}
if (idxNonElementTypeAfterSearched != -1 && searchLast && idxNonElementTypeAfterSearched > idxSearchElement) {
idxSearchElement = idxNonElementTypeAfterSearched;
}
return idxSearchElement;
}
public PDFStructureElement getFirstElement(PDFStructureRoleType type) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
PDFStructureRole role = null;
PDFStructureElement structElement = null;
if (this.getStructureType() == PDFStructureType.Element) {
structElement = (PDFStructureElement)this;
role = PDFStructureUtils.getRole(structElement);
}
if (role == null) {
return null;
}
if (type.contains(role.getType())) {
return structElement;
}
PDFStructureContentInterface kids = this.getContent();
if (kids == null) {
return null;
}
PDFStructureContentIterator kidsIter = kids.contentIterator();
while (kidsIter.hasNext()) {
PDFStructureElement nextElem;
PDFStructureContentInterface nextNode = kidsIter.next();
if (nextNode.getStructureType() != PDFStructureType.Element || (nextElem = ((PDFStructureElement)nextNode).getFirstElement(type)) == null) continue;
return nextElem;
}
return null;
}
public PDFStructureElement getFirstElement(PDFStructureContentArray pageElemArray, List matchedElems) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
return this.getFirstOrLastElement(null, pageElemArray, matchedElems);
}
public PDFStructureElement getLastElement(PDFStructureRoleType type) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
PDFStructureContentInterface kids = this.getContent();
if (kids == null) {
return null;
}
PDFStructureContentIterator kidsIter = kids.contentIterator();
while (kidsIter.hasNext()) {
PDFStructureContentInterface next = kidsIter.next();
if (next.getStructureType() != PDFStructureType.Element) {
return this.getStructureType() != PDFStructureType.Element ? null : (PDFStructureElement)this;
}
PDFStructureElement afterNext = ((PDFStructureElement)next).getLastElement(type);
if (afterNext == null) continue;
return afterNext;
}
return null;
}
public PDFStructureElement getLastElement(PDFPage page, PDFStructureContentArray pageElemArray, List matchedElems) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
return this.getFirstOrLastElement(page, pageElemArray, matchedElems);
}
public PDFStructureNode getNodeBefore(PDFStructureRole role) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
if (this.getStructureType() == PDFStructureType.Root) {
return this;
}
if (this.getStructureType() != PDFStructureType.Element) {
return null;
}
PDFStructureNode parent = ((PDFStructureElement)this).getParent();
if (parent.getStructureType() == PDFStructureType.Element && PDFStructureUtils.getRole((PDFStructureElement)parent) != role) {
return parent;
}
return parent.getNodeBefore(role);
}
/*
* Exception decompiling
*/
private PDFStructureElement getFirstOrLastElement(PDFPage page, PDFStructureContentArray pageElemArray, List matchedElems) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
// This method has failed to decompile. When submitting a bug report, please provide this stack trace, and (if you hold appropriate legal rights) the relevant class file.
// org.benf.cfr.reader.util.ConfusedCFRException: Tried to end blocks [1[UNCONDITIONALDOLOOP]], but top level block is 3[UNCONDITIONALDOLOOP]
// org.benf.cfr.reader.bytecode.analysis.opgraph.Op04StructuredStatement.processEndingBlocks(Op04StructuredStatement.java:397)
// org.benf.cfr.reader.bytecode.analysis.opgraph.Op04StructuredStatement.buildNestedBlocks(Op04StructuredStatement.java:449)
// org.benf.cfr.reader.bytecode.analysis.opgraph.Op03SimpleStatement.createInitialStructuredBlock(Op03SimpleStatement.java:2877)
// org.benf.cfr.reader.bytecode.CodeAnalyser.getAnalysisInner(CodeAnalyser.java:825)
// org.benf.cfr.reader.bytecode.CodeAnalyser.getAnalysisOrWrapFail(CodeAnalyser.java:217)
// org.benf.cfr.reader.bytecode.CodeAnalyser.getAnalysis(CodeAnalyser.java:162)
// org.benf.cfr.reader.entities.attributes.AttributeCode.analyse(AttributeCode.java:95)
// org.benf.cfr.reader.entities.Method.analyse(Method.java:355)
// org.benf.cfr.reader.entities.ClassFile.analyseMid(ClassFile.java:768)
// org.benf.cfr.reader.entities.ClassFile.analyseTop(ClassFile.java:700)
// org.benf.cfr.reader.Main.doJar(Main.java:134)
// org.benf.cfr.reader.Main.main(Main.java:189)
throw new IllegalStateException("Decompilation failed");
}
public ASName getStructureNameType() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
return this.getDictionaryNameValue(ASName.k_S);
}
}