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