001/** 002 * 003 * Copyright 2013 Georg Lukas 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 */ 017package org.jivesoftware.smackx.forward.provider; 018 019import org.jivesoftware.smack.packet.Packet; 020import org.jivesoftware.smack.packet.PacketExtension; 021import org.jivesoftware.smack.provider.PacketExtensionProvider; 022import org.jivesoftware.smack.util.PacketParserUtils; 023import org.jivesoftware.smackx.delay.packet.DelayInfo; 024import org.jivesoftware.smackx.forward.Forwarded; 025import org.xmlpull.v1.XmlPullParser; 026 027/** 028 * This class implements the {@link PacketExtensionProvider} to parse 029 * forwarded messages from a packet. It will return a {@link Forwarded} packet extension. 030 * 031 * @author Georg Lukas 032 */ 033public class ForwardedProvider implements PacketExtensionProvider { 034 public PacketExtension parseExtension(XmlPullParser parser) throws Exception { 035 DelayInfo di = null; 036 Packet packet = null; 037 038 boolean done = false; 039 while (!done) { 040 int eventType = parser.next(); 041 if (eventType == XmlPullParser.START_TAG) { 042 if (parser.getName().equals("delay")) 043 di = (DelayInfo)PacketParserUtils.parsePacketExtension(parser.getName(), parser.getNamespace(), parser); 044 else if (parser.getName().equals("message")) 045 packet = PacketParserUtils.parseMessage(parser); 046 else throw new Exception("Unsupported forwarded packet type: " + parser.getName()); 047 } 048 else if (eventType == XmlPullParser.END_TAG && parser.getName().equals(Forwarded.ELEMENT_NAME)) 049 done = true; 050 } 051 if (packet == null) 052 throw new Exception("forwarded extension must contain a packet"); 053 return new Forwarded(di, packet); 054 } 055}