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