use of freemarker.template.TemplateModel in project freemarker by apache.
the class Environment method getNodeProcessor.
private TemplateModel getNodeProcessor(Namespace ns, String localName, String nsURI) throws TemplateException {
TemplateModel result = null;
if (nsURI == null) {
result = ns.get(localName);
if (!(result instanceof Macro) && !(result instanceof TemplateTransformModel)) {
result = null;
}
} else {
Template template = ns.getTemplate();
String prefix = template.getPrefixForNamespace(nsURI);
if (prefix == null) {
// since it has no prefix registered for the namespace
return null;
}
if (prefix.length() > 0) {
result = ns.get(prefix + ":" + localName);
if (!(result instanceof Macro) && !(result instanceof TemplateTransformModel)) {
result = null;
}
} else {
if (nsURI.length() == 0) {
result = ns.get(Template.NO_NS_PREFIX + ":" + localName);
if (!(result instanceof Macro) && !(result instanceof TemplateTransformModel)) {
result = null;
}
}
if (nsURI.equals(template.getDefaultNS())) {
result = ns.get(Template.DEFAULT_NAMESPACE_PREFIX + ":" + localName);
if (!(result instanceof Macro) && !(result instanceof TemplateTransformModel)) {
result = null;
}
}
if (result == null) {
result = ns.get(localName);
if (!(result instanceof Macro) && !(result instanceof TemplateTransformModel)) {
result = null;
}
}
}
}
return result;
}
use of freemarker.template.TemplateModel in project freemarker by apache.
the class Environment method getTransform.
TemplateTransformModel getTransform(Expression exp) throws TemplateException {
TemplateTransformModel ttm = null;
TemplateModel tm = exp.eval(this);
if (tm instanceof TemplateTransformModel) {
ttm = (TemplateTransformModel) tm;
} else if (exp instanceof Identifier) {
tm = configuration.getSharedVariable(exp.toString());
if (tm instanceof TemplateTransformModel) {
ttm = (TemplateTransformModel) tm;
}
}
return ttm;
}
use of freemarker.template.TemplateModel in project freemarker by apache.
the class Expression method evalToNonMissing.
TemplateModel evalToNonMissing(Environment env) throws TemplateException {
TemplateModel result = this.eval(env);
assertNonNull(result, env);
return result;
}
use of freemarker.template.TemplateModel in project freemarker by apache.
the class GetOptionalTemplateMethod method exec.
public Object exec(List args) throws TemplateModelException {
final int argCnt = args.size();
if (argCnt < 1 || argCnt > 2) {
throw _MessageUtil.newArgCntError(methodName, argCnt, 1, 2);
}
final Environment env = Environment.getCurrentEnvironment();
if (env == null) {
throw new IllegalStateException("No freemarer.core.Environment is associated to the current thread.");
}
final String absTemplateName;
{
TemplateModel arg = (TemplateModel) args.get(0);
if (!(arg instanceof TemplateScalarModel)) {
throw _MessageUtil.newMethodArgMustBeStringException(methodName, 0, arg);
}
String templateName = EvalUtil.modelToString((TemplateScalarModel) arg, null, env);
try {
absTemplateName = env.toFullTemplateName(env.getCurrentTemplate().getName(), templateName);
} catch (MalformedTemplateNameException e) {
throw new _TemplateModelException(e, "Failed to convert template path to full path; see cause exception.");
}
}
final TemplateHashModelEx options;
if (argCnt > 1) {
TemplateModel arg = (TemplateModel) args.get(1);
if (!(arg instanceof TemplateHashModelEx)) {
throw _MessageUtil.newMethodArgMustBeExtendedHashException(methodName, 1, arg);
}
options = (TemplateHashModelEx) arg;
} else {
options = null;
}
String encoding = null;
boolean parse = true;
if (options != null) {
final KeyValuePairIterator kvpi = TemplateModelUtils.getKeyValuePairIterator(options);
while (kvpi.hasNext()) {
final KeyValuePair kvp = kvpi.next();
final String optName;
{
TemplateModel optNameTM = kvp.getKey();
if (!(optNameTM instanceof TemplateScalarModel)) {
throw _MessageUtil.newMethodArgInvalidValueException(methodName, 1, "All keys in the options hash must be strings, but found ", new _DelayedAOrAn(new _DelayedFTLTypeDescription(optNameTM)));
}
optName = ((TemplateScalarModel) optNameTM).getAsString();
}
final TemplateModel optValue = kvp.getValue();
if (OPTION_ENCODING.equals(optName)) {
encoding = getStringOption(OPTION_ENCODING, optValue);
} else if (OPTION_PARSE.equals(optName)) {
parse = getBooleanOption(OPTION_PARSE, optValue);
} else {
throw _MessageUtil.newMethodArgInvalidValueException(methodName, 1, "Unsupported option ", new _DelayedJQuote(optName), "; valid names are: ", new _DelayedJQuote(OPTION_ENCODING), ", ", new _DelayedJQuote(OPTION_PARSE), ".");
}
}
}
final Template template;
try {
template = env.getTemplateForInclusion(absTemplateName, encoding, parse, true);
} catch (IOException e) {
throw new _TemplateModelException(e, "I/O error when trying to load optional template ", new _DelayedJQuote(absTemplateName), "; see cause exception");
}
SimpleHash result = new SimpleHash(env.getObjectWrapper());
result.put(RESULT_EXISTS, template != null);
// conveniently provided like in <@optTemp.include!myDefaultMacro />.
if (template != null) {
result.put(RESULT_INCLUDE, new TemplateDirectiveModel() {
public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
if (!params.isEmpty()) {
throw new TemplateException("This directive supports no parameters.", env);
}
if (loopVars.length != 0) {
throw new TemplateException("This directive supports no loop variables.", env);
}
if (body != null) {
throw new TemplateException("This directive supports no nested content.", env);
}
env.include(template);
}
});
result.put(RESULT_IMPORT, new TemplateMethodModelEx() {
public Object exec(List args) throws TemplateModelException {
if (!args.isEmpty()) {
throw new TemplateModelException("This method supports no parameters.");
}
try {
return env.importLib(template, null);
} catch (IOException e) {
throw new _TemplateModelException(e, "Failed to import loaded template; see cause exception");
} catch (TemplateException e) {
throw new _TemplateModelException(e, "Failed to import loaded template; see cause exception");
}
}
});
}
return result;
}
use of freemarker.template.TemplateModel in project freemarker by apache.
the class Include method accept.
@Override
TemplateElement[] accept(Environment env) throws TemplateException, IOException {
final String includedTemplateName = includedTemplateNameExp.evalAndCoerceToPlainText(env);
final String fullIncludedTemplateName;
try {
fullIncludedTemplateName = env.toFullTemplateName(getTemplate().getName(), includedTemplateName);
} catch (MalformedTemplateNameException e) {
throw new _MiscTemplateException(e, env, "Malformed template name ", new _DelayedJQuote(e.getTemplateName()), ":\n", e.getMalformednessDescription());
}
final String encoding = this.encoding != null ? this.encoding : (encodingExp != null ? encodingExp.evalAndCoerceToPlainText(env) : null);
final boolean parse;
if (this.parse != null) {
parse = this.parse.booleanValue();
} else {
TemplateModel tm = parseExp.eval(env);
if (tm instanceof TemplateScalarModel) {
// Legacy
parse = getYesNo(parseExp, EvalUtil.modelToString((TemplateScalarModel) tm, parseExp, env));
} else {
parse = parseExp.modelToBoolean(tm, env);
}
}
final boolean ignoreMissing;
if (this.ignoreMissingExpPrecalcedValue != null) {
ignoreMissing = this.ignoreMissingExpPrecalcedValue.booleanValue();
} else if (ignoreMissingExp != null) {
ignoreMissing = ignoreMissingExp.evalToBoolean(env);
} else {
ignoreMissing = false;
}
final Template includedTemplate;
try {
includedTemplate = env.getTemplateForInclusion(fullIncludedTemplateName, encoding, parse, ignoreMissing);
} catch (IOException e) {
throw new _MiscTemplateException(e, env, "Template inclusion failed (for parameter value ", new _DelayedJQuote(includedTemplateName), "):\n", new _DelayedGetMessage(e));
}
if (includedTemplate != null) {
env.include(includedTemplate);
}
return null;
}
Aggregations