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