AdHocCommandDataProvider.java

  1. /**
  2.  *
  3.  * Copyright 2005-2007 Jive Software.
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.jivesoftware.smackx.commands.provider;

  18. import org.jivesoftware.smack.packet.XMPPError;
  19. import org.jivesoftware.smack.provider.IQProvider;
  20. import org.jivesoftware.smack.provider.ExtensionElementProvider;
  21. import org.jivesoftware.smack.util.PacketParserUtils;
  22. import org.jivesoftware.smackx.commands.AdHocCommand;
  23. import org.jivesoftware.smackx.commands.AdHocCommand.Action;
  24. import org.jivesoftware.smackx.commands.packet.AdHocCommandData;
  25. import org.jivesoftware.smackx.commands.AdHocCommandNote;
  26. import org.jivesoftware.smackx.xdata.packet.DataForm;
  27. import org.jivesoftware.smackx.xdata.provider.DataFormProvider;
  28. import org.xmlpull.v1.XmlPullParser;

  29. /**
  30.  * The AdHocCommandDataProvider parses AdHocCommandData packets.
  31.  *
  32.  * @author Gabriel Guardincerri
  33.  */
  34. public class AdHocCommandDataProvider extends IQProvider<AdHocCommandData> {

  35.     @Override
  36.     public AdHocCommandData parse(XmlPullParser parser, int initialDepth)
  37.                     throws Exception {
  38.         boolean done = false;
  39.         AdHocCommandData adHocCommandData = new AdHocCommandData();
  40.         DataFormProvider dataFormProvider = new DataFormProvider();

  41.         int eventType;
  42.         String elementName;
  43.         String namespace;
  44.         adHocCommandData.setSessionID(parser.getAttributeValue("", "sessionid"));
  45.         adHocCommandData.setNode(parser.getAttributeValue("", "node"));

  46.         // Status
  47.         String status = parser.getAttributeValue("", "status");
  48.         if (AdHocCommand.Status.executing.toString().equalsIgnoreCase(status)) {
  49.             adHocCommandData.setStatus(AdHocCommand.Status.executing);
  50.         }
  51.         else if (AdHocCommand.Status.completed.toString().equalsIgnoreCase(status)) {
  52.             adHocCommandData.setStatus(AdHocCommand.Status.completed);
  53.         }
  54.         else if (AdHocCommand.Status.canceled.toString().equalsIgnoreCase(status)) {
  55.             adHocCommandData.setStatus(AdHocCommand.Status.canceled);
  56.         }

  57.         // Action
  58.         String action = parser.getAttributeValue("", "action");
  59.         if (action != null) {
  60.             Action realAction = AdHocCommand.Action.valueOf(action);
  61.             if (realAction == null || realAction.equals(Action.unknown)) {
  62.                 adHocCommandData.setAction(Action.unknown);
  63.             }
  64.             else {
  65.                 adHocCommandData.setAction(realAction);
  66.             }
  67.         }
  68.         while (!done) {
  69.             eventType = parser.next();
  70.             elementName = parser.getName();
  71.             namespace = parser.getNamespace();
  72.             if (eventType == XmlPullParser.START_TAG) {
  73.                 if (parser.getName().equals("actions")) {
  74.                     String execute = parser.getAttributeValue("", "execute");
  75.                     if (execute != null) {
  76.                         adHocCommandData.setExecuteAction(AdHocCommand.Action.valueOf(execute));
  77.                     }
  78.                 }
  79.                 else if (parser.getName().equals("next")) {
  80.                     adHocCommandData.addAction(AdHocCommand.Action.next);
  81.                 }
  82.                 else if (parser.getName().equals("complete")) {
  83.                     adHocCommandData.addAction(AdHocCommand.Action.complete);
  84.                 }
  85.                 else if (parser.getName().equals("prev")) {
  86.                     adHocCommandData.addAction(AdHocCommand.Action.prev);
  87.                 }
  88.                 else if (elementName.equals("x") && namespace.equals("jabber:x:data")) {
  89.                     adHocCommandData.setForm((DataForm) dataFormProvider.parse(parser));
  90.                 }
  91.                 else if (parser.getName().equals("note")) {
  92.                     AdHocCommandNote.Type type = AdHocCommandNote.Type.valueOf(
  93.                             parser.getAttributeValue("", "type"));
  94.                     String value = parser.nextText();
  95.                     adHocCommandData.addNote(new AdHocCommandNote(type, value));
  96.                 }
  97.                 else if (parser.getName().equals("error")) {
  98.                     XMPPError error = PacketParserUtils.parseError(parser);
  99.                     adHocCommandData.setError(error);
  100.                 }
  101.             }
  102.             else if (eventType == XmlPullParser.END_TAG) {
  103.                 if (parser.getName().equals("command")) {
  104.                     done = true;
  105.                 }
  106.             }
  107.         }
  108.         return adHocCommandData;
  109.     }

  110.     public static class BadActionError extends ExtensionElementProvider<AdHocCommandData.SpecificError> {
  111.         @Override
  112.         public AdHocCommandData.SpecificError parse(XmlPullParser parser, int initialDepth)  {
  113.             return new AdHocCommandData.SpecificError(AdHocCommand.SpecificErrorCondition.badAction);
  114.         }
  115.     }

  116.     public static class MalformedActionError extends ExtensionElementProvider<AdHocCommandData.SpecificError> {
  117.         @Override
  118.         public AdHocCommandData.SpecificError parse(XmlPullParser parser, int initialDepth)  {
  119.             return new AdHocCommandData.SpecificError(AdHocCommand.SpecificErrorCondition.malformedAction);
  120.         }
  121.     }

  122.     public static class BadLocaleError extends ExtensionElementProvider<AdHocCommandData.SpecificError> {
  123.         @Override
  124.         public AdHocCommandData.SpecificError parse(XmlPullParser parser, int initialDepth)  {
  125.             return new AdHocCommandData.SpecificError(AdHocCommand.SpecificErrorCondition.badLocale);
  126.         }
  127.     }

  128.     public static class BadPayloadError extends ExtensionElementProvider<AdHocCommandData.SpecificError> {
  129.         @Override
  130.         public AdHocCommandData.SpecificError parse(XmlPullParser parser, int initialDepth)  {
  131.             return new AdHocCommandData.SpecificError(AdHocCommand.SpecificErrorCondition.badPayload);
  132.         }
  133.     }

  134.     public static class BadSessionIDError extends ExtensionElementProvider<AdHocCommandData.SpecificError> {
  135.         @Override
  136.         public AdHocCommandData.SpecificError parse(XmlPullParser parser, int initialDepth)  {
  137.             return new AdHocCommandData.SpecificError(AdHocCommand.SpecificErrorCondition.badSessionid);
  138.         }
  139.     }

  140.     public static class SessionExpiredError extends ExtensionElementProvider<AdHocCommandData.SpecificError> {
  141.         @Override
  142.         public AdHocCommandData.SpecificError parse(XmlPullParser parser, int initialDepth)  {
  143.             return new AdHocCommandData.SpecificError(AdHocCommand.SpecificErrorCondition.sessionExpired);
  144.         }
  145.     }
  146. }