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.PacketExtension;
020import org.jivesoftware.smack.provider.PacketExtensionProvider;
021import org.jivesoftware.smackx.pubsub.Subscription;
022import org.xmlpull.v1.XmlPullParser;
023
024/**
025 * Parses the <b>subscription</b> element out of the pubsub IQ message from 
026 * the server as specified in the <a href="http://xmpp.org/extensions/xep-0060.html#schemas-pubsub">subscription schema</a>.
027 * 
028 * @author Robin Collier
029 */
030public class SubscriptionProvider implements PacketExtensionProvider
031{
032        public PacketExtension parseExtension(XmlPullParser parser) throws Exception
033        {
034                String jid = parser.getAttributeValue(null, "jid");
035                String nodeId = parser.getAttributeValue(null, "node");
036                String subId = parser.getAttributeValue(null, "subid");
037                String state = parser.getAttributeValue(null, "subscription");
038                boolean isRequired = false;
039
040                int tag = parser.next();
041                
042                if ((tag == XmlPullParser.START_TAG) && parser.getName().equals("subscribe-options"))
043                {
044                        tag = parser.next();
045                        
046                        if ((tag == XmlPullParser.START_TAG) && parser.getName().equals("required"))
047                                isRequired = true;
048                        
049                        while (parser.next() != XmlPullParser.END_TAG && parser.getName() != "subscribe-options");
050                }
051                while (parser.getEventType() != XmlPullParser.END_TAG) parser.next();
052                return new Subscription(jid, nodeId, subId, (state == null ? null : Subscription.State.valueOf(state)), isRequired);
053        }
054
055}