blob: 63d24ec386f886f03b6096c9ef0feff37d9298db [file] [log] [blame]
Shuyi Chend7955ce2013-05-22 14:51:55 -07001/**
2 * $RCSfile$
3 * $Revision$
4 * $Date$
5 *
6 * Copyright 2005-2007 Jive Software.
7 *
8 * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20
21package org.jivesoftware.smackx.provider;
22
23import org.jivesoftware.smack.packet.IQ;
24import org.jivesoftware.smack.packet.PacketExtension;
25import org.jivesoftware.smack.packet.XMPPError;
26import org.jivesoftware.smack.provider.IQProvider;
27import org.jivesoftware.smack.provider.PacketExtensionProvider;
28import org.jivesoftware.smack.util.PacketParserUtils;
29import org.jivesoftware.smackx.commands.AdHocCommand;
30import org.jivesoftware.smackx.commands.AdHocCommand.Action;
31import org.jivesoftware.smackx.commands.AdHocCommandNote;
32import org.jivesoftware.smackx.packet.AdHocCommandData;
33import org.jivesoftware.smackx.packet.DataForm;
34import org.xmlpull.v1.XmlPullParser;
35
36/**
37 * The AdHocCommandDataProvider parses AdHocCommandData packets.
38 *
39 * @author Gabriel Guardincerri
40 */
41public class AdHocCommandDataProvider implements IQProvider {
42
43 public IQ parseIQ(XmlPullParser parser) throws Exception {
44 boolean done = false;
45 AdHocCommandData adHocCommandData = new AdHocCommandData();
46 DataFormProvider dataFormProvider = new DataFormProvider();
47
48 int eventType;
49 String elementName;
50 String namespace;
51 adHocCommandData.setSessionID(parser.getAttributeValue("", "sessionid"));
52 adHocCommandData.setNode(parser.getAttributeValue("", "node"));
53
54 // Status
55 String status = parser.getAttributeValue("", "status");
56 if (AdHocCommand.Status.executing.toString().equalsIgnoreCase(status)) {
57 adHocCommandData.setStatus(AdHocCommand.Status.executing);
58 }
59 else if (AdHocCommand.Status.completed.toString().equalsIgnoreCase(status)) {
60 adHocCommandData.setStatus(AdHocCommand.Status.completed);
61 }
62 else if (AdHocCommand.Status.canceled.toString().equalsIgnoreCase(status)) {
63 adHocCommandData.setStatus(AdHocCommand.Status.canceled);
64 }
65
66 // Action
67 String action = parser.getAttributeValue("", "action");
68 if (action != null) {
69 Action realAction = AdHocCommand.Action.valueOf(action);
70 if (realAction == null || realAction.equals(Action.unknown)) {
71 adHocCommandData.setAction(Action.unknown);
72 }
73 else {
74 adHocCommandData.setAction(realAction);
75 }
76 }
77 while (!done) {
78 eventType = parser.next();
79 elementName = parser.getName();
80 namespace = parser.getNamespace();
81 if (eventType == XmlPullParser.START_TAG) {
82 if (parser.getName().equals("actions")) {
83 String execute = parser.getAttributeValue("", "execute");
84 if (execute != null) {
85 adHocCommandData.setExecuteAction(AdHocCommand.Action.valueOf(execute));
86 }
87 }
88 else if (parser.getName().equals("next")) {
89 adHocCommandData.addAction(AdHocCommand.Action.next);
90 }
91 else if (parser.getName().equals("complete")) {
92 adHocCommandData.addAction(AdHocCommand.Action.complete);
93 }
94 else if (parser.getName().equals("prev")) {
95 adHocCommandData.addAction(AdHocCommand.Action.prev);
96 }
97 else if (elementName.equals("x") && namespace.equals("jabber:x:data")) {
98 adHocCommandData.setForm((DataForm) dataFormProvider.parseExtension(parser));
99 }
100 else if (parser.getName().equals("note")) {
101 AdHocCommandNote.Type type = AdHocCommandNote.Type.valueOf(
102 parser.getAttributeValue("", "type"));
103 String value = parser.nextText();
104 adHocCommandData.addNote(new AdHocCommandNote(type, value));
105 }
106 else if (parser.getName().equals("error")) {
107 XMPPError error = PacketParserUtils.parseError(parser);
108 adHocCommandData.setError(error);
109 }
110 }
111 else if (eventType == XmlPullParser.END_TAG) {
112 if (parser.getName().equals("command")) {
113 done = true;
114 }
115 }
116 }
117 return adHocCommandData;
118 }
119
120 public static class BadActionError implements PacketExtensionProvider {
121 public PacketExtension parseExtension(XmlPullParser parser) throws Exception {
122 return new AdHocCommandData.SpecificError(AdHocCommand.SpecificErrorCondition.badAction);
123 }
124 }
125
126 public static class MalformedActionError implements PacketExtensionProvider {
127 public PacketExtension parseExtension(XmlPullParser parser) throws Exception {
128 return new AdHocCommandData.SpecificError(AdHocCommand.SpecificErrorCondition.malformedAction);
129 }
130 }
131
132 public static class BadLocaleError implements PacketExtensionProvider {
133 public PacketExtension parseExtension(XmlPullParser parser) throws Exception {
134 return new AdHocCommandData.SpecificError(AdHocCommand.SpecificErrorCondition.badLocale);
135 }
136 }
137
138 public static class BadPayloadError implements PacketExtensionProvider {
139 public PacketExtension parseExtension(XmlPullParser parser) throws Exception {
140 return new AdHocCommandData.SpecificError(AdHocCommand.SpecificErrorCondition.badPayload);
141 }
142 }
143
144 public static class BadSessionIDError implements PacketExtensionProvider {
145 public PacketExtension parseExtension(XmlPullParser parser) throws Exception {
146 return new AdHocCommandData.SpecificError(AdHocCommand.SpecificErrorCondition.badSessionid);
147 }
148 }
149
150 public static class SessionExpiredError implements PacketExtensionProvider {
151 public PacketExtension parseExtension(XmlPullParser parser) throws Exception {
152 return new AdHocCommandData.SpecificError(AdHocCommand.SpecificErrorCondition.sessionExpired);
153 }
154 }
155}