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.SmackException;
023import org.jivesoftware.smack.packet.XMPPError;
024import org.jivesoftware.smack.provider.IQProvider;
025import org.jivesoftware.smack.provider.ExtensionElementProvider;
026import org.jivesoftware.smack.util.PacketParserUtils;
027import org.jivesoftware.smackx.commands.AdHocCommand;
028import org.jivesoftware.smackx.commands.AdHocCommand.Action;
029import org.jivesoftware.smackx.commands.packet.AdHocCommandData;
030import org.jivesoftware.smackx.commands.AdHocCommandNote;
031import org.jivesoftware.smackx.xdata.packet.DataForm;
032import org.jivesoftware.smackx.xdata.provider.DataFormProvider;
033import org.xmlpull.v1.XmlPullParser;
034import org.xmlpull.v1.XmlPullParserException;
035
036/**
037 * The AdHocCommandDataProvider parses AdHocCommandData packets.
038 * 
039 * @author Gabriel Guardincerri
040 */
041public class AdHocCommandDataProvider extends IQProvider<AdHocCommandData> {
042
043    @Override
044    public AdHocCommandData parse(XmlPullParser parser, int initialDepth)
045                    throws XmlPullParserException, IOException, SmackException {
046        boolean done = false;
047        AdHocCommandData adHocCommandData = new AdHocCommandData();
048        DataFormProvider dataFormProvider = new DataFormProvider();
049
050        int 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            elementName = parser.getName();
082            namespace = parser.getNamespace();
083            if (eventType == XmlPullParser.START_TAG) {
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((DataForm) dataFormProvider.parse(parser));
101                }
102                else if (parser.getName().equals("note")) {
103                    AdHocCommandNote.Type type = AdHocCommandNote.Type.valueOf(
104                            parser.getAttributeValue("", "type"));
105                    String value = parser.nextText();
106                    adHocCommandData.addNote(new AdHocCommandNote(type, value));
107                }
108                else if (parser.getName().equals("error")) {
109                    XMPPError error = PacketParserUtils.parseError(parser);
110                    adHocCommandData.setError(error);
111                }
112            }
113            else if (eventType == XmlPullParser.END_TAG) {
114                if (parser.getName().equals("command")) {
115                    done = true;
116                }
117            }
118        }
119        return adHocCommandData;
120    }
121
122    public static class BadActionError extends ExtensionElementProvider<AdHocCommandData.SpecificError> {
123        @Override
124        public AdHocCommandData.SpecificError parse(XmlPullParser parser, int initialDepth)  {
125            return new AdHocCommandData.SpecificError(AdHocCommand.SpecificErrorCondition.badAction);
126        }
127    }
128
129    public static class MalformedActionError extends ExtensionElementProvider<AdHocCommandData.SpecificError> {
130        @Override
131        public AdHocCommandData.SpecificError parse(XmlPullParser parser, int initialDepth)  {
132            return new AdHocCommandData.SpecificError(AdHocCommand.SpecificErrorCondition.malformedAction);
133        }
134    }
135
136    public static class BadLocaleError extends ExtensionElementProvider<AdHocCommandData.SpecificError> {
137        @Override
138        public AdHocCommandData.SpecificError parse(XmlPullParser parser, int initialDepth)  {
139            return new AdHocCommandData.SpecificError(AdHocCommand.SpecificErrorCondition.badLocale);
140        }
141    }
142
143    public static class BadPayloadError extends ExtensionElementProvider<AdHocCommandData.SpecificError> {
144        @Override
145        public AdHocCommandData.SpecificError parse(XmlPullParser parser, int initialDepth)  {
146            return new AdHocCommandData.SpecificError(AdHocCommand.SpecificErrorCondition.badPayload);
147        }
148    }
149
150    public static class BadSessionIDError extends ExtensionElementProvider<AdHocCommandData.SpecificError> {
151        @Override
152        public AdHocCommandData.SpecificError parse(XmlPullParser parser, int initialDepth)  {
153            return new AdHocCommandData.SpecificError(AdHocCommand.SpecificErrorCondition.badSessionid);
154        }
155    }
156
157    public static class SessionExpiredError extends ExtensionElementProvider<AdHocCommandData.SpecificError> {
158        @Override
159        public AdHocCommandData.SpecificError parse(XmlPullParser parser, int initialDepth)  {
160            return new AdHocCommandData.SpecificError(AdHocCommand.SpecificErrorCondition.sessionExpired);
161        }
162    }
163}