001/** 002 * 003 * Copyright 2013-2014 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 java.util.logging.Logger; 020 021import org.jivesoftware.smack.SmackException; 022import org.jivesoftware.smack.packet.Message; 023import org.jivesoftware.smack.packet.Stanza; 024import org.jivesoftware.smack.provider.ExtensionElementProvider; 025import org.jivesoftware.smack.util.PacketParserUtils; 026 027import org.jivesoftware.smackx.delay.packet.DelayInformation; 028import org.jivesoftware.smackx.delay.provider.DelayInformationProvider; 029import org.jivesoftware.smackx.forward.packet.Forwarded; 030 031import org.xmlpull.v1.XmlPullParser; 032 033/** 034 * This class implements the {@link ExtensionElementProvider} to parse 035 * forwarded messages from a packet. It will return a {@link Forwarded} stanza extension. 036 * 037 * @author Georg Lukas 038 */ 039public class ForwardedProvider extends ExtensionElementProvider<Forwarded> { 040 041 public static final ForwardedProvider INSTANCE = new ForwardedProvider(); 042 043 private static final Logger LOGGER = Logger.getLogger(ForwardedProvider.class.getName()); 044 045 @Override 046 public Forwarded parse(XmlPullParser parser, int initialDepth) throws Exception { 047 DelayInformation di = null; 048 Stanza packet = null; 049 050 outerloop: while (true) { 051 int eventType = parser.next(); 052 switch (eventType) { 053 case XmlPullParser.START_TAG: 054 String name = parser.getName(); 055 String namespace = parser.getNamespace(); 056 switch (name) { 057 case DelayInformation.ELEMENT: 058 if (DelayInformation.NAMESPACE.equals(namespace)) { 059 di = DelayInformationProvider.INSTANCE.parse(parser, parser.getDepth()); 060 } else { 061 LOGGER.warning("Namespace '" + namespace + "' does not match expected namespace '" 062 + DelayInformation.NAMESPACE + "'"); 063 } 064 break; 065 case Message.ELEMENT: 066 packet = PacketParserUtils.parseMessage(parser); 067 break; 068 default: 069 LOGGER.warning("Unsupported forwarded packet type: " + name); 070 } 071 break; 072 case XmlPullParser.END_TAG: 073 if (parser.getDepth() == initialDepth) { 074 break outerloop; 075 } 076 break; 077 } 078 } 079 080 if (packet == null) 081 throw new SmackException("forwarded extension must contain a packet"); 082 return new Forwarded(di, packet); 083 } 084}