EmbeddedExtensionProvider.java

  1. /**
  2.  *
  3.  * Copyright the original author or authors
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smack.provider;

  18. import java.util.ArrayList;
  19. import java.util.HashMap;
  20. import java.util.List;
  21. import java.util.Map;

  22. import org.jivesoftware.smack.packet.ExtensionElement;
  23. import org.jivesoftware.smack.util.PacketParserUtils;
  24. import org.xmlpull.v1.XmlPullParser;

  25. /**
  26.  *
  27.  * This class simplifies parsing of embedded elements by using the
  28.  * <a href="http://en.wikipedia.org/wiki/Template_method_pattern">Template Method Pattern</a>.  
  29.  * After extracting the current element attributes and content of any child elements, the template method
  30.  * ({@link #createReturnExtension(String, String, Map, List)} is called.  Subclasses
  31.  * then override this method to create the specific return type.
  32.  *
  33.  * <p>To use this class, you simply register your subclasses as extension providers in the
  34.  * <b>smack.properties</b> file.  Then they will be automatically picked up and used to parse
  35.  * any child elements.  
  36.  *
  37.  * <pre>
  38.  * For example, given the following message
  39.  *
  40.  * &lt;message from='pubsub.shakespeare.lit' to='francisco@denmark.lit' id='foo&gt;
  41.  *    &lt;event xmlns='http://jabber.org/protocol/pubsub#event&gt;
  42.  *       &lt;items node='princely_musings'&gt;
  43.  *          &lt;item id='asdjkwei3i34234n356'&gt;
  44.  *             &lt;entry xmlns='http://www.w3.org/2005/Atom'&gt;
  45.  *                &lt;title&gt;Soliloquy&lt;/title&gt;
  46.  *                &lt;link rel='alternative' type='text/html'/&gt;
  47.  *                &lt;id>tag:denmark.lit,2003:entry-32397&lt;/id&gt;
  48.  *             &lt;/entry&gt;
  49.  *          &lt;/item&gt;
  50.  *       &lt;/items&gt;
  51.  *    &lt;/event&gt;
  52.  * &lt;/message&gt;
  53.  *
  54.  * I would have a classes
  55.  * <tt>ItemsProvider</tt> extends {@link EmbeddedExtensionProvider}
  56.  * <tt>ItemProvider</tt> extends {@link EmbeddedExtensionProvider}
  57.  * and
  58.  * AtomProvider extends {@link ExtensionElementProvider}
  59.  *
  60.  * These classes are then registered in the meta-inf/smack.providers file
  61.  * as follows.
  62.  *
  63.  *   &lt;extensionProvider&gt;
  64.  *      &lt;elementName&gt;items&lt;/elementName&gt;
  65.  *      &lt;namespace&gt;http://jabber.org/protocol/pubsub#event&lt;/namespace&gt;
  66.  *      &lt;className&gt;org.jivesoftware.smackx.provider.ItemsEventProvider&lt;/className&gt;
  67.  *   &lt;/extensionProvider&gt;
  68.  *   &lt;extensionProvider&gt;
  69.  *       &lt;elementName&gt;item&lt;/elementName&gt;
  70.  *       &lt;namespace&gt;http://jabber.org/protocol/pubsub#event&lt;/namespace&gt;
  71.  *       &lt;className&gt;org.jivesoftware.smackx.provider.ItemProvider&lt;/className&gt;
  72.  *   &lt;/extensionProvider&gt;
  73.  *
  74.  * </pre>
  75.  *
  76.  * @author Robin Collier
  77.  */
  78. public abstract class EmbeddedExtensionProvider<PE extends ExtensionElement> extends ExtensionElementProvider<PE> {

  79.     @Override
  80.     public final PE parse(XmlPullParser parser, int initialDepth) throws Exception {
  81.         final String namespace = parser.getNamespace();
  82.         final String name = parser.getName();
  83.         final int attributeCount = parser.getAttributeCount();
  84.         Map<String, String> attMap = new HashMap<>(attributeCount);

  85.         for (int i = 0; i < attributeCount; i++) {
  86.             attMap.put(parser.getAttributeName(i), parser.getAttributeValue(i));
  87.         }

  88.         List<ExtensionElement> extensions = new ArrayList<>();
  89.         int event;
  90.         do {
  91.             event = parser.next();

  92.             if (event == XmlPullParser.START_TAG)
  93.                 PacketParserUtils.addExtensionElement(extensions, parser);
  94.         }
  95.         while (!(event == XmlPullParser.END_TAG && parser.getDepth() == initialDepth));

  96.         return createReturnExtension(name, namespace, attMap, extensions);
  97.     }

  98.     protected abstract PE createReturnExtension(String currentElement, String currentNamespace,
  99.                     Map<String, String> attributeMap, List<? extends ExtensionElement> content);
  100. }