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);
}
}
Aggregations