ISProtocolParser.java 3.86 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  org.apache.jackrabbit.util.Text
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.dam.scene7.impl.protocol.is;

import com.day.cq.dam.scene7.impl.protocol.NameValue;
import com.day.cq.dam.scene7.impl.protocol.is.ISProtocolFactory;
import java.net.URLDecoder;
import org.apache.jackrabbit.util.Text;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ISProtocolParser {
    private static final Logger log = LoggerFactory.getLogger(ISProtocolParser.class);

    public static NameValue parse(String parseStr, boolean validate) {
        if (parseStr == null || parseStr.length() == 0) {
            return null;
        }
        try {
            NameValue ret;
            parseStr = URLDecoder.decode(parseStr, "utf-8");
            String[] prefixArr = new String[]{"=is{", "}", "=is(", ")", "=ir{", "}", "=ir(", ")"};
            String prefix = null;
            String matchStr = null;
            int idx1 = -1;
            int idx2 = -1;
            for (int cnt = 0; cnt < prefixArr.length; cnt += 2) {
                idx1 = 0;
                prefix = prefixArr[cnt];
                matchStr = prefixArr[cnt + 1];
                while ((idx1 = parseStr.indexOf(prefix, idx1)) != -1) {
                    idx2 = parseStr.indexOf(matchStr, idx1);
                    if (idx2 != -1) {
                        parseStr = parseStr.substring(0, idx1) + prefix + Text.escape((String)parseStr.substring(idx1 + prefix.length(), idx2)) + parseStr.substring(idx2);
                    }
                    ++idx1;
                }
            }
            String[] arr = parseStr.split("&");
            String name = null;
            String value = null;
            NameValue currNode = ret = ISProtocolFactory.make("root", null);
            NameValue currLayer = null;
            NameValue subLayer = null;
            int idx = -1;
            for (int i = 0; i < arr.length; ++i) {
                idx = arr[i].indexOf("=");
                if (idx != -1) {
                    name = arr[i].substring(0, idx);
                    value = arr[i].substring(idx + 1);
                } else {
                    name = arr[i];
                    value = "";
                }
                try {
                    if (name.equals("layer") || name.equals(".sl") || name.equals("effect")) {
                        subLayer = ISProtocolFactory.make(name, value);
                        if (name.equals("layer") || name.equals(".sl")) {
                            ret.addNode(subLayer);
                            currLayer = subLayer;
                        } else if (currLayer != null) {
                            currLayer.addNode(subLayer);
                        } else {
                            ret.addNode(subLayer);
                        }
                        currNode = subLayer;
                        continue;
                    }
                    if (name.charAt(0) == '$') {
                        ret.addNode(ISProtocolFactory.make(name, value));
                        continue;
                    }
                    currNode.addNode(ISProtocolFactory.make(name, value));
                    continue;
                }
                catch (Exception e) {
                    if (validate) {
                        throw new Exception(name + "=" + value);
                    }
                    currNode.removeNode(name);
                    currNode.addNode(new NameValue(name, value));
                    ret.removeNode("parseerror");
                    ret.addNode(ISProtocolFactory.make("parseerror", null));
                }
            }
            return ret;
        }
        catch (Exception e) {
            log.error("ISProtocolParser, Unable to parse input string: " + e.getMessage(), (Throwable)e);
            return null;
        }
    }
}