Search in sources :

Example 1 with MarkupWriterListener

use of org.apache.tapestry5.MarkupWriterListener in project tapestry-5 by apache.

the class MarkupWriterImplTest method listeners.

@Test
public void listeners() {
    MarkupWriter w = new MarkupWriterImpl(new XMLMarkupModel());
    MarkupWriterListener l = new MarkupWriterListener() {

        public void elementDidStart(Element element) {
            element.text("[Start: " + element.getName() + "]");
        }

        public void elementDidEnd(Element element) {
            element.text("[End: " + element.getName() + "]");
        }
    };
    w.element("root");
    w.element("no-listener");
    w.write("before listener");
    w.addListener(l);
    w.element("listener");
    w.write("before n-w-l");
    w.element("nested-with-listener");
    w.write("n-w-l text");
    w.end();
    w.write("after n-w-l");
    w.end();
    w.removeListener(l);
    w.write("after listener");
    w.end();
    w.end();
    // Because we are invoking Element.text(), the text added by the listener is appended to the body of the
    // element,
    // which is correct but may not be what you'd expect.
    assertEquals(w.toString(), "<?xml version=\"1.0\"?>\n" + "<root><no-listener>before listener<listener>[Start: listener]before n-w-l<nested-with-listener>[Start: nested-with-listener]n-w-l text[End: nested-with-listener]</nested-with-listener>after n-w-l[End: listener]</listener>after listener</no-listener></root>");
}
Also used : MarkupWriterListener(org.apache.tapestry5.MarkupWriterListener) Element(org.apache.tapestry5.dom.Element) XMLMarkupModel(org.apache.tapestry5.dom.XMLMarkupModel) MarkupWriter(org.apache.tapestry5.MarkupWriter) Test(org.testng.annotations.Test)

Aggregations

MarkupWriter (org.apache.tapestry5.MarkupWriter)1 MarkupWriterListener (org.apache.tapestry5.MarkupWriterListener)1 Element (org.apache.tapestry5.dom.Element)1 XMLMarkupModel (org.apache.tapestry5.dom.XMLMarkupModel)1 Test (org.testng.annotations.Test)1