001/**
002 *
003 * Copyright 2016 Fernando Ramirez, 2020 Florian Schmaus
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.mam.provider;
018
019import java.io.IOException;
020
021import org.jivesoftware.smack.packet.Message;
022import org.jivesoftware.smack.packet.XmlEnvironment;
023import org.jivesoftware.smack.parsing.SmackParsingException;
024import org.jivesoftware.smack.provider.ExtensionElementProvider;
025import org.jivesoftware.smack.xml.XmlPullParser;
026import org.jivesoftware.smack.xml.XmlPullParserException;
027
028import org.jivesoftware.smackx.forward.packet.Forwarded;
029import org.jivesoftware.smackx.forward.provider.ForwardedProvider;
030import org.jivesoftware.smackx.mam.element.MamElements.MamResultExtension;
031
032/**
033 * MAM Result Provider class.
034 *
035 * @see <a href="http://xmpp.org/extensions/xep-0313.html">XEP-0313: Message
036 *      Archive Management</a>
037 * @author Fernando Ramirez
038 *
039 */
040public class MamResultProvider extends ExtensionElementProvider<MamResultExtension> {
041
042    @Override
043    public MamResultExtension parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException {
044        Forwarded<Message> forwarded = null;
045        String queryId = parser.getAttributeValue("", "queryid");
046        String id = parser.getAttributeValue("", "id");
047
048        outerloop: while (true) {
049            final XmlPullParser.Event eventType = parser.next();
050            final String name = parser.getName();
051            switch (eventType) {
052            case START_ELEMENT:
053                switch (name) {
054                case Forwarded.ELEMENT:
055                    forwarded = ForwardedProvider.parseForwardedMessage(parser, xmlEnvironment);
056                    break;
057                }
058                break;
059            case END_ELEMENT:
060                if (parser.getDepth() == initialDepth) {
061                    break outerloop;
062                }
063                break;
064            default:
065                // Catch all for incomplete switch (MissingCasesInEnumSwitch) statement.
066                break;
067            }
068        }
069
070        return new MamResultExtension(queryId, id, forwarded);
071    }
072
073}