Search in sources :

Example 1 with TransformControl

use of freemarker.template.TransformControl in project freemarker by apache.

the class Environment method visitAndTransform.

/**
 * "Visit" the template element, passing the output through a TemplateTransformModel
 *
 * @param elementBuffer
 *            the element to visit through a transform; might contains trailing {@code null}-s
 * @param transform
 *            the transform to pass the element output through
 * @param args
 *            optional arguments fed to the transform
 */
void visitAndTransform(TemplateElement[] elementBuffer, TemplateTransformModel transform, Map args) throws TemplateException, IOException {
    try {
        Writer tw = transform.getWriter(out, args);
        if (tw == null)
            tw = EMPTY_BODY_WRITER;
        TransformControl tc = tw instanceof TransformControl ? (TransformControl) tw : null;
        Writer prevOut = out;
        out = tw;
        try {
            if (tc == null || tc.onStart() != TransformControl.SKIP_BODY) {
                do {
                    visit(elementBuffer);
                } while (tc != null && tc.afterBody() == TransformControl.REPEAT_EVALUATION);
            }
        } catch (Throwable t) {
            try {
                if (tc != null && !(t instanceof FlowControlException && getConfiguration().getIncompatibleImprovements().intValue() >= _TemplateAPI.VERSION_INT_2_3_27)) {
                    tc.onError(t);
                } else {
                    throw t;
                }
            } catch (TemplateException e) {
                throw e;
            } catch (IOException e) {
                throw e;
            } catch (Error e) {
                throw e;
            } catch (Throwable e) {
                if (EvalUtil.shouldWrapUncheckedException(e, this)) {
                    throw new _MiscTemplateException(e, this, "Transform has thrown an unchecked exception; see the cause exception.");
                } else if (e instanceof RuntimeException) {
                    throw (RuntimeException) e;
                } else {
                    throw new UndeclaredThrowableException(e);
                }
            }
        } finally {
            out = prevOut;
            if (prevOut != tw) {
                tw.close();
            }
        }
    } catch (TemplateException te) {
        handleTemplateException(te);
    }
}
Also used : TemplateException(freemarker.template.TemplateException) TransformControl(freemarker.template.TransformControl) UndeclaredThrowableException(freemarker.template.utility.UndeclaredThrowableException) IOException(java.io.IOException) PrintWriter(java.io.PrintWriter) Writer(java.io.Writer) NullWriter(freemarker.template.utility.NullWriter) StringWriter(java.io.StringWriter)

Example 2 with TransformControl

use of freemarker.template.TransformControl in project be5 by DevelopmentOnTheEdge.

the class Environment method visitAndTransform.

/**
 * "Visit" the template element, passing the output
 * through a TemplateTransformModel
 * @param element the element to visit through a transform
 * @param transform the transform to pass the element output
 * through
 * @param args optional arguments fed to the transform
 */
void visitAndTransform(TemplateElement element, TemplateTransformModel transform, Map args) throws TemplateException, IOException {
    try {
        Writer tw = transform.getWriter(out, args);
        if (tw == null)
            tw = EMPTY_BODY_WRITER;
        TransformControl tc = tw instanceof TransformControl ? (TransformControl) tw : null;
        Writer prevOut = out;
        out = tw;
        try {
            if (tc == null || tc.onStart() != TransformControl.SKIP_BODY) {
                do {
                    if (element != null) {
                        visitByHiddingParent(element);
                    }
                } while (tc != null && tc.afterBody() == TransformControl.REPEAT_EVALUATION);
            }
        } catch (Throwable t) {
            try {
                if (tc != null) {
                    tc.onError(t);
                } else {
                    throw t;
                }
            } catch (TemplateException e) {
                throw e;
            } catch (IOException e) {
                throw e;
            } catch (RuntimeException e) {
                throw e;
            } catch (Error e) {
                throw e;
            } catch (Throwable e) {
                throw new UndeclaredThrowableException(e);
            }
        } finally {
            out = prevOut;
            tw.close();
        }
    } catch (TemplateException te) {
        handleTemplateException(te);
    }
}
Also used : TemplateException(freemarker.template.TemplateException) TransformControl(freemarker.template.TransformControl) UndeclaredThrowableException(freemarker.template.utility.UndeclaredThrowableException) IOException(java.io.IOException) PrintWriter(java.io.PrintWriter) Writer(java.io.Writer) NullWriter(freemarker.template.utility.NullWriter) StringWriter(java.io.StringWriter)

Aggregations

TemplateException (freemarker.template.TemplateException)2 TransformControl (freemarker.template.TransformControl)2 NullWriter (freemarker.template.utility.NullWriter)2 UndeclaredThrowableException (freemarker.template.utility.UndeclaredThrowableException)2 IOException (java.io.IOException)2 PrintWriter (java.io.PrintWriter)2 StringWriter (java.io.StringWriter)2 Writer (java.io.Writer)2