Search in sources :

Example 6 with XmppException

use of com.googlecode.asmack.XmppException in project AsmackService by rtreffer.

the class XmppOutputStream method send.

/**
     * Send a stanza through this stream. The stanza will be merged and
     * validated.
     * @param stanza Stanza The stanza to send.
     * @throws XmppException In case of an error.
     */
public void send(Stanza stanza) throws XmppException {
    XmlPullParser xmlPullParser;
    try {
        xmlPullParser = XMLUtils.getXMLPullParser();
    } catch (XmlPullParserException e) {
        throw new XmppException("Can't create xml parser", e);
    }
    try {
        xmlPullParser.setInput(new ByteArrayInputStream(stanza.getXml().getBytes()), "UTF-8");
    } catch (XmlPullParserException e) {
        throw new XmppException("Can't parse input", e);
    }
    StringWriter stringWriter = new StringWriter();
    XmlSerializer xmlSerializer;
    try {
        xmlSerializer = XMLUtils.getXMLSerializer();
    } catch (XmlPullParserException e) {
        throw new XmppException("Can't create xml serializer", e);
    }
    try {
        xmlSerializer.setOutput(stringWriter);
    } catch (IllegalArgumentException e) {
        throw new XmppException("Please report", e);
    } catch (IllegalStateException e) {
        throw new XmppException("Please report", e);
    } catch (IOException e) {
        throw new XmppException("Please report", e);
    }
    try {
        xmlSerializer.startTag(stanza.getNamespace(), stanza.getName());
        HashSet<String> addedAttributes = new HashSet<String>();
        if (stanza.getAttributes() != null) {
            for (Attribute attr : stanza.getAttributes()) {
                addedAttributes.add(attr.getNamespace() + "\0" + attr.getName());
                xmlSerializer.attribute(attr.getNamespace(), attr.getName(), attr.getValue());
            }
        }
        xmlPullParser.nextTag();
        int attributeCount = xmlPullParser.getAttributeCount();
        for (int i = 0; i < attributeCount; i++) {
            String key = xmlPullParser.getAttributeNamespace(i) + "\0" + xmlPullParser.getAttributeName(i);
            if (addedAttributes.contains(key)) {
                continue;
            }
            xmlSerializer.attribute(xmlPullParser.getAttributeNamespace(i), xmlPullParser.getAttributeName(i), xmlPullParser.getAttributeValue(i));
        }
        if (xmlPullParser.isEmptyElementTag()) {
            xmlSerializer.endTag(stanza.getNamespace(), stanza.getName());
        } else {
            XMLUtils.copyXML(xmlPullParser, xmlSerializer);
        }
        xmlSerializer.endDocument();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        new XmppException("Please report", e);
        e.printStackTrace();
    } catch (XmlPullParserException e) {
        e.printStackTrace();
    }
    sendUnchecked(stringWriter.toString());
}
Also used : XmppException(com.googlecode.asmack.XmppException) Attribute(com.googlecode.asmack.Attribute) XmlPullParser(org.xmlpull.v1.XmlPullParser) IOException(java.io.IOException) StringWriter(java.io.StringWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) XmlSerializer(org.xmlpull.v1.XmlSerializer) HashSet(java.util.HashSet)

Aggregations

XmppException (com.googlecode.asmack.XmppException)6 Attribute (com.googlecode.asmack.Attribute)3 Stanza (com.googlecode.asmack.Stanza)3 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 HashSet (java.util.HashSet)1 XmlPullParser (org.xmlpull.v1.XmlPullParser)1 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)1 XmlSerializer (org.xmlpull.v1.XmlSerializer)1