I18nDictionaryTranslator.java 9.47 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.NodeIterator
 *  javax.jcr.Property
 *  javax.jcr.PropertyIterator
 *  javax.jcr.RepositoryException
 *  org.apache.commons.lang3.StringUtils
 *  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.I18nDictionary;
import com.adobe.cq.wcm.translation.impl.I18nStringExtractor;
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.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.Property;
import javax.jcr.PropertyIterator;
import javax.jcr.RepositoryException;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

class I18nDictionaryTranslator {
    private static final Logger logger = LoggerFactory.getLogger(I18nDictionaryTranslator.class);
    protected TranslationService tSvc;
    protected TranslationRuleConfigurationFile ruleFile;
    protected ResourceResolver resourceResolver;
    protected I18nStringExtractor i18nStringExtractor;
    protected HashSet<String> existingI18nStringSet;
    protected HashSet<String> componentPathSet = new HashSet();

    public I18nDictionaryTranslator(TranslationRuleConfigurationFile ruleFile, TranslationService tSvc, ResourceResolver resourceResolver) {
        this.tSvc = tSvc;
        logger.trace("In function: I18nDictionaryTranslator");
        this.ruleFile = ruleFile;
        this.resourceResolver = resourceResolver;
        this.existingI18nStringSet = I18nDictionary.getAllI18nDictKeys(resourceResolver, ruleFile.getDestinationLanguage(), null);
        this.i18nStringExtractor = new I18nStringExtractor(this.existingI18nStringSet, ruleFile, resourceResolver);
    }

    protected boolean processI18nDictTranslationNode(Resource i18NDict) throws RepositoryException, TranslationException, IOException {
        logger.trace("In function: processI18nDictTranslationNode");
        if (TranslationUtils.checkFileExtension((Node)i18NDict.adaptTo(Node.class), ".json")) {
            return this.processI18nJson(i18NDict);
        }
        return this.processI18nJcrNodes(i18NDict);
    }

    private void processChildNodesForComponents(Node node) {
        try {
            NodeIterator nodesIterator = node.getNodes();
            if (nodesIterator != null) {
                while (nodesIterator.hasNext()) {
                    Node childNode = nodesIterator.nextNode();
                    if (childNode.isNodeType("cq:Page")) continue;
                    this.processSlingResourceTypeForComponents(childNode);
                }
            }
        }
        catch (Exception ignored) {
            // empty catch block
        }
    }

    private static boolean isResourceStringExtractionRequired(Resource resource) {
        return resource.getPath().startsWith("/apps/");
    }

    private void processSlingResourceTypeForComponents(Node node) {
        try {
            if (I18nStringExtractor.isI18nContentStringExtractionRequired(node)) {
                this.i18nStringExtractor.addFileForStringExtraction(node);
            } else {
                Resource resource;
                String resourceType = TranslationUtils.getStringAttribute(logger, node, "sling:resourceType", "");
                if (!StringUtils.isEmpty((CharSequence)resourceType) && I18nDictionaryTranslator.isResourceStringExtractionRequired(resource = this.resourceResolver.getResource(resourceType))) {
                    if (logger.isTraceEnabled()) {
                        logger.trace("Resource Type: {}", (Object)resourceType);
                        logger.trace("Path: {}", (Object)resource.getPath());
                    }
                    if (this.componentPathSet.add(resource.getPath())) {
                        this.processSlingResourceTypeForComponents((Node)resource.adaptTo(Node.class));
                    }
                }
            }
        }
        catch (Exception ignored) {
            // empty catch block
        }
        this.processSlingResourceSuperTypeForComponents(node);
        this.processChildNodesForComponents(node);
    }

    protected HashMap<String, String> extractComponentStringMapFromResourceList(ArrayList<Resource> resourceList) {
        logger.trace("In function: getI18nComponentStringDict");
        if (resourceList.size() > 0) {
            try {
                this.extractTranslatableComponentListFromResourceList(resourceList);
                if (logger.isTraceEnabled()) {
                    logger.trace("Components:{}", (Object)this.componentPathSet.toString());
                }
                HashMap<String, String> componentStringMap = new HashMap<String, String>();
                for (String componentPath : this.componentPathSet) {
                    Resource componentResource = this.resourceResolver.getResource(componentPath);
                    if (componentResource == null) continue;
                    this.extractTranslatableStringsFromNodeProperties((Node)componentResource.adaptTo(Node.class), componentStringMap, this.existingI18nStringSet);
                }
                this.i18nStringExtractor.extractAllI18nStrings(componentStringMap);
                return componentStringMap;
            }
            catch (Exception e) {
                logger.error("Can't get component strings");
                return null;
            }
        }
        return null;
    }

    private void processSlingResourceSuperTypeForComponents(Node node) {
        try {
            Resource resource;
            String resourceSuperType = TranslationUtils.getStringAttribute(logger, node, "sling:resourceSuperType", "");
            if (!StringUtils.isEmpty((CharSequence)resourceSuperType) && I18nDictionaryTranslator.isResourceStringExtractionRequired(resource = this.resourceResolver.getResource(resourceSuperType))) {
                if (logger.isTraceEnabled()) {
                    logger.trace("Resource Super Type: {}", (Object)resourceSuperType);
                    logger.trace("Path: {}", (Object)resource.getPath());
                }
                if (this.componentPathSet.add(resource.getPath())) {
                    this.processSlingResourceTypeForComponents((Node)resource.adaptTo(Node.class));
                }
            }
        }
        catch (Exception ignored) {
            // empty catch block
        }
    }

    private void extractTranslatableComponentListFromResourceList(ArrayList<Resource> resourceList) throws RepositoryException, TranslationException, IOException {
        if (resourceList.size() > 0) {
            for (Resource resource : resourceList) {
                this.processSlingResourceTypeForComponents((Node)resource.adaptTo(Node.class));
            }
        }
    }

    private boolean extractTranslatableStringsFromNodeProperties(Node node, HashMap<String, String> extractedStringMap, HashSet<String> existingI18nStringSet) throws TranslationException, IOException {
        boolean bSave = false;
        try {
            PropertyIterator propIterator = node.getProperties();
            ArrayList<String> translationList = this.ruleFile.getTranslatablePropertyListForNode(node);
            while (propIterator.hasNext()) {
                String componentString;
                Property prop = propIterator.nextProperty();
                String strPropertyName = prop.getName();
                if (translationList == null || !translationList.contains(strPropertyName) || extractedStringMap.containsKey(componentString = prop.getString()) || existingI18nStringSet.contains(componentString)) continue;
                extractedStringMap.put(componentString, componentString);
                bSave = true;
            }
            bSave = this.extractComponentStringsFromChildNodes(node, extractedStringMap, existingI18nStringSet);
        }
        catch (RepositoryException e) {
            logger.error("extractComponentStringsFromNode: Error processing component strings", (Throwable)e);
        }
        return bSave;
    }

    private boolean extractComponentStringsFromChildNodes(Node node, HashMap<String, String> componentStringMap, HashSet<String> existingI18nStringSet) throws TranslationException, IOException {
        boolean bSave = false;
        try {
            NodeIterator nodesIterator = node.getNodes();
            if (nodesIterator != null) {
                while (nodesIterator.hasNext()) {
                    Node childNode = nodesIterator.nextNode();
                    this.extractTranslatableStringsFromNodeProperties(childNode, componentStringMap, existingI18nStringSet);
                    bSave = true;
                }
            }
        }
        catch (Exception ignored) {
            // empty catch block
        }
        return bSave;
    }

    protected boolean processI18nJson(Resource i18nJsonDict) {
        return false;
    }

    protected boolean processI18nJcrNodes(Resource i18nJcrDict) {
        return false;
    }
}