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.xevent.provider;
019
020import java.io.IOException;
021
022import org.jivesoftware.smack.packet.XmlEnvironment;
023import org.jivesoftware.smack.provider.ExtensionElementProvider;
024import org.jivesoftware.smack.xml.XmlPullParser;
025import org.jivesoftware.smack.xml.XmlPullParserException;
026
027import org.jivesoftware.smackx.xevent.packet.MessageEvent;
028
029/**
030 *
031 * The MessageEventProvider parses Message Event packets.
032*
033 * @author Gaston Dombiak
034 */
035public class MessageEventProvider extends ExtensionElementProvider<MessageEvent> {
036
037    /**
038     * Parses a MessageEvent stanza (extension sub-packet).
039     *
040     * @param parser the XML parser, positioned at the starting element of the extension.
041     * @return a PacketExtension.
042     * @throws IOException if an I/O error occurred.
043     * @throws XmlPullParserException if an error in the XML parser occurred.
044     */
045    @Override
046    public MessageEvent parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment)
047                    throws XmlPullParserException, IOException {
048        MessageEvent messageEvent = new MessageEvent();
049        boolean done = false;
050        while (!done) {
051            XmlPullParser.Event eventType = parser.next();
052            if (eventType == XmlPullParser.Event.START_ELEMENT) {
053                if (parser.getName().equals("id"))
054                    messageEvent.setStanzaId(parser.nextText());
055                if (parser.getName().equals(MessageEvent.COMPOSING))
056                    messageEvent.setComposing(true);
057                if (parser.getName().equals(MessageEvent.DELIVERED))
058                    messageEvent.setDelivered(true);
059                if (parser.getName().equals(MessageEvent.DISPLAYED))
060                    messageEvent.setDisplayed(true);
061                if (parser.getName().equals(MessageEvent.OFFLINE))
062                    messageEvent.setOffline(true);
063            } else if (eventType == XmlPullParser.Event.END_ELEMENT) {
064                if (parser.getName().equals("x")) {
065                    done = true;
066                }
067            }
068        }
069
070        return messageEvent;
071    }
072
073}