DataUtils.java 3.57 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.icc.dbforms.exceptions.ICCException
 */
package com.adobe.fd.adaddon.internal.utils;

import com.adobe.icc.dbforms.exceptions.ICCException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class DataUtils {
    public static final String DSOM_PREFIX = "xfa[0].datasets[0].data[0].";

    public static Node resolveDataSom(Element rootElement, String dsom, boolean peek) {
        String[] components;
        if (dsom == null || rootElement == null) {
            return null;
        }
        String rootName = DataUtils.getRootElementName(dsom);
        String dataSom = dsom.substring("xfa[0].datasets[0].data[0].".length() + rootName.length() + 4);
        Node resolvedNode = null;
        Element parentElement = rootElement;
        for (String comp : components = dataSom.split("\\.")) {
            int elemOccur;
            Pattern pattern = Pattern.compile("(.+)\\[(\\d+)\\]");
            Matcher m = pattern.matcher(comp);
            if (!m.find()) {
                throw new ICCException("invalid component in DSOM {" + dsom + "} (expected '{name}[{occur}]'): " + comp);
            }
            String elemName = m.group(1);
            resolvedNode = DataUtils.getChildOccurrence(parentElement, elemName, elemOccur = Integer.parseInt(m.group(2)), !peek);
            if (resolvedNode == null || !(resolvedNode instanceof Element)) break;
            parentElement = (Element)resolvedNode;
        }
        return resolvedNode;
    }

    private static String getRootElementName(String dsom) {
        if (dsom == null) {
            return null;
        }
        if (dsom.indexOf("xfa[0].datasets[0].data[0].") != 0) {
            throw new ICCException("invalid DSOM expression: " + dsom);
        }
        String dataSom = dsom.substring("xfa[0].datasets[0].data[0].".length());
        String rootName = null;
        int rootPos = dataSom.indexOf("[0]");
        if (rootPos <= 0) {
            throw new ICCException("missing root subform/data element in DSOM: " + dsom);
        }
        rootName = dataSom.substring(0, rootPos);
        return rootName;
    }

    private static Node getChildOccurrence(Element parentElement, String elementName, int occurrence, boolean create) {
        if (occurrence < 0) {
            return null;
        }
        Node targetNode = null;
        int occur = -1;
        NodeList children = parentElement.getChildNodes();
        for (int i = 0; i < children.getLength(); ++i) {
            Node childNode = children.item(i);
            if (!(childNode instanceof Element) || !childNode.getNodeName().equals(elementName) || ++occur != occurrence) continue;
            targetNode = childNode;
            break;
        }
        if (targetNode == null && create) {
            for (int j = 0; j < occurrence - occur; ++j) {
                targetNode = DataUtils.addChildElement(parentElement, elementName, null);
            }
        }
        return targetNode;
    }

    private static Node addChildElement(Element parentElement, String elementName, String nameAttrValue) {
        if (parentElement == null || elementName == null) {
            return null;
        }
        Element childElement = parentElement.getOwnerDocument().createElement(elementName);
        parentElement.appendChild(childElement);
        if (nameAttrValue != null) {
            childElement.setTextContent(nameAttrValue);
        }
        return childElement;
    }
}