001/*
002 *
003 * Copyright © 2014-2019 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.rsm.provider;
018
019import java.io.IOException;
020
021import org.jivesoftware.smack.packet.XmlEnvironment;
022import org.jivesoftware.smack.provider.ExtensionElementProvider;
023import org.jivesoftware.smack.util.ParserUtils;
024import org.jivesoftware.smack.xml.XmlPullParser;
025import org.jivesoftware.smack.xml.XmlPullParserException;
026
027import org.jivesoftware.smackx.rsm.packet.RSMSet;
028
029import org.jxmpp.JxmppContext;
030
031public class RSMSetProvider extends ExtensionElementProvider<RSMSet> {
032
033    public static final RSMSetProvider INSTANCE = new RSMSetProvider();
034
035    @Override
036    public RSMSet parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment, JxmppContext jxmppContext)
037                    throws XmlPullParserException, IOException {
038        String after = null;
039        String before = null;
040        int count = -1;
041        int index = -1;
042        String last = null;
043        int max = -1;
044        String firstString = null;
045        int firstIndex = -1;
046
047        outerloop: while (true) {
048            XmlPullParser.Event event = parser.next();
049            switch (event) {
050            case START_ELEMENT:
051                String name = parser.getName();
052                switch (name) {
053                case "after":
054                    after = parser.nextText();
055                    break;
056                case "before":
057                    before = parser.nextText();
058                    break;
059                case "count":
060                    count = ParserUtils.getIntegerFromNextText(parser);
061                    break;
062                case "first":
063                    firstIndex = ParserUtils.getIntegerAttribute(parser,
064                                    "index", -1);
065                    firstString = parser.nextText();
066                    break;
067                case "index":
068                    index = ParserUtils.getIntegerFromNextText(parser);
069                    break;
070                case "last":
071                    last = parser.nextText();
072                    break;
073                case "max":
074                    max = ParserUtils.getIntegerFromNextText(parser);
075                    break;
076                }
077                break;
078            case END_ELEMENT:
079                if (parser.getDepth() == initialDepth) {
080                    break outerloop;
081                }
082                break;
083            default:
084                // Catch all for incomplete switch (MissingCasesInEnumSwitch) statement.
085                break;
086            }
087        }
088        return new RSMSet(after, before, count, index, last, max, firstString,
089                        firstIndex);
090    }
091
092}