RSMSetProvider.java

  1. /**
  2.  *
  3.  * Copyright © 2014 Florian Schmaus
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smackx.rsm.provider;

  18. import java.io.IOException;

  19. import org.jivesoftware.smack.provider.ExtensionElementProvider;
  20. import org.jivesoftware.smack.util.ParserUtils;
  21. import org.jivesoftware.smackx.rsm.packet.RSMSet;
  22. import org.xmlpull.v1.XmlPullParser;
  23. import org.xmlpull.v1.XmlPullParserException;

  24. public class RSMSetProvider extends ExtensionElementProvider<RSMSet> {

  25.     @Override
  26.     public RSMSet parse(XmlPullParser parser, int initialDepth)
  27.                     throws XmlPullParserException, IOException {
  28.         String after = null;
  29.         String before = null;
  30.         int count = -1;
  31.         int index = -1;
  32.         String last = null;
  33.         int max = -1;
  34.         String firstString = null;
  35.         int firstIndex = -1;

  36.         outerloop: while (true) {
  37.             int event = parser.next();
  38.             switch (event) {
  39.             case XmlPullParser.START_TAG:
  40.                 String name = parser.getName();
  41.                 switch (name) {
  42.                 case "after":
  43.                     after = parser.nextText();
  44.                     break;
  45.                 case "before":
  46.                     before = parser.nextText();
  47.                     break;
  48.                 case "count":
  49.                     count = ParserUtils.getIntegerFromNextText(parser);
  50.                     break;
  51.                 case "first":
  52.                     firstIndex = ParserUtils.getIntegerAttribute(parser,
  53.                                     "index", -1);
  54.                     firstString = parser.nextText();
  55.                     break;
  56.                 case "index":
  57.                     index = ParserUtils.getIntegerFromNextText(parser);
  58.                     break;
  59.                 case "last":
  60.                     last = parser.nextText();
  61.                     break;
  62.                 case "max":
  63.                     max = ParserUtils.getIntegerFromNextText(parser);
  64.                     break;
  65.                 }
  66.                 break;
  67.             case XmlPullParser.END_TAG:
  68.                 if (parser.getDepth() == initialDepth) {
  69.                     break outerloop;
  70.                 }
  71.             }
  72.         }
  73.         return new RSMSet(after, before, count, index, last, max, firstString,
  74.                         firstIndex);
  75.     }

  76. }