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