免费标记包括+ base64

问题描述 投票:1回答:1

我正在寻找像这样的免费标记功能:

<#include "big_file.json" parse="true" encode="base64">
  • 包含文件
  • 解析此文件的内容
  • 将结果编码为base64

我知道这是不可能的。有没有办法扩展freemarker?

java base64 freemarker
1个回答
0
投票

解决方案是使用:freemarker directives

解决此示例:

"grafana_dashboards": {
    <@list_dir folder="./grafana_dashboards/" suffix="json"; dashboard_file, dashboard_name, dashboard_file_has_next>
    ${dashboard_name}": "<@encode enc="base64"><#include dashboard_file></@encode>"<#if (dashboard_file_has_next)>,</#if>
    </@list_dir>
}

我同时添加了这两个变量:

cfg = new Configuration(Configuration.VERSION_2_3_29);

...

final Map<String, Object> vars = new HashMap<>();
vars.put("list_dir", new xxx.freemarker.directives.ListDirDirective());
vars.put("encode", new xxx.freemarker.directives.EncodeDirective());

final Template temp = cfg.getTemplate(file.getName());

try ( //
    final ByteArrayOutputStream bao = new ByteArrayOutputStream(); //
    final Writer out = new OutputStreamWriter(bao); //
) {
    temp.process(vars, out);
    return bao.toString();
}

以下是指令:

EncodeDirective

package xxx.freemarker.directives;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.util.Base64;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.function.Function;

import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
import freemarker.template.TemplateModelException;
import freemarker.template.TemplateScalarModel;

/**
 * FreeMarker user-defined directive that progressively transforms the output of
 * its nested content to given encoding.
 *
 *
 * <p>
 * <b>Directive info</b>
 * </p>
 *
 * Parameters:
 * <ul>
 * <li><code>enc</code>: The name of the encoding to use. Possible options:
 * "base64". Required.
 * </ul>
 * <p>
 * Loop variables: None
 * <p>
 * Directive nested content: Yes
 */
public class EncodeDirective implements TemplateDirectiveModel {

    private static final String PARAM_NAME_ENC = "enc";

    private static final Map<String, Function<String, String>> encoder = new HashMap<>();
    static {
        encoder.put("base64", EncodeDirective::encodeBase64);
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    public void execute(final Environment env, final Map rawParams, final TemplateModel[] loopVars, final TemplateDirectiveBody body)
            throws TemplateException, IOException {

        final Params params = parseAndValidateParams(rawParams, loopVars);

        // If there is non-empty nested content:
        if (body != null) {
            // Executes the nested body. Same as <#nested> in FTL, except
            // that we use our own writer instead of the current output writer.
            final EncodeFilterWriter writer = new EncodeFilterWriter(env.getOut(), params.getEncoderFunction());
            body.render(writer);
            writer.flush();
        } else {
            throw new RuntimeException("missing body");
        }
    }

    /**
     * A {@link Writer} that transforms the character stream to upper case and
     * forwards it to another {@link Writer}.
     */
    private static class EncodeFilterWriter extends Writer {

        private StringBuffer buffer = new StringBuffer();
        private final Writer out;
        private final Function<String, String> encoder;

        EncodeFilterWriter(final Writer out, final Function<String, String> encoder) {
            this.out = out;
            this.encoder = encoder;
        }

        public void write(final char[] cbuf, final int off, final int len) throws IOException {
            buffer.append(cbuf, off, len);
        }

        public void flush() throws IOException {
            out.write(encoder.apply(buffer.toString()));
            out.flush();
        }

        public void close() throws IOException {
            out.close();
        }
    }

    private Params parseAndValidateParams(final Map<String, TemplateModel> params, final TemplateModel[] loopVars)
            throws TemplateModelException {
        boolean encParamSet = false;
        final Params p = new Params();

        final Iterator<Entry<String, TemplateModel>> paramIter = params.entrySet().iterator();
        while (paramIter.hasNext()) {
            final Entry<String, TemplateModel> ent = paramIter.next();

            final String paramName = ent.getKey();
            final TemplateModel paramValue = ent.getValue();

            if (paramName.equals(PARAM_NAME_ENC)) {
                if (!(paramValue instanceof TemplateScalarModel)) {
                    throw new TemplateModelException("The \"" + PARAM_NAME_ENC + "\" parameter must be a string.");
                }
                p.setEnc(((TemplateScalarModel) paramValue).getAsString());
                encParamSet = true;
            } else {
                throw new TemplateModelException("Unsupported parameter: " + paramName);
            }
        }
        if (!encParamSet) {
            throw new TemplateModelException("The required \"" + PARAM_NAME_ENC + "\" paramter is missing.");
        }

        if (loopVars.length != 0) {
            throw new TemplateModelException("This directive doesn't allow loop variables.");
        }

        return p;
    }

    @Data
    private class Params {
        private String enc;

        public void setEnv(final String enc) {
            this.enc = enc;
        }

        public String getEnv() {
            return this.enc;
        }

        public Function<String, String> getEncoderFunction() throws TemplateModelException {
            final Function<String, String> encoderFunc = encoder.get(enc.toLowerCase());

            if (encoderFunc == null) {
                throw new TemplateModelException("The required \"" + PARAM_NAME_ENC + "\" paramter, must be one of: " + encoder.keySet()); 
            }

            return encoderFunc;
        }
    }

    private static String encodeBase64(final String in)  {
        try {
            return Base64.getEncoder().encodeToString( //
                    in.getBytes("UTF-8"));
        } catch (final UnsupportedEncodingException e) {
            throw new IllegalArgumentException("The required \"" + PARAM_NAME_ENC + "\" paramter, encode error:: " + e.getMessage(), e); 
        }
    }

}

ListDirDirective

package xxx.freemarker.directives;

import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import freemarker.cache.TemplateLoader;
import freemarker.core.Environment;
import freemarker.template.SimpleScalar;
import freemarker.template.TemplateBooleanModel;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
import freemarker.template.TemplateModelException;
import freemarker.template.TemplateScalarModel;

/**
 * FreeMarker user-defined directive for repeating a section of a template, that
 * scan a folder on file system and loop through matching files.
 *
 *
 * <p>
 * <b>Directive info</b>
 * </p>
 *
 * <p>
 * Parameters:
 * <ul>
 * <li><code>folder</code>: The relative path of the folder on file system.
 * Required.
 * <li><code>suffix</code>: File ending too scan for. Required.
 * </ul>
 *
 * Loop variables:
 * <ul>
 * <li><code>file_path</code>: String: The relative file path, used by
 * "<#include" or "<#import". Required.</li>
 * <li><code>file_name</code>: String: The file name without suffix.
 * Optional.</li>
 * <li><code>has_next</code>: Boolean: Indicator if it is last file or not.
 * Optional.</li>
 * </ul>
 * <p>
 * Nested content: Yes
 */
public class ListDirDirective implements TemplateDirectiveModel {

    private static final String PARAM_NAME_FOLDER = "folder";
    private static final String PARAM_NAME_SUFFIX = "suffix";

    @SuppressWarnings({ "rawtypes", "unchecked" })
    @Override
    public void execute(final Environment env, final Map rawParams, final TemplateModel[] loopVars,
            final TemplateDirectiveBody body) throws TemplateException, IOException {

        final Path basePath = getCurrentTemplate(env).getParentFile().toPath();

        final Params params = parseAndValidateParams(rawParams, loopVars);

        final List<String> files = findFiles("**/*." + params.getSuffix(), basePath, params.getFolder());
        if (files.isEmpty()) {
            throw new IllegalArgumentException(
                    "No files found with suffix: " + params.getSuffix() + " using base path: " + params.getFolder());
        }

        if (body != null) {
            final Iterator<String> filesIt = files.iterator();
            while (filesIt.hasNext()) {
                final String filePath = filesIt.next();

                loopVars[0] = new SimpleScalar(filePath);

                // Set file name without extension/suffix
                if (loopVars.length > 1) {
                    loopVars[1] = new SimpleScalar(getFilennameWithoutSuffix(filePath, params.getSuffix()));
                }

                // Set has_next variable if set
                if (loopVars.length > 2) {
                    loopVars[2] = filesIt.hasNext() ? TemplateBooleanModel.TRUE : TemplateBooleanModel.FALSE;
                }

                // Executes the nested body (same as <#nested> in FTL). In this
                // case we don't provide a special writer as the parameter:
                body.render(env.getOut());
            }
        }
    }

    private File getCurrentTemplate(final Environment env) throws IOException {
        final TemplateLoader templateLoader = env.getConfiguration().getTemplateLoader();
        final Object tmp = templateLoader.findTemplateSource(env.getCurrentTemplate().getSourceName());
        if (!(tmp instanceof File)) {
            throw new IllegalArgumentException("The ListDirDirective is only compatible with FileTemplateLoader");
        }

        return (File) tmp;
    }

    private static String getFilennameWithoutSuffix(final String filePath, final String suffix) {
        final File file = new File(filePath);
        return file.getName() //
                .replace("\\.?" + Pattern.quote(suffix) + "$", "");
    }

    private static List<String> findFiles(final String pattern, final Path basePath, final String pathName)
            throws IOException {
        final Path path = basePath.resolve(pathName).toAbsolutePath();

        final PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher("glob:" + pattern);
        try (final Stream<Path> paths = Files.find(path, 10,
                (currentPath, fileAttributes) -> pathMatcher.matches(currentPath))) {
            return paths //
                    .map(basePath::relativize) //
                    .map(Path::toString) //
                    .collect(Collectors.toList());
        }
    }

    private Params parseAndValidateParams(final Map<String, TemplateModel> params, final TemplateModel[] loopVars)
            throws TemplateModelException {
        boolean folderParamSet = false;
        boolean suffixParamSet = false;
        final Params p = new Params();

        final Iterator<Entry<String, TemplateModel>> paramIter = params.entrySet().iterator();
        while (paramIter.hasNext()) {
            final Entry<String, TemplateModel> ent = paramIter.next();

            final String paramName = ent.getKey();
            final TemplateModel paramValue = ent.getValue();

            if (paramName.equals(PARAM_NAME_FOLDER)) {
                if (!(paramValue instanceof TemplateScalarModel)) {
                    throw new TemplateModelException(
                            "The \"" + PARAM_NAME_FOLDER + "\" parameter must be a string.");
                }
                p.setFolder(((TemplateScalarModel) paramValue).getAsString());
                folderParamSet = true;
            } else if (paramName.equals(PARAM_NAME_SUFFIX)) {
                if (!(paramValue instanceof TemplateScalarModel)) {
                    throw new TemplateModelException(
                            "The \"" + PARAM_NAME_SUFFIX + "\" parameter must be a string.");
                }
                final String suffix = ((TemplateScalarModel) paramValue).getAsString();
                if (!suffix.matches("[a-zA-Z0-9]{1,10}")) {
                    throw new TemplateModelException("The \"" + PARAM_NAME_SUFFIX + "\" parameter "
                            + "must only contain letter and number and needs to be between 1-10 chars.");
                }
                p.setSuffix(suffix);
                suffixParamSet = true;
            } else {
                throw new TemplateModelException("Unsupported parameter: " + paramName);
            }
        }
        if (!folderParamSet) {
            throw new TemplateModelException("The required \"" + PARAM_NAME_FOLDER + "\" paramter is missing.");
        }

        if (!suffixParamSet) {
            throw new TemplateModelException("The required \"" + PARAM_NAME_SUFFIX + "\" paramter is missing.");
        }

        if (loopVars.length < 1) {
            throw new TemplateModelException("At least 1 loop vars is required: file_name, [name], [has_next]");
        }

        if (loopVars.length > 3) {
            throw new TemplateModelException("Max 3 loop vars are allowed: file_name, [name], [has_next]");
        }

        return p;
    }

    @Data
    private class Params {
        private String folder;
        private String suffix;

        public void setFolder(final String folder) {
            this.folder = folder;
        }

        public String getFolder() {
            return this.folder;
        }

        public void setSuffix(final String suffix) {
            this.suffix = suffix;
        }

        public String getSuffix() {
            return this.suffix;
        }
    }

}
© www.soinside.com 2019 - 2024. All rights reserved.