ISProtocolParser.java
3.86 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.apache.jackrabbit.util.Text
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.dam.scene7.impl.protocol.is;
import com.day.cq.dam.scene7.impl.protocol.NameValue;
import com.day.cq.dam.scene7.impl.protocol.is.ISProtocolFactory;
import java.net.URLDecoder;
import org.apache.jackrabbit.util.Text;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ISProtocolParser {
private static final Logger log = LoggerFactory.getLogger(ISProtocolParser.class);
public static NameValue parse(String parseStr, boolean validate) {
if (parseStr == null || parseStr.length() == 0) {
return null;
}
try {
NameValue ret;
parseStr = URLDecoder.decode(parseStr, "utf-8");
String[] prefixArr = new String[]{"=is{", "}", "=is(", ")", "=ir{", "}", "=ir(", ")"};
String prefix = null;
String matchStr = null;
int idx1 = -1;
int idx2 = -1;
for (int cnt = 0; cnt < prefixArr.length; cnt += 2) {
idx1 = 0;
prefix = prefixArr[cnt];
matchStr = prefixArr[cnt + 1];
while ((idx1 = parseStr.indexOf(prefix, idx1)) != -1) {
idx2 = parseStr.indexOf(matchStr, idx1);
if (idx2 != -1) {
parseStr = parseStr.substring(0, idx1) + prefix + Text.escape((String)parseStr.substring(idx1 + prefix.length(), idx2)) + parseStr.substring(idx2);
}
++idx1;
}
}
String[] arr = parseStr.split("&");
String name = null;
String value = null;
NameValue currNode = ret = ISProtocolFactory.make("root", null);
NameValue currLayer = null;
NameValue subLayer = null;
int idx = -1;
for (int i = 0; i < arr.length; ++i) {
idx = arr[i].indexOf("=");
if (idx != -1) {
name = arr[i].substring(0, idx);
value = arr[i].substring(idx + 1);
} else {
name = arr[i];
value = "";
}
try {
if (name.equals("layer") || name.equals(".sl") || name.equals("effect")) {
subLayer = ISProtocolFactory.make(name, value);
if (name.equals("layer") || name.equals(".sl")) {
ret.addNode(subLayer);
currLayer = subLayer;
} else if (currLayer != null) {
currLayer.addNode(subLayer);
} else {
ret.addNode(subLayer);
}
currNode = subLayer;
continue;
}
if (name.charAt(0) == '$') {
ret.addNode(ISProtocolFactory.make(name, value));
continue;
}
currNode.addNode(ISProtocolFactory.make(name, value));
continue;
}
catch (Exception e) {
if (validate) {
throw new Exception(name + "=" + value);
}
currNode.removeNode(name);
currNode.addNode(new NameValue(name, value));
ret.removeNode("parseerror");
ret.addNode(ISProtocolFactory.make("parseerror", null));
}
}
return ret;
}
catch (Exception e) {
log.error("ISProtocolParser, Unable to parse input string: " + e.getMessage(), (Throwable)e);
return null;
}
}
}