AttrBuilder.java
4.82 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.granite.xss.XSSAPI
* javax.servlet.http.HttpServletRequest
*/
package com.adobe.granite.ui.components;
import com.adobe.granite.xss.XSSAPI;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
/*
* This class specifies class file version 49.0 but uses Java 6 signatures. Assumed Java 6.
*/
public class AttrBuilder {
private final HttpServletRequest req;
private final XSSAPI xssAPI;
private Map<String, String> data = new LinkedHashMap<String, String>();
private Set<String> classes = new HashSet<String>();
public AttrBuilder(HttpServletRequest req, XSSAPI xssAPI) {
this.req = req;
this.xssAPI = xssAPI;
}
public void addRel(String value) {
this.addClass(value);
}
public void addClass(String value) {
if (value == null || value.length() == 0) {
return;
}
if (this.classes.add(value)) {
this.add("class", value);
}
}
public void addHref(String name, String value) {
if (value == null || value.length() == 0) {
return;
}
if (value.startsWith("/")) {
value = this.req.getContextPath() + value;
}
this.data.put(name, this.xssAPI.getValidHref(value));
}
public void addDisabled(boolean disabled) {
this.addBoolean("disabled", disabled);
}
public void addChecked(boolean checked) {
this.addBoolean("checked", checked);
}
public void addSelected(boolean selected) {
this.addBoolean("selected", selected);
}
public void addMultiple(boolean multiple) {
this.addBoolean("multiple", multiple);
}
public void addBoolean(String name, boolean value) {
if (!value) {
return;
}
this.add(name, "");
}
public void addOther(String name, String value) {
this.add("data-" + this.xssAPI.encodeForHTML(name), value);
}
public /* varargs */ void addOthers(Map<String, Object> data, String ... exclusions) {
List<String> blacklisted = Arrays.asList(exclusions);
for (Map.Entry<String, Object> e : data.entrySet()) {
String key = e.getKey();
if (key.indexOf(":") >= 0 || blacklisted.indexOf(key) >= 0) continue;
Object value = e.getValue();
if (value.getClass().isArray()) {
for (Object o : (Object[])value) {
this.addOther(key, o.toString());
}
continue;
}
this.addOther(key, value.toString());
}
}
public void add(String name, Boolean value) {
if (value == null || name == null || name.length() == 0) {
return;
}
this.addNoCheck(name, value.toString());
}
public void add(String name, Integer value) {
if (value == null || name == null || name.length() == 0) {
return;
}
this.addNoCheck(name, value.toString());
}
public void add(String name, Double value) {
if (value == null || name == null || name.length() == 0) {
return;
}
this.addNoCheck(name, value.toString());
}
public void add(String name, String value) {
if (value == null || name == null || name.length() == 0) {
return;
}
if (value.length() > 0) {
value = this.xssAPI.encodeForHTMLAttr(value);
}
this.addNoCheck(name, value);
}
public void set(String name, String value) {
if (value == null || name == null || name.length() == 0) {
return;
}
if (value.length() > 0) {
value = this.xssAPI.encodeForHTMLAttr(value);
}
this.data.put(name, value);
}
private void addNoCheck(String name, String v) {
if (this.data.containsKey(name)) {
v = this.data.get(name) + " " + v;
}
this.data.put(name, v);
}
public boolean isEmpty() {
return this.data.isEmpty();
}
public String build() {
try {
StringWriter out = new StringWriter();
this.build(out);
return out.toString();
}
catch (IOException impossible) {
throw new RuntimeException(impossible);
}
}
public void build(Writer out) throws IOException {
for (Map.Entry<String, String> e : this.data.entrySet()) {
out.append(" ").append(e.getKey()).append("=\"").append(e.getValue()).append("\"");
}
}
public String toString() {
return this.build();
}
}