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