HotSpotResponseGenerator.java 8.22 KB
/*
 * 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;
    }
}