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.smack.sm.provider;
018
019import java.io.IOException;
020
021import org.jivesoftware.smack.packet.XMPPError;
022import org.jivesoftware.smack.sm.packet.StreamManagement.AckAnswer;
023import org.jivesoftware.smack.sm.packet.StreamManagement.AckRequest;
024import org.jivesoftware.smack.sm.packet.StreamManagement.Enabled;
025import org.jivesoftware.smack.sm.packet.StreamManagement.Failed;
026import org.jivesoftware.smack.sm.packet.StreamManagement.Resumed;
027import org.jivesoftware.smack.util.ParserUtils;
028import org.xmlpull.v1.XmlPullParser;
029import org.xmlpull.v1.XmlPullParserException;
030
031public class ParseStreamManagement {
032
033    public static Enabled enabled(XmlPullParser parser) throws XmlPullParserException, IOException {
034        ParserUtils.assertAtStartTag(parser);
035        boolean resume = ParserUtils.getBooleanAttribute(parser, "resume", false);
036        String id = parser.getAttributeValue("", "id");
037        String location = parser.getAttributeValue("", "location");
038        int max = ParserUtils.getIntegerAttribute(parser, "max", -1);
039        parser.next();
040        ParserUtils.assertAtEndTag(parser);
041        return new Enabled(id, resume, location, max);
042    }
043
044    public static Failed failed(XmlPullParser parser) throws XmlPullParserException, IOException {
045        ParserUtils.assertAtStartTag(parser);
046        String name;
047        XMPPError.Condition condition = null;
048        outerloop:
049        while(true) {
050            int event = parser.next();
051            switch (event) {
052            case XmlPullParser.START_TAG:
053                name = parser.getName();
054                String namespace = parser.getNamespace();
055                if (XMPPError.NAMESPACE.equals(namespace)) {
056                    condition = XMPPError.Condition.fromString(name);
057                }
058                break;
059            case XmlPullParser.END_TAG:
060                name = parser.getName();
061                if (Failed.ELEMENT.equals(name)) {
062                    break outerloop;
063                }
064                break;
065            }
066        }
067        ParserUtils.assertAtEndTag(parser);
068        return new Failed(condition);
069    }
070
071    public static Resumed resumed(XmlPullParser parser) throws XmlPullParserException, IOException {
072        ParserUtils.assertAtStartTag(parser);
073        long h = ParserUtils.getLongAttribute(parser, "h");
074        String previd = parser.getAttributeValue("", "previd");
075        parser.next();
076        ParserUtils.assertAtEndTag(parser);
077        return new Resumed(h, previd);
078    }
079
080    public static AckAnswer ackAnswer(XmlPullParser parser) throws XmlPullParserException, IOException {
081        ParserUtils.assertAtStartTag(parser);
082        long h = ParserUtils.getLongAttribute(parser, "h");
083        parser.next();
084        ParserUtils.assertAtEndTag(parser);
085        return new AckAnswer(h);
086    }
087
088    public static AckRequest ackRequest(XmlPullParser parser) throws XmlPullParserException, IOException {
089        ParserUtils.assertAtStartTag(parser);
090        parser.next();
091        ParserUtils.assertAtEndTag(parser);
092        return AckRequest.INSTANCE;
093    }
094}