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