MediaSetNodeJsonWriter.java
16.2 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.scene7.is.catalog.util.localization.LocaleMap
* com.scene7.is.provider.ImageSetColorLabel
* com.scene7.is.provider.UserData
* com.scene7.is.provider.ZoomTargets
* com.scene7.is.sleng.HotSpot
* com.scene7.is.util.JSONUtil
* com.scene7.is.util.SizeInt
* com.scene7.is.util.error.ApplicationException
* net.sf.json.JSON
* net.sf.json.JSONArray
* net.sf.json.JSONException
* net.sf.json.JSONObject
* org.jetbrains.annotations.NotNull
*/
package com.scene7.is.ps.provider.fvctx;
import com.scene7.is.catalog.util.localization.LocaleMap;
import com.scene7.is.provider.ImageSetColorLabel;
import com.scene7.is.provider.UserData;
import com.scene7.is.provider.ZoomTargets;
import com.scene7.is.ps.provider.HotSpotResponseGenerator;
import com.scene7.is.ps.provider.IZoomException;
import com.scene7.is.ps.provider.Request;
import com.scene7.is.ps.provider.UserDataHandler;
import com.scene7.is.ps.provider.fvctx.FvctxImageProps;
import com.scene7.is.ps.provider.fvctx.MediaSetItemType;
import com.scene7.is.ps.provider.fvctx.MediaSetMetaData;
import com.scene7.is.ps.provider.fvctx.MediaSetNodeWriter;
import com.scene7.is.ps.provider.fvctx.MediaSetSetType;
import com.scene7.is.ps.provider.fvctx.MediaSetVideoExtension;
import com.scene7.is.ps.provider.fvctx.MediaSetVideoProps;
import com.scene7.is.sleng.HotSpot;
import com.scene7.is.util.JSONUtil;
import com.scene7.is.util.SizeInt;
import com.scene7.is.util.error.ApplicationException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;
import org.jetbrains.annotations.NotNull;
public class MediaSetNodeJsonWriter
extends MediaSetNodeWriter<JSONObject> {
@NotNull
private final JSONObject root = new JSONObject();
@NotNull
private JSONNode currentElement;
@NotNull
private List<Map<String, String>> nestedFrameSwatches = new ArrayList<Map<String, String>>();
public MediaSetNodeJsonWriter(int fvctxVersion, @NotNull LocaleMap localeMap, @NotNull String locale, @NotNull String labelKey, @NotNull MediaSetMetaData publicMetaData, @NotNull MediaSetSetType setType, @NotNull String netPath, int brochureLimit) {
super(fvctxVersion, localeMap, locale, labelKey, publicMetaData, setType, netPath, brochureLimit);
this.root.accumulate("set", this.getRootAttributes());
this.currentElement = new JSONNode((JSON)this.root.get("set"), new JSONNode((JSON)this.root, null));
}
@Override
public void postProcess() throws IZoomException {
this.appendMetaData(this.currentElement.node, this.publicMetaData, this.localeMap, this.locale, false);
}
@NotNull
public JSONObject getProduct() {
MediaSetNodeJsonWriter.addNumPagesAttribute(this.root);
return this.root;
}
@Override
public void startItem(MediaSetItemType type) {
JSONObject item = new JSONObject();
MediaSetNodeJsonWriter.accumulateElement(this.currentElement.node, "item", (Object)item);
this.currentElement = new JSONNode(MediaSetNodeJsonWriter.getElement(this.currentElement.node, "item"), this.currentElement);
MediaSetNodeJsonWriter.addAttributesToNode(this.currentElement.node, this.getStartItemAttributes(type));
}
@Override
public void endItem(@NotNull String imageVersion) {
MediaSetNodeJsonWriter.addAttributesToNode(this.currentElement.node, MediaSetNodeJsonWriter.getEndItemAttributes(imageVersion));
this.currentElement = this.currentElement.parent;
}
@Override
public void startSet(@NotNull String recordName, @NotNull MediaSetSetType setType) {
JSONObject set = new JSONObject();
MediaSetNodeJsonWriter.accumulateElement(this.currentElement.node, "set", (Object)set);
this.currentElement = new JSONNode(MediaSetNodeJsonWriter.getElement(this.currentElement.node, "set"), this.currentElement);
MediaSetNodeJsonWriter.addAttributesToNode(this.currentElement.node, MediaSetNodeJsonWriter.getStartSetAttributes(recordName, setType));
}
@Override
public void endSet(@NotNull MediaSetMetaData metaData) throws ApplicationException {
this.appendMetaData(this.currentElement.node, metaData, metaData.getLocaleMap(), metaData.getLocale(), false);
this.currentElement = this.currentElement.parent;
}
@Override
public void startFrameset() {
throw new UnsupportedOperationException();
}
@Override
public void endFrameset(SizeInt size, String imageVersion, String metaVersion, boolean anonymousFrames) {
throw new UnsupportedOperationException();
}
@Override
public void startFrame(@NotNull String netPath, @NotNull String query, Request parsedRequest, FvctxImageProps imageProps, boolean hasNestedFrame, boolean isMediaSetItem) throws ApplicationException {
JSONObject frame = new JSONObject();
MediaSetNodeJsonWriter.accumulateElement(this.currentElement.node, "item", (Object)frame);
this.currentElement = new JSONNode(MediaSetNodeJsonWriter.getElement(this.currentElement.node, "item"), this.currentElement);
if (!hasNestedFrame) {
MediaSetNodeJsonWriter.accumulateElement(this.currentElement.node, "i", this.getImageAttributes(netPath, query, imageProps, isMediaSetItem));
} else {
this.nestedFrameSwatches.add(null);
}
}
@Override
public void startVideoFrame(@NotNull String netPath, @NotNull String query, @NotNull MediaSetVideoProps videoProps) throws ApplicationException {
JSONObject frame = new JSONObject();
MediaSetNodeJsonWriter.accumulateElement(this.currentElement.node, "item", (Object)frame);
this.currentElement = new JSONNode(MediaSetNodeJsonWriter.getElement(this.currentElement.node, "item"), this.currentElement);
String key = "c";
if (MediaSetVideoExtension.isVideoFileExtension(videoProps.getFileSuffix()) || videoProps.getFileSuffix().isEmpty()) {
key = "v";
}
MediaSetNodeJsonWriter.accumulateElement(this.currentElement.node, key, this.getCommonVideoAttributes(netPath, query, videoProps));
}
@Override
public void endFrame(SizeInt size, @NotNull String imageVersion, @NotNull MediaSetMetaData metaData, boolean hasNestedFrames) throws IZoomException {
Map<String, String> nestedFrameSwatch;
MediaSetNodeJsonWriter.addAttributesToNode(this.currentElement.node, this.getFrameAttributes(size, imageVersion, hasNestedFrames, MediaSetNodeJsonWriter.findFrameItemType(hasNestedFrames, this.currentElement.node)));
if (!hasNestedFrames) {
boolean[] metadataFlags = this.appendMetaData(this.currentElement.node, metaData, metaData.getLocaleMap(), metaData.getLocale(), true);
MediaSetNodeJsonWriter.addAttributesToNode(this.currentElement.node, MediaSetNodeJsonWriter.getSuppressedMetaDataAttributes(metadataFlags));
} else if (!this.nestedFrameSwatches.isEmpty() && (nestedFrameSwatch = this.nestedFrameSwatches.remove(this.nestedFrameSwatches.size() - 1)) != null) {
MediaSetNodeJsonWriter.accumulateElement(this.currentElement.node, "s", nestedFrameSwatch);
}
this.currentElement = this.currentElement.parent;
}
@Override
public void endVideoFrame(@NotNull MediaSetMetaData metaData, @NotNull String version, @NotNull String suffix) throws IZoomException {
MediaSetNodeJsonWriter.addAttributesToNode(this.currentElement.node, MediaSetNodeJsonWriter.getVideoFrameAttributes(version, suffix));
this.appendMetaData(this.currentElement.node, metaData, metaData.getLocaleMap(), metaData.getLocale(), false);
this.currentElement = this.currentElement.parent;
}
@Override
public void startAnonymousFrame() throws IZoomException {
JSONObject frame = new JSONObject();
MediaSetNodeJsonWriter.accumulateElement(this.currentElement.node, "item", (Object)frame);
this.currentElement = new JSONNode(MediaSetNodeJsonWriter.getElement(this.currentElement.node, "item"), this.currentElement);
}
@Override
public void endAnonymousFrame() throws IZoomException {
this.currentElement = this.currentElement.parent;
}
@Override
public void image(@NotNull String netPath, @NotNull String query, Request parsedRequest, FvctxImageProps imageProps) throws ApplicationException {
MediaSetNodeJsonWriter.accumulateElement(this.currentElement.node, "i", this.getImageAttributes(netPath, query, imageProps, false));
}
@Override
public void swatch(@NotNull String netPath, @NotNull String query, Request parsedRequest, FvctxImageProps imageProps, boolean hasNestedFrame, boolean isMediaItemSwatch) throws ApplicationException {
Map<String, String> swatchProps = this.getCommonImageAttributes(netPath, query, imageProps);
if (hasNestedFrame && !isMediaItemSwatch) {
assert (!this.nestedFrameSwatches.isEmpty());
this.nestedFrameSwatches.set(this.nestedFrameSwatches.size() - 1, swatchProps);
}
if (!hasNestedFrame || isMediaItemSwatch) {
MediaSetNodeJsonWriter.accumulateElement(this.currentElement.node, "s", swatchProps);
}
}
@Override
public void swatch(@NotNull ImageSetColorLabel data, @NotNull LocaleMap localeMap, @NotNull String locale, boolean hasNestedFrame) throws ApplicationException {
Map<String, String> swatchProps = MediaSetNodeJsonWriter.getSwatchProps(data, localeMap, locale);
if (hasNestedFrame) {
assert (!this.nestedFrameSwatches.isEmpty());
this.nestedFrameSwatches.set(this.nestedFrameSwatches.size() - 1, swatchProps);
} else {
MediaSetNodeJsonWriter.accumulateElement(this.currentElement.node, "s", swatchProps);
}
}
@Override
public void defaultSwatch() throws ApplicationException {
}
private boolean[] appendMetaData(@NotNull JSON json, @NotNull MediaSetMetaData metaData, @NotNull LocaleMap localeMap, @NotNull String locale, boolean isPrivate) {
boolean[] shouldAppendMetaData = this.shouldAppendMetadata(metaData, isPrivate);
if (shouldAppendMetaData[0]) {
MediaSetNodeJsonWriter.addElement(json, "targets", (Object)metaData.getTargets().createJSONObject(localeMap, locale, true, true));
}
if (shouldAppendMetaData[1]) {
MediaSetNodeJsonWriter.addElement(json, "map", (Object)HotSpotResponseGenerator.createHotspotJsonObject(localeMap, locale, metaData.getMaps()));
}
if (shouldAppendMetaData[2]) {
for (UserData userData : metaData.getUserData()) {
if (!this.shouldAppendUserData(userData)) continue;
MediaSetNodeJsonWriter.accumulateElement(json, "userdata", UserDataHandler.getJsonAttributes(localeMap, locale, userData, this.labelKey));
}
}
return shouldAppendMetaData;
}
private static JSON addElement(@NotNull JSON json, String key, Object value) {
JSONObject jsonObject = MediaSetNodeJsonWriter.getJSONObject(json);
return jsonObject.element(key, value);
}
private static JSON accumulateElement(@NotNull JSON json, String key, Object value) {
JSONObject jsonObject = MediaSetNodeJsonWriter.getJSONObject(json);
return jsonObject.accumulate(key, value);
}
private static JSON getElement(@NotNull JSON json, String key) {
JSONObject jsonObject = MediaSetNodeJsonWriter.getJSONObject(json);
Object element = jsonObject.get(key);
assert (element instanceof JSONObject || element instanceof JSONArray);
return (JSON)element;
}
private static void addAttributesToNode(@NotNull JSON json, Map<String, String> map) {
JSONObject jsonObject = MediaSetNodeJsonWriter.getJSONObject(json);
JSONUtil.addAttributes((JSONObject)jsonObject, map);
}
@NotNull
private static String findFrameItemType(boolean hasNestedFrame, JSON currentNode) {
Object childJson;
String itemType = MediaSetItemType.IMG.name().toLowerCase();
JSONObject jsonObject = MediaSetNodeJsonWriter.getJSONObject(currentNode);
if (hasNestedFrame && (childJson = jsonObject.get("set")) != null) {
JSONObject childNode = MediaSetNodeJsonWriter.getJSONObject(childJson);
itemType = childNode.getString("type");
assert (itemType != null);
assert (itemType.length() > 0);
}
return itemType;
}
private static JSONObject getJSONObject(JSON json) {
if (json instanceof JSONObject) {
return (JSONObject)json;
}
if (json instanceof JSONArray) {
JSONArray jsonArray = (JSONArray)json;
assert (!jsonArray.isEmpty());
Object o = jsonArray.get(jsonArray.size() - 1);
assert (o instanceof JSONObject);
return (JSONObject)o;
}
throw new AssertionError((Object)"unexpected JSON type");
}
private static JSONObject getJSONObject(Object obj) {
if (obj instanceof JSONObject) {
return (JSONObject)obj;
}
if (obj instanceof JSONArray) {
JSONArray jsonArray = (JSONArray)obj;
assert (!jsonArray.isEmpty());
Object o = jsonArray.get(jsonArray.size() - 1);
assert (o instanceof JSONObject);
return (JSONObject)o;
}
throw new AssertionError((Object)"unexpected JSON type");
}
private static void addNumPagesAttribute(@NotNull JSONObject root) {
try {
JSON set = (JSON)root.get("set");
if (set instanceof JSONObject) {
JSONObject setAsObject = (JSONObject)set;
String elementSetType = setAsObject.getString("type");
JSON items = (JSON)setAsObject.get("item");
if (elementSetType != null && MediaSetSetType.isECatSet(elementSetType)) {
MediaSetNodeJsonWriter.setNumPagesAttribute(items);
}
if (items instanceof JSONObject) {
MediaSetNodeJsonWriter.addNumPagesAttribute((JSONObject)items);
} else if (items instanceof JSONArray) {
JSONArray itemsAsArray = (JSONArray)items;
for (Object o : itemsAsArray) {
if (!(o instanceof JSONObject)) continue;
MediaSetNodeJsonWriter.addNumPagesAttribute((JSONObject)o);
}
}
}
}
catch (JSONException ignore) {
// empty catch block
}
}
private static void setNumPagesAttribute(@NotNull JSON items) {
JSONArray jsonArray;
if (items instanceof JSONArray && (jsonArray = (JSONArray)items).size() > 2) {
Object firstItem = jsonArray.get(0);
Object secondItem = jsonArray.get(1);
MediaSetNodeJsonWriter.setNumPagesAttribute(firstItem, secondItem);
Object nextToLastItem = jsonArray.get(jsonArray.size() - 2);
Object lastItem = jsonArray.get(jsonArray.size() - 1);
MediaSetNodeJsonWriter.setNumPagesAttribute(lastItem, nextToLastItem);
}
}
private static void setNumPagesAttribute(@NotNull Object item1, @NotNull Object item2) {
if (item1 instanceof JSONObject && item2 instanceof JSONObject) {
JSONObject a = (JSONObject)item1;
JSONObject b = (JSONObject)item2;
try {
String aWidth = a.getString("dx");
String aHeight = a.getString("dy");
String bWidth = b.getString("dx");
String bHeight = b.getString("dy");
if (MediaSetNodeJsonWriter.shouldSetPagesAttribute(aWidth, aHeight, bWidth, bHeight)) {
a.put((Object)"np", (Object)"1");
}
}
catch (JSONException ignore) {
// empty catch block
}
}
}
private static class JSONNode {
private final JSON node;
private final JSONNode parent;
private JSONNode(JSON node, JSONNode parent) {
this.node = node;
this.parent = parent;
}
}
}