@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 testMergeStreamOpen() throws Exception {
        List<Object> list = Lists.newArrayList();
        streamOpenXmlEventList.forEach(xmlEvent -> {
            try {
                xmlMerger.decode(new ChannelHandlerContextAdapter(), xmlEvent, list);
            } catch (Exception e) {
                fail();
            }
        });
        // StreamOpen should not be merged, should be passed as XMLEvent
        assertThat(list.size(), Matchers.is(1));
        assertThat(list.get(0), Matchers.is(instanceOf(XMLEvent.class)));
        assertThat(((XMLEvent) list.get(0)).isStartElement(), Matchers.is(true));
    }