JOWriter.java
4.94 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.pdfg.exception.ErrorCode
* com.adobe.pdfg.exception.JobOptionsWriteException
*/
package com.adobe.pdfg.joboptions.parser;
import com.adobe.pdfg.exception.ErrorCode;
import com.adobe.pdfg.exception.JobOptionsWriteException;
import com.adobe.pdfg.joboptions.parser.JOParser;
import com.adobe.pdfg.joboptions.parser.ParamSpecs;
import java.io.StringWriter;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Set;
public class JOWriter {
private final Map jmap;
private int depth = 0;
private boolean indent = false;
private String currentKey = null;
private static final String ENDLINE = "\r\n";
private static final String DICTSTART = "<<\r\n";
private static final String DICTEND = ">>\r\n";
private static final String LISTSTART = "[\r\n";
private static final String LISTEND = "]\r\n";
private static final String SPACESTRINGSTART = "(";
private static final String SPACESTRINGEND = ")\r\n";
private static final String NAMESTART = "/";
private static final String NAMEEND = " ";
private static final String SIMPLESTRINGEND = "\r\n";
private static final String HEXSTRINGSTART = "<";
private static final String HEXSTRINGEND = ">\r\n";
public JOWriter(Map jm) {
this.jmap = jm;
}
public String write(boolean indent) throws JobOptionsWriteException {
this.depth = 0;
this.indent = indent;
if (this.jmap == null) {
throw new JobOptionsWriteException(ErrorCode.NoJobOptionsMapFound);
}
StringWriter fout = new StringWriter();
Map distMap = (Map)this.jmap.get("setdistillerparams");
if (distMap == null) {
throw new JobOptionsWriteException(ErrorCode.NoDistillerParamsMapFound);
}
Map pageMap = (Map)this.jmap.get("setpagedevice");
if (pageMap == null) {
throw new JobOptionsWriteException(ErrorCode.NoPageParamsMapFound);
}
this.writeMap(distMap, fout);
fout.write("setdistillerparams\r\n");
this.writeMap(pageMap, fout);
fout.write("setpagedevice\r\n");
return fout.toString();
}
private void write(Object val, StringWriter fo) throws JobOptionsWriteException {
if (Map.class.isAssignableFrom(val.getClass())) {
this.writeMap((Map)val, fo);
} else if (List.class.isAssignableFrom(val.getClass())) {
this.writeList((List)val, fo);
} else {
this.writePrimitive(val, fo);
}
}
private void writeMap(Map m, StringWriter fo) throws JobOptionsWriteException {
++this.depth;
fo.write("<<\r\n");
for (Map.Entry entry : m.entrySet()) {
this.currentKey = (String)entry.getKey();
Object val = entry.getValue();
if (val == null) continue;
this.indentOutput(fo);
fo.write("/" + this.currentKey + " ");
this.write(val, fo);
}
--this.depth;
this.indentOutput(fo);
fo.write(">>\r\n");
}
private void writeList(List l, StringWriter fo) throws JobOptionsWriteException {
++this.depth;
fo.write("[\r\n");
ListIterator liter = l.listIterator();
while (liter.hasNext()) {
Object val = liter.next();
this.indentOutput(fo);
this.write(val, fo);
}
--this.depth;
this.indentOutput(fo);
fo.write("]\r\n");
}
private void writePrimitive(Object val, StringWriter fo) throws JobOptionsWriteException {
if (val instanceof Boolean) {
fo.write(val.toString() + "\r\n");
} else if (val instanceof Double || val instanceof Integer) {
fo.write(val.toString() + "\r\n");
} else if (val instanceof String) {
String strVal = (String)val;
if (!ParamSpecs.isStringParam(this.currentKey)) {
fo.write("/" + strVal + "\r\n");
} else if (strVal.length() != 0 && this.isHexString(strVal)) {
fo.write("<" + strVal + ">\r\n");
} else {
fo.write("(" + strVal.replaceAll("[(]", "\\\\050").replaceAll("[)]", "\\\\051") + ")\r\n");
}
} else {
throw new JobOptionsWriteException(ErrorCode.UnknownDataClassFound);
}
}
private void indentOutput(StringWriter fo) throws JobOptionsWriteException {
if (!this.indent) {
return;
}
StringBuffer sb = new StringBuffer();
for (int i = 0; i < this.depth; ++i) {
sb.append('\t');
}
fo.write(sb.toString());
}
private boolean isHexString(String str) {
boolean isHex = true;
for (int i = 0; i < str.length(); ++i) {
if (JOParser.isHexLetter(str.charAt(i))) continue;
isHex = false;
break;
}
return isHex;
}
}