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.carbons.provider; 018 019import org.jivesoftware.smack.SmackException; 020import org.jivesoftware.smack.provider.ExtensionElementProvider; 021import org.jivesoftware.smackx.carbons.packet.CarbonExtension; 022import org.jivesoftware.smackx.carbons.packet.CarbonExtension.Direction; 023import org.jivesoftware.smackx.forward.packet.Forwarded; 024import org.jivesoftware.smackx.forward.provider.ForwardedProvider; 025import org.xmlpull.v1.XmlPullParser; 026 027/** 028 * This class implements the {@link ExtensionElementProvider} to parse 029 * carbon copied messages from a packet. It will return a {@link CarbonExtension} stanza(/packet) extension. 030 * 031 * @author Georg Lukas 032 * 033 */ 034public class CarbonManagerProvider extends ExtensionElementProvider<CarbonExtension> { 035 036 private static final ForwardedProvider FORWARDED_PROVIDER = new ForwardedProvider(); 037 038 @Override 039 public CarbonExtension parse(XmlPullParser parser, int initialDepth) 040 throws Exception { 041 Direction dir = Direction.valueOf(parser.getName()); 042 Forwarded fwd = null; 043 044 boolean done = false; 045 while (!done) { 046 int eventType = parser.next(); 047 if (eventType == XmlPullParser.START_TAG && parser.getName().equals("forwarded")) { 048 fwd = FORWARDED_PROVIDER.parse(parser); 049 } 050 else if (eventType == XmlPullParser.END_TAG && dir == Direction.valueOf(parser.getName())) 051 done = true; 052 } 053 if (fwd == null) 054 throw new SmackException("sent/received must contain exactly one <forwarded> tag"); 055 return new CarbonExtension(dir, fwd); 056 } 057}