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.smack.provider; 018 019import java.util.ArrayList; 020import java.util.HashMap; 021import java.util.List; 022import java.util.Map; 023 024import org.jivesoftware.smack.packet.PacketExtension; 025import org.jivesoftware.smack.provider.PacketExtensionProvider; 026import org.jivesoftware.smack.util.PacketParserUtils; 027import org.xmlpull.v1.XmlPullParser; 028 029/** 030 * 031 * This class simplifies parsing of embedded elements by using the 032 * <a href="http://en.wikipedia.org/wiki/Template_method_pattern">Template Method Pattern</a>. 033 * After extracting the current element attributes and content of any child elements, the template method 034 * ({@link #createReturnExtension(String, String, Map, List)} is called. Subclasses 035 * then override this method to create the specific return type. 036 * 037 * <p>To use this class, you simply register your subclasses as extension providers in the 038 * <b>smack.properties</b> file. Then they will be automatically picked up and used to parse 039 * any child elements. 040 * 041 * <pre> 042 * For example, given the following message 043 * 044 * <message from='pubsub.shakespeare.lit' to='francisco@denmark.lit' id='foo> 045 * <event xmlns='http://jabber.org/protocol/pubsub#event> 046 * <items node='princely_musings'> 047 * <item id='asdjkwei3i34234n356'> 048 * <entry xmlns='http://www.w3.org/2005/Atom'> 049 * <title>Soliloquy</title> 050 * <link rel='alternative' type='text/html'/> 051 * <id>tag:denmark.lit,2003:entry-32397</id> 052 * </entry> 053 * </item> 054 * </items> 055 * </event> 056 * </message> 057 * 058 * I would have a classes 059 * <tt>ItemsProvider</tt> extends {@link EmbeddedExtensionProvider} 060 * <tt>ItemProvider</tt> extends {@link EmbeddedExtensionProvider} 061 * and 062 * AtomProvider extends {@link PacketExtensionProvider} 063 * 064 * These classes are then registered in the meta-inf/smack.providers file 065 * as follows. 066 * 067 * <extensionProvider> 068 * <elementName>items</elementName> 069 * <namespace>http://jabber.org/protocol/pubsub#event</namespace> 070 * <className>org.jivesoftware.smackx.provider.ItemsEventProvider</className> 071 * </extensionProvider> 072 * <extensionProvider> 073 * <elementName>item</elementName> 074 * <namespace>http://jabber.org/protocol/pubsub#event</namespace> 075 * <className>org.jivesoftware.smackx.provider.ItemProvider</className> 076 * </extensionProvider> 077 * 078 * </pre> 079 * 080 * @author Robin Collier 081 */ 082abstract public class EmbeddedExtensionProvider implements PacketExtensionProvider 083{ 084 085 final public PacketExtension parseExtension(XmlPullParser parser) throws Exception 086 { 087 String namespace = parser.getNamespace(); 088 String name = parser.getName(); 089 Map<String, String> attMap = new HashMap<String, String>(); 090 091 for(int i=0; i<parser.getAttributeCount(); i++) 092 { 093 attMap.put(parser.getAttributeName(i), parser.getAttributeValue(i)); 094 } 095 List<PacketExtension> extensions = new ArrayList<PacketExtension>(); 096 097 do 098 { 099 int tag = parser.next(); 100 101 if (tag == XmlPullParser.START_TAG) 102 extensions.add(PacketParserUtils.parsePacketExtension(parser.getName(), parser.getNamespace(), parser)); 103 } while (!name.equals(parser.getName())); 104 105 return createReturnExtension(name, namespace, attMap, extensions); 106 } 107 108 abstract protected PacketExtension createReturnExtension(String currentElement, String currentNamespace, Map<String, String> attributeMap, List<? extends PacketExtension> content); 109}