2008-05-28 20:59:52 +02:00
|
|
|
/**
|
|
|
|
* $RCSfile$
|
|
|
|
* $Revision: $
|
|
|
|
* $Date: $
|
|
|
|
*
|
|
|
|
* Copyright 2005-2007 Jive Software.
|
|
|
|
*
|
|
|
|
* All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package org.jivesoftware.smackx.provider;
|
|
|
|
|
|
|
|
import org.jivesoftware.smack.packet.IQ;
|
|
|
|
import org.jivesoftware.smack.packet.PacketExtension;
|
|
|
|
import org.jivesoftware.smack.packet.XMPPError;
|
|
|
|
import org.jivesoftware.smack.provider.IQProvider;
|
|
|
|
import org.jivesoftware.smack.provider.PacketExtensionProvider;
|
|
|
|
import org.jivesoftware.smack.util.PacketParserUtils;
|
|
|
|
import org.jivesoftware.smackx.commands.AdHocCommand;
|
|
|
|
import org.jivesoftware.smackx.commands.AdHocCommand.Action;
|
|
|
|
import org.jivesoftware.smackx.commands.AdHocCommandNote;
|
|
|
|
import org.jivesoftware.smackx.packet.AdHocCommandData;
|
|
|
|
import org.jivesoftware.smackx.packet.DataForm;
|
|
|
|
import org.xmlpull.v1.XmlPullParser;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The AdHocCommandDataProvider parses AdHocCommandData packets.
|
|
|
|
*
|
|
|
|
* @author Gabriel Guardincerri
|
|
|
|
*/
|
|
|
|
public class AdHocCommandDataProvider implements IQProvider {
|
|
|
|
|
|
|
|
public IQ parseIQ(XmlPullParser parser) throws Exception {
|
|
|
|
boolean done = false;
|
|
|
|
AdHocCommandData adHocCommandData = new AdHocCommandData();
|
|
|
|
DataFormProvider dataFormProvider = new DataFormProvider();
|
|
|
|
|
|
|
|
int eventType;
|
|
|
|
String elementName;
|
|
|
|
String namespace;
|
|
|
|
adHocCommandData.setSessionID(parser.getAttributeValue("", "sessionid"));
|
|
|
|
adHocCommandData.setNode(parser.getAttributeValue("", "node"));
|
|
|
|
|
|
|
|
// Status
|
|
|
|
String status = parser.getAttributeValue("", "status");
|
|
|
|
if (AdHocCommand.Status.executing.toString().equalsIgnoreCase(status)) {
|
|
|
|
adHocCommandData.setStatus(AdHocCommand.Status.executing);
|
2008-11-03 17:28:57 +01:00
|
|
|
}
|
|
|
|
else if (AdHocCommand.Status.completed.toString().equalsIgnoreCase(status)) {
|
2008-05-28 20:59:52 +02:00
|
|
|
adHocCommandData.setStatus(AdHocCommand.Status.completed);
|
2008-11-03 17:28:57 +01:00
|
|
|
}
|
|
|
|
else if (AdHocCommand.Status.canceled.toString().equalsIgnoreCase(status)) {
|
2008-05-28 20:59:52 +02:00
|
|
|
adHocCommandData.setStatus(AdHocCommand.Status.canceled);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Action
|
|
|
|
String action = parser.getAttributeValue("", "action");
|
|
|
|
if (action != null) {
|
|
|
|
Action realAction = AdHocCommand.Action.valueOf(action);
|
|
|
|
if (realAction == null || realAction.equals(Action.unknown)) {
|
|
|
|
adHocCommandData.setAction(Action.unknown);
|
2008-11-03 17:28:57 +01:00
|
|
|
}
|
|
|
|
else {
|
2008-05-28 20:59:52 +02:00
|
|
|
adHocCommandData.setAction(realAction);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (!done) {
|
|
|
|
eventType = parser.next();
|
|
|
|
elementName = parser.getName();
|
|
|
|
namespace = parser.getNamespace();
|
|
|
|
if (eventType == XmlPullParser.START_TAG) {
|
|
|
|
if (parser.getName().equals("actions")) {
|
|
|
|
String execute = parser.getAttributeValue("", "execute");
|
|
|
|
if (execute != null) {
|
|
|
|
adHocCommandData.setExecuteAction(AdHocCommand.Action.valueOf(execute));
|
|
|
|
}
|
2008-11-03 17:28:57 +01:00
|
|
|
}
|
|
|
|
else if (parser.getName().equals("next")) {
|
2008-05-28 20:59:52 +02:00
|
|
|
adHocCommandData.addAction(AdHocCommand.Action.next);
|
2008-11-03 17:28:57 +01:00
|
|
|
}
|
|
|
|
else if (parser.getName().equals("complete")) {
|
2008-05-28 20:59:52 +02:00
|
|
|
adHocCommandData.addAction(AdHocCommand.Action.complete);
|
2008-11-03 17:28:57 +01:00
|
|
|
}
|
|
|
|
else if (parser.getName().equals("prev")) {
|
2008-05-28 20:59:52 +02:00
|
|
|
adHocCommandData.addAction(AdHocCommand.Action.prev);
|
2008-11-03 17:28:57 +01:00
|
|
|
}
|
|
|
|
else if (elementName.equals("x") && namespace.equals("jabber:x:data")) {
|
2008-05-28 20:59:52 +02:00
|
|
|
adHocCommandData.setForm((DataForm) dataFormProvider.parseExtension(parser));
|
2008-11-03 17:28:57 +01:00
|
|
|
}
|
|
|
|
else if (parser.getName().equals("note")) {
|
|
|
|
AdHocCommandNote.Type type = AdHocCommandNote.Type.valueOf(
|
|
|
|
parser.getAttributeValue("", "type"));
|
2008-05-28 20:59:52 +02:00
|
|
|
String value = parser.nextText();
|
|
|
|
adHocCommandData.addNote(new AdHocCommandNote(type, value));
|
2008-11-03 17:28:57 +01:00
|
|
|
}
|
|
|
|
else if (parser.getName().equals("error")) {
|
2008-05-28 20:59:52 +02:00
|
|
|
XMPPError error = PacketParserUtils.parseError(parser);
|
|
|
|
adHocCommandData.setError(error);
|
|
|
|
}
|
2008-11-03 17:28:57 +01:00
|
|
|
}
|
|
|
|
else if (eventType == XmlPullParser.END_TAG) {
|
2008-05-28 20:59:52 +02:00
|
|
|
if (parser.getName().equals("command")) {
|
|
|
|
done = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return adHocCommandData;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static class BadActionError implements PacketExtensionProvider {
|
|
|
|
public PacketExtension parseExtension(XmlPullParser parser) throws Exception {
|
|
|
|
return new AdHocCommandData.SpecificError(AdHocCommand.SpecificErrorCondition.badAction);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static class MalformedActionError implements PacketExtensionProvider {
|
|
|
|
public PacketExtension parseExtension(XmlPullParser parser) throws Exception {
|
|
|
|
return new AdHocCommandData.SpecificError(AdHocCommand.SpecificErrorCondition.malformedAction);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static class BadLocaleError implements PacketExtensionProvider {
|
|
|
|
public PacketExtension parseExtension(XmlPullParser parser) throws Exception {
|
|
|
|
return new AdHocCommandData.SpecificError(AdHocCommand.SpecificErrorCondition.badLocale);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static class BadPayloadError implements PacketExtensionProvider {
|
|
|
|
public PacketExtension parseExtension(XmlPullParser parser) throws Exception {
|
|
|
|
return new AdHocCommandData.SpecificError(AdHocCommand.SpecificErrorCondition.badPayload);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static class BadSessionIDError implements PacketExtensionProvider {
|
|
|
|
public PacketExtension parseExtension(XmlPullParser parser) throws Exception {
|
|
|
|
return new AdHocCommandData.SpecificError(AdHocCommand.SpecificErrorCondition.badSessionid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static class SessionExpiredError implements PacketExtensionProvider {
|
|
|
|
public PacketExtension parseExtension(XmlPullParser parser) throws Exception {
|
|
|
|
return new AdHocCommandData.SpecificError(AdHocCommand.SpecificErrorCondition.sessionExpired);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|