Search in sources :

Example 21 with WebXmlRenderContext

use of com.github.bordertech.wcomponents.servlet.WebXmlRenderContext in project wcomponents by BorderTech.

the class DebugStructureInterceptor method paint.

/**
 * Override paint to render additional information for debugging purposes.
 *
 * @param renderContext the renderContext to send the output to.
 */
@Override
public void paint(final RenderContext renderContext) {
    super.paint(renderContext);
    if (!DebugUtil.isDebugFeaturesEnabled() || !(renderContext instanceof WebXmlRenderContext)) {
        return;
    }
    XmlStringBuilder xml = ((WebXmlRenderContext) renderContext).getWriter();
    xml.appendTag("ui:debug");
    writeDebugInfo(getUI(), xml);
    xml.appendEndTag("ui:debug");
}
Also used : WebXmlRenderContext(com.github.bordertech.wcomponents.servlet.WebXmlRenderContext) XmlStringBuilder(com.github.bordertech.wcomponents.XmlStringBuilder)

Example 22 with WebXmlRenderContext

use of com.github.bordertech.wcomponents.servlet.WebXmlRenderContext in project wcomponents by BorderTech.

the class InterceptorComponent method render.

/**
 * Renders the given component to a web-XML String and returns it. This occurs outside the context of a Servlet.
 *
 * @param component the component to render.
 * @return the rendered output as a String.
 */
protected static String render(final WebComponent component) {
    StringWriter stringWriter = new StringWriter();
    PrintWriter printWriter = new PrintWriter(stringWriter);
    component.paint(new WebXmlRenderContext(printWriter));
    printWriter.flush();
    String content = stringWriter.toString();
    return content;
}
Also used : WebXmlRenderContext(com.github.bordertech.wcomponents.servlet.WebXmlRenderContext) StringWriter(java.io.StringWriter) PrintWriter(java.io.PrintWriter)

Example 23 with WebXmlRenderContext

use of com.github.bordertech.wcomponents.servlet.WebXmlRenderContext in project wcomponents by BorderTech.

the class TemplateRenderInterceptor method paint.

/**
 * {@inheritDoc}
 */
@Override
public void paint(final RenderContext renderContext) {
    UIContext uic = UIContextHolder.getCurrent();
    // Generate the HTML
    StringWriter outputBuffer = new StringWriter();
    PrintWriter outputWriter = new PrintWriter(outputBuffer);
    WebXmlRenderContext outputContext = new WebXmlRenderContext(outputWriter, uic.getLocale());
    super.paint(outputContext);
    String html = outputBuffer.toString();
    // Only process TEMPLATE if has I18N brackets
    if (html.contains("{{#i18n")) {
        // Create a new instance of factory to avoid caching the page.
        // https://github.com/spullara/mustache.java/issues/117
        final MustacheFactory mf = new DefaultMustacheFactory();
        Mustache mustache = mf.compile(new StringReader(html), UUID.randomUUID().toString());
        StringWriter templateWriter = new StringWriter();
        mustache.execute(templateWriter, new I18NContext(getResourceBundle()));
        html = templateWriter.toString();
    }
    // Get the OUTPUT writer
    WebXmlRenderContext webRenderContext = (WebXmlRenderContext) renderContext;
    PrintWriter writer = webRenderContext.getWriter();
    writer.print(html);
}
Also used : WebXmlRenderContext(com.github.bordertech.wcomponents.servlet.WebXmlRenderContext) StringWriter(java.io.StringWriter) UIContext(com.github.bordertech.wcomponents.UIContext) DefaultMustacheFactory(com.github.mustachejava.DefaultMustacheFactory) StringReader(java.io.StringReader) Mustache(com.github.mustachejava.Mustache) PrintWriter(java.io.PrintWriter) DefaultMustacheFactory(com.github.mustachejava.DefaultMustacheFactory) MustacheFactory(com.github.mustachejava.MustacheFactory)

Example 24 with WebXmlRenderContext

use of com.github.bordertech.wcomponents.servlet.WebXmlRenderContext in project wcomponents by BorderTech.

the class VelocityInterceptor method paint.

/**
 * Renders the component using the velocity template which has been provided.
 *
 * @param renderContext the context for rendering.
 */
@Override
public void paint(final RenderContext renderContext) {
    if (!(renderContext instanceof WebXmlRenderContext)) {
        throw new SystemException("Unable to render to " + renderContext);
    }
    PrintWriter writer = ((WebXmlRenderContext) renderContext).getWriter();
    Template template = null;
    try {
        template = VelocityEngineFactory.getVelocityEngine().getTemplate(templateUrl);
    } catch (Exception ex) {
        String message = "Could not open velocity template \"" + templateUrl + "\" for \"" + this.getClass().getName() + "\"";
        LOG.error(message, ex);
        writer.println(message);
        return;
    }
    try {
        VelocityContext context = new VelocityContext();
        fillContext(context);
        template.merge(context, writer);
    } catch (ResourceNotFoundException rnfe) {
        LOG.error("Could not find template " + templateUrl, rnfe);
    } catch (ParseErrorException pee) {
        // syntax error : problem parsing the template
        LOG.error("Parse problems", pee);
    } catch (MethodInvocationException mie) {
        // something invoked in the template
        // threw an exception
        Throwable wrapped = mie.getWrappedThrowable();
        LOG.error("Problems with velocity", mie);
        if (wrapped != null) {
            LOG.error("Wrapped exception...", wrapped);
        }
    } catch (Exception e) {
        LOG.error("Problems with velocity", e);
    }
}
Also used : WebXmlRenderContext(com.github.bordertech.wcomponents.servlet.WebXmlRenderContext) SystemException(com.github.bordertech.wcomponents.util.SystemException) VelocityContext(org.apache.velocity.VelocityContext) ParseErrorException(org.apache.velocity.exception.ParseErrorException) MethodInvocationException(org.apache.velocity.exception.MethodInvocationException) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException) ParseErrorException(org.apache.velocity.exception.ParseErrorException) SystemException(com.github.bordertech.wcomponents.util.SystemException) MethodInvocationException(org.apache.velocity.exception.MethodInvocationException) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException) PrintWriter(java.io.PrintWriter) Template(org.apache.velocity.Template)

Example 25 with WebXmlRenderContext

use of com.github.bordertech.wcomponents.servlet.WebXmlRenderContext in project wcomponents by BorderTech.

the class WhitespaceFilterInterceptor method paint.

/**
 * Paints the component.
 *
 * @param renderContext the renderContext to send the output to.
 */
@Override
public void paint(final RenderContext renderContext) {
    boolean enabled = ConfigurationProperties.getWhitespaceFilter();
    if (enabled && renderContext instanceof WebXmlRenderContext) {
        PrintWriter writer = ((WebXmlRenderContext) renderContext).getWriter();
        writer = new WhiteSpaceFilterPrintWriter(writer);
        WebXmlRenderContext filteredContext = new WebXmlRenderContext(writer, UIContextHolder.getCurrent().getLocale());
        super.paint(filteredContext);
    } else {
        super.paint(renderContext);
    }
}
Also used : WebXmlRenderContext(com.github.bordertech.wcomponents.servlet.WebXmlRenderContext) WhiteSpaceFilterPrintWriter(com.github.bordertech.wcomponents.util.WhiteSpaceFilterPrintWriter) WhiteSpaceFilterPrintWriter(com.github.bordertech.wcomponents.util.WhiteSpaceFilterPrintWriter) PrintWriter(java.io.PrintWriter)

Aggregations

WebXmlRenderContext (com.github.bordertech.wcomponents.servlet.WebXmlRenderContext)63 PrintWriter (java.io.PrintWriter)48 StringWriter (java.io.StringWriter)27 Test (org.junit.Test)27 MockRequest (com.github.bordertech.wcomponents.util.mock.MockRequest)17 MockResponse (com.github.bordertech.wcomponents.util.mock.MockResponse)17 UIContext (com.github.bordertech.wcomponents.UIContext)15 NullWriter (com.github.bordertech.wcomponents.util.NullWriter)7 SystemException (com.github.bordertech.wcomponents.util.SystemException)7 WText (com.github.bordertech.wcomponents.WText)6 RenderContext (com.github.bordertech.wcomponents.RenderContext)5 WComponent (com.github.bordertech.wcomponents.WComponent)5 XmlStringBuilder (com.github.bordertech.wcomponents.XmlStringBuilder)5 WServlet (com.github.bordertech.wcomponents.servlet.WServlet)5 MockHttpServletRequest (com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletRequest)4 Configuration (org.apache.commons.configuration.Configuration)4 ActionEscape (com.github.bordertech.wcomponents.ActionEscape)3 AllComponents (com.github.bordertech.wcomponents.AllComponents)3 ComponentWithContext (com.github.bordertech.wcomponents.ComponentWithContext)3 Response (com.github.bordertech.wcomponents.Response)3