I18nJSPProcessor.java 7.71 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  javax.jcr.Node
 *  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.I18nStringExtractor;
import java.util.HashMap;
import java.util.HashSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.jcr.Node;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class I18nJSPProcessor
extends I18nContentProcessor {
    private static Logger logger = LoggerFactory.getLogger(I18nJSPProcessor.class);

    public I18nJSPProcessor(I18nStringExtractor i18nStringExtractor, HashSet<String> existingI18nStringSet, ResourceResolver resourceResolver) {
        super(i18nStringExtractor, existingI18nStringSet, resourceResolver);
    }

    @Override
    protected boolean processFileForI18nStrings(Node node, HashMap<String, String> i18nStringMap) {
        String regEx;
        boolean bSave = false;
        String fileContent = I18nStringExtractor.getFileContentsFromNode(node);
        try {
            logger.trace("In function: processFileForI18nStrings for {}", (Object)node.getPath());
            regEx = "[iI]18n.get\\(\\s*\"([^,]*?)\"\\)";
            if (this.searchAndAddI18nString(fileContent, i18nStringMap, regEx, 1)) {
                bSave = true;
            }
        }
        catch (Exception ignored) {
            // empty catch block
        }
        try {
            regEx = "[iI]18n.get\\(\\s*\"([^\\)]*?)\"\\s*,\\s*(\"(.*?)\"|null\\s*)";
            if (this.searchAndAddI18nStringWithComment(fileContent, i18nStringMap, regEx, 1, 3)) {
                bSave = true;
            }
        }
        catch (Exception ignored) {
            // empty catch block
        }
        return bSave;
    }

    @Override
    protected boolean processFileForIncludes(Node node) {
        boolean bSave = false;
        try {
            String includePath;
            Node includeFileNode;
            String jspContent = I18nStringExtractor.getFileContentsFromNode(node);
            String regEx = "<cq:include\\s+script\\s*=\\s*\"(.*?)\"/>";
            Pattern pattern = Pattern.compile(regEx);
            Matcher matcher = pattern.matcher(jspContent);
            while (matcher.find()) {
                try {
                    includePath = matcher.group(1);
                    if (includePath == null) continue;
                    if (!includePath.startsWith("/")) {
                        includePath = node.getParent().getPath() + "/" + includePath;
                    }
                    if (!I18nStringExtractor.isI18nContentStringExtractionRequired(includeFileNode = (Node)this.resourceResolver.getResource(includePath).adaptTo(Node.class))) continue;
                    logger.trace("cqInclude : {}", (Object)includePath);
                    if (!this.i18nStringExtractor.addFileForStringExtraction(includeFileNode)) continue;
                    bSave = true;
                }
                catch (Exception ignored) {}
            }
            regEx = "include\\s*file\\s*=\\s*\"(.*?)\"";
            pattern = Pattern.compile(regEx);
            matcher = pattern.matcher(jspContent);
            while (matcher.find()) {
                try {
                    includePath = matcher.group(1);
                    if (includePath == null) continue;
                    if (!includePath.startsWith("/")) {
                        includePath = node.getParent().getPath() + "/" + includePath;
                    }
                    if (!I18nStringExtractor.isI18nContentStringExtractionRequired(includeFileNode = (Node)this.resourceResolver.getResource(includePath).adaptTo(Node.class))) continue;
                    logger.trace("include : {}", (Object)includePath);
                    if (!this.i18nStringExtractor.addFileForStringExtraction(includeFileNode)) continue;
                    bSave = true;
                }
                catch (Exception ignored) {}
            }
            regEx = "<cq:include\\s+path\\s*=\\s*\"(.*?)\"\\s+resourceType\\s*=\\s*\"(.*?)\"";
            pattern = Pattern.compile(regEx);
            matcher = pattern.matcher(jspContent);
            while (matcher.find()) {
                try {
                    String name = matcher.group(1);
                    String resourceType = matcher.group(2);
                    if (name == null || resourceType == null) continue;
                    Resource resource = this.resourceResolver.getResource(resourceType);
                    String includePath2 = resource.getPath() + "/" + name + ".jsp";
                    try {
                        includeFileNode = (Node)this.resourceResolver.getResource(includePath2).adaptTo(Node.class);
                    }
                    catch (Exception e) {
                        includePath2 = resource.getPath() + "/" + name + ".html";
                        includeFileNode = (Node)this.resourceResolver.getResource(includePath2).adaptTo(Node.class);
                    }
                    if (!I18nStringExtractor.isI18nContentStringExtractionRequired(includeFileNode)) continue;
                    logger.trace("cqInclude path  : {}", (Object)includePath2);
                    if (!this.i18nStringExtractor.addFileForStringExtraction(includeFileNode)) continue;
                    bSave = true;
                }
                catch (Exception ignored) {}
            }
            regEx = "<script\\s+.*?src\\s*=\\s*\"(.*?)\"";
            pattern = Pattern.compile(regEx);
            matcher = pattern.matcher(jspContent);
            while (matcher.find()) {
                try {
                    includePath = matcher.group(1);
                    if (includePath == null) continue;
                    if (!includePath.startsWith("/")) {
                        includePath = node.getParent().getPath() + "/" + includePath;
                    }
                    if (!I18nStringExtractor.isI18nContentStringExtractionRequired(includeFileNode = (Node)this.resourceResolver.getResource(includePath).adaptTo(Node.class))) continue;
                    logger.trace("include : {}", (Object)includePath);
                    if (!this.i18nStringExtractor.addFileForStringExtraction(includeFileNode)) continue;
                    bSave = true;
                }
                catch (Exception ignored) {}
            }
            if (this.processFileForClientLibCategories(node)) {
                bSave = true;
            }
        }
        catch (Exception ignored) {
            // empty catch block
        }
        return bSave;
    }

    public boolean processFileForClientLibCategories(Node node) {
        boolean bSave = false;
        try {
            String jspContent = I18nStringExtractor.getFileContentsFromNode(node);
            String regEx = "<cq:includeClientLib\\s+(categories|js)\\s*=\\s*\"\\s*(.*?)\"";
            Pattern pattern = Pattern.compile(regEx);
            Matcher matcher = pattern.matcher(jspContent);
            while (matcher.find()) {
                try {
                    String[] categories;
                    for (String category : categories = matcher.group(2).trim().split(",")) {
                        bSave = this.i18nStringExtractor.addClientCategory(category);
                    }
                    continue;
                }
                catch (Exception ignored) {
                    continue;
                }
            }
        }
        catch (Exception ignored) {
            // empty catch block
        }
        return bSave;
    }
}