2014-02-17 23:58:40 +01:00
|
|
|
/**
|
|
|
|
*
|
2014-07-16 11:46:30 +02:00
|
|
|
* Copyright 2013-2014 Georg Lukas
|
2014-02-17 23:58:40 +01:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
package org.jivesoftware.smackx.forward.provider;
|
|
|
|
|
|
|
|
import org.jivesoftware.smack.packet.Packet;
|
|
|
|
import org.jivesoftware.smack.packet.PacketExtension;
|
|
|
|
import org.jivesoftware.smack.provider.PacketExtensionProvider;
|
|
|
|
import org.jivesoftware.smack.util.PacketParserUtils;
|
2014-06-30 13:36:49 +02:00
|
|
|
import org.jivesoftware.smackx.delay.packet.DelayInformation;
|
2014-02-17 23:58:40 +01:00
|
|
|
import org.jivesoftware.smackx.forward.Forwarded;
|
|
|
|
import org.xmlpull.v1.XmlPullParser;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class implements the {@link PacketExtensionProvider} to parse
|
|
|
|
* forwarded messages from a packet. It will return a {@link Forwarded} packet extension.
|
|
|
|
*
|
|
|
|
* @author Georg Lukas
|
|
|
|
*/
|
|
|
|
public class ForwardedProvider implements PacketExtensionProvider {
|
|
|
|
public PacketExtension parseExtension(XmlPullParser parser) throws Exception {
|
2014-06-30 13:36:49 +02:00
|
|
|
DelayInformation di = null;
|
2014-02-17 23:58:40 +01:00
|
|
|
Packet packet = null;
|
|
|
|
|
|
|
|
boolean done = false;
|
|
|
|
while (!done) {
|
|
|
|
int eventType = parser.next();
|
|
|
|
if (eventType == XmlPullParser.START_TAG) {
|
|
|
|
if (parser.getName().equals("delay"))
|
2014-06-30 13:36:49 +02:00
|
|
|
di = (DelayInformation)PacketParserUtils.parsePacketExtension(parser.getName(), parser.getNamespace(), parser);
|
2014-02-17 23:58:40 +01:00
|
|
|
else if (parser.getName().equals("message"))
|
|
|
|
packet = PacketParserUtils.parseMessage(parser);
|
|
|
|
else throw new Exception("Unsupported forwarded packet type: " + parser.getName());
|
|
|
|
}
|
2014-07-16 11:46:30 +02:00
|
|
|
else if (eventType == XmlPullParser.END_TAG && parser.getName().equals(Forwarded.ELEMENT))
|
2014-02-17 23:58:40 +01:00
|
|
|
done = true;
|
|
|
|
}
|
|
|
|
if (packet == null)
|
|
|
|
throw new Exception("forwarded extension must contain a packet");
|
|
|
|
return new Forwarded(di, packet);
|
|
|
|
}
|
|
|
|
}
|