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.Event tag = parser.next(); 054 055 if (tag == XmlPullParser.Event.END_ELEMENT) { 056 return new Item(itemNamespace, id, node); 057 } 058 else { 059 String payloadElemName = parser.getName(); 060 String payloadNS = parser.getNamespace(); 061 062 final ExtensionElementProvider<ExtensionElement> extensionProvider = ProviderManager.getExtensionProvider(payloadElemName, payloadNS); 063 if (extensionProvider == null) { 064 // TODO: Should we use StandardExtensionElement in this case? And probably remove SimplePayload all together. 065 CharSequence payloadText = PacketParserUtils.parseElement(parser, true); 066 return new PayloadItem<>(itemNamespace, id, node, new SimplePayload(payloadText.toString())); 067 } 068 else { 069 return new PayloadItem<>(itemNamespace, id, node, extensionProvider.parse(parser)); 070 } 071 } 072 } 073 074}