1 /** 2 * $RCSfile$ 3 * $Revision$ 4 * $Date$ 5 * 6 * Copyright 2003-2007 Jive Software. 7 * 8 * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); 9 * you may not use this file except in compliance with the License. 10 * You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, software 15 * distributed under the License is distributed on an "AS IS" BASIS, 16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 * See the License for the specific language governing permissions and 18 * limitations under the License. 19 */ 20 21 package org.jivesoftware.smackx.provider; 22 23 import org.jivesoftware.smack.packet.PacketExtension; 24 import org.jivesoftware.smack.provider.PacketExtensionProvider; 25 import org.jivesoftware.smackx.packet.MessageEvent; 26 import org.xmlpull.v1.XmlPullParser; 27 28 /** 29 * 30 * The MessageEventProvider parses Message Event packets. 31 * 32 * @author Gaston Dombiak 33 */ 34 public class MessageEventProvider implements PacketExtensionProvider { 35 36 /** 37 * Creates a new MessageEventProvider. 38 * ProviderManager requires that every PacketExtensionProvider has a public, no-argument constructor 39 */ 40 public MessageEventProvider() { 41 } 42 43 /** 44 * Parses a MessageEvent packet (extension sub-packet). 45 * 46 * @param parser the XML parser, positioned at the starting element of the extension. 47 * @return a PacketExtension. 48 * @throws Exception if a parsing error occurs. 49 */ 50 public PacketExtension parseExtension(XmlPullParser parser) 51 throws Exception { 52 MessageEvent messageEvent = new MessageEvent(); 53 boolean done = false; 54 while (!done) { 55 int eventType = parser.next(); 56 if (eventType == XmlPullParser.START_TAG) { 57 if (parser.getName().equals("id")) 58 messageEvent.setPacketID(parser.nextText()); 59 if (parser.getName().equals(MessageEvent.COMPOSING)) 60 messageEvent.setComposing(true); 61 if (parser.getName().equals(MessageEvent.DELIVERED)) 62 messageEvent.setDelivered(true); 63 if (parser.getName().equals(MessageEvent.DISPLAYED)) 64 messageEvent.setDisplayed(true); 65 if (parser.getName().equals(MessageEvent.OFFLINE)) 66 messageEvent.setOffline(true); 67 } else if (eventType == XmlPullParser.END_TAG) { 68 if (parser.getName().equals("x")) { 69 done = true; 70 } 71 } 72 } 73 74 return messageEvent; 75 } 76 77 } 78