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 java.io.IOException;
020
021import org.jivesoftware.smack.SmackException;
022import org.jivesoftware.smack.packet.ExtensionElement;
023import org.jivesoftware.smack.provider.ExtensionElementProvider;
024import org.jivesoftware.smack.provider.ProviderManager;
025import org.jivesoftware.smack.util.PacketParserUtils;
026import org.jivesoftware.smackx.pubsub.Item;
027import org.jivesoftware.smackx.pubsub.PayloadItem;
028import org.jivesoftware.smackx.pubsub.SimplePayload;
029import org.jivesoftware.smackx.pubsub.packet.PubSubNamespace;
030import org.xmlpull.v1.XmlPullParser;
031import org.xmlpull.v1.XmlPullParserException;
032
033/**
034 * Parses an <b>item</b> element as is defined in both the {@link PubSubNamespace#BASIC} and
035 * {@link PubSubNamespace#EVENT} namespaces. To parse the item contents, it will use whatever
036 * {@link ExtensionElementProvider} is registered in <b>smack.providers</b> for its element name and namespace. If no
037 * provider is registered, it will return a {@link SimplePayload}.
038 * 
039 * @author Robin Collier
040 */
041public class ItemProvider extends ExtensionElementProvider<Item> 
042{
043    @Override
044    public Item parse(XmlPullParser parser, int initialDepth)
045                    throws XmlPullParserException, IOException, SmackException {
046        String id = parser.getAttributeValue(null, "id");
047        String node = parser.getAttributeValue(null, "node");
048
049        int tag = parser.next();
050
051        if (tag == XmlPullParser.END_TAG) 
052        {
053            return new Item(id, node);
054        }
055        else 
056        {
057            String payloadElemName = parser.getName();
058            String payloadNS = parser.getNamespace();
059
060            final ExtensionElementProvider<ExtensionElement> extensionProvider = ProviderManager.getExtensionProvider(payloadElemName, payloadNS);
061            if (extensionProvider == null)
062            {
063                CharSequence payloadText = PacketParserUtils.parseElement(parser, true);
064                return new PayloadItem<SimplePayload>(id, node, new SimplePayload(payloadElemName, payloadNS, payloadText));
065            }
066            else 
067            {
068                return new PayloadItem<ExtensionElement>(id, node, extensionProvider.parse(parser));
069            }
070        }
071    }
072
073}