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