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