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.StanzaError;
021import org.jivesoftware.smack.provider.ExtensionElementProvider;
022import org.jivesoftware.smack.provider.IQProvider;
023import org.jivesoftware.smack.util.PacketParserUtils;
024
025import org.jivesoftware.smackx.commands.AdHocCommand;
026import org.jivesoftware.smackx.commands.AdHocCommand.Action;
027import org.jivesoftware.smackx.commands.AdHocCommandNote;
028import org.jivesoftware.smackx.commands.packet.AdHocCommandData;
029import org.jivesoftware.smackx.xdata.provider.DataFormProvider;
030
031import org.xmlpull.v1.XmlPullParser;
032
033/**
034 * The AdHocCommandDataProvider parses AdHocCommandData packets.
035 *
036 * @author Gabriel Guardincerri
037 */
038public class AdHocCommandDataProvider extends IQProvider<AdHocCommandData> {
039
040    @Override
041    public AdHocCommandData parse(XmlPullParser parser, int initialDepth)
042                    throws Exception {
043        boolean done = false;
044        AdHocCommandData adHocCommandData = new AdHocCommandData();
045        DataFormProvider dataFormProvider = new DataFormProvider();
046
047        int eventType;
048        String elementName;
049        String namespace;
050        adHocCommandData.setSessionID(parser.getAttributeValue("", "sessionid"));
051        adHocCommandData.setNode(parser.getAttributeValue("", "node"));
052
053        // Status
054        String status = parser.getAttributeValue("", "status");
055        if (AdHocCommand.Status.executing.toString().equalsIgnoreCase(status)) {
056            adHocCommandData.setStatus(AdHocCommand.Status.executing);
057        }
058        else if (AdHocCommand.Status.completed.toString().equalsIgnoreCase(status)) {
059            adHocCommandData.setStatus(AdHocCommand.Status.completed);
060        }
061        else if (AdHocCommand.Status.canceled.toString().equalsIgnoreCase(status)) {
062            adHocCommandData.setStatus(AdHocCommand.Status.canceled);
063        }
064
065        // Action
066        String action = parser.getAttributeValue("", "action");
067        if (action != null) {
068            Action realAction = AdHocCommand.Action.valueOf(action);
069            if (realAction == null || realAction.equals(Action.unknown)) {
070                adHocCommandData.setAction(Action.unknown);
071            }
072            else {
073                adHocCommandData.setAction(realAction);
074            }
075        }
076        while (!done) {
077            eventType = parser.next();
078            elementName = parser.getName();
079            namespace = parser.getNamespace();
080            if (eventType == XmlPullParser.START_TAG) {
081                if (parser.getName().equals("actions")) {
082                    String execute = parser.getAttributeValue("", "execute");
083                    if (execute != null) {
084                        adHocCommandData.setExecuteAction(AdHocCommand.Action.valueOf(execute));
085                    }
086                }
087                else if (parser.getName().equals("next")) {
088                    adHocCommandData.addAction(AdHocCommand.Action.next);
089                }
090                else if (parser.getName().equals("complete")) {
091                    adHocCommandData.addAction(AdHocCommand.Action.complete);
092                }
093                else if (parser.getName().equals("prev")) {
094                    adHocCommandData.addAction(AdHocCommand.Action.prev);
095                }
096                else if (elementName.equals("x") && namespace.equals("jabber:x:data")) {
097                    adHocCommandData.setForm(dataFormProvider.parse(parser));
098                }
099                else if (parser.getName().equals("note")) {
100                    String typeString = parser.getAttributeValue("", "type");
101                    AdHocCommandNote.Type type;
102                    if (typeString != null) {
103                        type = AdHocCommandNote.Type.valueOf(typeString);
104                    } else {
105                        // Type is optional and 'info' if not present.
106                        type = AdHocCommandNote.Type.info;
107                    }
108                    String value = parser.nextText();
109                    adHocCommandData.addNote(new AdHocCommandNote(type, value));
110                }
111                else if (parser.getName().equals("error")) {
112                    StanzaError.Builder error = PacketParserUtils.parseError(parser);
113                    adHocCommandData.setError(error);
114                }
115            }
116            else if (eventType == XmlPullParser.END_TAG) {
117                if (parser.getName().equals("command")) {
118                    done = true;
119                }
120            }
121        }
122        return adHocCommandData;
123    }
124
125    public static class BadActionError extends ExtensionElementProvider<AdHocCommandData.SpecificError> {
126        @Override
127        public AdHocCommandData.SpecificError parse(XmlPullParser parser, int initialDepth)  {
128            return new AdHocCommandData.SpecificError(AdHocCommand.SpecificErrorCondition.badAction);
129        }
130    }
131
132    public static class MalformedActionError extends ExtensionElementProvider<AdHocCommandData.SpecificError> {
133        @Override
134        public AdHocCommandData.SpecificError parse(XmlPullParser parser, int initialDepth)  {
135            return new AdHocCommandData.SpecificError(AdHocCommand.SpecificErrorCondition.malformedAction);
136        }
137    }
138
139    public static class BadLocaleError extends ExtensionElementProvider<AdHocCommandData.SpecificError> {
140        @Override
141        public AdHocCommandData.SpecificError parse(XmlPullParser parser, int initialDepth)  {
142            return new AdHocCommandData.SpecificError(AdHocCommand.SpecificErrorCondition.badLocale);
143        }
144    }
145
146    public static class BadPayloadError extends ExtensionElementProvider<AdHocCommandData.SpecificError> {
147        @Override
148        public AdHocCommandData.SpecificError parse(XmlPullParser parser, int initialDepth)  {
149            return new AdHocCommandData.SpecificError(AdHocCommand.SpecificErrorCondition.badPayload);
150        }
151    }
152
153    public static class BadSessionIDError extends ExtensionElementProvider<AdHocCommandData.SpecificError> {
154        @Override
155        public AdHocCommandData.SpecificError parse(XmlPullParser parser, int initialDepth)  {
156            return new AdHocCommandData.SpecificError(AdHocCommand.SpecificErrorCondition.badSessionid);
157        }
158    }
159
160    public static class SessionExpiredError extends ExtensionElementProvider<AdHocCommandData.SpecificError> {
161        @Override
162        public AdHocCommandData.SpecificError parse(XmlPullParser parser, int initialDepth)  {
163            return new AdHocCommandData.SpecificError(AdHocCommand.SpecificErrorCondition.sessionExpired);
164        }
165    }
166}