001/*
002 *
003 * Copyright the original author or authors
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.smackx.bytestreams.ibb.provider;
018
019import java.io.IOException;
020import java.util.Locale;
021
022import org.jivesoftware.smack.packet.IqData;
023import org.jivesoftware.smack.packet.XmlEnvironment;
024import org.jivesoftware.smack.provider.IqProvider;
025import org.jivesoftware.smack.xml.XmlPullParser;
026import org.jivesoftware.smack.xml.XmlPullParserException;
027
028import org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamManager.StanzaType;
029import org.jivesoftware.smackx.bytestreams.ibb.packet.Open;
030
031import org.jxmpp.JxmppContext;
032
033/**
034 * Parses an In-Band Bytestream open packet.
035 *
036 * @author Henning Staib
037 */
038public class OpenIQProvider extends IqProvider<Open> {
039
040    @Override
041    public Open parse(XmlPullParser parser, int initialDepth, IqData iqData, XmlEnvironment xmlEnvironment, JxmppContext jxmppContext) throws XmlPullParserException, IOException {
042        String sessionID = parser.getAttributeValue("", "sid");
043        int blockSize = Integer.parseInt(parser.getAttributeValue("", "block-size"));
044
045        String stanzaValue = parser.getAttributeValue("", "stanza");
046        StanzaType stanza;
047        if (stanzaValue == null) {
048            stanza = StanzaType.IQ;
049        }
050        else {
051            stanza = StanzaType.valueOf(stanzaValue.toUpperCase(Locale.US));
052        }
053
054        parser.next();
055
056        return new Open(sessionID, blockSize, stanza);
057    }
058
059}