I18nStringExtractor.java 7.98 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.granite.ui.clientlibs.ClientLibrary
 *  com.adobe.granite.ui.clientlibs.HtmlLibraryManager
 *  com.adobe.granite.ui.clientlibs.LibraryType
 *  javax.jcr.Binary
 *  javax.jcr.Node
 *  javax.jcr.Property
 *  javax.jcr.RepositoryException
 *  org.apache.commons.io.IOUtils
 *  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.I18nContentProcessor;
import com.adobe.cq.wcm.translation.impl.I18nDictionaryTranslator;
import com.adobe.cq.wcm.translation.impl.I18nHandlebarsProcessor;
import com.adobe.cq.wcm.translation.impl.I18nJSPProcessor;
import com.adobe.cq.wcm.translation.impl.I18nJavaScriptProcessor;
import com.adobe.cq.wcm.translation.impl.I18nSightlyProcessor;
import com.adobe.cq.wcm.translation.impl.TranslationRuleConfigurationFile;
import com.adobe.cq.wcm.translation.impl.TranslationUtils;
import com.adobe.granite.ui.clientlibs.ClientLibrary;
import com.adobe.granite.ui.clientlibs.HtmlLibraryManager;
import com.adobe.granite.ui.clientlibs.LibraryType;
import java.io.BufferedInputStream;
import java.io.InputStream;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.jcr.Binary;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import org.apache.commons.io.IOUtils;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class I18nStringExtractor {
    private static final Logger logger = LoggerFactory.getLogger(I18nDictionaryTranslator.class);
    private static final String JS_TXT = "js.txt";
    private HashSet<String> filePathSet = new HashSet();
    private HashSet<String> clientLibCategorySet = new HashSet();
    private TranslationRuleConfigurationFile ruleFile = null;
    private ResourceResolver resourceResolver = null;
    private I18nContentProcessor jspProcessor = null;
    private I18nContentProcessor sightlyProcessor = null;
    private I18nContentProcessor javaScriptProcessor = null;
    private I18nContentProcessor handlebarsProcessor = null;

    public I18nStringExtractor(HashSet<String> existingI18nStringSet, TranslationRuleConfigurationFile ruleFile, ResourceResolver resourceResolver) {
        this.ruleFile = ruleFile;
        this.resourceResolver = resourceResolver;
        this.jspProcessor = new I18nJSPProcessor(this, existingI18nStringSet, resourceResolver);
        this.sightlyProcessor = new I18nSightlyProcessor(this, existingI18nStringSet, resourceResolver);
        this.javaScriptProcessor = new I18nJavaScriptProcessor(this, existingI18nStringSet, resourceResolver);
        this.handlebarsProcessor = new I18nHandlebarsProcessor(this, existingI18nStringSet, resourceResolver);
    }

    private I18nContentProcessor getContentProcessor(Node fileNode) {
        if (TranslationUtils.checkFileExtension(fileNode, ".jsp")) {
            return this.jspProcessor;
        }
        if (TranslationUtils.checkFileExtension(fileNode, ".html")) {
            return this.sightlyProcessor;
        }
        if (TranslationUtils.checkFileExtension(fileNode, ".js")) {
            return this.javaScriptProcessor;
        }
        if (TranslationUtils.checkFileExtension(fileNode, ".hbs")) {
            return this.handlebarsProcessor;
        }
        return null;
    }

    public boolean extractAllI18nStrings(HashMap<String, String> i18nStringMap) {
        boolean bSave = false;
        this.getFilesFromClientLibCategories();
        for (String file : this.filePathSet) {
            try {
                if (!this.extractI18nStringsFromFile((Node)this.resourceResolver.getResource(file).adaptTo(Node.class), i18nStringMap)) continue;
                bSave = true;
            }
            catch (Exception ignored) {}
        }
        return bSave;
    }

    private void getFilesFromClientLibCategories() {
        if (this.ruleFile != null && this.ruleFile.htmlLibraryManager != null) {
            Collection clientLibraries = this.ruleFile.htmlLibraryManager.getLibraries(this.clientLibCategorySet.toArray(new String[this.clientLibCategorySet.size()]), LibraryType.JS, true, true);
            for (ClientLibrary clientLibrary : clientLibraries) {
                try {
                    String path = clientLibrary.getPath();
                    if (!path.startsWith("/apps/")) continue;
                    this.processClientLibForFiles(this.resourceResolver.getResource(path));
                }
                catch (Exception ignored) {}
            }
            this.clientLibCategorySet.clear();
        }
    }

    private void processClientLibForFiles(Resource clientLib) {
        Node jsTxtNode = (Node)clientLib.getChild("js.txt").adaptTo(Node.class);
        if (jsTxtNode != null) {
            String jsTxtContent = I18nStringExtractor.getFileContentsFromNode(jsTxtNode);
            String jsRootPath = this.getJSRelRootPath(jsTxtContent);
            if (jsRootPath == null) {
                return;
            }
            String regEx = "\"^\\\\s*([^#].*)\"";
            Pattern pattern = Pattern.compile(regEx);
            Matcher matcher = pattern.matcher(jsTxtContent);
            while (matcher.find()) {
                String jsRelPath = matcher.group(1);
                this.filePathSet.add(jsRootPath + "/" + jsRelPath);
            }
        }
    }

    private String getJSRelRootPath(String jsTxtContent) {
        String regEx = "^\\s*#base\\s*=\\s*(.*)";
        Pattern pattern = Pattern.compile(regEx);
        Matcher matcher = pattern.matcher(jsTxtContent);
        if (matcher.find()) {
            return matcher.group(1);
        }
        return null;
    }

    protected boolean addClientCategory(String category) {
        return this.clientLibCategorySet.add(category);
    }

    private boolean extractI18nStringsFromFile(Node fileNode, HashMap<String, String> i18nStringMap) {
        logger.trace("In function: extractI18nStringsFromFile");
        I18nContentProcessor i18NContentProcessor = this.getContentProcessor(fileNode);
        return i18NContentProcessor != null && i18NContentProcessor.processFileForI18nStrings(fileNode, i18nStringMap);
    }

    protected boolean addFileForStringExtraction(Node node) {
        boolean bSave = false;
        try {
            if (this.filePathSet.add(node.getPath())) {
                bSave = true;
                this.addRelatedFilesForStringExtraction(node);
            }
        }
        catch (RepositoryException ignored) {
            // empty catch block
        }
        return bSave;
    }

    private boolean addRelatedFilesForStringExtraction(Node fileNode) {
        I18nContentProcessor i18NContentProcessor = this.getContentProcessor(fileNode);
        return i18NContentProcessor != null && i18NContentProcessor.processFileForIncludes(fileNode);
    }

    public static boolean isI18nContentStringExtractionRequired(Node node) throws RepositoryException {
        return node.getPath().startsWith("/apps/") && (TranslationUtils.checkFileExtension(node, ".jsp") || TranslationUtils.checkFileExtension(node, ".html") || TranslationUtils.checkFileExtension(node, ".js") || TranslationUtils.checkFileExtension(node, ".hbs"));
    }

    public static String getFileContentsFromNode(Node jspNode) {
        String result = null;
        try {
            Node ntResourceNode = jspNode.getNode("jcr:content");
            InputStream inputStream = ntResourceNode.getProperty("jcr:data").getBinary().getStream();
            BufferedInputStream bin = new BufferedInputStream(inputStream);
            result = IOUtils.toString((InputStream)bin, (String)"UTF-8");
            bin.close();
            inputStream.close();
        }
        catch (Exception ignored) {
            // empty catch block
        }
        return result;
    }
}