@Override
    protected void decode(ChannelHandlerContext ctx, Object object, List out) throws Exception {
        try {
            if (object instanceof  XMLEvent) {
                final XMLEvent event = (XMLEvent) object;

                if (event.isStartDocument() || event.isEndDocument()) {
                    return;
                }

                if (event.isCharacters() && depth <= 1) {
                    return;
                }

                if (depth < 1 && event.isStartElement()) {
                    out.add(object);
                    depth++;
                    return;
                }

                if (depth <= 1 && event.isEndElement()) {
                    out.add(object);
                    depth--;
                    return;
                }

                writer.add(event);

                if (event.isStartElement()) {
                    depth++;
                } else if (event.isEndElement()) {
                    depth--;

                    if (depth == 1) {
                        writer.flush();
                        org.dom4j.Element xmlElement = transform().getRootElement();
                        out.add(xmlElement);
                        writer.close();
                        resetWriter();
                    }
                }
            }
        } catch (Exception e) {
            logger.info(e.getCause().getMessage());
            throw e;
        }
    }
@Test
    public void testMergeSubscribeMsg() throws Exception {
        List<Object> list = Lists.newArrayList();
        xmlMerger.depth = 1;
        subscribeMsgEventList.forEach(xmlEvent -> {
            try {
                xmlMerger.decode(new ChannelHandlerContextAdapter(), xmlEvent, list);
            } catch (Exception e) {
                fail();
            }
        });
        assertThat("Output list should have size of 1", list.size(), Matchers.is(1));
        assertThat("Output object should be of type org.dom4j.Element",
                   list.get(0), Matchers.is(instanceOf(Element.class)));
        Element root = (Element) list.get(0);
        assertThat("Top level element should be of type IQ",
                   root.getQName().getName(), Matchers.is("iq"));
        assertThat(root.attributes().size(), Matchers.is(4));
        assertThat(root.attribute("type").getValue(), Matchers.is("set"));
        assertNotNull("<pubsub> element should be accessible", root.element("pubsub"));
        assertThat(root.element("pubsub").getNamespaceURI(), Matchers.is("http://jabber.org/protocol/pubsub"));
        assertNotNull("<subscribe> element should be accessible",
                      root.element("pubsub").element("subscribe"));

    }