PDFUtil.java
4.19 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.internal.pdftoolkit.core.cos.CosObject
* com.adobe.internal.pdftoolkit.core.util.ByteOps
*/
package com.adobe.internal.pdftoolkit.pdf.utils;
import com.adobe.internal.pdftoolkit.core.cos.CosObject;
import com.adobe.internal.pdftoolkit.core.util.ByteOps;
import com.adobe.internal.pdftoolkit.pdf.document.PDFCosObject;
import java.util.ArrayList;
import java.util.Collection;
import java.util.StringTokenizer;
/*
* This class specifies class file version 49.0 but uses Java 6 signatures. Assumed Java 6.
*/
public class PDFUtil {
private static final String RICH_TEXT_START = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><body xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:xfa=\"http://www.xfa.org/schema/xfa-data/1.0/\" xfa:APIVersion=\"2.2\"><p>";
private static final String RICH_TEXT_END = "</p></body>";
public static String toString(byte[] b) {
StringBuilder rslt = new StringBuilder();
for (int i = 0; i < b.length; ++i) {
rslt.append(b[i]);
}
return rslt.toString();
}
public static ArrayList parseNumbers(String numbersString, String separator) {
StringTokenizer numbersTokens = new StringTokenizer(numbersString, separator);
ArrayList<Double> numbers = new ArrayList<Double>();
while (numbersTokens.hasMoreTokens()) {
StringBuilder oneNumber = new StringBuilder(numbersTokens.nextToken());
int numInd = 0;
while (numInd < oneNumber.length()) {
if (!ByteOps.isWhitespace((char)oneNumber.charAt(numInd))) {
++numInd;
continue;
}
oneNumber.delete(numInd, numInd + 1);
}
numbers.add(new Double(oneNumber.toString()));
}
return numbers.isEmpty() ? null : numbers;
}
public static int findStringInArray(String look, String[] lookup) {
for (int strInd = 0; strInd < lookup.length; ++strInd) {
if (!look.equals(lookup[strInd])) continue;
return strInd;
}
return -1;
}
public static String substituteStringFromArray(String look, String[] lookup, String[] substitute) {
int arrayInd = PDFUtil.findStringInArray(look, lookup);
return arrayInd == -1 ? null : substitute[arrayInd];
}
public static String makeRichTextFromString(String simpleString) {
if (simpleString == null) {
return null;
}
StringBuilder bufStr = new StringBuilder("<?xml version=\"1.0\" encoding=\"UTF-8\"?><body xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:xfa=\"http://www.xfa.org/schema/xfa-data/1.0/\" xfa:APIVersion=\"2.2\"><p>".length() + "</p></body>".length() + simpleString.length());
bufStr.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><body xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:xfa=\"http://www.xfa.org/schema/xfa-data/1.0/\" xfa:APIVersion=\"2.2\"><p>");
bufStr.append(simpleString);
bufStr.append("</p></body>");
return bufStr.toString();
}
public static <T extends PDFCosObject> boolean containsPDFCosObjectRef(Collection<T> objectList, T obj) {
for (PDFCosObject nextObject : objectList) {
if (!PDFUtil.isPDFCosObjectRefEqual(obj, nextObject)) continue;
return true;
}
return false;
}
public static boolean isPDFCosObjectRefEqual(PDFCosObject pdfCosObject1, PDFCosObject pdfCosObject2) {
if (pdfCosObject1 == null && pdfCosObject2 == null) {
return true;
}
if (pdfCosObject1 == null || pdfCosObject2 == null) {
return false;
}
if (pdfCosObject1.getClass() != pdfCosObject2.getClass()) {
return false;
}
return pdfCosObject1.getCosObject() == pdfCosObject2.getCosObject();
}
public static <T extends PDFCosObject> int indexOfPDFCosObjectRef(Collection<T> objectList, T obj) {
int index = 0;
for (PDFCosObject nextObject : objectList) {
if (PDFUtil.isPDFCosObjectRefEqual(obj, nextObject)) {
return index;
}
++index;
}
return -1;
}
}