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