ContentFragmentHumanTranslator.java 14.5 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
 *  javax.jcr.ValueFactory
 *  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.ContentFragmentTranslator;
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.jcr.ValueFactory;
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 ContentFragmentHumanTranslator
extends ContentFragmentTranslator {
    private Element currentPropertiesElement = null;
    private Document currentDoc = null;
    private static final Logger log = LoggerFactory.getLogger(ContentFragmentHumanTranslator.class);

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

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

    @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, ValueFactory valueFactory) 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.contentFragmentRoot = 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.contentFragmentRoot != null && this.contentFragmentRoot.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);
                        if (TranslationUtils.isContentFragmentVariationNode(childNode)) {
                            this.importContentFragmentVariation(childNode, resourceResolver, sourceFilePath, valueFactory);
                            continue;
                        }
                        this.importChildProperty(childNode, resourceResolver, sourceFilePath);
                    }
                }
            }
        }
    }

    private void importChildProperty(Node childNode, ResourceResolver resourceResolver, ArrayList<String> sourceFilePath) throws PathNotFoundException, RepositoryException {
        javax.jcr.Node currentNode;
        Resource resource;
        Element childElement;
        String nodePath;
        if (childNode != null && TranslationUtils.TRANSLATION_OBJECT_PROPERTY_NODE.equals(childNode.getNodeName()) && this.isNodePathPresentInSourceList(sourceFilePath, nodePath = (childElement = (Element)childNode).getAttribute(TranslationUtils.ATTRIBUTE_TRANSLATION_NODE_PATH)) && (resource = resourceResolver.getResource(nodePath)) != null && (currentNode = (javax.jcr.Node)resource.adaptTo(javax.jcr.Node.class)) != null) {
            Property prop;
            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) && (prop = currentNode.getProperty(propertyName)) != 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());
                }
            }
        }
    }

    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);
        for (Resource currentRoot : resourceList) {
            javax.jcr.Node currentRootNode = (javax.jcr.Node)currentRoot.adaptTo(javax.jcr.Node.class);
            this.processTranslationOrTemporaryUpdateNode(currentRootNode);
            this.processVariations(currentRootNode);
        }
        rootElement.appendChild(this.currentPropertiesElement);
        document = this.currentDoc;
        this.currentPropertiesElement = null;
        this.currentDoc = null;
        return document;
    }

    @Override
    protected boolean processVariation(javax.jcr.Node variationNode) {
        log.debug("FUNCTION: processVariation");
        boolean bSave = false;
        try {
            String mimeType = this.getMimeType(variationNode);
            if (mimeType.equals(MIME_TYPE_HTML) || mimeType.equals(MIME_TYPE_PLAIN) || mimeType.equals(MIME_TYPE_MARKDOWN)) {
                String valueString;
                String strStringToTranslate;
                Element propElement = this.currentDoc.createElement(TranslationUtils.TRANSLATION_OBJECT_PROPERTY_NODE);
                propElement.setAttribute(TranslationUtils.ATTRIBUTE_TRANSLATION_PROPERTY_NAME, "nt:file");
                propElement.setAttribute(TranslationUtils.ATTRIBUTE_TRANSLATION_NODE_PATH, variationNode.getPath());
                propElement.setAttribute(TranslationUtils.ATTRIBUTE_TRANSLATION_MULTI_VALUE, "false");
                Value value = variationNode.getProperty("jcr:content/jcr:data").getValue();
                if (value != null && (strStringToTranslate = (valueString = value.toString())) != null && !strStringToTranslate.isEmpty()) {
                    propElement.setTextContent(strStringToTranslate);
                    bSave = true;
                }
                if (bSave) {
                    this.currentPropertiesElement.appendChild(propElement);
                }
            }
        }
        catch (RepositoryException ex) {
            // empty catch block
        }
        return bSave;
    }

    private void importContentFragmentVariation(Node childNode, ResourceResolver resourceResolver, ArrayList<String> sourceFilePath, ValueFactory valueFactory) throws RepositoryException {
        Element childElement;
        String nodePath;
        javax.jcr.Node variationNode;
        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)) && (resource = resourceResolver.getResource(nodePath)) != null && (variationNode = (javax.jcr.Node)resource.adaptTo(javax.jcr.Node.class)) != null) {
            Value variationValue = valueFactory.createValue(childElement.getTextContent(), 2);
            variationNode.getProperty("jcr:content/jcr:data").setValue(variationValue);
        }
    }
}