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

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

    @Override
    boolean processFileForI18nStrings(Node node, HashMap<String, String> i18nStringMap) {
        String regEx = "\\{\\{\\s*i18n\\s+['\"](.*?)(?<!\\\\)['\"](\\s+.*?comment\\s*=\\s*['\"](.*?)(?<!\\\\)['\"])?";
        String fileContent = I18nStringExtractor.getFileContentsFromNode(node);
        return this.searchAndAddI18nStringWithComment(fileContent, i18nStringMap, regEx, 1, 3);
    }

    @Override
    boolean processFileForIncludes(Node node) {
        boolean bSave = false;
        String fileContent = I18nStringExtractor.getFileContentsFromNode(node);
        String regEx = "<script\\s+.*?src\\s*=\\s*\"(.*?)\"";
        Pattern pattern = Pattern.compile(regEx);
        Matcher matcher = pattern.matcher(fileContent);
        while (matcher.find()) {
            try {
                Node includeFileNode;
                String 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 script : {}", (Object)includePath);
                if (!this.i18nStringExtractor.addFileForStringExtraction(includeFileNode)) continue;
                bSave = true;
            }
            catch (Exception ignored) {}
        }
        return bSave;
    }
}