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 java.io.IOException;
021
022import org.jivesoftware.smack.packet.ExtensionElement;
023import org.jivesoftware.smack.provider.ExtensionElementProvider;
024import org.xmlpull.v1.XmlPullParser;
025import org.xmlpull.v1.XmlPullParserException;
026
027/**
028 * OfflineMessageInfo is an extension included in the retrieved offline messages requested by
029 * the {@link org.jivesoftware.smackx.offline.OfflineMessageManager}. This extension includes a stamp
030 * that uniquely identifies the offline message. This stamp may be used for deleting the offline
031 * message. The stamp may be of the form UTC timestamps but it is not required to have that format.
032 *
033 * @author Gaston Dombiak
034 */
035public class OfflineMessageInfo implements ExtensionElement {
036
037    private String node = null;
038
039    /**
040    * Returns the XML element name of the extension sub-packet root element.
041    * Always returns "offline"
042    *
043    * @return the XML element name of the stanza(/packet) extension.
044    */
045    public String getElementName() {
046        return "offline";
047    }
048
049    /**
050     * Returns the XML namespace of the extension sub-packet root element.
051     * According the specification the namespace is always "http://jabber.org/protocol/offline"
052     *
053     * @return the XML namespace of the stanza(/packet) extension.
054     */
055    public String getNamespace() {
056        return "http://jabber.org/protocol/offline";
057    }
058
059    /**
060     * Returns the stamp that uniquely identifies the offline message. This stamp may
061     * be used for deleting the offline message. The stamp may be of the form UTC timestamps
062     * but it is not required to have that format.
063     *
064     * @return the stamp that uniquely identifies the offline message.
065     */
066    public String getNode() {
067        return node;
068    }
069
070    /**
071     * Sets the stamp that uniquely identifies the offline message. This stamp may
072     * be used for deleting the offline message. The stamp may be of the form UTC timestamps
073     * but it is not required to have that format.
074     *
075     * @param node the stamp that uniquely identifies the offline message.
076     */
077    public void setNode(String node) {
078        this.node = node;
079    }
080
081    public String toXML() {
082        StringBuilder buf = new StringBuilder();
083        buf.append("<").append(getElementName()).append(" xmlns=\"").append(getNamespace()).append(
084            "\">");
085        if (getNode() != null)
086            buf.append("<item node=\"").append(getNode()).append("\"/>");
087        buf.append("</").append(getElementName()).append(">");
088        return buf.toString();
089    }
090
091    public static class Provider extends ExtensionElementProvider<OfflineMessageInfo> {
092
093        /**
094         * Parses a OfflineMessageInfo stanza(/packet) (extension sub-packet).
095         *
096         * @param parser the XML parser, positioned at the starting element of the extension.
097         * @return a PacketExtension.
098         * @throws IOException 
099         * @throws XmlPullParserException 
100         */
101        @Override
102        public OfflineMessageInfo parse(XmlPullParser parser,
103                        int initialDepth) throws XmlPullParserException,
104                        IOException {
105            OfflineMessageInfo info = new OfflineMessageInfo();
106            boolean done = false;
107            while (!done) {
108                int eventType = parser.next();
109                if (eventType == XmlPullParser.START_TAG) {
110                    if (parser.getName().equals("item"))
111                        info.setNode(parser.getAttributeValue("", "node"));
112                } else if (eventType == XmlPullParser.END_TAG) {
113                    if (parser.getName().equals("offline")) {
114                        done = true;
115                    }
116                }
117            }
118
119            return info;
120        }
121
122    }
123}