Use XmlStringBuilder in AdHocCommandData

also remove 'lang' attribute, since it belongs into IQ, see XEP-50 3.7:

"The requester SHOULD provide its locale information using the "xml:lang"
attribute on either the <iq/> (RECOMMENDED) or <command/> element."
This commit is contained in:
Florian Schmaus 2014-09-12 11:03:26 +02:00
parent fca884d76b
commit 3a37b71e19
2 changed files with 29 additions and 40 deletions

View File

@ -170,7 +170,7 @@ public abstract class AdHocCommand {
} }
public String getRaw() { public String getRaw() {
return data.getChildElementXML(); return data.getChildElementXML().toString();
} }
/** /**

View File

@ -19,6 +19,8 @@ package org.jivesoftware.smackx.commands.packet;
import org.jivesoftware.smack.packet.IQ; import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.PacketExtension; import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smack.util.XmlStringBuilder;
import org.jivesoftware.smackx.commands.AdHocCommand; import org.jivesoftware.smackx.commands.AdHocCommand;
import org.jivesoftware.smackx.commands.AdHocCommand.Action; import org.jivesoftware.smackx.commands.AdHocCommand.Action;
import org.jivesoftware.smackx.commands.AdHocCommand.SpecificErrorCondition; import org.jivesoftware.smackx.commands.AdHocCommand.SpecificErrorCondition;
@ -35,6 +37,9 @@ import java.util.List;
*/ */
public class AdHocCommandData extends IQ { public class AdHocCommandData extends IQ {
public static final String ELEMENT = "command";
public static final String NAMESPACE = "http://jabber.org/protocol/commands";
/* JID of the command host */ /* JID of the command host */
private String id; private String id;
@ -61,61 +66,42 @@ public class AdHocCommandData extends IQ {
private AdHocCommand.Action executeAction; private AdHocCommand.Action executeAction;
private String lang;
public AdHocCommandData() { public AdHocCommandData() {
} }
@Override @Override
public String getChildElementXML() { public XmlStringBuilder getChildElementXML() {
StringBuilder buf = new StringBuilder(); XmlStringBuilder xml = new XmlStringBuilder();
buf.append("<command xmlns=\"http://jabber.org/protocol/commands\""); xml.halfOpenElement(ELEMENT).xmlnsAttribute(NAMESPACE);
buf.append(" node=\"").append(node).append("\""); xml.attribute("node", node);
if (sessionID != null) { xml.optAttribute("sessionid", sessionID);
if (!sessionID.equals("")) { xml.optAttribute("status", status);
buf.append(" sessionid=\"").append(sessionID).append("\""); xml.optAttribute("action", action);
} xml.rightAngleBracket();
}
if (status != null) {
buf.append(" status=\"").append(status).append("\"");
}
if (action != null) {
buf.append(" action=\"").append(action).append("\"");
}
if (lang != null) {
if (!lang.equals("")) {
buf.append(" lang=\"").append(lang).append("\"");
}
}
buf.append(">");
if (getType() == Type.result) { if (getType() == Type.result) {
buf.append("<actions"); xml.halfOpenElement("actions");
xml.optAttribute("execute", executeAction);
if (executeAction != null) {
buf.append(" execute=\"").append(executeAction).append("\"");
}
if (actions.size() == 0) { if (actions.size() == 0) {
buf.append("/>"); xml.closeEmptyElement();
} else { } else {
buf.append(">"); xml.rightAngleBracket();
for (AdHocCommand.Action action : actions) { for (AdHocCommand.Action action : actions) {
buf.append("<").append(action).append("/>"); xml.emptyElement(action);
} }
buf.append("</actions>"); xml.closeElement("actions");
} }
} }
if (form != null) { if (form != null) {
buf.append(form.toXML()); xml.append(form.toXML());
} }
for (AdHocCommandNote note : notes) { for (AdHocCommandNote note : notes) {
buf.append("<note type=\"").append(note.getType().toString()).append("\">"); xml.halfOpenElement("note").attribute("type", note.getType().toString()).rightAngleBracket();
buf.append(note.getValue()); xml.append(note.getValue());
buf.append("</note>"); xml.closeElement("note");
} }
// TODO ERRORS // TODO ERRORS
@ -123,8 +109,8 @@ public class AdHocCommandData extends IQ {
// buf.append(getError().toXML()); // buf.append(getError().toXML());
// } // }
buf.append("</command>"); xml.closeElement(ELEMENT);
return buf.toString(); return xml;
} }
/** /**
@ -239,6 +225,9 @@ public class AdHocCommandData extends IQ {
} }
public void setSessionID(String sessionID) { public void setSessionID(String sessionID) {
if (StringUtils.isNullOrEmpty(sessionID)) {
throw new IllegalArgumentException("session id must not be null or empty");
}
this.sessionID = sessionID; this.sessionID = sessionID;
} }