1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.velocity.tools; |
21 |
|
|
22 |
|
import java.io.UnsupportedEncodingException; |
23 |
|
import java.net.URLEncoder; |
24 |
|
import java.util.Collection; |
25 |
|
import java.util.Map; |
26 |
|
|
27 |
|
import org.apache.commons.codec.EncoderException; |
28 |
|
import org.apache.commons.codec.net.BCodec; |
29 |
|
import org.apache.commons.codec.net.QCodec; |
30 |
|
import org.apache.commons.codec.net.QuotedPrintableCodec; |
31 |
|
import org.apache.commons.lang3.StringEscapeUtils; |
32 |
|
import org.slf4j.Logger; |
33 |
|
import org.slf4j.LoggerFactory; |
34 |
|
import org.xwiki.xml.XMLUtils; |
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
@version |
48 |
|
@since |
49 |
|
|
|
|
| 98.7% |
Uncovered Elements: 1 (77) |
Complexity: 26 |
Complexity Density: 0.54 |
|
50 |
|
public class EscapeTool extends org.apache.velocity.tools.generic.EscapeTool |
51 |
|
{ |
52 |
|
private static final Logger LOGGER = LoggerFactory.getLogger(EscapeTool.class); |
53 |
|
|
54 |
|
|
55 |
|
private static final String EQUALS = "="; |
56 |
|
|
57 |
|
|
58 |
|
private static final String AND = "&"; |
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
@param |
66 |
|
@return |
67 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
68 |
106133 |
@Override... |
69 |
|
public String xml(Object content) |
70 |
|
{ |
71 |
106133 |
return XMLUtils.escape(content); |
72 |
|
} |
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
@link |
78 |
|
|
79 |
|
|
80 |
|
@param |
81 |
|
|
82 |
|
@return |
83 |
|
@since |
84 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
85 |
8 |
public String json(Object string)... |
86 |
|
{ |
87 |
8 |
if (string == null) { |
88 |
1 |
return null; |
89 |
|
} |
90 |
7 |
return StringEscapeUtils.escapeJson(String.valueOf(string)); |
91 |
|
} |
92 |
|
|
93 |
|
|
94 |
|
|
95 |
|
|
96 |
|
|
97 |
|
|
98 |
|
@param |
99 |
|
@return |
100 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 3 |
Complexity Density: 0.75 |
|
101 |
4 |
public String quotedPrintable(Object content)... |
102 |
|
{ |
103 |
4 |
if (content != null) { |
104 |
3 |
try { |
105 |
3 |
return new QuotedPrintableCodec().encode(String.valueOf(content)); |
106 |
|
} catch (EncoderException ex) { |
107 |
|
|
108 |
|
} |
109 |
|
} |
110 |
1 |
return null; |
111 |
|
} |
112 |
|
|
113 |
|
|
114 |
|
|
115 |
|
|
116 |
|
|
117 |
|
|
118 |
|
@param |
119 |
|
@return |
120 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 3 |
Complexity Density: 0.75 |
|
121 |
5 |
public String q(Object content)... |
122 |
|
{ |
123 |
5 |
if (content != null) { |
124 |
4 |
try { |
125 |
4 |
return new QCodec().encode(String.valueOf(content)).replace(' ', '_'); |
126 |
|
} catch (EncoderException ex) { |
127 |
|
|
128 |
|
} |
129 |
|
} |
130 |
1 |
return null; |
131 |
|
} |
132 |
|
|
133 |
|
|
134 |
|
|
135 |
|
|
136 |
|
|
137 |
|
|
138 |
|
@param |
139 |
|
@return |
140 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 3 |
Complexity Density: 0.75 |
|
141 |
4 |
public String b(Object content)... |
142 |
|
{ |
143 |
4 |
if (content != null) { |
144 |
3 |
try { |
145 |
3 |
return new BCodec().encode(String.valueOf(content)); |
146 |
|
} catch (EncoderException ex) { |
147 |
|
|
148 |
|
} |
149 |
|
} |
150 |
1 |
return null; |
151 |
|
} |
152 |
|
|
153 |
|
|
154 |
|
|
155 |
|
@link |
156 |
|
|
157 |
|
|
158 |
|
|
159 |
|
@param |
160 |
|
@return |
161 |
|
@since |
162 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (22) |
Complexity: 6 |
Complexity Density: 0.38 |
|
163 |
13510 |
public String url(Map<String, ?> parametersMap)... |
164 |
|
{ |
165 |
13510 |
StringBuilder queryStringBuilder = new StringBuilder(); |
166 |
13510 |
for (Map.Entry<String, ?> entry : parametersMap.entrySet()) { |
167 |
22603 |
if (entry.getKey() == null) { |
168 |
|
|
169 |
1 |
continue; |
170 |
|
} |
171 |
22603 |
String cleanKey = this.url(entry.getKey()); |
172 |
22603 |
Object mapValues = entry.getValue(); |
173 |
22603 |
if (mapValues != null && mapValues.getClass().isArray()) { |
174 |
|
|
175 |
1 |
Object[] values = (Object[]) mapValues; |
176 |
1 |
for (Object value : values) { |
177 |
3 |
addQueryStringPair(cleanKey, value, queryStringBuilder); |
178 |
|
} |
179 |
22603 |
} else if (mapValues != null && Collection.class.isAssignableFrom(mapValues.getClass())) { |
180 |
|
|
181 |
9056 |
Collection<?> values = (Collection<?>) mapValues; |
182 |
9056 |
for (Object value : values) { |
183 |
9058 |
addQueryStringPair(cleanKey, value, queryStringBuilder); |
184 |
|
} |
185 |
|
} else { |
186 |
13547 |
addQueryStringPair(cleanKey, mapValues, queryStringBuilder); |
187 |
|
} |
188 |
|
} |
189 |
13509 |
return queryStringBuilder.toString(); |
190 |
|
} |
191 |
|
|
192 |
|
|
193 |
|
|
194 |
|
|
195 |
|
@param |
196 |
|
@param |
197 |
|
@param |
198 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 3 |
Complexity Density: 0.6 |
|
199 |
22605 |
private void addQueryStringPair(String cleanKey, Object rawValue, StringBuilder queryStringBuilder)... |
200 |
|
{ |
201 |
|
|
202 |
22608 |
String valueAsString = rawValue == null ? "" : String.valueOf(rawValue); |
203 |
22609 |
String cleanValue = this.url(valueAsString); |
204 |
22607 |
if (queryStringBuilder.length() != 0) { |
205 |
9110 |
queryStringBuilder.append(AND); |
206 |
|
} |
207 |
22608 |
queryStringBuilder.append(cleanKey).append(EQUALS).append(cleanValue); |
208 |
|
} |
209 |
|
|
210 |
|
|
211 |
|
|
212 |
|
|
213 |
|
|
214 |
|
|
215 |
|
|
216 |
|
@param |
217 |
|
@return |
218 |
|
@since |
219 |
|
@since |
220 |
|
@since |
221 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 2 |
Complexity Density: 0.5 |
|
222 |
2 |
public String css(String identifier)... |
223 |
|
{ |
224 |
2 |
try { |
225 |
2 |
return new CSSIdentifierSerializer().serialize(identifier); |
226 |
|
} catch (IllegalArgumentException e) { |
227 |
1 |
LOGGER.warn("Failed to escape CSS identifier. {}", e.getMessage()); |
228 |
1 |
return null; |
229 |
|
} |
230 |
|
} |
231 |
|
|
232 |
|
|
233 |
|
|
234 |
|
|
235 |
|
|
236 |
|
@param |
237 |
|
@return |
238 |
|
@since |
239 |
|
|
|
|
| 88.9% |
Uncovered Elements: 1 (9) |
Complexity: 3 |
Complexity Density: 0.43 |
|
240 |
55802 |
@Override... |
241 |
|
public String url(Object string) |
242 |
|
{ |
243 |
|
|
244 |
|
|
245 |
55802 |
String encodedURL = null; |
246 |
55802 |
if (string != null) { |
247 |
55483 |
try { |
248 |
55482 |
encodedURL = URLEncoder.encode(String.valueOf(string), "UTF-8"); |
249 |
|
} catch (UnsupportedEncodingException e) { |
250 |
|
|
251 |
0 |
throw new RuntimeException("Missing charset [UTF-8]", e); |
252 |
|
} |
253 |
|
|
254 |
|
|
255 |
55483 |
encodedURL = encodedURL.replaceAll("\\+", "%20"); |
256 |
|
} |
257 |
55803 |
return encodedURL; |
258 |
|
} |
259 |
|
} |