001/** 002 * 003 * Copyright © 2014-2018 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.smack.sm.provider; 018 019import java.io.IOException; 020import java.util.ArrayList; 021import java.util.List; 022 023import org.jivesoftware.smack.packet.AbstractTextElement; 024import org.jivesoftware.smack.packet.StanzaError; 025import org.jivesoftware.smack.packet.StanzaErrorTextElement; 026import org.jivesoftware.smack.sm.packet.StreamManagement.AckAnswer; 027import org.jivesoftware.smack.sm.packet.StreamManagement.AckRequest; 028import org.jivesoftware.smack.sm.packet.StreamManagement.Enabled; 029import org.jivesoftware.smack.sm.packet.StreamManagement.Failed; 030import org.jivesoftware.smack.sm.packet.StreamManagement.Resumed; 031import org.jivesoftware.smack.util.ParserUtils; 032import org.jivesoftware.smack.xml.XmlPullParser; 033import org.jivesoftware.smack.xml.XmlPullParserException; 034 035public class ParseStreamManagement { 036 037 public static Enabled enabled(XmlPullParser parser) throws XmlPullParserException, IOException { 038 ParserUtils.assertAtStartTag(parser); 039 boolean resume = ParserUtils.getBooleanAttribute(parser, "resume", false); 040 String id = parser.getAttributeValue("", "id"); 041 String location = parser.getAttributeValue("", "location"); 042 int max = ParserUtils.getIntegerAttribute(parser, "max", -1); 043 parser.next(); 044 ParserUtils.assertAtEndTag(parser); 045 return new Enabled(id, resume, location, max); 046 } 047 048 public static Failed failed(XmlPullParser parser) throws XmlPullParserException, IOException { 049 ParserUtils.assertAtStartTag(parser); 050 String name; 051 StanzaError.Condition condition = null; 052 List<StanzaErrorTextElement> textElements = new ArrayList<>(4); 053 outerloop: 054 while (true) { 055 XmlPullParser.Event event = parser.next(); 056 switch (event) { 057 case START_ELEMENT: 058 name = parser.getName(); 059 String namespace = parser.getNamespace(); 060 if (StanzaError.ERROR_CONDITION_AND_TEXT_NAMESPACE.equals(namespace)) { 061 if (name.equals(AbstractTextElement.ELEMENT)) { 062 String lang = ParserUtils.getXmlLang(parser); 063 String text = parser.nextText(); 064 StanzaErrorTextElement stanzaErrorTextElement = new StanzaErrorTextElement(text, lang); 065 textElements.add(stanzaErrorTextElement); 066 } else { 067 condition = StanzaError.Condition.fromString(name); 068 } 069 } 070 break; 071 case END_ELEMENT: 072 name = parser.getName(); 073 if (Failed.ELEMENT.equals(name)) { 074 break outerloop; 075 } 076 break; 077 default: 078 // Catch all for incomplete switch (MissingCasesInEnumSwitch) statement. 079 break; 080 } 081 } 082 ParserUtils.assertAtEndTag(parser); 083 return new Failed(condition, textElements); 084 } 085 086 public static Resumed resumed(XmlPullParser parser) throws XmlPullParserException, IOException { 087 ParserUtils.assertAtStartTag(parser); 088 long h = ParserUtils.getLongAttribute(parser, "h"); 089 String previd = parser.getAttributeValue("", "previd"); 090 parser.next(); 091 ParserUtils.assertAtEndTag(parser); 092 return new Resumed(h, previd); 093 } 094 095 public static AckAnswer ackAnswer(XmlPullParser parser) throws XmlPullParserException, IOException { 096 ParserUtils.assertAtStartTag(parser); 097 long h = ParserUtils.getLongAttribute(parser, "h"); 098 parser.next(); 099 ParserUtils.assertAtEndTag(parser); 100 return new AckAnswer(h); 101 } 102 103 public static AckRequest ackRequest(XmlPullParser parser) throws XmlPullParserException, IOException { 104 ParserUtils.assertAtStartTag(parser); 105 parser.next(); 106 ParserUtils.assertAtEndTag(parser); 107 return AckRequest.INSTANCE; 108 } 109}