CMDocumentDataMerger.java 6.61 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.aemds.guide.utils.DocumentDataMerger
 *  com.adobe.dct.transfer.DataDictionary
 *  com.adobe.dct.transfer.DataDictionaryInstance
 *  org.apache.commons.lang.StringUtils
 *  org.apache.sling.commons.json.JSONException
 *  org.apache.sling.commons.json.JSONObject
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.fd.adaddon.internal.utils;

import com.adobe.aemds.guide.utils.DocumentDataMerger;
import com.adobe.dct.transfer.DataDictionary;
import com.adobe.dct.transfer.DataDictionaryInstance;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import org.apache.commons.lang.StringUtils;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;

public class CMDocumentDataMerger
extends DocumentDataMerger {
    private static Logger logger = LoggerFactory.getLogger(CMDocumentDataMerger.class);
    private DataDictionaryInstance ddi;
    private static final String REF_NAME_SEPARATOR = "%";

    public CMDocumentDataMerger(JSONObject guideJson, Document mergeReferenceDocument, Map<String, Object> params) {
        super(guideJson, mergeReferenceDocument, params);
        this.ddi = (DataDictionaryInstance)params.get("moduleDDI");
        this.setCurrentContext((Object)null);
        this.currentRepeatPath = "";
    }

    protected void setBindProperty() {
        this.bindProperty = "bindRef";
    }

    protected String getComputedBindProperty(JSONObject jsonObject) throws JSONException {
        if (jsonObject.has(this.bindProperty)) {
            String bindProp = jsonObject.getString(this.bindProperty);
            if (StringUtils.isNotEmpty((String)bindProp)) {
                return (String)this.ddi.getDataDictionary().getReferenceNameToPathMap().get(bindProp) + "%" + bindProp;
            }
            return "";
        }
        return "";
    }

    protected boolean isInCurrentContext(String bindPath) {
        if (StringUtils.isEmpty((String)bindPath)) {
            return false;
        }
        String absPath = bindPath.split("%")[0];
        String currentAbsPath = this.currentRepeatPath.split("%")[0];
        return StringUtils.isNotEmpty((String)absPath) && StringUtils.isNotEmpty((String)currentAbsPath) && absPath.startsWith(currentAbsPath);
    }

    protected String getRelativePath(String bindPath) {
        if (StringUtils.isEmpty((String)bindPath)) {
            return "";
        }
        String[] parts = bindPath.split("%");
        if (parts.length < 2) {
            return "";
        }
        String absPath = parts[0];
        String ref = parts[1];
        bindPath = StringUtils.substringAfter((String)absPath, (String)this.currentRepeatPath.split("%")[0]);
        if (bindPath.startsWith(".")) {
            bindPath = bindPath.substring(1);
        }
        return bindPath + "%" + ref;
    }

    private Object resolveValue(String relativePath, boolean isRepeatChild) {
        if (StringUtils.isEmpty((String)relativePath)) {
            return null;
        }
        String[] parts = relativePath.split("%");
        if (parts.length < 2) {
            return null;
        }
        String path = parts[0];
        String ref = parts[1];
        Object val = null;
        if (isRepeatChild && this.getCurrentContext() != null && StringUtils.isNotEmpty((String)path)) {
            StringTokenizer st = new StringTokenizer(path);
            val = this.deepSearchContext(st, (Map)this.getCurrentContext());
        }
        if (val == null) {
            val = this.ddi.getValue2(ref);
        }
        return val;
    }

    protected String matchPrimitive(String relativePath, boolean isRepeatChild, boolean isRichText) throws Exception {
        return this.matchPrimitive(relativePath, isRepeatChild);
    }

    protected String matchPrimitive(String relativePath, boolean isRepeatChild) throws Exception {
        Object val = this.resolveValue(relativePath, isRepeatChild);
        if (val != null) {
            if (val instanceof Map || val instanceof List) {
                logger.debug("matchPrimitive: Matched composite/collection for " + relativePath);
            } else {
                logger.debug("matchPrimitive: Matched value=" + val + " , " + relativePath);
            }
            return val + "";
        }
        return null;
    }

    private Object deepSearchContext(StringTokenizer pathElements, Map curContext) {
        Object matchedValue = null;
        while (pathElements.hasMoreElements()) {
            String element = pathElements.nextToken();
            if (curContext.get(element) == null) continue;
            matchedValue = curContext.get(element);
            if (matchedValue instanceof Map) {
                curContext = matchedValue;
                continue;
            }
            if (matchedValue instanceof List) {
                return this.flattenList(pathElements, matchedValue);
            }
            if (!pathElements.hasMoreElements()) continue;
            logger.debug("More elements to search on path but literal value found");
            return null;
        }
        return matchedValue;
    }

    private List flattenList(StringTokenizer pathElements, List list) {
        ArrayList<Object> matchedList = new ArrayList<Object>();
        for (Object listItem : list) {
            if (listItem instanceof Map) {
                matchedList.add(this.deepSearchContext(pathElements, (Map)listItem));
                continue;
            }
            matchedList.add(listItem);
        }
        return matchedList;
    }

    protected Object matchComposite(String relativePath, String fullPath) throws Exception {
        Object val = this.resolveValue(relativePath, true);
        if (val instanceof List) {
            return val;
        }
        if (val instanceof Map) {
            ArrayList<Object> list = new ArrayList<Object>();
            list.add(val);
            return list;
        }
        logger.debug("matchComposite: Matched primitive " + val + " for " + relativePath);
        return null;
    }

    protected int getMatchCount(Object match) {
        if (match != null) {
            List nodeList = (List)match;
            return nodeList.size();
        }
        return 0;
    }

    protected Object getMatchItem(Object match, int ith) {
        if (match != null) {
            List nodeList = (List)match;
            return nodeList.get(ith);
        }
        return null;
    }

    protected void resetContext() {
        this.setCurrentContext((Object)null);
    }
}