@Override
    public void endElement(final String uri, final String localName, final String qName) throws SAXException {
        //if (LOGGER.isTraceEnabled()) {
        //    LOGGER.trace(String.format("endElement called for %s at depth %s, buffer [%s]",
        //            localName, depth, truncateAndStripWhitespace(getBuffer())));
        //}
        handler.endElement(uri, localName, qName);

        // Decrease the element depth
        depth--;

        try {
            if (depth <= 1) {
                bufferedWriter.flush();
                final CharBuffer cb = stringWriter.getBuffer();

                // If depth = 1 then we have finished an event.
                if (depth == 1) {
                    if (cb.length() > 0) {
                        // Compensate for the fact that the writer will now have
                        // received a closing bracket see comment in
                        // startElement() for an explanation.
                        cb.trimCharStart('>');

                        // Trim new lines off the character buffer.
                        cb.trimChar('\n');

                        // If we are indenting the output then add a new line
                        // after records.
                        if (indentOutput) {
                            cb.append('\n');
                        }

                        final String body = cb.toString();

                        borrowDestinations(header, footer);

                        //if (LOGGER.isTraceEnabled()) {
                        //    LOGGER.trace(String.format("Writing %s chars [%s] to destinations",
                        //            body.length(),
                        //            truncateAndStripWhitespace(body)));
                        //}
                        getWriter().write(body);
                        returnDestinations();
                    }
                } else {
                    doneElement = false;
                }

                cb.setLength(0);
            }
        } catch (final IOException e) {
            throw new SAXException(e);
        }

        super.endElement(uri, localName, qName);
    }
@param uri       the Namespace URI, or the empty string if the element has no
                 Namespace URI or if Namespace processing is not being
                 performed
@param localName the local name (without prefix), or the empty string if
                 Namespace processing is not being performed
@param qName     the qualified XML name (with prefix), or the empty string if
                 qualified names are not available
@throws SAXException any SAX exception, possibly wrapping another exception
@see AbstractXMLFilter#endElement(String,
String, String)
stroom-pipeline/src/main/java/stroom/pipeline/writer/XMLWriter.java