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