Search in sources :

Example 1 with TemplateBooleanModel

use of freemarker.template.TemplateBooleanModel in project asterixdb by apache.

the class LoadFileDirective method execute.

@Override
public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
    String fileParam = null;
    String defaultParam = null;
    boolean trimParam = false;
    for (Object paramObj : params.entrySet()) {
        Map.Entry param = (Map.Entry) paramObj;
        String paramName = (String) param.getKey();
        TemplateModel paramValue = (TemplateModel) param.getValue();
        switch(paramName) {
            case PARAM_FILE:
                if (paramValue instanceof TemplateScalarModel) {
                    fileParam = ((TemplateScalarModel) paramValue).getAsString();
                } else {
                    throw new TemplateModelException(PARAM_FILE + " must be a string");
                }
                break;
            case PARAM_DEFAULT_TEXT:
                if (paramValue instanceof TemplateScalarModel) {
                    defaultParam = ((TemplateScalarModel) paramValue).getAsString();
                } else {
                    throw new TemplateModelException(PARAM_DEFAULT_TEXT + " must be a string");
                }
                break;
            case PARAM_TRIM:
                if (paramValue instanceof TemplateBooleanModel) {
                    trimParam = ((TemplateBooleanModel) paramValue).getAsBoolean();
                } else {
                    throw new TemplateModelException(PARAM_TRIM + " must be a boolean");
                }
                break;
            default:
                throw new TemplateModelException("Unknown param: " + paramName);
        }
    }
    if (fileParam == null) {
        throw new TemplateModelException("The required \"" + PARAM_FILE + "\" parameter" + "is missing.");
    }
    if (body != null) {
        throw new TemplateModelException("Body is not supported by this directive");
    }
    Writer out = env.getOut();
    File baseDir = ((FileTemplateLoader) ((Configuration) env.getTemplate().getParent()).getTemplateLoader()).baseDir;
    File file = new File(baseDir, fileParam);
    if (file.exists()) {
        if (trimParam) {
            LicenseUtil.readAndTrim(out, file);
            out.write('\n');
        } else {
            IOUtils.copy(new FileInputStream(file), out, StandardCharsets.UTF_8);
        }
    } else if (defaultParam != null) {
        out.append(defaultParam).append("\n");
    } else {
        throw new IOException("File not found: " + file);
    }
}
Also used : TemplateModelException(freemarker.template.TemplateModelException) TemplateModel(freemarker.template.TemplateModel) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) TemplateScalarModel(freemarker.template.TemplateScalarModel) FileTemplateLoader(freemarker.cache.FileTemplateLoader) Map(java.util.Map) File(java.io.File) TemplateBooleanModel(freemarker.template.TemplateBooleanModel) Writer(java.io.Writer)

Aggregations

FileTemplateLoader (freemarker.cache.FileTemplateLoader)1 TemplateBooleanModel (freemarker.template.TemplateBooleanModel)1 TemplateModel (freemarker.template.TemplateModel)1 TemplateModelException (freemarker.template.TemplateModelException)1 TemplateScalarModel (freemarker.template.TemplateScalarModel)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 Writer (java.io.Writer)1 Map (java.util.Map)1