1 /** 2 * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); 3 * you may not use this file except in compliance with the License. 4 * You may obtain a copy of the License at 5 * 6 * http://www.apache.org/licenses/LICENSE-2.0 7 * 8 * Unless required by applicable law or agreed to in writing, software 9 * distributed under the License is distributed on an "AS IS" BASIS, 10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 * See the License for the specific language governing permissions and 12 * limitations under the License. 13 */ 14 15 package org.jivesoftware.smackx.provider; 16 17 import java.util.ArrayList; 18 import java.util.HashMap; 19 import java.util.List; 20 import java.util.Map; 21 22 import org.jivesoftware.smack.packet.PacketExtension; 23 import org.jivesoftware.smack.provider.PacketExtensionProvider; 24 import org.jivesoftware.smack.util.PacketParserUtils; 25 import org.jivesoftware.smackx.pubsub.provider.ItemProvider; 26 import org.jivesoftware.smackx.pubsub.provider.ItemsProvider; 27 import org.xmlpull.v1.XmlPullParser; 28 29 /** 30 * 31 * This class simplifies parsing of embedded elements by using the 32 * <a href="http://en.wikipedia.org/wiki/Template_method_pattern">Template Method Pattern</a>. 33 * After extracting the current element attributes and content of any child elements, the template method 34 * ({@link #createReturnExtension(String, String, Map, List)} is called. Subclasses 35 * then override this method to create the specific return type. 36 * 37 * <p>To use this class, you simply register your subclasses as extension providers in the 38 * <b>smack.properties</b> file. Then they will be automatically picked up and used to parse 39 * any child elements. 40 * 41 * <pre> 42 * For example, given the following message 43 * 44 * <message from='pubsub.shakespeare.lit' to='francisco (at) denmark.lit' id='foo> 45 * <event xmlns='http://jabber.org/protocol/pubsub#event> 46 * <items node='princely_musings'> 47 * <item id='asdjkwei3i34234n356'> 48 * <entry xmlns='http://www.w3.org/2005/Atom'> 49 * <title>Soliloquy</title> 50 * <link rel='alternative' type='text/html'/> 51 * <id>tag:denmark.lit,2003:entry-32397</id> 52 * </entry> 53 * </item> 54 * </items> 55 * </event> 56 * </message> 57 * 58 * I would have a classes 59 * {@link ItemsProvider} extends {@link EmbeddedExtensionProvider} 60 * {@link ItemProvider} extends {@link EmbeddedExtensionProvider} 61 * and 62 * AtomProvider extends {@link PacketExtensionProvider} 63 * 64 * These classes are then registered in the meta-inf/smack.providers file 65 * as follows. 66 * 67 * <extensionProvider> 68 * <elementName>items</elementName> 69 * <namespace>http://jabber.org/protocol/pubsub#event</namespace> 70 * <className>org.jivesoftware.smackx.provider.ItemsEventProvider</className> 71 * </extensionProvider> 72 * <extensionProvider> 73 * <elementName>item</elementName> 74 * <namespace>http://jabber.org/protocol/pubsub#event</namespace> 75 * <className>org.jivesoftware.smackx.provider.ItemProvider</className> 76 * </extensionProvider> 77 * 78 * </pre> 79 * 80 * @author Robin Collier 81 * 82 * @deprecated This has been moved to {@link org.jivesoftware.smack.provider.EmbeddedExtensionProvider} 83 */ 84 abstract public class EmbeddedExtensionProvider implements PacketExtensionProvider 85 { 86 87 final public PacketExtension parseExtension(XmlPullParser parser) throws Exception 88 { 89 String namespace = parser.getNamespace(); 90 String name = parser.getName(); 91 Map<String, String> attMap = new HashMap<String, String>(); 92 93 for(int i=0; i<parser.getAttributeCount(); i++) 94 { 95 attMap.put(parser.getAttributeName(i), parser.getAttributeValue(i)); 96 } 97 List<PacketExtension> extensions = new ArrayList<PacketExtension>(); 98 99 do 100 { 101 int tag = parser.next(); 102 103 if (tag == XmlPullParser.START_TAG) 104 extensions.add(PacketParserUtils.parsePacketExtension(parser.getName(), parser.getNamespace(), parser)); 105 } while (!name.equals(parser.getName())); 106 107 return createReturnExtension(name, namespace, attMap, extensions); 108 } 109 110 abstract protected PacketExtension createReturnExtension(String currentElement, String currentNamespace, Map<String, String> attributeMap, List<? extends PacketExtension> content); 111 } 112