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