1d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen/**
2d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * $RCSfile$
3d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * $Revision: 2407 $
4d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * $Date: 2004-11-02 15:37:00 -0800 (Tue, 02 Nov 2004) $
5d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen *
6d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * Copyright 2003-2007 Jive Software.
7d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen *
8d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
9d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * you may not use this file except in compliance with the License.
10d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * You may obtain a copy of the License at
11d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen *
12d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen *     http://www.apache.org/licenses/LICENSE-2.0
13d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen *
14d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * Unless required by applicable law or agreed to in writing, software
15d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * distributed under the License is distributed on an "AS IS" BASIS,
16d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * See the License for the specific language governing permissions and
18d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * limitations under the License.
19d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen */
20d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen
21d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chenpackage org.jivesoftware.smackx.packet;
22d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen
23d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chenimport org.jivesoftware.smackx.ChatState;
24d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chenimport org.jivesoftware.smack.packet.PacketExtension;
25d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chenimport org.jivesoftware.smack.provider.PacketExtensionProvider;
26d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chenimport org.xmlpull.v1.XmlPullParser;
27d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen
28d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen/**
29d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * Represents a chat state which is an extension to message packets which is used to indicate
30d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * the current status of a chat participant.
31d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen *
32d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * @author Alexander Wenckus
33d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * @see org.jivesoftware.smackx.ChatState
34d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen */
35d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chenpublic class ChatStateExtension implements PacketExtension {
36d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen
37d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen    private ChatState state;
38d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen
39d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen    /**
40d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen     * Default constructor. The argument provided is the state that the extension will represent.
41d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen     *
42d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen     * @param state the state that the extension represents.
43d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen     */
44d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen    public ChatStateExtension(ChatState state) {
45d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen        this.state = state;
46d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen    }
47d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen
48d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen    public String getElementName() {
49d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen        return state.name();
50d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen    }
51d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen
52d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen    public String getNamespace() {
53d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen        return "http://jabber.org/protocol/chatstates";
54d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen    }
55d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen
56d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen    public String toXML() {
57d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen        return "<" + getElementName() + " xmlns=\"" + getNamespace() + "\" />";
58d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen    }
59d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen
60d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen    public static class Provider implements PacketExtensionProvider {
61d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen
62d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen        public PacketExtension parseExtension(XmlPullParser parser) throws Exception {
63d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen            ChatState state;
64d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen            try {
65d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen                state = ChatState.valueOf(parser.getName());
66d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen            }
67d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen            catch (Exception ex) {
68d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen                state = ChatState.active;
69d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen            }
70d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen            return new ChatStateExtension(state);
71d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen        }
72d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen    }
73d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen}
74