Translator.java 12.7 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.cq.wcm.launches.utils.LaunchUtils
 *  com.adobe.granite.translation.api.TranslationConstants
 *  com.adobe.granite.translation.api.TranslationConstants$ContentType
 *  com.adobe.granite.translation.api.TranslationException
 *  com.adobe.granite.translation.api.TranslationException$ErrorCode
 *  com.adobe.granite.translation.api.TranslationManager
 *  com.adobe.granite.translation.api.TranslationResult
 *  com.adobe.granite.translation.api.TranslationService
 *  com.day.cq.wcm.api.LanguageManager
 *  com.day.cq.wcm.api.Page
 *  javax.jcr.Node
 *  javax.jcr.NodeIterator
 *  javax.jcr.PathNotFoundException
 *  javax.jcr.Property
 *  javax.jcr.PropertyIterator
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  javax.jcr.Value
 *  javax.jcr.ValueFormatException
 *  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.launches.utils.LaunchUtils;
import com.adobe.cq.wcm.translation.impl.TranslationConfiguration;
import com.adobe.cq.wcm.translation.impl.TranslationPropertyType;
import com.adobe.cq.wcm.translation.impl.TranslationResult;
import com.adobe.cq.wcm.translation.impl.TranslationRuleConfigurationFile;
import com.adobe.cq.wcm.translation.impl.TranslationUtils;
import com.adobe.granite.translation.api.TranslationConstants;
import com.adobe.granite.translation.api.TranslationException;
import com.adobe.granite.translation.api.TranslationManager;
import com.adobe.granite.translation.api.TranslationService;
import com.day.cq.wcm.api.LanguageManager;
import com.day.cq.wcm.api.Page;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.PathNotFoundException;
import javax.jcr.Property;
import javax.jcr.PropertyIterator;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Value;
import javax.jcr.ValueFormatException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Translator {
    private static final Logger log = LoggerFactory.getLogger(Translator.class);
    private TranslationConfiguration config;
    TranslationRuleConfigurationFile ruleFile;
    private LanguageManager langManager;

    public Translator(TranslationConfiguration config, LanguageManager languageManager) {
        this.config = config;
        this.langManager = languageManager;
    }

    public Translator(TranslationRuleConfigurationFile ruleFile) {
        this.ruleFile = ruleFile;
    }

    private boolean translateChildNodes(TranslationResult result, Node node, TranslationService tSvc) {
        log.debug("FUNCTION: translateChildNodes");
        boolean bSave = false;
        try {
            NodeIterator inodes = node.getNodes();
            if (inodes != null) {
                while (inodes.hasNext()) {
                    if (!this.translateJCRNode(result, inodes.nextNode(), tSvc)) continue;
                    bSave = true;
                }
            }
        }
        catch (RepositoryException e) {
            log.error("translateChildNodes: Error translating", (Throwable)e);
        }
        return bSave;
    }

    private boolean translateNodeProperty(TranslationService tSvc, TranslationResult result, Node node, Property prop, String strPropertyName, TranslationPropertyType translationType) {
        log.debug("FUNCTION: translateNodeProperty ", (Object)strPropertyName);
        boolean bSave = false;
        try {
            if (prop.isMultiple()) {
                Value[] val_array = prop.getValues();
                if (val_array != null && val_array.length > 0) {
                    bSave = false;
                    String[] newVals = new String[val_array.length];
                    for (int index = 0; index < val_array.length; ++index) {
                        Value value = val_array[index];
                        String strOutput = this.getTranslatedText(tSvc, value, node, strPropertyName, result, translationType);
                        if (strOutput == null || strOutput.isEmpty()) {
                            strOutput = value.getString();
                        } else {
                            bSave = true;
                        }
                        newVals[index] = strOutput;
                    }
                    prop.setValue(newVals);
                }
            } else {
                Value value = prop.getValue();
                String strOutput = this.getTranslatedText(tSvc, value, node, strPropertyName, result, translationType);
                if (strOutput != null && !strOutput.isEmpty()) {
                    prop.setValue(strOutput);
                    bSave = true;
                }
            }
        }
        catch (TranslationException exp) {
            result.addNewFailure(node, strPropertyName, "", (Exception)exp);
        }
        catch (ValueFormatException ex) {
            result.addNewFailure(node, strPropertyName, "", (Exception)ex);
        }
        catch (RepositoryException ex) {
            result.addNewFailure(node, strPropertyName, "", (Exception)ex);
        }
        return bSave;
    }

    private String getTranslatedString(TranslationService tSvc, String strStringToTranslate) throws TranslationException {
        String contentCategory = tSvc.getDefaultCategory();
        String strSrcLang = this.config.getSourceLanguage();
        String strDstLang = this.config.getDestinationLanguage();
        TranslationConstants.ContentType contentType = TranslationConstants.ContentType.HTML;
        com.adobe.granite.translation.api.TranslationResult tResult = tSvc.translateString(strStringToTranslate, strSrcLang, strDstLang, contentType, contentCategory);
        String strRetVal = tResult.getTranslation();
        return strRetVal;
    }

    private String getTranslatedText(TranslationService tSvc, Value value, Node node, String strPropertyName, TranslationResult wfResult, TranslationPropertyType translationType) throws TranslationException, RepositoryException {
        String strRetVal = null;
        if (value != null && value.getType() == 1) {
            String strStringToTranslate = value.getString();
            String strTrimmedString = strStringToTranslate;
            if (strTrimmedString != null) {
                strTrimmedString = strTrimmedString.trim();
            }
            if (strTrimmedString != null && !strTrimmedString.isEmpty()) {
                if (translationType == TranslationPropertyType.TRANSLATETEXT) {
                    strRetVal = this.getTranslatedString(tSvc, strStringToTranslate);
                } else if (translationType == TranslationPropertyType.CONVERTLANGUAGE) {
                    strRetVal = this.config.getDestinationLanguage();
                }
                if (strRetVal == null || strRetVal.isEmpty()) {
                    throw new TranslationException("TranslationResult returned null value", TranslationException.ErrorCode.GENERAL_EXCEPTION);
                }
            }
        }
        return strRetVal;
    }

    private boolean translateNodeProperties(TranslationResult result, Node node, TranslationService tSvc) {
        log.debug("FUNCTION: translateNodeProperties");
        boolean bSave = false;
        try {
            PropertyIterator propIterator = node.getProperties();
            while (propIterator.hasNext()) {
                Property prop = propIterator.nextProperty();
                String strPropertyName = prop.getName();
                TranslationPropertyType translationType = this.config.getTranslationPropertyType(strPropertyName);
                if (translationType == TranslationPropertyType.UNDEFINED || !this.translateNodeProperty(tSvc, result, node, prop, strPropertyName, translationType)) continue;
                bSave = true;
            }
        }
        catch (RepositoryException e) {
            log.error("translateNodeProperties: Error translating", (Throwable)e);
        }
        return bSave;
    }

    private Resource getCorrectPageResource(ResourceResolver resourceResolver, Resource inputResource) {
        log.debug("FUNCTION: inside getCorrectPageResource");
        Resource retResource = inputResource;
        if (resourceResolver != null && inputResource != null && LaunchUtils.isLaunchResourcePath((String)inputResource.getPath())) {
            retResource = TranslationUtils.getSourceResourceFromLaunch(inputResource);
        }
        return retResource;
    }

    private boolean traverseAndTranslateParent(TranslationResult result, Resource pageResource, TranslationService tSvc) throws PathNotFoundException, RepositoryException {
        boolean bSave = false;
        if (pageResource != null && this.langManager != null) {
            log.trace("FUNCTION: traverseAndTranslateParent {}", (Object)pageResource.getPath());
            Page languageRootPage = this.langManager.getLanguageRoot(pageResource);
            Resource parentResource = pageResource.getParent();
            if (parentResource != null && languageRootPage != null) {
                log.trace("parent resource path {}", (Object)parentResource.getPath());
                Page pageNode = (Page)parentResource.adaptTo(Page.class);
                if (pageNode != null) {
                    int iDepth = pageNode.getDepth();
                    int iLangDepth = languageRootPage.getDepth();
                    log.trace("Got iDepth as {}", (Object)iDepth);
                    log.trace("Got iLangDepth as {}", (Object)iLangDepth);
                    if (iDepth >= iLangDepth) {
                        Node node = (Node)parentResource.adaptTo(Node.class);
                        if (node != null) {
                            Node jcrContent;
                            if (this.translateNodeProperties(result, node, tSvc)) {
                                bSave = true;
                            }
                            if ((jcrContent = node.getNode("jcr:content")) != null && this.translateJCRNode(result, jcrContent, tSvc)) {
                                bSave = true;
                            }
                        }
                        if (this.traverseAndTranslateParent(result, parentResource, tSvc)) {
                            bSave = true;
                        }
                    }
                }
            }
        }
        return bSave;
    }

    public void translateResourcePage(TranslationResult result, ResourceResolver resourceResolver, Resource inputResource) {
        block10 : {
            log.debug("FUNCTION: translateResourcePage");
            try {
                Resource pageResource = this.getCorrectPageResource(resourceResolver, inputResource);
                if (pageResource != null && inputResource != null) {
                    boolean bSave = false;
                    TranslationService tSvc = this.config.getTranslationManager().createTranslationService(pageResource);
                    Node rootNode = (Node)inputResource.adaptTo(Node.class);
                    if (this.translateJCRNode(result, rootNode, tSvc)) {
                        bSave = true;
                    }
                    if (this.traverseAndTranslateParent(result, inputResource, tSvc)) {
                        bSave = true;
                    }
                    try {
                        if (bSave) {
                            Page page = (Page)inputResource.adaptTo(Page.class);
                            if (page != null) {
                                TranslationUtils.updateLastModifiedTime(page);
                            }
                            rootNode.getSession().save();
                        }
                        break block10;
                    }
                    catch (Exception ex) {
                        log.error("Error while saving the node", (Throwable)ex);
                    }
                    break block10;
                }
                log.error("Error resource object is null");
            }
            catch (TranslationException ex) {
                log.error("Error while translating JCR Node ", (Throwable)ex);
            }
            catch (RepositoryException ex) {
                log.error("Error while translating JCR Node ", (Throwable)ex);
            }
        }
    }

    private boolean translateJCRNode(TranslationResult result, Node node, TranslationService tSvc) throws RepositoryException {
        log.debug("FUNCTION: translateJCRNode ", (Object)node.getPath());
        boolean bSave = false;
        if (this.translateNodeProperties(result, node, tSvc)) {
            bSave = true;
        }
        if (this.translateChildNodes(result, node, tSvc)) {
            bSave = true;
        }
        return bSave;
    }
}