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