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;
020
021import org.jivesoftware.smack.datatypes.UInt16;
022import org.jivesoftware.smack.packet.IqData;
023import org.jivesoftware.smack.packet.XmlEnvironment;
024import org.jivesoftware.smack.parsing.SmackParsingException;
025import org.jivesoftware.smack.parsing.SmackParsingException.RequiredAttributeMissingException;
026import org.jivesoftware.smack.util.ParserUtils;
027import org.jivesoftware.smack.xml.XmlPullParser;
028import org.jivesoftware.smack.xml.XmlPullParserException;
029
030import org.jivesoftware.smackx.bytestreams.ibb.packet.Data;
031import org.jivesoftware.smackx.bytestreams.ibb.packet.DataPacketExtension;
032
033/**
034 * Parses an In-Band Bytestream data stanza which can be a stanza extension of
035 * either an IQ stanza or a message stanza.
036 *
037 * @author Henning Staib
038 */
039public class DataPacketProvider {
040
041    public static class IQProvider extends org.jivesoftware.smack.provider.IqProvider<Data> {
042
043        private static final PacketExtensionProvider packetExtensionProvider = new PacketExtensionProvider();
044
045        @Override
046        public Data parse(XmlPullParser parser, int initialDepth, IqData iqData, XmlEnvironment xmlEnvironment)
047                        throws IOException, XmlPullParserException, SmackParsingException {
048            DataPacketExtension data = packetExtensionProvider.parse(parser);
049            return new Data(data);
050        }
051    }
052
053    public static class PacketExtensionProvider extends org.jivesoftware.smack.provider.ExtensionElementProvider<DataPacketExtension> {
054
055        @Override
056        public DataPacketExtension parse(XmlPullParser parser,
057                        int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException,
058                        IOException, RequiredAttributeMissingException {
059            String sessionID = parser.getAttributeValue("", "sid");
060            UInt16 seq = ParserUtils.getRequiredUInt16Attribute(parser, "seq");
061            String data = parser.nextText();
062            return new DataPacketExtension(sessionID, seq, data);
063        }
064
065    }
066}