Search in sources :

Example 1 with SimpleTag

use of javax.servlet.jsp.tagext.SimpleTag in project freemarker by apache.

the class SimpleTagDirectiveModel method execute.

public void execute(Environment env, Map args, TemplateModel[] outArgs, final TemplateDirectiveBody body) throws TemplateException, IOException {
    try {
        SimpleTag tag = (SimpleTag) getTagInstance();
        final FreeMarkerPageContext pageContext = PageContextFactory.getCurrentPageContext();
        pageContext.pushWriter(new JspWriterAdapter(env.getOut()));
        try {
            tag.setJspContext(pageContext);
            JspTag parentTag = (JspTag) pageContext.peekTopTag(JspTag.class);
            if (parentTag != null) {
                tag.setParent(parentTag);
            }
            setupTag(tag, args, pageContext.getObjectWrapper());
            if (body != null) {
                tag.setJspBody(new JspFragment() {

                    @Override
                    public JspContext getJspContext() {
                        return pageContext;
                    }

                    @Override
                    public void invoke(Writer out) throws JspException, IOException {
                        try {
                            body.render(out == null ? pageContext.getOut() : out);
                        } catch (TemplateException e) {
                            throw new TemplateExceptionWrapperJspException(e);
                        }
                    }
                });
                pageContext.pushTopTag(tag);
                try {
                    tag.doTag();
                } finally {
                    pageContext.popTopTag();
                }
            } else {
                tag.doTag();
            }
        } finally {
            pageContext.popWriter();
        }
    } catch (TemplateException e) {
        throw e;
    } catch (Exception e) {
        throw toTemplateModelExceptionOrRethrow(e);
    }
}
Also used : JspContext(javax.servlet.jsp.JspContext) TemplateException(freemarker.template.TemplateException) IOException(java.io.IOException) JspException(javax.servlet.jsp.JspException) TemplateException(freemarker.template.TemplateException) IOException(java.io.IOException) IntrospectionException(java.beans.IntrospectionException) JspFragment(javax.servlet.jsp.tagext.JspFragment) JspException(javax.servlet.jsp.JspException) SimpleTag(javax.servlet.jsp.tagext.SimpleTag) JspTag(javax.servlet.jsp.tagext.JspTag) Writer(java.io.Writer)

Aggregations

TemplateException (freemarker.template.TemplateException)1 IntrospectionException (java.beans.IntrospectionException)1 IOException (java.io.IOException)1 Writer (java.io.Writer)1 JspContext (javax.servlet.jsp.JspContext)1 JspException (javax.servlet.jsp.JspException)1 JspFragment (javax.servlet.jsp.tagext.JspFragment)1 JspTag (javax.servlet.jsp.tagext.JspTag)1 SimpleTag (javax.servlet.jsp.tagext.SimpleTag)1