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