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.packet.ExtensionElement;
022import org.jivesoftware.smack.packet.XmlEnvironment;
023import org.jivesoftware.smack.parsing.SmackParsingException;
024import org.jivesoftware.smack.provider.ExtensionElementProvider;
025import org.jivesoftware.smack.provider.ProviderManager;
026import org.jivesoftware.smack.util.PacketParserUtils;
027import org.jivesoftware.smack.xml.XmlPullParser;
028import org.jivesoftware.smack.xml.XmlPullParserException;
029
030import org.jivesoftware.smackx.pubsub.Item;
031import org.jivesoftware.smackx.pubsub.Item.ItemNamespace;
032import org.jivesoftware.smackx.pubsub.PayloadItem;
033import org.jivesoftware.smackx.pubsub.SimplePayload;
034import org.jivesoftware.smackx.pubsub.packet.PubSubNamespace;
035
036/**
037 * Parses an <b>item</b> element as is defined in both the {@link PubSubNamespace#basic} and
038 * {@link PubSubNamespace#event} namespaces. To parse the item contents, it will use whatever
039 * {@link ExtensionElementProvider} is registered in <b>smack.providers</b> for its element name and namespace. If no
040 * provider is registered, it will return a {@link SimplePayload}.
041 *
042 * @author Robin Collier
043 */
044public class ItemProvider extends ExtensionElementProvider<Item>  {
045    @Override
046    public Item parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment)
047                    throws XmlPullParserException, IOException, SmackParsingException {
048        String id = parser.getAttributeValue(null, "id");
049        String node = parser.getAttributeValue(null, "node");
050        String xmlns = parser.getNamespace();
051        ItemNamespace itemNamespace = ItemNamespace.fromXmlns(xmlns);
052
053        XmlPullParser.TagEvent event = parser.nextTag();
054        switch (event) {
055            case START_ELEMENT:
056                String payloadElemName = parser.getName();
057                String payloadNS = parser.getNamespace();
058
059                final ExtensionElementProvider<ExtensionElement> extensionProvider = ProviderManager.getExtensionProvider(payloadElemName, payloadNS);
060                if (extensionProvider == null) {
061                    // TODO: Should we use StandardExtensionElement in this case? And probably remove SimplePayload all together.
062                    CharSequence payloadText = PacketParserUtils.parseElement(parser, true);
063                    return new PayloadItem<>(itemNamespace, id, node, new SimplePayload(payloadText.toString()));
064                }
065                else {
066                    return new PayloadItem<>(itemNamespace, id, node, extensionProvider.parse(parser));
067                }
068            case END_ELEMENT:
069                return new Item(itemNamespace, id, node);
070            default:
071                throw new AssertionError("unknown: " + event);
072        }
073    }
074
075}