ParseStreamManagement.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.smack.sm.provider;

  18. import java.io.IOException;

  19. import org.jivesoftware.smack.packet.XMPPError;
  20. import org.jivesoftware.smack.sm.packet.StreamManagement.AckAnswer;
  21. import org.jivesoftware.smack.sm.packet.StreamManagement.AckRequest;
  22. import org.jivesoftware.smack.sm.packet.StreamManagement.Enabled;
  23. import org.jivesoftware.smack.sm.packet.StreamManagement.Failed;
  24. import org.jivesoftware.smack.sm.packet.StreamManagement.Resumed;
  25. import org.jivesoftware.smack.util.ParserUtils;
  26. import org.xmlpull.v1.XmlPullParser;
  27. import org.xmlpull.v1.XmlPullParserException;

  28. public class ParseStreamManagement {

  29.     public static Enabled enabled(XmlPullParser parser) throws XmlPullParserException, IOException {
  30.         ParserUtils.assertAtStartTag(parser);
  31.         boolean resume = ParserUtils.getBooleanAttribute(parser, "resume", false);
  32.         String id = parser.getAttributeValue("", "id");
  33.         String location = parser.getAttributeValue("", "location");
  34.         int max = ParserUtils.getIntegerAttribute(parser, "max", -1);
  35.         parser.next();
  36.         ParserUtils.assertAtEndTag(parser);
  37.         return new Enabled(id, resume, location, max);
  38.     }

  39.     public static Failed failed(XmlPullParser parser) throws XmlPullParserException, IOException {
  40.         ParserUtils.assertAtStartTag(parser);
  41.         String name;
  42.         XMPPError.Condition condition = null;
  43.         outerloop:
  44.         while(true) {
  45.             int event = parser.next();
  46.             switch (event) {
  47.             case XmlPullParser.START_TAG:
  48.                 name = parser.getName();
  49.                 String namespace = parser.getNamespace();
  50.                 if (XMPPError.NAMESPACE.equals(namespace)) {
  51.                     condition = XMPPError.Condition.fromString(name);
  52.                 }
  53.                 break;
  54.             case XmlPullParser.END_TAG:
  55.                 name = parser.getName();
  56.                 if (Failed.ELEMENT.equals(name)) {
  57.                     break outerloop;
  58.                 }
  59.                 break;
  60.             }
  61.         }
  62.         ParserUtils.assertAtEndTag(parser);
  63.         return new Failed(condition);
  64.     }

  65.     public static Resumed resumed(XmlPullParser parser) throws XmlPullParserException, IOException {
  66.         ParserUtils.assertAtStartTag(parser);
  67.         long h = ParserUtils.getLongAttribute(parser, "h");
  68.         String previd = parser.getAttributeValue("", "previd");
  69.         parser.next();
  70.         ParserUtils.assertAtEndTag(parser);
  71.         return new Resumed(h, previd);
  72.     }

  73.     public static AckAnswer ackAnswer(XmlPullParser parser) throws XmlPullParserException, IOException {
  74.         ParserUtils.assertAtStartTag(parser);
  75.         long h = ParserUtils.getLongAttribute(parser, "h");
  76.         parser.next();
  77.         ParserUtils.assertAtEndTag(parser);
  78.         return new AckAnswer(h);
  79.     }

  80.     public static AckRequest ackRequest(XmlPullParser parser) throws XmlPullParserException, IOException {
  81.         ParserUtils.assertAtStartTag(parser);
  82.         parser.next();
  83.         ParserUtils.assertAtEndTag(parser);
  84.         return AckRequest.INSTANCE;
  85.     }
  86. }