DoRElement.java 16.9 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.aemds.guide.addon.dor.DoRThreadLocal
 *  com.adobe.aemds.guide.addon.dor.DoRUtils
 *  com.adobe.aemds.guide.addon.dor.GuideType
 *  com.adobe.aemds.guide.utils.XMLUtils
 *  com.adobe.xfa.Attribute
 *  com.adobe.xfa.DOMSaveOptions
 *  com.adobe.xfa.Element
 *  com.adobe.xfa.Measurement
 *  com.adobe.xfa.Node
 *  com.adobe.xfa.StringAttr
 *  com.adobe.xfa.XFA
 *  com.adobe.xfa.content.IntegerValue
 *  com.adobe.xfa.data.DataModel
 *  com.adobe.xfa.data.DataNode
 *  com.adobe.xfa.template.Value
 *  com.adobe.xfa.template.containers.Draw
 *  com.adobe.xfa.template.containers.Subform
 *  com.adobe.xfa.ut.UnitSpan
 *  org.apache.commons.lang3.StringUtils
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.aemds.guide.addon.dor.elements;

import com.adobe.aemds.guide.addon.dor.DoRThreadLocal;
import com.adobe.aemds.guide.addon.dor.DoRUtils;
import com.adobe.aemds.guide.addon.dor.GuideType;
import com.adobe.aemds.guide.addon.dor.elements.DoRElementUtils;
import com.adobe.aemds.guide.addon.dor.elements.GuideElementUtils;
import com.adobe.aemds.guide.utils.XMLUtils;
import com.adobe.xfa.Attribute;
import com.adobe.xfa.DOMSaveOptions;
import com.adobe.xfa.Measurement;
import com.adobe.xfa.Node;
import com.adobe.xfa.StringAttr;
import com.adobe.xfa.XFA;
import com.adobe.xfa.content.IntegerValue;
import com.adobe.xfa.data.DataModel;
import com.adobe.xfa.data.DataNode;
import com.adobe.xfa.template.Value;
import com.adobe.xfa.template.containers.Draw;
import com.adobe.xfa.template.containers.Subform;
import com.adobe.xfa.ut.UnitSpan;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.AbstractMap;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.Stack;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathFactory;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public abstract class DoRElement {
    protected static DocumentBuilderFactory documentBuilderFactory;
    protected static DocumentBuilder documentBuilder;
    protected static XPath xPath;
    private static Logger logger;
    protected Map<String, String> xPathExprs;
    protected com.adobe.xfa.Element xfaElement;

    public DoRElement(com.adobe.xfa.Element xfaElement) throws Exception {
        this.xfaElement = xfaElement;
        this.xPathExprs = new HashMap<String, String>();
        this.xPathExprs.put("jcr:title", "caption/value/text/text()");
        this.xPathExprs.put("templateId", "@id");
        this.xPathExprs.put("_value", "value/text/text()");
        this.xPathExprs.put("assistPriority", "assist/speak/@priority");
        this.xPathExprs.put("custom", "assist/speak/text()");
        this.xPathExprs.put("shortDescription", "assist/toolTip/text()");
        this.xPathExprs.put("bindRef", "bind/@ref");
        this.xPathExprs.put("bindMatch", "bind/@match");
        this.xPathExprs.put("colspan", "@colSpan");
        this.xPathExprs.put("border", "border");
        this.xPathExprs.put("paraHalign", "para/@hAlign");
        this.xPathExprs.put("paraValign", "para/@vAlign");
        this.xPathExprs.put("halign", "@hAlign");
        this.xPathExprs.put("displayPictureClause", "format/picture/text()");
        this.xPathExprs.put("validatePictureClause", "validate/picture/text()");
        this.xPathExprs.put("editPictureClause", "ui/picture/text()");
        this.xPathExprs.put("dorExclusion", "@presence");
        this.xPathExprs.put("w", "@w");
    }

    public boolean isContainer() {
        return false;
    }

    public String getCaptionValueSom() {
        return "#caption.#value";
    }

    public abstract void applyProperties(Properties var1) throws Exception;

    protected void applyProperties(Properties properties, boolean simple) throws Exception {
        com.adobe.xfa.Element parent = this.xfaElement.getXFAParent();
        boolean isRootSubform = !(parent instanceof Subform);
        boolean isDraw = this.xfaElement instanceof Draw;
        boolean dorExcludeTitle = properties.getProperty("dorExcludeTitle", "false").equals("true");
        if (!(isRootSubform || isDraw || dorExcludeTitle || properties.containsKey("jcr:title") || properties.containsKey("hideTitle"))) {
            properties.put("jcr:title", "");
        }
        if (DoRElementUtils.isTableRow(parent)) {
            this.xfaElement.removeAttr(null, "w");
            if (!DoRElementUtils.isTableHeaderRow(parent)) {
                properties.setProperty("border", "");
                properties.setProperty("paraHalign", "center");
                properties.setProperty("paraValign", "middle");
            }
        }
        if (parent != null) {
            int dorNumCols = 1;
            Node dorNumColsNode = parent.resolveNode("extras.dorNumCols");
            if (dorNumColsNode != null) {
                dorNumCols = ((IntegerValue)dorNumColsNode).getValue();
                int dorColspan = 1;
                String width = parent.getAttribute(XFA.WTAG).getAttrValue();
                String widthValue = DoRUtils.getValueMatch((String)"\\+?\\d+\\.?\\d*", (String)width);
                if (StringUtils.isNotEmpty((CharSequence)widthValue)) {
                    double dWidth;
                    String widthUnit = width.substring(widthValue.length());
                    double dWidthValue = Double.parseDouble(widthValue);
                    if (properties.containsKey("dorColspan")) {
                        dorColspan = Integer.parseInt(properties.getProperty("dorColspan"));
                        if (dorColspan < 1) {
                            dorColspan = 1;
                        }
                        properties.remove("dorColspan");
                    }
                    if ((dWidth = dWidthValue * (double)dorColspan / (double)dorNumCols) > dWidthValue) {
                        dWidth = dWidthValue;
                    }
                    properties.setProperty("w", "" + dWidth + widthUnit);
                }
                double scalingFactor = (double)dorColspan / (double)dorNumCols;
                properties.setProperty("halign", "left");
                this.modifyCaptionAttributes(this.xfaElement, scalingFactor);
                this.modifyParaAttributes(this.xfaElement, scalingFactor);
                this.modifyMarginAttributes(this.xfaElement, scalingFactor);
            }
        }
        if (properties.containsKey("dorExclusion")) {
            if ("true".equals(properties.getProperty("dorExclusion"))) {
                properties.setProperty("dorExclusion", "hidden");
            } else if ("false".equals(properties.getProperty("dorExclusion"))) {
                properties.setProperty("dorExclusion", "visible");
            }
        }
        DoRUtils.enablePlainText((Value)((Value)this.xfaElement.resolveNode(this.getCaptionValueSom(), true, false, true)));
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        this.xfaElement.saveXML((OutputStream)bos, null);
        Document elDoc = documentBuilder.parse(new ByteArrayInputStream(bos.toByteArray()));
        for (Map.Entry<K, V> property : properties.entrySet()) {
            Object object;
            String propertyName = null;
            String propertyValue = null;
            if (!simple || !((object = property.getKey()) instanceof String)) continue;
            propertyName = (String)object;
            object = property.getValue();
            if (!(object instanceof String)) continue;
            propertyValue = (String)object;
            String propertyXPath = this.xPathExprs.get(propertyName);
            if (propertyXPath == null || propertyXPath.length() <= 0) continue;
            if (propertyXPath.startsWith("@")) {
                if (propertyXPath.equals("@colSpan")) {
                    StringAttr attribute = new StringAttr("colSpan", propertyValue);
                    this.xfaElement.setAttribute((Attribute)attribute, XFA.COLSPANTAG);
                    continue;
                }
                if (propertyXPath.equals("@columnWidths")) {
                    StringAttr attribute = new StringAttr("columnWidths", propertyValue);
                    this.xfaElement.setAttribute((Attribute)attribute, XFA.COLUMNWIDTHSTAG);
                    continue;
                }
                if (propertyXPath.equals("@id")) {
                    StringAttr attribute = new StringAttr("id", propertyValue);
                    this.xfaElement.setAttribute((Attribute)attribute, XFA.IDTAG);
                    continue;
                }
                if (propertyXPath.equals("@presence")) {
                    StringAttr attribute = new StringAttr("presence", propertyValue);
                    this.xfaElement.setAttribute((Attribute)attribute, XFA.PRESENCETAG);
                    continue;
                }
                if (propertyXPath.equals("@layout")) {
                    StringAttr attribute = new StringAttr("layout", propertyValue);
                    this.xfaElement.setAttribute((Attribute)attribute, XFA.LAYOUTTAG);
                    continue;
                }
                if (propertyXPath.equals("@w")) {
                    StringAttr attribute = new StringAttr("w", propertyValue);
                    this.xfaElement.setAttribute((Attribute)attribute, XFA.WTAG);
                    continue;
                }
                if (!propertyXPath.equals("@hAlign")) continue;
                StringAttr attribute = new StringAttr("hAlign", propertyValue);
                this.xfaElement.setAttribute((Attribute)attribute, XFA.HALIGNTAG);
                continue;
            }
            XMLUtils.addToDocument((Document)elDoc, (XPath)xPath, (org.w3c.dom.Node)elDoc.getDocumentElement(), (String)this.xPathExprs.get(propertyName), (Object)propertyValue);
        }
        String xmlStr = XMLUtils.getXMLfromXsdDom((Element)elDoc.getDocumentElement());
        this.xfaElement.loadXML((InputStream)new ByteArrayInputStream(xmlStr.getBytes("UTF-8")), true, true);
        if (this.xfaElement.resolveNode("#caption") != null && !properties.containsKey("jcr:title")) {
            this.xfaElement.resolveNode("#caption").remove();
        }
    }

    protected void changeAttribute(com.adobe.xfa.Element node, double scalingFactor, String attributeName, int attributeTag) {
        Measurement attribute = (Measurement)node.getAttributeByName(attributeName, false);
        if (attribute != null) {
            UnitSpan unitSpan = attribute.getUnitSpan().multiply(scalingFactor);
            node.removeAttr(null, attributeName);
            attribute = new Measurement(unitSpan);
            node.setAttribute((Attribute)attribute, attributeTag);
        }
    }

    protected void modifyCaptionAttributes(com.adobe.xfa.Element node, double scalingFactor) {
        com.adobe.xfa.Element captionNode = (com.adobe.xfa.Element)node.resolveNode("caption", true, false, true);
        if (captionNode != null) {
            Attribute attribute = captionNode.getAttributeByName("placement", false);
            String placement = "left";
            if (attribute != null) {
                placement = attribute.getAttrValue();
            }
            if (StringUtils.equals((CharSequence)placement, (CharSequence)"left") || StringUtils.equals((CharSequence)placement, (CharSequence)"right") || StringUtils.equals((CharSequence)placement, (CharSequence)"inline")) {
                this.changeAttribute(captionNode, scalingFactor, "reserve", XFA.RESERVETAG);
            }
            this.modifyParaAttributes(captionNode, scalingFactor);
            this.modifyMarginAttributes(captionNode, scalingFactor);
        }
    }

    protected void modifyParaAttributes(com.adobe.xfa.Element node, double scalingFactor) {
        com.adobe.xfa.Element paraNode = node.getElement(XFA.PARATAG, true, 0, false, true);
        if (paraNode != null) {
            this.changeAttribute(paraNode, scalingFactor, "marginLeft", XFA.MARGINLEFTTAG);
            this.changeAttribute(paraNode, scalingFactor, "marginRight", XFA.MARGINRIGHTTAG);
        }
    }

    protected void modifyMarginAttributes(com.adobe.xfa.Element node, double scalingFactor) {
        com.adobe.xfa.Element marginNode = node.getElement(XFA.MARGINTAG, true, 0, false, true);
        if (marginNode != null) {
            this.changeAttribute(marginNode, scalingFactor, "leftInset", XFA.LEFTINSETTAG);
            this.changeAttribute(marginNode, scalingFactor, "rightInset", XFA.RIGHTINSETTAG);
        }
    }

    private void generateBindingForUnboundElement(String bindRef, Properties properties, Stack<Map.Entry<String, String>> bindings) {
        if (this.isContainer() && !DoRElementUtils.isRepeatable(properties)) {
            properties.put("bindMatch", "none");
            properties.remove("bindRef");
        }
    }

    private void generateBindingForBoundElement(String bindRef, Properties properties, Stack<Map.Entry<String, String>> bindings) {
        int lastComponentIndex = bindRef.lastIndexOf(47);
        String lastComponent = bindRef.substring(lastComponentIndex + 1);
        String parentComponent = bindRef.substring(0, lastComponentIndex);
        Map.Entry<String, String> stackTop = bindings.isEmpty() ? null : bindings.peek();
        String topBindRef = stackTop != null ? stackTop.getKey() : null;
        lastComponent = lastComponent.replace("@", "").replace("text()", "");
        String xfaBindRef = null;
        xfaBindRef = StringUtils.equals((CharSequence)parentComponent, (CharSequence)topBindRef) ? (StringUtils.endsWith((CharSequence)stackTop.getValue(), (CharSequence)"[*]") ? (lastComponent.isEmpty() ? "" : "$." + lastComponent) : stackTop.getValue() + (lastComponent.isEmpty() ? "" : new StringBuilder().append(".").append(lastComponent).toString())) : properties.getProperty("bindRef");
        if (this.isContainer()) {
            if (DoRElementUtils.isRepeatable(properties)) {
                xfaBindRef = xfaBindRef + "[*]";
                properties.put("bindRef", xfaBindRef);
            } else {
                properties.put("bindMatch", "none");
                properties.remove("bindRef");
            }
            bindings.add(new AbstractMap.SimpleEntry<String, String>(bindRef, xfaBindRef));
        } else {
            properties.put("bindRef", xfaBindRef);
        }
    }

    public void adaptBinding(String bindRef, Properties properties, Stack<Map.Entry<String, String>> bindings) {
        if (bindRef == null || bindRef.isEmpty()) {
            this.generateBindingForUnboundElement(bindRef, properties, bindings);
        } else {
            this.generateBindingForBoundElement(bindRef, properties, bindings);
        }
    }

    public void generateDataDescription(String bindRef, String xfaBindRef, DataNode dataRoot, DataModel dataModel) {
        int count;
        DataNode parent = dataRoot;
        boolean relativeBinding = StringUtils.startsWith((CharSequence)xfaBindRef, (CharSequence)"$.");
        String[] tokens = relativeBinding ? StringUtils.split((String)bindRef, (String)"\\/") : xfaBindRef.split("\\.");
        int tokensCount = tokens.length;
        boolean hasAttributeBinding = GuideElementUtils.hasAttributeBinding(bindRef);
        if (hasAttributeBinding) {
            --tokensCount;
        }
        if ("$record".equals(tokens[count = 0]) || relativeBinding && DoRThreadLocal.getGuideType().equals((Object)GuideType.XSD_BASED)) {
            ++count;
        }
        while (count < tokensCount) {
            String token = tokens[count].replaceAll("\\[.\\]", "");
            DataNode dataNode = (DataNode)DoRElementUtils.locateChildByName((Node)parent, "dd:" + token, 0);
            if (dataNode == null) {
                dataNode = (DataNode)dataModel.createNode(XFA.DATAGROUPTAG, (com.adobe.xfa.Element)parent, "dd:" + token, DataModel.dataDescriptionNS(), true);
            }
            parent = dataNode;
            ++count;
        }
        if (hasAttributeBinding) {
            String attribute = StringUtils.replace((String)tokens[count], (String)"@", (String)"");
            parent.setAttribute(DataModel.dataDescriptionNS(), attribute, attribute, "");
        } else if (XFA.DATAGROUPTAG == parent.getClassTag()) {
            parent.setClass("dataValue", XFA.DATAVALUETAG);
            parent.setAttribute("http://www.xfa.org/schema/xfa-data/1.0/", "dataNode", "xfa:dataNode", "dataValue");
        }
    }

    static {
        logger = LoggerFactory.getLogger(DoRElement.class);
        try {
            documentBuilderFactory = DocumentBuilderFactory.newInstance();
            documentBuilder = documentBuilderFactory.newDocumentBuilder();
            xPath = XPathFactory.newInstance().newXPath();
        }
        catch (ParserConfigurationException e) {
            logger.error("[DoRElement]Failed to parse ", (Throwable)e);
        }
    }
}