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());
}
Aggregations