HotSpotResponseGenerator.java
8.22 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.scene7.is.catalog.util.localization.LocaleMap
* com.scene7.is.catalog.util.localization.LocalizedText
* com.scene7.is.provider.JsonResponse
* com.scene7.is.provider.TextResponseGenerator
* com.scene7.is.sleng.HotSpot
* com.scene7.is.util.TextEncodingTypeEnum
* com.scene7.is.util.XMLUtil
* com.scene7.is.util.callbacks.Option
* com.scene7.is.util.text.coders.HTMLCoder
* net.sf.json.JSONObject
* org.apache.commons.collections.primitives.ArrayIntList
* org.jetbrains.annotations.NotNull
*/
package com.scene7.is.ps.provider;
import com.scene7.is.catalog.util.localization.LocaleMap;
import com.scene7.is.catalog.util.localization.LocalizedText;
import com.scene7.is.provider.JsonResponse;
import com.scene7.is.provider.TextResponseGenerator;
import com.scene7.is.sleng.HotSpot;
import com.scene7.is.util.TextEncodingTypeEnum;
import com.scene7.is.util.XMLUtil;
import com.scene7.is.util.callbacks.Option;
import com.scene7.is.util.text.coders.HTMLCoder;
import java.awt.geom.FlatteningPathIterator;
import java.awt.geom.PathIterator;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import net.sf.json.JSONObject;
import org.apache.commons.collections.primitives.ArrayIntList;
import org.jetbrains.annotations.NotNull;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
public class HotSpotResponseGenerator {
public static String toHtml(@NotNull LocaleMap localeMap, @NotNull String locale, @NotNull List<HotSpot> hotSpots) {
StringBuilder result = new StringBuilder();
for (HotSpot hotSpot : hotSpots) {
if (hotSpot.isDefault()) {
result.append("<area");
Map props = LocalizedText.localize((LocaleMap)localeMap, (String)locale, (Map)hotSpot.getProperties());
HotSpotResponseGenerator.htmlStoreProperties(result, props);
result.append('>');
continue;
}
List<int[]> pathList = HotSpotResponseGenerator.flattenPath(hotSpot.getPathIterator());
for (int[] path : pathList) {
result.append("<area");
Map props = LocalizedText.localize((LocaleMap)localeMap, (String)locale, (Map)hotSpot.getProperties());
HotSpotResponseGenerator.htmlStoreProperties(result, props);
result.append(" shape=\"poly\"");
String pathString = HotSpotResponseGenerator.polygonToString(path);
result.append(" coords=\"").append(pathString).append("\">\n");
}
}
return result.toString();
}
public static String toXml(@NotNull LocaleMap localeMap, @NotNull String locale, @NotNull List<HotSpot> hotSpots, @NotNull TextEncodingTypeEnum encoding) {
Document doc = XMLUtil.createDocument();
doc.appendChild(HotSpotResponseGenerator.createHotspotXmlElement(doc, localeMap, locale, hotSpots));
return XMLUtil.toXMLString((Document)doc, (TextEncodingTypeEnum)encoding, (boolean)false);
}
public static String toJson(@NotNull LocaleMap localeMap, @NotNull String locale, @NotNull List<HotSpot> hotSpots, @NotNull String id, @NotNull String handler) {
ArrayList areaList = new ArrayList();
for (HotSpot hotSpot : hotSpots) {
HotSpotResponseGenerator.collectHotSpotPaths(areaList, localeMap, locale, hotSpot);
}
return JsonResponse.generateJsonResponse((Option)TextResponseGenerator.callbackToOption((String)handler), areaList, (String)id);
}
public static Element createHotspotXmlElement(@NotNull Document doc, @NotNull LocaleMap localeMap, @NotNull String locale, @NotNull List<HotSpot> hotSpots) {
Element root = XMLUtil.createElement((Document)doc, (String)"map");
for (HotSpot hotSpot : hotSpots) {
List<int[]> pathList = HotSpotResponseGenerator.flattenPath(hotSpot.getPathIterator());
for (int[] points : pathList) {
Element area = XMLUtil.createElement((Document)doc, (String)"area");
Map<String, String> props = HotSpotResponseGenerator.getHotSpotAttributes(localeMap, locale, hotSpot, points);
XMLUtil.appendAsAttributes((Element)area, props);
root.appendChild(area);
}
}
return root;
}
public static JSONObject createHotspotJsonObject(@NotNull LocaleMap localeMap, @NotNull String locale, @NotNull List<HotSpot> hotSpots) {
JSONObject hotSpotsAsJson = new JSONObject();
for (HotSpot hotSpot : hotSpots) {
List<int[]> pathList = HotSpotResponseGenerator.flattenPath(hotSpot.getPathIterator());
for (int[] points : pathList) {
hotSpotsAsJson.accumulate("area", HotSpotResponseGenerator.getHotSpotAttributes(localeMap, locale, hotSpot, points));
}
}
return hotSpotsAsJson;
}
private static Map<String, String> getHotSpotAttributes(@NotNull LocaleMap localeMap, @NotNull String locale, @NotNull HotSpot hotSpot, int[] points) {
Map props = LocalizedText.localize((LocaleMap)localeMap, (String)locale, (Map)hotSpot.getProperties());
props.put("shape", "poly");
String pathStr = HotSpotResponseGenerator.polygonToString(points);
props.put("coords", pathStr);
return props;
}
private static void collectHotSpotPaths(@NotNull List<Map<String, ?>> result, @NotNull LocaleMap localeMap, @NotNull String locale, @NotNull HotSpot hotSpot) {
Map props = LocalizedText.localize((LocaleMap)localeMap, (String)locale, (Map)hotSpot.getProperties());
List<int[]> pathList = HotSpotResponseGenerator.flattenPath(hotSpot.getPathIterator());
for (int[] path : pathList) {
Map areaSpec = HotSpotResponseGenerator.createAreaSpec(path, props);
result.add(areaSpec);
}
}
private static Map<String, ?> createAreaSpec(int[] path, Map<String, String> props) {
HashMap<String, Object> result = new HashMap<String, Object>();
result.put("coords", path);
result.put("shape", "poly");
for (Map.Entry<String, String> entry : props.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
result.put(key, value);
}
return result;
}
private static String polygonToString(int[] points) {
StringBuilder result = new StringBuilder();
for (int point : points) {
result.append(point);
result.append(',');
}
if (result.length() > 0) {
result.setLength(result.length() - 1);
}
return result.toString();
}
private static void htmlStoreProperties(StringBuilder result, Map<String, String> props) {
for (Map.Entry<String, String> entry : props.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
value = HTMLCoder.htmlCoder().encode(value);
result.append(' ');
result.append(key);
result.append("=\"");
result.append(value);
result.append('\"');
}
}
private static List<int[]> flattenPath(PathIterator path) {
double flatness = 1.0;
int limit = 16;
ArrayList<int[]> pathList = new ArrayList<int[]>();
double[] coords = new double[6];
FlatteningPathIterator i = new FlatteningPathIterator(path, flatness, limit);
ArrayIntList area = new ArrayIntList();
while (!i.isDone()) {
int type = i.currentSegment(coords);
int x = (int)Math.round(coords[0]);
int y = (int)Math.round(coords[1]);
switch (type) {
case 0:
case 1: {
area.add(x);
area.add(y);
break;
}
case 4: {
pathList.add(area.toArray());
area = new ArrayIntList();
break;
}
default: {
throw new AssertionError(type);
}
}
i.next();
}
return pathList;
}
}