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.pubsub.provider;
018
019import org.jivesoftware.smack.packet.IQ;
020import org.jivesoftware.smack.packet.PacketExtension;
021import org.jivesoftware.smack.provider.IQProvider;
022import org.jivesoftware.smack.util.PacketParserUtils;
023import org.jivesoftware.smackx.pubsub.packet.PubSub;
024import org.jivesoftware.smackx.pubsub.packet.PubSubNamespace;
025import org.xmlpull.v1.XmlPullParser;
026
027/**
028 * Parses the root pubsub packet extensions of the {@link IQ} packet and returns
029 * a {@link PubSub} instance.
030 * 
031 * @author Robin Collier
032 */
033public class PubSubProvider implements IQProvider
034{
035        public IQ parseIQ(XmlPullParser parser) throws Exception
036        {
037        PubSub pubsub = new PubSub();
038        String namespace = parser.getNamespace();
039        pubsub.setPubSubNamespace(PubSubNamespace.valueOfFromXmlns(namespace));
040        boolean done = false;
041
042        while (!done) 
043        {
044            int eventType = parser.next();
045            
046            if (eventType == XmlPullParser.START_TAG) 
047            {
048                PacketExtension ext = PacketParserUtils.parsePacketExtension(parser.getName(), namespace, parser);
049                
050                if (ext != null)
051                {
052                        pubsub.addExtension(ext);
053                }
054            }
055            else if (eventType == XmlPullParser.END_TAG) 
056            {
057                if (parser.getName().equals("pubsub")) 
058                {
059                    done = true;
060                }
061            }
062        }
063        return pubsub;
064        }
065}