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