use of freemarker.template.TemplateModelException in project pcgen by PCGen.
the class EquipSetLoopDirective method execute.
@SuppressWarnings("rawtypes")
@Override
public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
if (body == null) {
throw new TemplateModelException("This directive must have content.");
}
List<EquipSet> eqSetList = new ArrayList<>();
EquipSet currSet = null;
String currIdPath = pc.getCalcEquipSetId();
for (EquipSet es : pc.getDisplay().getEquipSet()) {
if (es.getParentIdPath().equals("0")) {
eqSetList.add(es);
if (es.getIdPath().equals(currIdPath)) {
currSet = es;
}
}
}
for (EquipSet equipSet : eqSetList) {
pc.setCalcEquipSetId(equipSet.getIdPath());
pc.setCalcEquipmentList(equipSet.getUseTempMods());
// 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());
}
if (currSet != null) {
pc.setCalcEquipSetId(currSet.getIdPath());
pc.setCalcEquipmentList(currSet.getUseTempMods());
}
}
use of freemarker.template.TemplateModelException in project pcgen by PCGen.
the class LoopDirective method execute.
@SuppressWarnings("rawtypes")
@Override
public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
// Check if no parameters were given:
int fromVal = 0;
Integer toVal = null;
int step = 1;
for (Object entryObj : params.entrySet()) {
Map.Entry entry = (Entry) entryObj;
String paramName = (String) entry.getKey();
TemplateModel paramValue = (TemplateModel) entry.getValue();
switch(paramName) {
case "from":
if (!(paramValue instanceof TemplateNumberModel)) {
throw new TemplateModelException("The \"" + paramName + "\" parameter " + "must be a number.");
}
fromVal = ((TemplateNumberModel) paramValue).getAsNumber().intValue();
break;
case "to":
if (!(paramValue instanceof TemplateNumberModel)) {
throw new TemplateModelException("The \"" + paramName + "\" parameter " + "must be a number.");
}
toVal = ((TemplateNumberModel) paramValue).getAsNumber().intValue();
break;
case "step":
if (!(paramValue instanceof TemplateNumberModel)) {
throw new TemplateModelException("The \"" + paramName + "\" parameter " + "must be a number.");
}
step = ((TemplateNumberModel) paramValue).getAsNumber().intValue();
if (step == 0) {
throw new TemplateModelException("The \"" + paramName + "\" parameter must not be 0.");
}
break;
}
}
if (toVal == null) {
throw new TemplateModelException("The \"to\" parameter must be provided.");
}
if (body == null) {
throw new TemplateModelException("This directive must have content.");
}
if (step > 0) {
for (int i = fromVal; i <= toVal; i += step) {
// Set the loop variable, if there is one:
if (loopVars.length > 0) {
loopVars[0] = new SimpleNumber(i);
}
if (loopVars.length > 1) {
loopVars[1] = i + step <= toVal ? 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());
}
} else {
for (int i = fromVal; i >= toVal; i += step) {
// Set the loop variable, if there is one:
if (loopVars.length > 0) {
loopVars[0] = new SimpleNumber(i);
}
if (loopVars.length > 1) {
loopVars[1] = i + step >= toVal ? 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());
}
}
}
use of freemarker.template.TemplateModelException 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);
}
}
use of freemarker.template.TemplateModelException in project sling by apache.
the class AdaptToMethod method exec.
@Override
public Object exec(final List arguments) throws TemplateModelException {
if (arguments.size() != 2) {
throw new TemplateModelException("wrong number of arguments, expecting 2 (adaptable and adapter type).");
}
try {
final String classname = arguments.get(1).toString();
final Class<?> clazz = dynamicClassLoaderManager.getDynamicClassLoader().loadClass(classname);
final TemplateModel templateModel = (TemplateModel) arguments.get(0);
final Object adaptable = DeepUnwrap.unwrap(templateModel);
return adapterManager.getAdapter(adaptable, clazz);
} catch (Exception e) {
throw new TemplateModelException(e);
}
}
use of freemarker.template.TemplateModelException in project siddhi by wso2.
the class FormatDescriptionMethod method exec.
@Override
public Object exec(List args) throws TemplateModelException {
if (args.size() != 1) {
throw new TemplateModelException("There should be a single argument of type string " + "passed to format description method");
}
SimpleScalar arg1 = (SimpleScalar) args.get(0);
String inputString = arg1.getAsString();
// Replacing spaces that should not be considered in text wrapping with non breaking spaces
inputString = replaceLeadingSpaces(inputString);
inputString = inputString.replaceAll("<", "<");
inputString = inputString.replaceAll(">", ">");
// Replacing new line characters
inputString = replaceNewLines(inputString);
inputString = inputString.replaceAll("\t", " ");
inputString = inputString.replaceAll("```([^```]*)```", "<pre>$1</pre>");
inputString = inputString.replaceAll("`([^`]*)`", "<code>$1</code>");
return inputString;
}
Aggregations