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