001/**
002 *
003 * Copyright 2003-2007 Jive Software.
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.jivesoftware.smackx.offline.packet;
019
020import org.jivesoftware.smack.packet.PacketExtension;
021import org.jivesoftware.smack.provider.PacketExtensionProvider;
022import org.xmlpull.v1.XmlPullParser;
023
024/**
025 * OfflineMessageInfo is an extension included in the retrieved offline messages requested by
026 * the {@link org.jivesoftware.smackx.offline.OfflineMessageManager}. This extension includes a stamp
027 * that uniquely identifies the offline message. This stamp may be used for deleting the offline
028 * message. The stamp may be of the form UTC timestamps but it is not required to have that format.
029 *
030 * @author Gaston Dombiak
031 */
032public class OfflineMessageInfo implements PacketExtension {
033
034    private String node = null;
035
036    /**
037    * Returns the XML element name of the extension sub-packet root element.
038    * Always returns "offline"
039    *
040    * @return the XML element name of the packet extension.
041    */
042    public String getElementName() {
043        return "offline";
044    }
045
046    /**
047     * Returns the XML namespace of the extension sub-packet root element.
048     * According the specification the namespace is always "http://jabber.org/protocol/offline"
049     *
050     * @return the XML namespace of the packet extension.
051     */
052    public String getNamespace() {
053        return "http://jabber.org/protocol/offline";
054    }
055
056    /**
057     * Returns the stamp that uniquely identifies the offline message. This stamp may
058     * be used for deleting the offline message. The stamp may be of the form UTC timestamps
059     * but it is not required to have that format.
060     *
061     * @return the stamp that uniquely identifies the offline message.
062     */
063    public String getNode() {
064        return node;
065    }
066
067    /**
068     * Sets the stamp that uniquely identifies the offline message. This stamp may
069     * be used for deleting the offline message. The stamp may be of the form UTC timestamps
070     * but it is not required to have that format.
071     *
072     * @param node the stamp that uniquely identifies the offline message.
073     */
074    public void setNode(String node) {
075        this.node = node;
076    }
077
078    public String toXML() {
079        StringBuilder buf = new StringBuilder();
080        buf.append("<").append(getElementName()).append(" xmlns=\"").append(getNamespace()).append(
081            "\">");
082        if (getNode() != null)
083            buf.append("<item node=\"").append(getNode()).append("\"/>");
084        buf.append("</").append(getElementName()).append(">");
085        return buf.toString();
086    }
087
088    public static class Provider implements PacketExtensionProvider {
089
090        /**
091         * Creates a new Provider.
092         * ProviderManager requires that every PacketExtensionProvider has a public,
093         * no-argument constructor
094         */
095        public Provider() {
096        }
097
098        /**
099         * Parses a OfflineMessageInfo packet (extension sub-packet).
100         *
101         * @param parser the XML parser, positioned at the starting element of the extension.
102         * @return a PacketExtension.
103         * @throws Exception if a parsing error occurs.
104         */
105        public PacketExtension parseExtension(XmlPullParser parser)
106            throws Exception {
107            OfflineMessageInfo info = new OfflineMessageInfo();
108            boolean done = false;
109            while (!done) {
110                int eventType = parser.next();
111                if (eventType == XmlPullParser.START_TAG) {
112                    if (parser.getName().equals("item"))
113                        info.setNode(parser.getAttributeValue("", "node"));
114                } else if (eventType == XmlPullParser.END_TAG) {
115                    if (parser.getName().equals("offline")) {
116                        done = true;
117                    }
118                }
119            }
120
121            return info;
122        }
123
124    }
125}