blob: e7b4b9317100ea5f7a7a77a0821b5949ef90293c [file] [log] [blame]
Shuyi Chend7955ce2013-05-22 14:51:55 -07001/**
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
15package org.jivesoftware.smack.provider;
16
17import java.util.ArrayList;
18import java.util.HashMap;
19import java.util.List;
20import java.util.Map;
21
22import org.jivesoftware.smack.packet.PacketExtension;
23import org.jivesoftware.smack.provider.PacketExtensionProvider;
24import org.jivesoftware.smack.util.PacketParserUtils;
25import org.jivesoftware.smackx.pubsub.provider.ItemProvider;
26import org.jivesoftware.smackx.pubsub.provider.ItemsProvider;
27import 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@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 */
82abstract public class EmbeddedExtensionProvider implements PacketExtensionProvider
83{
84
85 final public PacketExtension parseExtension(XmlPullParser parser) throws Exception
86 {
87 String namespace = parser.getNamespace();
88 String name = parser.getName();
89 Map<String, String> attMap = new HashMap<String, String>();
90
91 for(int i=0; i<parser.getAttributeCount(); i++)
92 {
93 attMap.put(parser.getAttributeName(i), parser.getAttributeValue(i));
94 }
95 List<PacketExtension> extensions = new ArrayList<PacketExtension>();
96
97 do
98 {
99 int tag = parser.next();
100
101 if (tag == XmlPullParser.START_TAG)
102 extensions.add(PacketParserUtils.parsePacketExtension(parser.getName(), parser.getNamespace(), parser));
103 } while (!name.equals(parser.getName()));
104
105 return createReturnExtension(name, namespace, attMap, extensions);
106 }
107
108 abstract protected PacketExtension createReturnExtension(String currentElement, String currentNamespace, Map<String, String> attributeMap, List<? extends PacketExtension> content);
109}