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