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