1/**
2 * $RCSfile$
3 * $Revision$
4 * $Date$
5 *
6 * Copyright 2003-2007 Jive Software.
7 *
8 * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 *     http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20
21package org.jivesoftware.smackx.packet;
22
23import org.jivesoftware.smack.packet.PacketExtension;
24import org.jivesoftware.smack.provider.PacketExtensionProvider;
25import org.xmlpull.v1.XmlPullParser;
26
27/**
28 * OfflineMessageInfo is an extension included in the retrieved offline messages requested by
29 * the {@link org.jivesoftware.smackx.OfflineMessageManager}. This extension includes a stamp
30 * that uniquely identifies the offline message. This stamp may be used for deleting the offline
31 * message. The stamp may be of the form UTC timestamps but it is not required to have that format.
32 *
33 * @author Gaston Dombiak
34 */
35public class OfflineMessageInfo implements PacketExtension {
36
37    private String node = null;
38
39    /**
40    * Returns the XML element name of the extension sub-packet root element.
41    * Always returns "offline"
42    *
43    * @return the XML element name of the packet extension.
44    */
45    public String getElementName() {
46        return "offline";
47    }
48
49    /**
50     * Returns the XML namespace of the extension sub-packet root element.
51     * According the specification the namespace is always "http://jabber.org/protocol/offline"
52     *
53     * @return the XML namespace of the packet extension.
54     */
55    public String getNamespace() {
56        return "http://jabber.org/protocol/offline";
57    }
58
59    /**
60     * Returns the stamp that uniquely identifies the offline message. This stamp may
61     * be used for deleting the offline message. The stamp may be of the form UTC timestamps
62     * but it is not required to have that format.
63     *
64     * @return the stamp that uniquely identifies the offline message.
65     */
66    public String getNode() {
67        return node;
68    }
69
70    /**
71     * Sets the stamp that uniquely identifies the offline message. This stamp may
72     * be used for deleting the offline message. The stamp may be of the form UTC timestamps
73     * but it is not required to have that format.
74     *
75     * @param node the stamp that uniquely identifies the offline message.
76     */
77    public void setNode(String node) {
78        this.node = node;
79    }
80
81    public String toXML() {
82        StringBuilder buf = new StringBuilder();
83        buf.append("<").append(getElementName()).append(" xmlns=\"").append(getNamespace()).append(
84            "\">");
85        if (getNode() != null)
86            buf.append("<item node=\"").append(getNode()).append("\"/>");
87        buf.append("</").append(getElementName()).append(">");
88        return buf.toString();
89    }
90
91    public static class Provider implements PacketExtensionProvider {
92
93        /**
94         * Creates a new Provider.
95         * ProviderManager requires that every PacketExtensionProvider has a public,
96         * no-argument constructor
97         */
98        public Provider() {
99        }
100
101        /**
102         * Parses a OfflineMessageInfo packet (extension sub-packet).
103         *
104         * @param parser the XML parser, positioned at the starting element of the extension.
105         * @return a PacketExtension.
106         * @throws Exception if a parsing error occurs.
107         */
108        public PacketExtension parseExtension(XmlPullParser parser)
109            throws Exception {
110            OfflineMessageInfo info = new OfflineMessageInfo();
111            boolean done = false;
112            while (!done) {
113                int eventType = parser.next();
114                if (eventType == XmlPullParser.START_TAG) {
115                    if (parser.getName().equals("item"))
116                        info.setNode(parser.getAttributeValue("", "node"));
117                } else if (eventType == XmlPullParser.END_TAG) {
118                    if (parser.getName().equals("offline")) {
119                        done = true;
120                    }
121                }
122            }
123
124            return info;
125        }
126
127    }
128}
129