mirror of
https://codeberg.org/Mercury-IM/Smack
synced 2025-01-10 21:47:58 +01:00
Use XmlStringBuilder in RosterPacket and XMPPError
This commit is contained in:
parent
ed7dc1800d
commit
bdf9a75c4f
4 changed files with 49 additions and 38 deletions
smack-core/src/main/java/org/jivesoftware/smack
|
@ -36,6 +36,9 @@ import java.util.concurrent.atomic.AtomicLong;
|
|||
*/
|
||||
public abstract class Packet {
|
||||
|
||||
public static final String TEXT = "text";
|
||||
public static final String ITEM = "item";
|
||||
|
||||
protected static final String DEFAULT_LANGUAGE =
|
||||
java.util.Locale.getDefault().getLanguage().toLowerCase(Locale.US);
|
||||
|
||||
|
|
|
@ -17,10 +17,14 @@
|
|||
|
||||
package org.jivesoftware.smack.packet;
|
||||
|
||||
import org.jivesoftware.smack.util.StringUtils;
|
||||
import org.jivesoftware.smack.util.XmlStringBuilder;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.CopyOnWriteArraySet;
|
||||
|
||||
/**
|
||||
|
@ -66,7 +70,8 @@ public class RosterPacket extends IQ {
|
|||
}
|
||||
}
|
||||
|
||||
public CharSequence getChildElementXML() {
|
||||
@Override
|
||||
public XmlStringBuilder getChildElementXML() {
|
||||
XmlStringBuilder buf = new XmlStringBuilder();
|
||||
buf.halfOpenElement("query");
|
||||
buf.xmlnsAttribute("jabber:iq:roster");
|
||||
|
@ -96,6 +101,8 @@ public class RosterPacket extends IQ {
|
|||
*/
|
||||
public static class Item {
|
||||
|
||||
public static final String GROUP = "group";
|
||||
|
||||
private String user;
|
||||
private String name;
|
||||
private ItemType itemType;
|
||||
|
@ -207,24 +214,19 @@ public class RosterPacket extends IQ {
|
|||
groupNames.remove(groupName);
|
||||
}
|
||||
|
||||
public String toXML() {
|
||||
StringBuilder buf = new StringBuilder();
|
||||
buf.append("<item jid=\"").append(StringUtils.escapeForXML(user)).append("\"");
|
||||
if (name != null) {
|
||||
buf.append(" name=\"").append(StringUtils.escapeForXML(name)).append("\"");
|
||||
}
|
||||
if (itemType != null) {
|
||||
buf.append(" subscription=\"").append(itemType).append("\"");
|
||||
}
|
||||
if (itemStatus != null) {
|
||||
buf.append(" ask=\"").append(itemStatus).append("\"");
|
||||
}
|
||||
buf.append(">");
|
||||
public XmlStringBuilder toXML() {
|
||||
XmlStringBuilder xml = new XmlStringBuilder();
|
||||
xml.halfOpenElement(Packet.ITEM).attribute("jid", user);
|
||||
xml.optAttribute("name", name);
|
||||
xml.optAttribute("subscription", itemType);
|
||||
xml.optAttribute("ask", itemStatus);
|
||||
xml.rightAngelBracket();
|
||||
|
||||
for (String groupName : groupNames) {
|
||||
buf.append("<group>").append(StringUtils.escapeForXML(groupName)).append("</group>");
|
||||
xml.openElement(GROUP).escape(groupName).closeElement(GROUP);
|
||||
}
|
||||
buf.append("</item>");
|
||||
return buf.toString();
|
||||
xml.closeElement(Packet.ITEM);
|
||||
return xml;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -23,6 +23,8 @@ import java.util.List;
|
|||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
import org.jivesoftware.smack.util.XmlStringBuilder;
|
||||
|
||||
/**
|
||||
* Represents a XMPP error sub-packet. Typically, a server responds to a request that has
|
||||
* problems by sending the packet back and including an error packet. Each error has a type,
|
||||
|
@ -60,6 +62,9 @@ import java.util.Map;
|
|||
*/
|
||||
public class XMPPError {
|
||||
|
||||
public static final String NAMESPACE = "urn:ietf:params:xml:ns:xmpp-stanzas";
|
||||
public static final String ERROR = "error";
|
||||
|
||||
private final Type type;
|
||||
private final String condition;
|
||||
private String message;
|
||||
|
@ -153,29 +158,30 @@ public class XMPPError {
|
|||
*
|
||||
* @return the error as XML.
|
||||
*/
|
||||
public CharSequence toXML() {
|
||||
StringBuilder buf = new StringBuilder();
|
||||
buf.append("<error");
|
||||
if (type != null) {
|
||||
buf.append(" type=\"");
|
||||
buf.append(type.name().toLowerCase(Locale.US));
|
||||
buf.append("\"");
|
||||
}
|
||||
buf.append(">");
|
||||
public XmlStringBuilder toXML() {
|
||||
XmlStringBuilder xml = new XmlStringBuilder();
|
||||
xml.halfOpenElement(ERROR);
|
||||
xml.optAttribute("type", type.name().toLowerCase(Locale.US));
|
||||
xml.rightAngelBracket();
|
||||
|
||||
if (condition != null) {
|
||||
buf.append("<").append(condition);
|
||||
buf.append(" xmlns=\"urn:ietf:params:xml:ns:xmpp-stanzas\"/>");
|
||||
xml.halfOpenElement(condition);
|
||||
xml.xmlnsAttribute(NAMESPACE);
|
||||
xml.closeEmptyElement();
|
||||
}
|
||||
if (message != null) {
|
||||
buf.append("<text xml:lang=\"en\" xmlns=\"urn:ietf:params:xml:ns:xmpp-stanzas\">");
|
||||
buf.append(message);
|
||||
buf.append("</text>");
|
||||
xml.halfOpenElement(Packet.TEXT);
|
||||
xml.xmllangAttribute("en");
|
||||
xml.xmlnsAttribute(NAMESPACE);
|
||||
xml.rightAngelBracket();
|
||||
xml.escape(message);
|
||||
xml.closeElement(Packet.TEXT);
|
||||
}
|
||||
for (PacketExtension element : this.getExtensions()) {
|
||||
buf.append(element.toXML());
|
||||
xml.append(element.toXML());
|
||||
}
|
||||
buf.append("</error>");
|
||||
return buf.toString();
|
||||
xml.closeElement(ERROR);
|
||||
return xml;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
|
|
|
@ -779,7 +779,7 @@ public class PacketParserUtils {
|
|||
String namespace = parser.getNamespace();
|
||||
if (StreamError.NAMESPACE.equals(namespace)) {
|
||||
String name = parser.getName();
|
||||
if (name.equals("text") && !parser.isEmptyElementTag()) {
|
||||
if (name.equals(Packet.TEXT) && !parser.isEmptyElementTag()) {
|
||||
parser.next();
|
||||
text = parser.getText();
|
||||
}
|
||||
|
@ -822,7 +822,7 @@ public class PacketParserUtils {
|
|||
while (!done) {
|
||||
int eventType = parser.next();
|
||||
if (eventType == XmlPullParser.START_TAG) {
|
||||
if (parser.getName().equals("text")) {
|
||||
if (parser.getName().equals(Packet.TEXT)) {
|
||||
message = parser.nextText();
|
||||
}
|
||||
else {
|
||||
|
|
Loading…
Reference in a new issue