use of freemarker.template.TemplateModel in project freemarker by apache.
the class AssertDirective method execute.
public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
TemplateModel test = null;
for (Object paramEnt : params.entrySet()) {
Map.Entry<String, TemplateModel> param = (Map.Entry) paramEnt;
String paramName = param.getKey();
if (paramName.equals(TEST_PARAM)) {
test = param.getValue();
} else {
throw new UnsupportedParameterException(paramName, env);
}
}
if (test == null) {
throw new MissingRequiredParameterException(TEST_PARAM, env);
}
_CoreAPI.checkHasNoNestedContent(body);
if (!(test instanceof TemplateBooleanModel)) {
throw new AssertationFailedInTemplateException("Assertion failed:\n" + "The value had to be boolean, but it was of type" + ClassUtil.getFTLTypeDescription(test), env);
}
if (!((TemplateBooleanModel) test).getAsBoolean()) {
throw new AssertationFailedInTemplateException("Assertion failed:\n" + "the value was false.", env);
}
}
use of freemarker.template.TemplateModel in project freemarker by apache.
the class AssertEqualsDirective method execute.
public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
TemplateModel actual = null;
TemplateModel expected = null;
for (Object paramEnt : params.entrySet()) {
Map.Entry<String, TemplateModel> param = (Map.Entry) paramEnt;
String paramName = param.getKey();
if (paramName.equals(ACTUAL_PARAM)) {
actual = param.getValue();
} else if (paramName.equals(EXPECTED_PARAM)) {
expected = param.getValue();
} else {
throw new UnsupportedParameterException(paramName, env);
}
}
if (actual == null) {
throw new MissingRequiredParameterException(ACTUAL_PARAM, env);
}
if (expected == null) {
throw new MissingRequiredParameterException(EXPECTED_PARAM, env);
}
_CoreAPI.checkHasNoNestedContent(body);
if (!env.applyEqualsOperatorLenient(actual, expected)) {
throw new AssertationFailedInTemplateException("Assertion failed:\n" + "Expected: " + tryUnwrapp(expected) + "\n" + "Actual: " + tryUnwrapp(actual), env);
}
}
use of freemarker.template.TemplateModel in project freemarker by apache.
the class AllHttpScopesHashModel method get.
@Override
public TemplateModel get(String key) throws TemplateModelException {
// Lookup in page scope
TemplateModel model = super.get(key);
if (model != null) {
return model;
}
// Look in unlisted models
model = (TemplateModel) unlistedModels.get(key);
if (model != null) {
return model;
}
// Lookup in request scope
Object obj = request.getAttribute(key);
if (obj != null) {
return wrap(obj);
}
// Lookup in session scope
HttpSession session = request.getSession(false);
if (session != null) {
obj = session.getAttribute(key);
if (obj != null) {
return wrap(obj);
}
}
// Lookup in application scope
obj = context.getAttribute(key);
if (obj != null) {
return wrap(obj);
}
// return wrapper's null object (probably null).
return wrap(null);
}
use of freemarker.template.TemplateModel in project freemarker by apache.
the class IncludePage method execute.
public void execute(final Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
// Determine the path
final TemplateModel path = (TemplateModel) params.get("path");
if (path == null) {
throw new _MiscTemplateException(env, "Missing required parameter \"path\"");
}
if (!(path instanceof TemplateScalarModel)) {
throw new _MiscTemplateException(env, "Expected a scalar model. \"path\" is instead ", new _DelayedFTLTypeDescription(path));
}
final String strPath = ((TemplateScalarModel) path).getAsString();
if (strPath == null) {
throw new _MiscTemplateException(env, "String value of \"path\" parameter is null");
}
// See whether we need to use a custom response (if we're inside a TTM
// or TDM or macro nested body, we'll need to as then the current
// FM environment writer is not identical to HTTP servlet response
// writer.
final Writer envOut = env.getOut();
final HttpServletResponse wrappedResponse;
if (envOut == response.getWriter()) {
// Don't bother wrapping if environment's writer is same as
// response writer
wrappedResponse = response;
} else {
final PrintWriter printWriter = (envOut instanceof PrintWriter) ? (PrintWriter) envOut : new PrintWriter(envOut);
// Otherwise, create a response wrapper that will pass the
// env writer, potentially first wrapping it in a print
// writer when it ain't one already.
wrappedResponse = new HttpServletResponseWrapper(response) {
@Override
public PrintWriter getWriter() {
return printWriter;
}
};
}
// Determine inherit_params value
final boolean inheritParams;
final TemplateModel inheritParamsModel = (TemplateModel) params.get("inherit_params");
if (inheritParamsModel == null) {
// defaults to true when not specified
inheritParams = true;
} else {
if (!(inheritParamsModel instanceof TemplateBooleanModel)) {
throw new _MiscTemplateException(env, "\"inherit_params\" should be a boolean but it's a(n) ", inheritParamsModel.getClass().getName(), " instead");
}
inheritParams = ((TemplateBooleanModel) inheritParamsModel).getAsBoolean();
}
// Get explicit params, if any
final TemplateModel paramsModel = (TemplateModel) params.get("params");
// Determine whether we need to wrap the request
final HttpServletRequest wrappedRequest;
if (paramsModel == null && inheritParams) {
// Inherit original request params & no params explicitly
// specified, so use the original request
wrappedRequest = request;
} else {
// In any other case, use a custom request wrapper
final Map paramsMap;
if (paramsModel != null) {
// Convert params to a Map
final Object unwrapped = DeepUnwrap.unwrap(paramsModel);
if (!(unwrapped instanceof Map)) {
throw new _MiscTemplateException(env, "Expected \"params\" to unwrap into a java.util.Map. It unwrapped into ", unwrapped.getClass().getName(), " instead.");
}
paramsMap = (Map) unwrapped;
} else {
paramsMap = Collections.EMPTY_MAP;
}
wrappedRequest = new CustomParamsRequest(request, paramsMap, inheritParams);
}
// Finally, do the include
try {
request.getRequestDispatcher(strPath).include(wrappedRequest, wrappedResponse);
} catch (ServletException e) {
throw new _MiscTemplateException(e, env);
}
}
use of freemarker.template.TemplateModel in project freemarker by apache.
the class PageContextFactory method getCurrentPageContext.
static FreeMarkerPageContext getCurrentPageContext() throws TemplateModelException {
Environment env = Environment.getCurrentEnvironment();
TemplateModel pageContextModel = env.getGlobalVariable(PageContext.PAGECONTEXT);
if (pageContextModel instanceof FreeMarkerPageContext) {
return (FreeMarkerPageContext) pageContextModel;
}
try {
FreeMarkerPageContext pageContext = (FreeMarkerPageContext) pageContextImpl.newInstance();
env.setGlobalVariable(PageContext.PAGECONTEXT, pageContext);
return pageContext;
} catch (IllegalAccessException e) {
throw new IllegalAccessError(e.getMessage());
} catch (InstantiationException e) {
throw new UndeclaredThrowableException(e);
}
}
Aggregations