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; 023 024import org.jivesoftware.smackx.xevent.packet.MessageEvent; 025 026import org.xmlpull.v1.XmlPullParser; 027import org.xmlpull.v1.XmlPullParserException; 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 043 * @throws XmlPullParserException 044 */ 045 @Override 046 public MessageEvent parse(XmlPullParser parser, int initialDepth) 047 throws XmlPullParserException, IOException { 048 MessageEvent messageEvent = new MessageEvent(); 049 boolean done = false; 050 while (!done) { 051 int eventType = parser.next(); 052 if (eventType == XmlPullParser.START_TAG) { 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.END_TAG) { 064 if (parser.getName().equals("x")) { 065 done = true; 066 } 067 } 068 } 069 070 return messageEvent; 071 } 072 073}