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    @Override
046    public String getElementName() {
047        return "offline";
048    }
049
050    /**
051     * Returns the XML namespace of the extension sub-packet root element.
052     * According the specification the namespace is always "http://jabber.org/protocol/offline"
053     *
054     * @return the XML namespace of the stanza(/packet) extension.
055     */
056    @Override
057    public String getNamespace() {
058        return "http://jabber.org/protocol/offline";
059    }
060
061    /**
062     * Returns the stamp that uniquely identifies the offline message. This stamp may
063     * be used for deleting the offline message. The stamp may be of the form UTC timestamps
064     * but it is not required to have that format.
065     *
066     * @return the stamp that uniquely identifies the offline message.
067     */
068    public String getNode() {
069        return node;
070    }
071
072    /**
073     * Sets the stamp that uniquely identifies the offline message. This stamp may
074     * be used for deleting the offline message. The stamp may be of the form UTC timestamps
075     * but it is not required to have that format.
076     *
077     * @param node the stamp that uniquely identifies the offline message.
078     */
079    public void setNode(String node) {
080        this.node = node;
081    }
082
083    @Override
084    public String toXML() {
085        StringBuilder buf = new StringBuilder();
086        buf.append('<').append(getElementName()).append(" xmlns=\"").append(getNamespace()).append(
087            "\">");
088        if (getNode() != null)
089            buf.append("<item node=\"").append(getNode()).append("\"/>");
090        buf.append("</").append(getElementName()).append('>');
091        return buf.toString();
092    }
093
094    public static class Provider extends ExtensionElementProvider<OfflineMessageInfo> {
095
096        /**
097         * Parses a OfflineMessageInfo stanza(/packet) (extension sub-packet).
098         *
099         * @param parser the XML parser, positioned at the starting element of the extension.
100         * @return a PacketExtension.
101         * @throws IOException 
102         * @throws XmlPullParserException 
103         */
104        @Override
105        public OfflineMessageInfo parse(XmlPullParser parser,
106                        int initialDepth) throws XmlPullParserException,
107                        IOException {
108            OfflineMessageInfo info = new OfflineMessageInfo();
109            boolean done = false;
110            while (!done) {
111                int eventType = parser.next();
112                if (eventType == XmlPullParser.START_TAG) {
113                    if (parser.getName().equals("item"))
114                        info.setNode(parser.getAttributeValue("", "node"));
115                } else if (eventType == XmlPullParser.END_TAG) {
116                    if (parser.getName().equals("offline")) {
117                        done = true;
118                    }
119                }
120            }
121
122            return info;
123        }
124
125    }
126}