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 * &lt;message from='pubsub.shakespeare.lit' to='francisco@denmark.lit' id='foo&gt;
045 *    &lt;event xmlns='http://jabber.org/protocol/pubsub#event&gt;
046 *       &lt;items node='princely_musings'&gt;
047 *          &lt;item id='asdjkwei3i34234n356'&gt;
048 *             &lt;entry xmlns='http://www.w3.org/2005/Atom'&gt;
049 *                &lt;title&gt;Soliloquy&lt;/title&gt;
050 *                &lt;link rel='alternative' type='text/html'/&gt;
051 *                &lt;id>tag:denmark.lit,2003:entry-32397&lt;/id&gt;
052 *             &lt;/entry&gt;
053 *          &lt;/item&gt;
054 *       &lt;/items&gt;
055 *    &lt;/event&gt;
056 * &lt;/message&gt;
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 *   &lt;extensionProvider&gt;
068 *      &lt;elementName&gt;items&lt;/elementName&gt;
069 *      &lt;namespace&gt;http://jabber.org/protocol/pubsub#event&lt;/namespace&gt;
070 *      &lt;className&gt;org.jivesoftware.smackx.provider.ItemsEventProvider&lt;/className&gt;
071 *   &lt;/extensionProvider&gt;
072 *   &lt;extensionProvider&gt;
073 *       &lt;elementName&gt;item&lt;/elementName&gt;
074 *       &lt;namespace&gt;http://jabber.org/protocol/pubsub#event&lt;/namespace&gt;
075 *       &lt;className&gt;org.jivesoftware.smackx.provider.ItemProvider&lt;/className&gt;
076 *   &lt;/extensionProvider&gt;
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}