I18nSightlyProcessor.java 8 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 I18nSightlyProcessor
extends I18nContentProcessor {
    private static Logger logger = LoggerFactory.getLogger(I18nSightlyProcessor.class);

    public I18nSightlyProcessor(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: processSightlyFileForI18nStrings for {}", (Object)node.getPath());
            regEx = "\\$\\{\\s*\"(.*?)\"\\s+@\\s+i18n\\s*,(?!.*?,\\s*hint\\s*=)";
            if (this.searchAndAddI18nString(fileContent, i18nStringMap, regEx, 1)) {
                bSave = true;
            }
        }
        catch (Exception ignored) {
            // empty catch block
        }
        try {
            regEx = "\\$\\{\\s*\"(.*?)\"\\s*@\\s+i18n\\s*,.*?,\\s*hint\\s*=\\s*\"(.*?)\"";
            if (this.searchAndAddI18nStringWithComment(fileContent, i18nStringMap, regEx, 1, 2)) {
                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 fileContent = I18nStringExtractor.getFileContentsFromNode(node);
            String regEx = "\\s+data-sly-include\\s*=\\s*['\"](.*?)['\"]";
            Pattern pattern = Pattern.compile(regEx);
            Matcher matcher = pattern.matcher(fileContent);
            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("data-sly-include : {}", (Object)includePath);
                    if (!this.i18nStringExtractor.addFileForStringExtraction(includeFileNode)) continue;
                    bSave = true;
                }
                catch (Exception ignored) {}
            }
            regEx = "data-sly-resource\\s*=\\s*\"(.*?)\"(?!\\s*@)";
            pattern = Pattern.compile(regEx);
            matcher = pattern.matcher(fileContent);
            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 = "data-sly-resource\\s*=\\s*\"\\$\\{\\s*@\\s*path\\s*=\\s*['\"](.*?)['\"]\\s*,\\s*resourceType\\s*=\\s*['\"](.*?)['\"]";
            pattern = Pattern.compile(regEx);
            matcher = pattern.matcher(fileContent);
            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("data-sly-resource  : {}", (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(fileContent);
            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) {}
            }
        }
        catch (Exception ignored) {
            // empty catch block
        }
        if (this.processFileForClientLibCategories(node)) {
            bSave = true;
        }
        return bSave;
    }

    public boolean processFileForClientLibCategories(Node node) {
        boolean bSave = false;
        try {
            String fileContent = I18nStringExtractor.getFileContentsFromNode(node);
            String regEx = "data-sly-call\\s*=\\s*\"\\$\\{\\s*((.*?\\.all\\s*@)|(.*?\\.js\\s*@))\\s*categories\\s*=\\s*\\[\\s*(.*?)\\]";
            Pattern pattern = Pattern.compile(regEx);
            Matcher matcher = pattern.matcher(fileContent);
            while (matcher.find()) {
                try {
                    String[] categories;
                    for (String category : categories = matcher.group(2).trim().split(",")) {
                        category = category.replaceAll("'", "");
                        category = category.replaceAll("\"", "");
                        bSave = this.i18nStringExtractor.addClientCategory(category);
                    }
                    continue;
                }
                catch (Exception ignored) {
                    continue;
                }
            }
        }
        catch (Exception ignored) {
            // empty catch block
        }
        return bSave;
    }
}