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