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
| public class AjaxResponseXMLView extends AbstractView {
@Override
protected void renderMergedOutputModel(Map map, HttpServletRequest request,
HttpServletResponse response) throws Exception {
String xmlHeader = "\r\n";
StringBuffer xmlSb = new StringBuffer();
xmlSb.append(xmlHeader);
xmlSb.append("");
writeFromMap(xmlSb, map);
xmlSb.append("");
response.setContentType("application/xml");
response.setCharacterEncoding("utf-8");
response.setHeader("Cache-Control", "no-cache");
response.setContentLength(xmlSb.toString().getBytes("utf-8").length);
response.getWriter().print(xmlSb.toString());
}
private void writeFromMap(StringBuffer sb, Map map) {
for(Object str : map.keySet()) {
Object v = map.get(str);
sb.append("<" + str + ">");
if(v instanceof Map) {
writeFromMap(sb, (Map) v);
}
else if(v instanceof List) {
writeFromList(sb, (List) v);
}
else {
writeFromData(sb, v);
}
sb.append("</" + str + ">");
}
}
private void writeFromList(StringBuffer sb, List list) {
for(Object v : list) {
sb.append("");
if(v instanceof Map) {
writeFromMap(sb, (Map)v);
}
else if(v instanceof List) {
writeFromList(sb, (List) v);
}
else {
writeFromData(sb, v);
}
sb.append("");
}
}
private void writeFromData(StringBuffer sb, Object data) {
sb.append(escapeXml(data+""));
}
private String escapeXml(String src) {
// " "
// < <
// > >
// & &
src = src.replace("\"", """);
src = src.replace("<", "<");
src = src.replace(">", ">");
src = src.replace("&", "&");
return src;
}
}
|