@Override
    protected void decode(ChannelHandlerContext channelHandlerContext, Object object, List out) throws Exception {
            if (object instanceof Element) {
                Element root = (Element) object;

                try {
                    Packet packet = recognizeAndReturnXmppPacket(root);
                    validate(packet);
                    out.add(packet);
                } catch (UnsupportedStanzaTypeException e) {
                    throw e;
                } catch (Exception e) {
                    throw new XmppValidationException(false);
                }

            } else if (object instanceof XMLEvent) {

                XMLEvent event = (XMLEvent) object;
                if (event.isStartElement()) {
                    final StartElement element = event.asStartElement();

                    if (element.getName().getLocalPart().equals(XmppConstants.STREAM_QNAME)) {
                        DocumentFactory df = DocumentFactory.getInstance();
                        QName qname = (element.getName().getPrefix() == null) ?
                                df.createQName(element.getName().getLocalPart(),
                                               element.getName().getNamespaceURI()) :
                                df.createQName(element.getName().getLocalPart(),
                                               element.getName().getPrefix(), element.getName().getNamespaceURI());

                        Element newElement = df.createElement(qname);

                        Iterator nsIt = element.getNamespaces();
                        // add all relevant XML namespaces to Element
                        while (nsIt.hasNext()) {
                            Namespace ns = (Namespace) nsIt.next();
                            newElement.addNamespace(ns.getPrefix(), ns.getNamespaceURI());
                        }

                        Iterator attrIt = element.getAttributes();
                        // add all attributes to Element
                        while (attrIt.hasNext()) {
                            Attribute attr = (Attribute) attrIt.next();
                            newElement.addAttribute(attr.getName().getLocalPart(), attr.getValue());
                        }
                        XmppStreamOpen xmppStreamOpen = new XmppStreamOpen(newElement);
                        validator.validateStream(xmppStreamOpen);
                        out.add(xmppStreamOpen);
                    }
                } else if (event.isEndElement()) {
                    out.add(new XmppStreamClose());
                }
            }

    }
@Test
    public void testDecodeXmppStanza() throws Exception {
        // TODO: complete it
        List<Object> out = Lists.newArrayList();
        xmppDecoder.decode(mockChannelHandlerContext, xmppStanzaElement, out);
        assertThat(out.size(), is(1));
        assertThat(out.get(0), is(instanceOf(Packet.class)));
        assertThat(out.get(0), is(instanceOf(IQ.class)));
        IQ iq = (IQ) out.get(0);
        assertThat(iq.getElement(), is(notNullValue()));
        assertThat(iq.getFrom(), is(new JID("test@xmpp.org")));
        assertThat(iq.getTo(), is(new JID("xmpp.onosproject.org")));
        assertThat(iq.getType(), is(IQ.Type.set));
    }