Search in sources :

Example 46 with WebXmlRenderContext

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

the class ValidateXMLInterceptor method paint.

/**
 * Override paint to include XML Validation.
 *
 * @param renderContext the renderContext to send the output to.
 */
@Override
public void paint(final RenderContext renderContext) {
    // Check interceptor is enabled
    if (!DebugValidateXML.isEnabled()) {
        super.paint(renderContext);
        return;
    }
    if (!(renderContext instanceof WebXmlRenderContext)) {
        LOG.warn("Unable to validate against a " + renderContext);
        super.paint(renderContext);
        return;
    }
    LOG.debug("Validate XML Interceptor: Start");
    WebXmlRenderContext webRenderContext = (WebXmlRenderContext) renderContext;
    PrintWriter writer = webRenderContext.getWriter();
    // Generate XML
    StringWriter tempBuffer = new StringWriter();
    PrintWriter tempWriter = new PrintWriter(tempBuffer);
    WebXmlRenderContext tempContext = new WebXmlRenderContext(tempWriter, UIContextHolder.getCurrent().getLocale());
    super.paint(tempContext);
    String xml = tempBuffer.toString();
    // If no errors, check against the schema
    String error = DebugValidateXML.validateXMLAgainstSchema(xml);
    if (error != null) {
        // XML is NOT valid, so Report Errors and Wrap the original XML
        LOG.debug("Validate XML Interceptor: XML Has Errors");
        writer.println("<div>");
        writer.println("<div>");
        writer.println("Invalid XML");
        writer.println("<ul><li>" + WebUtilities.encode(error) + "</li></ul>");
        writer.println("<br/>");
        writer.println("</div>");
        // If a schema error detected, Wrap XML so line numbers reported in validation message are correct
        String testXML = DebugValidateXML.wrapXMLInRootElement(xml);
        paintOriginalXML(testXML, writer);
        writer.println("</div>");
    } else {
        // XML is valid
        writer.write(xml);
    }
    LOG.debug("Validate XML Interceptor: Finished");
}
Also used : WebXmlRenderContext(com.github.bordertech.wcomponents.servlet.WebXmlRenderContext) StringWriter(java.io.StringWriter) PrintWriter(java.io.PrintWriter)

Example 47 with WebXmlRenderContext

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

the class TemplateWriter method doReplace.

/**
 * Replaces the search string by rendering the corresponding component.
 *
 * @param search the search String that was matched.
 * @param backing the underlying writer to write the replacement to.
 */
@Override
protected void doReplace(final String search, final Writer backing) {
    WComponent component = componentsByKey.get(search);
    UIContextHolder.pushContext(uic);
    try {
        component.paint(new WebXmlRenderContext((PrintWriter) backing));
    } finally {
        UIContextHolder.popContext();
    }
}
Also used : WComponent(com.github.bordertech.wcomponents.WComponent) WebXmlRenderContext(com.github.bordertech.wcomponents.servlet.WebXmlRenderContext) PrintWriter(java.io.PrintWriter)

Example 48 with WebXmlRenderContext

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

the class AbstractWComponent_Test method testDuplicateId.

@Test(expected = SystemException.class)
public void testDuplicateId() {
    WNamingContext root = new WNamingContext("TEST");
    Assert.assertEquals("Incorrect context id", "TEST", root.getId());
    SimpleComponent component = new SimpleComponent();
    component.setIdName("X");
    root.add(component);
    component = new SimpleComponent();
    component.setIdName("X");
    root.add(component);
    setActiveContext(new UIContextImpl());
    PrintWriter writer = new XmlStringBuilder(new StringWriter());
    root.preparePaint(new MockRequest());
    root.paint(new WebXmlRenderContext(writer));
}
Also used : WebXmlRenderContext(com.github.bordertech.wcomponents.servlet.WebXmlRenderContext) StringWriter(java.io.StringWriter) MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 49 with WebXmlRenderContext

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

the class FatalErrorPage_Test method testPaintComponentDeveloperFriendly.

@Test
public void testPaintComponentDeveloperFriendly() {
    setActiveContext(createUIContext());
    TestSampleException exception = new TestSampleException("sample exception only");
    FatalErrorPage fatalErrPage = new FatalErrorPage(true, exception);
    String correctMsg = fatalErrPage.getMessage();
    StringWriter strWriter = new StringWriter();
    PrintWriter writer = new PrintWriter(strWriter);
    fatalErrPage.paintComponent(new WebXmlRenderContext(writer));
    String result = strWriter.toString();
    Assert.assertTrue("should contain contents of getMessage()", result.indexOf(correctMsg) != -1);
    Assert.assertTrue("should contain the name of the Exception", result.indexOf(exception.getClass().getName()) != -1);
}
Also used : WebXmlRenderContext(com.github.bordertech.wcomponents.servlet.WebXmlRenderContext) StringWriter(java.io.StringWriter) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 50 with WebXmlRenderContext

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

the class FatalErrorPage_Test method testPaintComponentNotDeveloperFriendly.

@Test
public void testPaintComponentNotDeveloperFriendly() {
    setActiveContext(createUIContext());
    TestSampleException exception = new TestSampleException("sample exception only");
    FatalErrorPage fatalErrPage = new FatalErrorPage(false, exception);
    String correctMsg = fatalErrPage.getMessage();
    StringWriter strWriter = new StringWriter();
    PrintWriter writer = new PrintWriter(strWriter);
    fatalErrPage.paintComponent(new WebXmlRenderContext(writer));
    Assert.assertTrue("Should equal the contents of getMessage()", strWriter.toString().equals(correctMsg + System.getProperty("line.separator")));
}
Also used : WebXmlRenderContext(com.github.bordertech.wcomponents.servlet.WebXmlRenderContext) StringWriter(java.io.StringWriter) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

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