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