001/** 002 * 003 * Copyright 2005-2007 Jive Software. 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018package org.jivesoftware.smackx.commands.provider; 019 020import org.jivesoftware.smack.packet.IQ; 021import org.jivesoftware.smack.packet.PacketExtension; 022import org.jivesoftware.smack.packet.XMPPError; 023import org.jivesoftware.smack.provider.IQProvider; 024import org.jivesoftware.smack.provider.PacketExtensionProvider; 025import org.jivesoftware.smack.util.PacketParserUtils; 026import org.jivesoftware.smackx.commands.AdHocCommand; 027import org.jivesoftware.smackx.commands.AdHocCommand.Action; 028import org.jivesoftware.smackx.commands.packet.AdHocCommandData; 029import org.jivesoftware.smackx.commands.AdHocCommandNote; 030import org.jivesoftware.smackx.xdata.packet.DataForm; 031import org.jivesoftware.smackx.xdata.provider.DataFormProvider; 032import org.xmlpull.v1.XmlPullParser; 033 034/** 035 * The AdHocCommandDataProvider parses AdHocCommandData packets. 036 * 037 * @author Gabriel Guardincerri 038 */ 039public class AdHocCommandDataProvider implements IQProvider { 040 041 public IQ parseIQ(XmlPullParser parser) throws Exception { 042 boolean done = false; 043 AdHocCommandData adHocCommandData = new AdHocCommandData(); 044 DataFormProvider dataFormProvider = new DataFormProvider(); 045 046 int eventType; 047 String elementName; 048 String namespace; 049 adHocCommandData.setSessionID(parser.getAttributeValue("", "sessionid")); 050 adHocCommandData.setNode(parser.getAttributeValue("", "node")); 051 052 // Status 053 String status = parser.getAttributeValue("", "status"); 054 if (AdHocCommand.Status.executing.toString().equalsIgnoreCase(status)) { 055 adHocCommandData.setStatus(AdHocCommand.Status.executing); 056 } 057 else if (AdHocCommand.Status.completed.toString().equalsIgnoreCase(status)) { 058 adHocCommandData.setStatus(AdHocCommand.Status.completed); 059 } 060 else if (AdHocCommand.Status.canceled.toString().equalsIgnoreCase(status)) { 061 adHocCommandData.setStatus(AdHocCommand.Status.canceled); 062 } 063 064 // Action 065 String action = parser.getAttributeValue("", "action"); 066 if (action != null) { 067 Action realAction = AdHocCommand.Action.valueOf(action); 068 if (realAction == null || realAction.equals(Action.unknown)) { 069 adHocCommandData.setAction(Action.unknown); 070 } 071 else { 072 adHocCommandData.setAction(realAction); 073 } 074 } 075 while (!done) { 076 eventType = parser.next(); 077 elementName = parser.getName(); 078 namespace = parser.getNamespace(); 079 if (eventType == XmlPullParser.START_TAG) { 080 if (parser.getName().equals("actions")) { 081 String execute = parser.getAttributeValue("", "execute"); 082 if (execute != null) { 083 adHocCommandData.setExecuteAction(AdHocCommand.Action.valueOf(execute)); 084 } 085 } 086 else if (parser.getName().equals("next")) { 087 adHocCommandData.addAction(AdHocCommand.Action.next); 088 } 089 else if (parser.getName().equals("complete")) { 090 adHocCommandData.addAction(AdHocCommand.Action.complete); 091 } 092 else if (parser.getName().equals("prev")) { 093 adHocCommandData.addAction(AdHocCommand.Action.prev); 094 } 095 else if (elementName.equals("x") && namespace.equals("jabber:x:data")) { 096 adHocCommandData.setForm((DataForm) dataFormProvider.parseExtension(parser)); 097 } 098 else if (parser.getName().equals("note")) { 099 AdHocCommandNote.Type type = AdHocCommandNote.Type.valueOf( 100 parser.getAttributeValue("", "type")); 101 String value = parser.nextText(); 102 adHocCommandData.addNote(new AdHocCommandNote(type, value)); 103 } 104 else if (parser.getName().equals("error")) { 105 XMPPError error = PacketParserUtils.parseError(parser); 106 adHocCommandData.setError(error); 107 } 108 } 109 else if (eventType == XmlPullParser.END_TAG) { 110 if (parser.getName().equals("command")) { 111 done = true; 112 } 113 } 114 } 115 return adHocCommandData; 116 } 117 118 public static class BadActionError implements PacketExtensionProvider { 119 public PacketExtension parseExtension(XmlPullParser parser) throws Exception { 120 return new AdHocCommandData.SpecificError(AdHocCommand.SpecificErrorCondition.badAction); 121 } 122 } 123 124 public static class MalformedActionError implements PacketExtensionProvider { 125 public PacketExtension parseExtension(XmlPullParser parser) throws Exception { 126 return new AdHocCommandData.SpecificError(AdHocCommand.SpecificErrorCondition.malformedAction); 127 } 128 } 129 130 public static class BadLocaleError implements PacketExtensionProvider { 131 public PacketExtension parseExtension(XmlPullParser parser) throws Exception { 132 return new AdHocCommandData.SpecificError(AdHocCommand.SpecificErrorCondition.badLocale); 133 } 134 } 135 136 public static class BadPayloadError implements PacketExtensionProvider { 137 public PacketExtension parseExtension(XmlPullParser parser) throws Exception { 138 return new AdHocCommandData.SpecificError(AdHocCommand.SpecificErrorCondition.badPayload); 139 } 140 } 141 142 public static class BadSessionIDError implements PacketExtensionProvider { 143 public PacketExtension parseExtension(XmlPullParser parser) throws Exception { 144 return new AdHocCommandData.SpecificError(AdHocCommand.SpecificErrorCondition.badSessionid); 145 } 146 } 147 148 public static class SessionExpiredError implements PacketExtensionProvider { 149 public PacketExtension parseExtension(XmlPullParser parser) throws Exception { 150 return new AdHocCommandData.SpecificError(AdHocCommand.SpecificErrorCondition.sessionExpired); 151 } 152 } 153}