Search in sources :

Example 86 with TemplateModel

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);
    }
}
Also used : TemplateModel(freemarker.template.TemplateModel) Map(java.util.Map) TemplateBooleanModel(freemarker.template.TemplateBooleanModel)

Example 87 with TemplateModel

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);
    }
}
Also used : TemplateModel(freemarker.template.TemplateModel) Map(java.util.Map)

Example 88 with TemplateModel

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);
}
Also used : HttpSession(javax.servlet.http.HttpSession) TemplateModel(freemarker.template.TemplateModel)

Example 89 with TemplateModel

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);
    }
}
Also used : freemarker.core._MiscTemplateException(freemarker.core._MiscTemplateException) HttpServletResponseWrapper(javax.servlet.http.HttpServletResponseWrapper) HttpServletResponse(javax.servlet.http.HttpServletResponse) freemarker.core._DelayedFTLTypeDescription(freemarker.core._DelayedFTLTypeDescription) TemplateModel(freemarker.template.TemplateModel) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) TemplateScalarModel(freemarker.template.TemplateScalarModel) HashMap(java.util.HashMap) Map(java.util.Map) PrintWriter(java.io.PrintWriter) Writer(java.io.Writer) TemplateBooleanModel(freemarker.template.TemplateBooleanModel) PrintWriter(java.io.PrintWriter)

Example 90 with TemplateModel

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);
    }
}
Also used : UndeclaredThrowableException(freemarker.template.utility.UndeclaredThrowableException) Environment(freemarker.core.Environment) TemplateModel(freemarker.template.TemplateModel)

Aggregations

TemplateModel (freemarker.template.TemplateModel)108 TemplateModelException (freemarker.template.TemplateModelException)26 Map (java.util.Map)23 TemplateScalarModel (freemarker.template.TemplateScalarModel)18 Test (org.junit.Test)17 ArrayList (java.util.ArrayList)15 TemplateException (freemarker.template.TemplateException)13 HashMap (java.util.HashMap)13 SimpleScalar (freemarker.template.SimpleScalar)11 IOException (java.io.IOException)11 List (java.util.List)11 Template (freemarker.template.Template)9 TemplateSequenceModel (freemarker.template.TemplateSequenceModel)8 TemplateTransformModel (freemarker.template.TemplateTransformModel)8 SimpleHash (freemarker.template.SimpleHash)7 SimpleSequence (freemarker.template.SimpleSequence)7 Writer (java.io.Writer)7 Iterator (java.util.Iterator)7 Environment (freemarker.core.Environment)6 TemplateHashModel (freemarker.template.TemplateHashModel)6