From 298841cf54647ba8fe89b12a636efb736d912c2a Mon Sep 17 00:00:00 2001 From: rcollier Date: Mon, 19 Dec 2011 17:26:47 +0000 Subject: [PATCH] Moved the EmbeddedExtensionProvider from smackx to smack, since it is not tied to extensions but is a simple utility for aiding in parsing of custom cabinets. git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/branches/smack_3_2_0@12921 b35dd754-fafc-0310-a699-88a17e54d16e --- .../provider/EmbeddedExtensionProvider.java | 109 ++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 source/org/jivesoftware/smack/provider/EmbeddedExtensionProvider.java diff --git a/source/org/jivesoftware/smack/provider/EmbeddedExtensionProvider.java b/source/org/jivesoftware/smack/provider/EmbeddedExtensionProvider.java new file mode 100644 index 000000000..e7b4b9317 --- /dev/null +++ b/source/org/jivesoftware/smack/provider/EmbeddedExtensionProvider.java @@ -0,0 +1,109 @@ +/** + * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jivesoftware.smack.provider; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.jivesoftware.smack.packet.PacketExtension; +import org.jivesoftware.smack.provider.PacketExtensionProvider; +import org.jivesoftware.smack.util.PacketParserUtils; +import org.jivesoftware.smackx.pubsub.provider.ItemProvider; +import org.jivesoftware.smackx.pubsub.provider.ItemsProvider; +import org.xmlpull.v1.XmlPullParser; + +/** + * + * This class simplifies parsing of embedded elements by using the + * Template Method Pattern. + * After extracting the current element attributes and content of any child elements, the template method + * ({@link #createReturnExtension(String, String, Map, List)} is called. Subclasses + * then override this method to create the specific return type. + * + *

To use this class, you simply register your subclasses as extension providers in the + * smack.properties file. Then they will be automatically picked up and used to parse + * any child elements. + * + *

+ * For example, given the following message
+ * 
+ * <message from='pubsub.shakespeare.lit' to='francisco@denmark.lit' id='foo>
+ *    <event xmlns='http://jabber.org/protocol/pubsub#event>
+ *       <items node='princely_musings'>
+ *          <item id='asdjkwei3i34234n356'>
+ *             <entry xmlns='http://www.w3.org/2005/Atom'>
+ *                <title>Soliloquy</title>
+ *                <link rel='alternative' type='text/html'/>
+ *                <id>tag:denmark.lit,2003:entry-32397</id>
+ *             </entry>
+ *          </item>
+ *       </items>
+ *    </event>
+ * </message>
+ * 
+ * I would have a classes
+ * {@link ItemsProvider} extends {@link EmbeddedExtensionProvider}
+ * {@link ItemProvider} extends {@link EmbeddedExtensionProvider}
+ * and
+ * AtomProvider extends {@link PacketExtensionProvider}
+ * 
+ * These classes are then registered in the meta-inf/smack.providers file
+ * as follows.
+ * 
+ *   <extensionProvider>
+ *      <elementName>items</elementName>
+ *      <namespace>http://jabber.org/protocol/pubsub#event</namespace>
+ *      <className>org.jivesoftware.smackx.provider.ItemsEventProvider</className>
+ *   </extensionProvider>
+ *   <extensionProvider>
+ *       <elementName>item</elementName>
+ *       <namespace>http://jabber.org/protocol/pubsub#event</namespace>
+ *       <className>org.jivesoftware.smackx.provider.ItemProvider</className>
+ *   </extensionProvider>
+ * 
+ * 
+ * + * @author Robin Collier + */ +abstract public class EmbeddedExtensionProvider implements PacketExtensionProvider +{ + + final public PacketExtension parseExtension(XmlPullParser parser) throws Exception + { + String namespace = parser.getNamespace(); + String name = parser.getName(); + Map attMap = new HashMap(); + + for(int i=0; i extensions = new ArrayList(); + + do + { + int tag = parser.next(); + + if (tag == XmlPullParser.START_TAG) + extensions.add(PacketParserUtils.parsePacketExtension(parser.getName(), parser.getNamespace(), parser)); + } while (!name.equals(parser.getName())); + + return createReturnExtension(name, namespace, attMap, extensions); + } + + abstract protected PacketExtension createReturnExtension(String currentElement, String currentNamespace, Map attributeMap, List content); +}