JOParser.java
12.5 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.pdfg.exception.ErrorCode
* com.adobe.pdfg.exception.JobOptionsParseException
* com.adobe.pdfg.logging.PDFGLogger
*/
package com.adobe.pdfg.joboptions.parser;
import com.adobe.pdfg.exception.ErrorCode;
import com.adobe.pdfg.exception.JobOptionsParseException;
import com.adobe.pdfg.joboptions.parser.ParamSpecs;
import com.adobe.pdfg.logging.PDFGLogger;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Stack;
public class JOParser {
private static final String DICTIONARY = "dictionary";
private static final String DICTKEY = "dictionarykey";
private static final String DICTVALUE = "dictionaryvalue";
private static final String ARRAY = "array";
private static final String ARRAYVALUE = "arrayvalue";
private static final String IMPOSSIBLE = "impossible";
private static final String LASTCHARACTER = "lastcharacter";
private static PDFGLogger pdfgLogger = PDFGLogger.getPDFGLogger(JOParser.class);
private Stack stateStck = new Stack();
private Stack dataStack = new Stack();
private Map jobOptionsMap = null;
public void parse(String jobStr) throws JobOptionsParseException {
this.jobOptionsMap = new HashMap();
StringReader fis = null;
try {
fis = new StringReader(jobStr);
this.readObject(fis);
this.postParseProcess();
}
catch (Exception e) {
pdfgLogger.trace(e.getMessage(), null, (Throwable)e);
this.jobOptionsMap = null;
throw new JobOptionsParseException(e.getMessage());
}
finally {
if (fis != null) {
fis.close();
fis = null;
}
}
}
public Map getJobOptionsMap() {
return this.jobOptionsMap;
}
private void readObject(StringReader fis) throws JobOptionsParseException, IOException {
block16 : {
int lastch = 0;
boolean stringRead = false;
do {
char ch;
if (!stringRead) {
lastch = fis.read();
}
stringRead = false;
if (lastch <= 0) break block16;
if (Character.isWhitespace((char)lastch)) {
if (this.peek().equals("dictionary") || !this.peek().equals("array")) continue;
continue;
}
if (lastch == 60) {
ch = '\u0000';
ch = (char)fis.read();
if (ch == '<') {
this.stateStck.push("dictionary");
this.dataStack.push(new HashMap());
continue;
}
if (JOParser.isHexLetter(ch)) {
HashMap fillMap = new HashMap();
stringRead = this.readString(ch, fis, fillMap, true);
if (!stringRead) continue;
lastch = ((Character)fillMap.get("lastcharacter")).charValue();
continue;
}
throw new JobOptionsParseException(ErrorCode.ExpectedLessThanCharacterInJobOption, (Object)new Character(ch));
}
if (lastch == 62) {
ch = '\u0000';
ch = (char)fis.read();
if (ch != '>') {
throw new JobOptionsParseException(ErrorCode.ExpectedGreaterThanCharacterInJobOption, (Object)new Character(ch));
}
if (this.peek().equals("dictionary")) {
Map hmap;
this.stateStck.pop();
if (this.peek().equals("dictionarykey")) {
this.stateStck.pop();
hmap = (Map)this.dataStack.pop();
String key = (String)this.dataStack.pop();
((Map)this.dataStack.peek()).put(key, hmap);
continue;
}
if (this.peek().equals("array")) {
hmap = (Map)this.dataStack.pop();
((List)this.dataStack.peek()).add(hmap);
continue;
}
if (this.peek().equals("impossible")) continue;
throw new JobOptionsParseException(ErrorCode.IllegalDictionaryLocation);
}
throw new JobOptionsParseException(ErrorCode.UnexpectedDictionaryEnd);
}
if (lastch == 91) {
this.stateStck.push("array");
this.dataStack.push(new ArrayList());
continue;
}
if (lastch == 93) {
if (this.peek().equals("array")) {
this.stateStck.pop();
if (this.peek().equals("dictionarykey")) {
this.stateStck.pop();
List lst = (List)this.dataStack.pop();
String key = (String)this.dataStack.pop();
((Map)this.dataStack.peek()).put(key, lst);
continue;
}
if (this.peek().equals("array")) {
List lst = (List)this.dataStack.pop();
((List)this.dataStack.peek()).add(lst);
continue;
}
throw new JobOptionsParseException(ErrorCode.IllegalArrayLocation);
}
throw new JobOptionsParseException(ErrorCode.UnexpectedArrayEnd);
}
if (!JOParser.isString((char)lastch)) break;
HashMap fillMap = new HashMap();
stringRead = this.readString((char)lastch, fis, fillMap, false);
if (!stringRead) continue;
lastch = ((Character)fillMap.get("lastcharacter")).charValue();
} while (true);
throw new JobOptionsParseException(ErrorCode.UnknownCharacterInJobOption, (Object)new Integer(lastch));
}
}
private boolean readString(char lastch, StringReader fis, Map retMap, boolean isHex) throws JobOptionsParseException, IOException {
char tch = '\u0000';
StringBuffer sb = new StringBuffer();
if (lastch != '/' && lastch != '(') {
sb.append(lastch);
}
boolean retVal = false;
if (this.peek().equals("dictionary")) {
this.stateStck.push("dictionarykey");
} else if (this.peek().equals("dictionarykey")) {
this.stateStck.push("dictionaryvalue");
} else if (this.peek().equals("array")) {
this.stateStck.push("arrayvalue");
} else if (this.peek().equals("impossible") && (this.dataStack.isEmpty() || !Map.class.isAssignableFrom(this.dataStack.peek().getClass()))) {
throw new JobOptionsParseException(ErrorCode.IllegalDataLocationInJobOption);
}
int bracs = 1;
int fromStream = -1;
while ((fromStream = fis.read()) != -1) {
tch = (char)fromStream;
if (lastch == '(') {
if (tch == '(') {
++bracs;
} else if (tch == ')' && --bracs == 0) {
break;
}
} else if (lastch != '(' && Character.isWhitespace(tch) || JOParser.isReserved(tch)) break;
sb.append(tch);
}
if (fromStream == -1) {
tch = '\u0000';
}
if (this.peek().equals("impossible")) {
Map hmap = (Map)this.dataStack.pop();
this.jobOptionsMap.put(sb.toString(), hmap);
} else {
Object strObj = this.getStringObject(sb.toString(), lastch, isHex);
if (this.peek().equals("dictionarykey")) {
this.dataStack.push(strObj);
} else if (this.peek().equals("dictionaryvalue")) {
this.stateStck.pop();
this.stateStck.pop();
String key = (String)this.dataStack.pop();
((Map)this.dataStack.peek()).put(key, strObj);
} else if (this.peek().equals("arrayvalue")) {
this.stateStck.pop();
((List)this.dataStack.peek()).add(strObj);
}
}
if (tch != ')' && tch != '>') {
retMap.put("lastcharacter", new Character(tch));
retVal = true;
}
return retVal;
}
public static boolean isReserved(char ch) {
return ch == '[' || ch == ']' || ch == '<' || ch == '>';
}
public static boolean isString(char ch) {
return ch == '/' || ch == '(' || ch == '-' || Character.isLetterOrDigit(ch);
}
public static boolean isHexLetter(char ch) {
return Character.isDigit(ch) || ch >= 'a' && ch <= 'f' || ch >= 'A' && ch <= 'F';
}
/*
* Enabled force condition propagation
* Lifted jumps to return sites
*/
private Object getStringObject(String strRead, char lastch, boolean isHex) throws JobOptionsParseException {
String obj = null;
if (isHex) {
return strRead;
}
if (lastch == '/') {
return strRead;
}
if (lastch == '(') {
return strRead.replaceAll("\\\\050", "(").replaceAll("\\\\051", ")");
}
if (lastch == '-') return new Double(strRead);
if (lastch == '.') return new Double(strRead);
if (Character.isDigit(lastch)) {
return new Double(strRead);
}
if (lastch != 't') {
if (lastch != 'f') throw new JobOptionsParseException(ErrorCode.UnknownLiteralInJobOption, (Object)strRead);
}
if (strRead.equalsIgnoreCase("true")) {
return Boolean.TRUE;
}
if (!strRead.equalsIgnoreCase("false")) throw new JobOptionsParseException(ErrorCode.UnknownLiteralInJobOption, (Object)strRead);
return Boolean.FALSE;
}
private String peek() {
if (this.stateStck.isEmpty()) {
return "impossible";
}
return (String)this.stateStck.peek();
}
private void postParseProcess() {
Object ispec;
int i;
for (i = 0; i < ParamSpecs.INTPARAMS.length; ++i) {
ispec = ParamSpecs.INTPARAMS[i];
this.makeInteger(ispec.getKeyName());
}
for (i = 0; i < ParamSpecs.INTSETPARAMS.length; ++i) {
ispec = ParamSpecs.INTSETPARAMS[i];
this.makeInteger(ispec.getKeyName());
}
this.makeInteger("JPEG2000ColorACSImageDict");
this.makeInteger("JPEG2000ColorImageDict");
this.makeInteger("JPEG2000GrayACSImageDict");
this.makeInteger("JPEG2000GrayImageDict");
this.makeInteger("MonoImageDict");
Map m = (Map)this.jobOptionsMap.get("setpagedevice");
List l = (List)m.get("HWResolution");
l.set(0, this.getInteger(l.get(0)));
l.set(1, this.getInteger(l.get(1)));
}
private void makeInteger(String key) {
Map m = (Map)this.jobOptionsMap.get("setdistillerparams");
Object val = m.get(key);
if (val != null) {
if (val instanceof Map) {
m.put(key, this.makeIntegerMap((Map)val));
} else if (val instanceof List) {
m.put(key, this.makeIntegerList((List)val));
} else {
m.put(key, this.getInteger(val));
}
}
}
private Map makeIntegerMap(Map map) {
Map m = (Map)this.jobOptionsMap.get("setdistillerparams");
HashMap t = new HashMap(map.size());
for (Map.Entry entry : map.entrySet()) {
t.put(entry.getKey(), this.getInteger(entry.getValue()));
}
return t;
}
private List makeIntegerList(List lst) {
Map m = (Map)this.jobOptionsMap.get("setdistillerparams");
ArrayList<Integer> t = new ArrayList<Integer>(lst.size());
Iterator iter = lst.iterator();
while (iter.hasNext()) {
t.add(this.getInteger(iter.next()));
}
return t;
}
private Integer getInteger(Object dbl) {
double d = (Double)dbl;
return d >= 0.0 ? new Integer((int)(d + 0.5)) : new Integer((int)(d - 0.5));
}
}