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