Home | History | Annotate | Download | only in provider
      1 /**
      2  * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
      3  * you may not use this file except in compliance with the License.
      4  * You may obtain a copy of the License at
      5  *
      6  *     http://www.apache.org/licenses/LICENSE-2.0
      7  *
      8  * Unless required by applicable law or agreed to in writing, software
      9  * distributed under the License is distributed on an "AS IS" BASIS,
     10  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     11  * See the License for the specific language governing permissions and
     12  * limitations under the License.
     13  */
     14 
     15 package org.jivesoftware.smackx.provider;
     16 
     17 import java.util.ArrayList;
     18 import java.util.HashMap;
     19 import java.util.List;
     20 import java.util.Map;
     21 
     22 import org.jivesoftware.smack.packet.PacketExtension;
     23 import org.jivesoftware.smack.provider.PacketExtensionProvider;
     24 import org.jivesoftware.smack.util.PacketParserUtils;
     25 import org.jivesoftware.smackx.pubsub.provider.ItemProvider;
     26 import org.jivesoftware.smackx.pubsub.provider.ItemsProvider;
     27 import org.xmlpull.v1.XmlPullParser;
     28 
     29 /**
     30  *
     31  * This class simplifies parsing of embedded elements by using the
     32  * <a href="http://en.wikipedia.org/wiki/Template_method_pattern">Template Method Pattern</a>.
     33  * After extracting the current element attributes and content of any child elements, the template method
     34  * ({@link #createReturnExtension(String, String, Map, List)} is called.  Subclasses
     35  * then override this method to create the specific return type.
     36  *
     37  * <p>To use this class, you simply register your subclasses as extension providers in the
     38  * <b>smack.properties</b> file.  Then they will be automatically picked up and used to parse
     39  * any child elements.
     40  *
     41  * <pre>
     42  * For example, given the following message
     43  *
     44  * &lt;message from='pubsub.shakespeare.lit' to='francisco (at) denmark.lit' id='foo&gt;
     45  *    &lt;event xmlns='http://jabber.org/protocol/pubsub#event&gt;
     46  *       &lt;items node='princely_musings'&gt;
     47  *          &lt;item id='asdjkwei3i34234n356'&gt;
     48  *             &lt;entry xmlns='http://www.w3.org/2005/Atom'&gt;
     49  *                &lt;title&gt;Soliloquy&lt;/title&gt;
     50  *                &lt;link rel='alternative' type='text/html'/&gt;
     51  *                &lt;id>tag:denmark.lit,2003:entry-32397&lt;/id&gt;
     52  *             &lt;/entry&gt;
     53  *          &lt;/item&gt;
     54  *       &lt;/items&gt;
     55  *    &lt;/event&gt;
     56  * &lt;/message&gt;
     57  *
     58  * I would have a classes
     59  * {@link ItemsProvider} extends {@link EmbeddedExtensionProvider}
     60  * {@link ItemProvider} extends {@link EmbeddedExtensionProvider}
     61  * and
     62  * AtomProvider extends {@link PacketExtensionProvider}
     63  *
     64  * These classes are then registered in the meta-inf/smack.providers file
     65  * as follows.
     66  *
     67  *   &lt;extensionProvider&gt;
     68  *      &lt;elementName&gt;items&lt;/elementName&gt;
     69  *      &lt;namespace&gt;http://jabber.org/protocol/pubsub#event&lt;/namespace&gt;
     70  *      &lt;className&gt;org.jivesoftware.smackx.provider.ItemsEventProvider&lt;/className&gt;
     71  *   &lt;/extensionProvider&gt;
     72  *   &lt;extensionProvider&gt;
     73  *       &lt;elementName&gt;item&lt;/elementName&gt;
     74  *       &lt;namespace&gt;http://jabber.org/protocol/pubsub#event&lt;/namespace&gt;
     75  *       &lt;className&gt;org.jivesoftware.smackx.provider.ItemProvider&lt;/className&gt;
     76  *   &lt;/extensionProvider&gt;
     77  *
     78  * </pre>
     79  *
     80  * @author Robin Collier
     81  *
     82  * @deprecated This has been moved to {@link org.jivesoftware.smack.provider.EmbeddedExtensionProvider}
     83  */
     84 abstract public class EmbeddedExtensionProvider implements PacketExtensionProvider
     85 {
     86 
     87 	final public PacketExtension parseExtension(XmlPullParser parser) throws Exception
     88 	{
     89         String namespace = parser.getNamespace();
     90         String name = parser.getName();
     91         Map<String, String> attMap = new HashMap<String, String>();
     92 
     93         for(int i=0; i<parser.getAttributeCount(); i++)
     94         {
     95         	attMap.put(parser.getAttributeName(i), parser.getAttributeValue(i));
     96         }
     97         List<PacketExtension> extensions = new ArrayList<PacketExtension>();
     98 
     99         do
    100         {
    101             int tag = parser.next();
    102 
    103             if (tag == XmlPullParser.START_TAG)
    104             	extensions.add(PacketParserUtils.parsePacketExtension(parser.getName(), parser.getNamespace(), parser));
    105         } while (!name.equals(parser.getName()));
    106 
    107 		return createReturnExtension(name, namespace, attMap, extensions);
    108 	}
    109 
    110 	abstract protected PacketExtension createReturnExtension(String currentElement, String currentNamespace, Map<String, String> attributeMap, List<? extends PacketExtension> content);
    111 }
    112