ParseStreamManagement.java

  1. /**
  2.  *
  3.  * Copyright © 2014-2018 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 java.util.ArrayList;
  20. import java.util.List;

  21. import org.jivesoftware.smack.packet.AbstractTextElement;
  22. import org.jivesoftware.smack.packet.StanzaError;
  23. import org.jivesoftware.smack.packet.StanzaErrorTextElement;
  24. import org.jivesoftware.smack.sm.packet.StreamManagement.AckAnswer;
  25. import org.jivesoftware.smack.sm.packet.StreamManagement.AckRequest;
  26. import org.jivesoftware.smack.sm.packet.StreamManagement.Enabled;
  27. import org.jivesoftware.smack.sm.packet.StreamManagement.Failed;
  28. import org.jivesoftware.smack.sm.packet.StreamManagement.Resumed;
  29. import org.jivesoftware.smack.util.ParserUtils;
  30. import org.jivesoftware.smack.xml.XmlPullParser;
  31. import org.jivesoftware.smack.xml.XmlPullParserException;

  32. public class ParseStreamManagement {

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

  43.     public static Failed failed(XmlPullParser parser) throws XmlPullParserException, IOException {
  44.         ParserUtils.assertAtStartTag(parser);
  45.         String name;
  46.         StanzaError.Condition condition = null;
  47.         List<StanzaErrorTextElement> textElements = new ArrayList<>(4);
  48.         outerloop:
  49.         while (true) {
  50.             XmlPullParser.Event event = parser.next();
  51.             switch (event) {
  52.             case START_ELEMENT:
  53.                 name = parser.getName();
  54.                 String namespace = parser.getNamespace();
  55.                 if (StanzaError.ERROR_CONDITION_AND_TEXT_NAMESPACE.equals(namespace)) {
  56.                     if (name.equals(AbstractTextElement.ELEMENT)) {
  57.                         String lang = ParserUtils.getXmlLang(parser);
  58.                         String text = parser.nextText();
  59.                         StanzaErrorTextElement stanzaErrorTextElement = new StanzaErrorTextElement(text, lang);
  60.                         textElements.add(stanzaErrorTextElement);
  61.                     } else {
  62.                         condition = StanzaError.Condition.fromString(name);
  63.                     }
  64.                 }
  65.                 break;
  66.             case END_ELEMENT:
  67.                 name = parser.getName();
  68.                 if (Failed.ELEMENT.equals(name)) {
  69.                     break outerloop;
  70.                 }
  71.                 break;
  72.             default:
  73.                 // Catch all for incomplete switch (MissingCasesInEnumSwitch) statement.
  74.                 break;
  75.             }
  76.         }
  77.         ParserUtils.assertAtEndTag(parser);
  78.         return new Failed(condition, textElements);
  79.     }

  80.     public static Resumed resumed(XmlPullParser parser) throws XmlPullParserException, IOException {
  81.         ParserUtils.assertAtStartTag(parser);
  82.         long h = ParserUtils.getLongAttribute(parser, "h");
  83.         String previd = parser.getAttributeValue("", "previd");
  84.         parser.next();
  85.         ParserUtils.assertAtEndTag(parser);
  86.         return new Resumed(h, previd);
  87.     }

  88.     public static AckAnswer ackAnswer(XmlPullParser parser) throws XmlPullParserException, IOException {
  89.         ParserUtils.assertAtStartTag(parser);
  90.         long h = ParserUtils.getLongAttribute(parser, "h");
  91.         parser.next();
  92.         ParserUtils.assertAtEndTag(parser);
  93.         return new AckAnswer(h);
  94.     }

  95.     public static AckRequest ackRequest(XmlPullParser parser) throws XmlPullParserException, IOException {
  96.         ParserUtils.assertAtStartTag(parser);
  97.         parser.next();
  98.         ParserUtils.assertAtEndTag(parser);
  99.         return AckRequest.INSTANCE;
  100.     }
  101. }