summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMili Verma <mili.verma@isode.com>2012-01-06 15:00:55 (GMT)
committerKevin Smith <git@kismith.co.uk>2012-01-09 15:54:52 (GMT)
commitcc760bfd15caadb56bfef477cb54dc94c25f7fa7 (patch)
tree05f5918b97488b05ca1266c0644a3874adb98129 /src/com/isode/stroke/serializer/payloadserializers/CommandSerializer.java
parent12b1d667965556002ea0fd300a71bcdf57634e90 (diff)
downloadstroke-cc760bfd15caadb56bfef477cb54dc94c25f7fa7.zip
stroke-cc760bfd15caadb56bfef477cb54dc94c25f7fa7.tar.bz2
Port Adhoc commands to Stroke
This patch ports the Adhoc commands from Swiften to Stroke. It also ports their unit tests. Test-information: Unit tests pass. MLC able to use the ad-hoc command fine.
Diffstat (limited to 'src/com/isode/stroke/serializer/payloadserializers/CommandSerializer.java')
-rw-r--r--src/com/isode/stroke/serializer/payloadserializers/CommandSerializer.java96
1 files changed, 96 insertions, 0 deletions
diff --git a/src/com/isode/stroke/serializer/payloadserializers/CommandSerializer.java b/src/com/isode/stroke/serializer/payloadserializers/CommandSerializer.java
new file mode 100644
index 0000000..411e711
--- /dev/null
+++ b/src/com/isode/stroke/serializer/payloadserializers/CommandSerializer.java
@@ -0,0 +1,96 @@
+/*
+ * Copyright (c) 2012 Isode Limited, London, England.
+ * All rights reserved.
+ */
+/*
+ * Copyright (c) 2010 Kevin Smith
+ * All rights reserved.
+ */
+
+package com.isode.stroke.serializer.payloadserializers;
+
+import com.isode.stroke.elements.Command;
+import com.isode.stroke.elements.Form;
+import com.isode.stroke.elements.Command.Action;
+import com.isode.stroke.elements.Command.Note;
+import com.isode.stroke.serializer.GenericPayloadSerializer;
+import com.isode.stroke.serializer.xml.XMLElement;
+import com.isode.stroke.serializer.xml.XMLRawTextNode;
+import com.isode.stroke.serializer.xml.XMLTextNode;
+
+/**
+ * Serializer for {@link Command} element.
+ */
+public class CommandSerializer extends GenericPayloadSerializer<Command> {
+ /**
+ * Constructor
+ */
+ public CommandSerializer() {
+ super(Command.class);
+ }
+
+ public String serializePayload(Command command) {
+ XMLElement commandElement = new XMLElement("command",
+ "http://jabber.org/protocol/commands");
+ commandElement.setAttribute(Command.COMMAND_ATTRIBUTE_NODE, command
+ .getNode());
+
+ if (!command.getSessionID().isEmpty()) {
+ commandElement.setAttribute(Command.COMMAND_ATTRIBUTE_SESSION_ID,
+ command.getSessionID());
+ }
+
+ String action = actionToString(command.getAction());
+ if (!action.isEmpty()) {
+ commandElement.setAttribute(Command.COMMAND_ATTRIBUTE_ACTION,
+ action);
+ }
+
+ String status = command.getStatus().getStringForm();
+ if (!status.isEmpty()) {
+ commandElement.setAttribute(Command.COMMAND_ATTRIBUTE_STATUS,
+ status);
+ }
+
+ if (command.getAvailableActions().size() > 0) {
+ String actions = "<" + Command.COMMAND_ELEMENT_ACTIONS;
+ String executeAction = actionToString(command.getExecuteAction());
+ if (!executeAction.isEmpty()) {
+ actions += " " + Command.COMMAND_ATTRIBUTE_EXECUTE + "='"
+ + executeAction + "'";
+ }
+ actions += ">";
+ for (Action act : command.getAvailableActions()) {
+ actions += "<" + actionToString(act) + "/>";
+ }
+ actions += "</" + Command.COMMAND_ELEMENT_ACTIONS + ">";
+ commandElement.addNode(new XMLRawTextNode(actions));
+ }
+
+ for (Note note : command.getNotes()) {
+ XMLElement noteElement = new XMLElement(
+ Command.COMMAND_ELEMENT_NOTE);
+ String type = note.type.getStringForm();
+ noteElement.setAttribute(Command.COMMAND_ATTRIBUTE_TYPE, type);
+ noteElement.addNode(new XMLTextNode(note.note));
+ commandElement.addNode(noteElement);
+ }
+
+ Form form = command.getForm();
+ if (form != null) {
+ FormSerializer formSerializer = new FormSerializer();
+ commandElement.addNode(new XMLRawTextNode(formSerializer
+ .serialize(form)));
+ }
+ return commandElement.serialize();
+ }
+
+ private String actionToString(Action action) {
+ return action.getStringForm();
+ }
+
+ @Override
+ public String toString() {
+ return CommandSerializer.class.getSimpleName();
+ }
+}