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 java.io.IOException;

  19. import org.jivesoftware.smack.packet.IqData;
  20. import org.jivesoftware.smack.packet.StanzaError;
  21. import org.jivesoftware.smack.packet.XmlEnvironment;
  22. import org.jivesoftware.smack.parsing.SmackParsingException;
  23. import org.jivesoftware.smack.provider.ExtensionElementProvider;
  24. import org.jivesoftware.smack.provider.IqProvider;
  25. import org.jivesoftware.smack.util.PacketParserUtils;
  26. import org.jivesoftware.smack.xml.XmlPullParser;
  27. import org.jivesoftware.smack.xml.XmlPullParserException;

  28. import org.jivesoftware.smackx.commands.AdHocCommandNote;
  29. import org.jivesoftware.smackx.commands.SpecificErrorCondition;
  30. import org.jivesoftware.smackx.commands.packet.AdHocCommandData;
  31. import org.jivesoftware.smackx.commands.packet.AdHocCommandData.Action;
  32. import org.jivesoftware.smackx.commands.packet.AdHocCommandData.AllowedAction;
  33. import org.jivesoftware.smackx.commands.packet.AdHocCommandDataBuilder;
  34. import org.jivesoftware.smackx.xdata.packet.DataForm;
  35. import org.jivesoftware.smackx.xdata.provider.DataFormProvider;

  36. /**
  37.  * The AdHocCommandDataProvider parses AdHocCommandData packets.
  38.  *
  39.  * @author Gabriel Guardincerri
  40.  */
  41. public class AdHocCommandDataProvider extends IqProvider<AdHocCommandData> {

  42.     @Override
  43.     public AdHocCommandData parse(XmlPullParser parser, int initialDepth, IqData iqData, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException {
  44.         String commandNode = parser.getAttributeValue("node");
  45.         AdHocCommandDataBuilder builder = AdHocCommandData.builder(commandNode, iqData);
  46.         DataFormProvider dataFormProvider = new DataFormProvider();

  47.         String sessionId = parser.getAttributeValue("sessionid");
  48.         builder.setSessionId(sessionId);

  49.         // Status
  50.         String status = parser.getAttributeValue("", "status");
  51.         if (AdHocCommandData.Status.executing.toString().equalsIgnoreCase(status)) {
  52.             builder.setStatus(AdHocCommandData.Status.executing);
  53.         }
  54.         else if (AdHocCommandData.Status.completed.toString().equalsIgnoreCase(status)) {
  55.             builder.setStatus(AdHocCommandData.Status.completed);
  56.         }
  57.         else if (AdHocCommandData.Status.canceled.toString().equalsIgnoreCase(status)) {
  58.             builder.setStatus(AdHocCommandData.Status.canceled);
  59.         }

  60.         // Action
  61.         String action = parser.getAttributeValue("", "action");
  62.         if (action != null) {
  63.             Action realAction = Action.valueOf(action);
  64.             if (realAction == null) {
  65.                 throw new SmackParsingException("Invalid value for action attribute: " + action);
  66.             }

  67.             builder.setAction(realAction);
  68.         }

  69.         // TODO: Improve parsing below. Currently, the next actions like <prev/> are not checked for the correct position.
  70.         outerloop:
  71.         while (true) {
  72.             String elementName;
  73.             XmlPullParser.Event event = parser.next();
  74.             String namespace = parser.getNamespace();
  75.             switch (event) {
  76.             case START_ELEMENT:
  77.                 elementName = parser.getName();
  78.                 switch (elementName) {
  79.                 case "actions":
  80.                     String execute = parser.getAttributeValue("execute");
  81.                     if (execute != null) {
  82.                         builder.setExecuteAction(AllowedAction.valueOf(execute));
  83.                     }
  84.                     break;
  85.                 case "next":
  86.                     builder.addAction(AllowedAction.next);
  87.                     break;
  88.                 case "complete":
  89.                     builder.addAction(AllowedAction.complete);
  90.                     break;
  91.                 case "prev":
  92.                     builder.addAction(AllowedAction.prev);
  93.                     break;
  94.                 case "x":
  95.                     if (namespace.equals("jabber:x:data")) {
  96.                         DataForm form = dataFormProvider.parse(parser);
  97.                         builder.setForm(form);
  98.                     }
  99.                     break;
  100.                 case "note":
  101.                     String typeString = parser.getAttributeValue("type");
  102.                     AdHocCommandNote.Type type;
  103.                     if (typeString != null) {
  104.                         type = AdHocCommandNote.Type.valueOf(typeString);
  105.                     } else {
  106.                         // Type is optional and 'info' if not present.
  107.                         type = AdHocCommandNote.Type.info;
  108.                     }
  109.                     String value = parser.nextText();
  110.                     builder.addNote(new AdHocCommandNote(type, value));
  111.                     break;
  112.                 case "error":
  113.                     StanzaError error = PacketParserUtils.parseError(parser);
  114.                     builder.setError(error);
  115.                     break;
  116.                 }
  117.                 break;
  118.             case END_ELEMENT:
  119.                 if (parser.getName().equals("command")) {
  120.                     break outerloop;
  121.                 }
  122.                 break;
  123.             default:
  124.                 // Catch all for incomplete switch (MissingCasesInEnumSwitch) statement.
  125.                 break;
  126.             }
  127.         }

  128.         return builder.build();
  129.     }

  130.     public static class BadActionError extends ExtensionElementProvider<AdHocCommandData.SpecificError> {
  131.         @Override
  132.         public AdHocCommandData.SpecificError parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment)  {
  133.             return new AdHocCommandData.SpecificError(SpecificErrorCondition.badAction);
  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(SpecificErrorCondition.malformedAction);
  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(SpecificErrorCondition.badLocale);
  146.         }
  147.     }

  148.     public static class BadPayloadError extends ExtensionElementProvider<AdHocCommandData.SpecificError> {
  149.         @Override
  150.         public AdHocCommandData.SpecificError parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment)  {
  151.             return new AdHocCommandData.SpecificError(SpecificErrorCondition.badPayload);
  152.         }
  153.     }

  154.     public static class BadSessionIDError extends ExtensionElementProvider<AdHocCommandData.SpecificError> {
  155.         @Override
  156.         public AdHocCommandData.SpecificError parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment)  {
  157.             return new AdHocCommandData.SpecificError(SpecificErrorCondition.badSessionid);
  158.         }
  159.     }

  160.     public static class SessionExpiredError extends ExtensionElementProvider<AdHocCommandData.SpecificError> {
  161.         @Override
  162.         public AdHocCommandData.SpecificError parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment)  {
  163.             return new AdHocCommandData.SpecificError(SpecificErrorCondition.sessionExpired);
  164.         }
  165.     }
  166. }