CQPageHumanTranslator.java 13.1 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.granite.translation.api.TranslationException
 *  com.adobe.granite.translation.api.TranslationService
 *  javax.jcr.Node
 *  javax.jcr.PathNotFoundException
 *  javax.jcr.Property
 *  javax.jcr.RepositoryException
 *  javax.jcr.Value
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.wcm.translation.impl;

import com.adobe.cq.wcm.translation.impl.CQPageTranslator;
import com.adobe.cq.wcm.translation.impl.TranslationObjectImpl;
import com.adobe.cq.wcm.translation.impl.TranslationRuleConfigurationFile;
import com.adobe.cq.wcm.translation.impl.TranslationUtils;
import com.adobe.granite.translation.api.TranslationException;
import com.adobe.granite.translation.api.TranslationService;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import javax.jcr.PathNotFoundException;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Value;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class CQPageHumanTranslator
extends CQPageTranslator {
    private Element currentPropertiesElement = null;
    private Document currentDoc = null;
    private static final Logger log = LoggerFactory.getLogger(CQPageHumanTranslator.class);

    public CQPageHumanTranslator(TranslationRuleConfigurationFile ruleFile) {
        super(ruleFile, null);
    }

    protected boolean translateNow() throws RepositoryException, TranslationException {
        return false;
    }

    @Override
    protected boolean processTagElement(javax.jcr.Node tagNode, String strTagAttributeName, String strSourceTagTitle) throws TranslationException, RepositoryException {
        Boolean bMultiValue = false;
        Element propElement = this.currentDoc.createElement(TranslationUtils.TRANSLATION_OBJECT_PROPERTY_NODE);
        propElement.setAttribute(TranslationUtils.ATTRIBUTE_TRANSLATION_PROPERTY_NAME, strTagAttributeName);
        propElement.setAttribute(TranslationUtils.ATTRIBUTE_TRANSLATION_NODE_PATH, tagNode.getPath());
        propElement.setAttribute(TranslationUtils.ATTRIBUTE_TRANSLATION_MULTI_VALUE, bMultiValue.toString());
        propElement.setTextContent(strSourceTagTitle);
        this.currentPropertiesElement.appendChild(propElement);
        return true;
    }

    @Override
    protected boolean processNodeProperty(javax.jcr.Node node, Property prop, String strPropertyName) {
        log.debug("FUNCTION: translateNodeProperty ", (Object)strPropertyName);
        boolean bSave = false;
        try {
            Element propElement = this.currentDoc.createElement(TranslationUtils.TRANSLATION_OBJECT_PROPERTY_NODE);
            Boolean bMultiValue = prop.isMultiple();
            propElement.setAttribute(TranslationUtils.ATTRIBUTE_TRANSLATION_PROPERTY_NAME, strPropertyName);
            propElement.setAttribute(TranslationUtils.ATTRIBUTE_TRANSLATION_NODE_PATH, node.getPath());
            propElement.setAttribute(TranslationUtils.ATTRIBUTE_TRANSLATION_MULTI_VALUE, bMultiValue.toString());
            if (bMultiValue.booleanValue()) {
                Value[] val_array = prop.getValues();
                if (val_array != null && val_array.length > 0) {
                    bSave = false;
                    for (int index = 0; index < val_array.length; ++index) {
                        String strStringToTranslate;
                        Value value = val_array[index];
                        if (value == null || value.getType() != 1 || (strStringToTranslate = value.getString()) == null || strStringToTranslate.isEmpty()) continue;
                        Element valueElement = this.currentDoc.createElement(TranslationUtils.TRANSLATION_OBJECT_VALUE_NODE);
                        valueElement.setTextContent(strStringToTranslate);
                        valueElement.setAttribute(TranslationUtils.ATTRIBUTE_TRANSLATION_ORDER, new Integer(index).toString());
                        propElement.appendChild(valueElement);
                        bSave = true;
                    }
                }
            } else {
                String strStringToTranslate;
                Value value = prop.getValue();
                if (value != null && value.getType() == 1 && (strStringToTranslate = value.getString()) != null && !strStringToTranslate.isEmpty()) {
                    propElement.setTextContent(strStringToTranslate);
                    bSave = true;
                }
            }
            if (bSave) {
                this.currentPropertiesElement.appendChild(propElement);
            }
        }
        catch (RepositoryException ex) {
            // empty catch block
        }
        return bSave;
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    public static String getSourcePathOrFileTypeFromFile(File file) {
        String strSourcePathOrFileType;
        strSourcePathOrFileType = null;
        if (file != null && file.exists()) {
            FileInputStream is = null;
            try {
                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                DocumentBuilder db = dbf.newDocumentBuilder();
                is = new FileInputStream(file);
                Document doc = db.parse(is);
                Node rootNode = doc.getFirstChild();
                Element rootElement = (Element)rootNode;
                if (TranslationUtils.TRANSLATION_OBJECT_FILE_NODE.equals(rootElement.getNodeName())) {
                    if (rootElement.hasAttribute("sourcePath")) {
                        strSourcePathOrFileType = rootElement.getAttribute("sourcePath");
                    } else if (rootElement.hasAttribute("fileType")) {
                        strSourcePathOrFileType = rootElement.getAttribute("fileType");
                    }
                }
            }
            catch (Exception e) {
                log.trace("getSourcePathOrFileTypeFromFile Error while parsing file " + file.getAbsolutePath());
            }
            finally {
                if (is != null) {
                    try {
                        is.close();
                    }
                    catch (IOException e) {}
                }
            }
        }
        return strSourcePathOrFileType;
    }

    public void importInputStream(InputStream inputStream, Resource cqPageResource, ResourceResolver resourceResolver, String strObjectType, ArrayList<String> sourceFilePath, boolean bTagMetadata) throws ParserConfigurationException, SAXException, IOException, PathNotFoundException, RepositoryException {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(inputStream);
        Node rootNode = doc.getFirstChild();
        Element rootElement = (Element)rootNode;
        this.rootPage = cqPageResource;
        if (TranslationUtils.TRANSLATION_OBJECT_FILE_NODE.equals(rootElement.getNodeName())) {
            String strSourcePath = rootElement.getAttribute("sourcePath");
            String strFileType = rootElement.getAttribute("fileType");
            boolean bValid = false;
            if (this.rootPage != null && this.rootPage.getPath().equals(strSourcePath)) {
                bValid = true;
            } else if (strFileType != null && !strFileType.isEmpty() && strFileType.equals(strObjectType)) {
                bValid = true;
            }
            if (bValid) {
                NodeList rootChildNodeList = rootNode.getChildNodes();
                for (int rootIndex = 0; rootIndex < rootChildNodeList.getLength(); ++rootIndex) {
                    Node propertiesNode = rootChildNodeList.item(rootIndex);
                    if (propertiesNode == null || !TranslationUtils.TRANSLATION_OBJECT_PROPERTIES_NODE.equals(propertiesNode.getNodeName())) continue;
                    NodeList nodeList = propertiesNode.getChildNodes();
                    for (int index = 0; index < nodeList.getLength(); ++index) {
                        Node childNode = nodeList.item(index);
                        this.importChildProperty(childNode, resourceResolver, sourceFilePath, bTagMetadata);
                    }
                }
            }
        }
    }

    private void importChildProperty(Node childNode, ResourceResolver resourceResolver, ArrayList<String> sourceFilePath, boolean bTagMetadata) throws PathNotFoundException, RepositoryException {
        javax.jcr.Node currentNode;
        Element childElement;
        String nodePath;
        Resource resource;
        if (childNode != null && TranslationUtils.TRANSLATION_OBJECT_PROPERTY_NODE.equals(childNode.getNodeName()) && (this.isNodePathPresentInSourceList(sourceFilePath, nodePath = (childElement = (Element)childNode).getAttribute(TranslationUtils.ATTRIBUTE_TRANSLATION_NODE_PATH)) || bTagMetadata && TranslationUtils.isETCTagsPath(nodePath)) && (resource = resourceResolver.getResource(nodePath)) != null && (currentNode = (javax.jcr.Node)resource.adaptTo(javax.jcr.Node.class)) != null) {
            String propertyName = childElement.getAttribute(TranslationUtils.ATTRIBUTE_TRANSLATION_PROPERTY_NAME);
            String isMultiValue = childElement.getAttribute(TranslationUtils.ATTRIBUTE_TRANSLATION_MULTI_VALUE);
            boolean bMultiValue = "true".equals(isMultiValue);
            if (currentNode.hasProperty(propertyName)) {
                Property prop = currentNode.getProperty(propertyName);
                if (prop != null && bMultiValue == prop.isMultiple()) {
                    if (bMultiValue) {
                        NodeList nodeList = childNode.getChildNodes();
                        ArrayList<String> strValArray = new ArrayList<String>();
                        for (int index = 0; index < nodeList.getLength(); ++index) {
                            Node valueNode = nodeList.item(index);
                            if (!TranslationUtils.TRANSLATION_OBJECT_VALUE_NODE.equals(valueNode.getNodeName())) continue;
                            strValArray.add(valueNode.getTextContent());
                        }
                        String[] valueArray = TranslationUtils.getStringArray(strValArray);
                        prop.setValue(valueArray);
                    } else {
                        prop.setValue(childElement.getTextContent());
                    }
                }
            } else if (bTagMetadata) {
                currentNode.setProperty(propertyName, childElement.getTextContent());
            }
        }
    }

    private boolean isNodePathPresentInSourceList(ArrayList<String> sourceFilePath, String nodePath) {
        boolean bRetVal = false;
        if (sourceFilePath != null) {
            for (String strSourcePath : sourceFilePath) {
                if (nodePath.indexOf(strSourcePath) != 0) continue;
                bRetVal = true;
                break;
            }
        }
        return bRetVal;
    }

    public Document generateXMLForResourceList(ArrayList<Resource> resourceList, TranslationObjectImpl.TranslationObjectType objectType, String strSourcePath) throws ParserConfigurationException, RepositoryException, TranslationException, TransformerException, IOException {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document document = null;
        this.currentDoc = db.newDocument();
        Element rootElement = this.currentDoc.createElement(TranslationUtils.TRANSLATION_OBJECT_FILE_NODE);
        if (objectType != null) {
            rootElement.setAttribute("fileType", objectType.toString());
        }
        if (strSourcePath != null) {
            rootElement.setAttribute("sourcePath", strSourcePath);
        }
        this.currentDoc.appendChild(rootElement);
        this.currentPropertiesElement = this.currentDoc.createElement(TranslationUtils.TRANSLATION_OBJECT_PROPERTIES_NODE);
        ArrayList<String> tagList = new ArrayList<String>();
        for (Resource currentRoot : resourceList) {
            if (objectType == TranslationObjectImpl.TranslationObjectType.TAGMETADATA) {
                this.processTagTranslationNode((javax.jcr.Node)currentRoot.adaptTo(javax.jcr.Node.class), tagList);
                continue;
            }
            this.processTranslationOrTemporaryUpdateNode((javax.jcr.Node)currentRoot.adaptTo(javax.jcr.Node.class));
        }
        if (objectType == TranslationObjectImpl.TranslationObjectType.TAGMETADATA) {
            this.processTagListNow(tagList);
        }
        rootElement.appendChild(this.currentPropertiesElement);
        document = this.currentDoc;
        this.currentPropertiesElement = null;
        this.currentDoc = null;
        return document;
    }
}