ChatStateExtension.java

  1. /**
  2.  *
  3.  * Copyright 2003-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.chatstates.packet;

  18. import org.jivesoftware.smackx.chatstates.ChatState;
  19. import org.jivesoftware.smack.packet.ExtensionElement;
  20. import org.jivesoftware.smack.provider.ExtensionElementProvider;
  21. import org.jivesoftware.smack.util.XmlStringBuilder;
  22. import org.xmlpull.v1.XmlPullParser;

  23. /**
  24.  * Represents a chat state which is an extension to message packets which is used to indicate
  25.  * the current status of a chat participant.
  26.  *
  27.  * @author Alexander Wenckus
  28.  * @see org.jivesoftware.smackx.chatstates.ChatState
  29.  */
  30. public class ChatStateExtension implements ExtensionElement {

  31.     public static final String NAMESPACE = "http://jabber.org/protocol/chatstates";

  32.     private final ChatState state;

  33.     /**
  34.      * Default constructor. The argument provided is the state that the extension will represent.
  35.      *
  36.      * @param state the state that the extension represents.
  37.      */
  38.     public ChatStateExtension(ChatState state) {
  39.         this.state = state;
  40.     }

  41.     @Override
  42.     public String getElementName() {
  43.         return state.name();
  44.     }

  45.     @Override
  46.     public String getNamespace() {
  47.         return NAMESPACE;
  48.     }

  49.     public ChatState getChatState() {
  50.         return state;
  51.     }

  52.     @Override
  53.     public XmlStringBuilder toXML() {
  54.         XmlStringBuilder xml = new XmlStringBuilder(this);
  55.         xml.closeEmptyElement();
  56.         return xml;
  57.     }

  58.     public static class Provider extends ExtensionElementProvider<ChatStateExtension> {

  59.         @Override
  60.         public ChatStateExtension parse(XmlPullParser parser, int initialDepth) {
  61.             ChatState state;
  62.             try {
  63.                 state = ChatState.valueOf(parser.getName());
  64.             }
  65.             catch (Exception ex) {
  66.                 state = ChatState.active;
  67.             }
  68.             return new ChatStateExtension(state);
  69.         }
  70.     }
  71. }