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