001/**
002 *
003 * Copyright 2003-2007 Jive Software.
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 */
017
018package org.jivesoftware.smackx.pep.provider;
019
020import java.util.HashMap;
021import java.util.Map;
022
023import org.jivesoftware.smack.packet.PacketExtension;
024import org.jivesoftware.smack.provider.PacketExtensionProvider;
025import org.xmlpull.v1.XmlPullParser;
026
027/**
028 *
029 * The PEPProvider parses incoming PEPEvent packets.
030 * (XEP-163 has a weird asymmetric deal: outbound PEP are <iq> + <pubsub> and inbound are <message> + <event>.
031 * The provider only deals with inbound, and so it only deals with <message>.
032 * 
033 * Anyhoo...
034 * 
035 * The way this works is that PEPxxx classes are generic <pubsub> and <message> providers, and anyone who
036 * wants to publish/receive PEPs, such as <tune>, <geoloc>, etc., simply need to extend PEPItem and register (here)
037 * a PacketExtensionProvider that knows how to parse that PEPItem extension.
038 *
039 * @author Jeff Williams
040 */
041public class PEPProvider implements PacketExtensionProvider {
042
043    Map<String, PacketExtensionProvider> nodeParsers = new HashMap<String, PacketExtensionProvider>();
044    PacketExtension pepItem;
045    
046    /**
047     * Creates a new PEPProvider.
048     * ProviderManager requires that every PacketExtensionProvider has a public, no-argument constructor
049     */
050    public PEPProvider() {
051    }
052
053    public void registerPEPParserExtension(String node, PacketExtensionProvider pepItemParser) {
054        nodeParsers.put(node, pepItemParser);
055    }
056
057    /**
058     * Parses a PEPEvent packet and extracts a PEPItem from it.
059     * (There is only one per <event>.)
060     *
061     * @param parser the XML parser, positioned at the starting element of the extension.
062     * @return a PacketExtension.
063     * @throws Exception if a parsing error occurs.
064     */
065    public PacketExtension parseExtension(XmlPullParser parser) throws Exception {
066
067        boolean done = false;
068        while (!done) {
069            int eventType = parser.next();
070            if (eventType == XmlPullParser.START_TAG) {
071                if (parser.getName().equals("event")) {
072                } else if (parser.getName().equals("items")) {
073                    // Figure out the node for this event.
074                    String node = parser.getAttributeValue("", "node");
075                    // Get the parser for this kind of node, and if found then parse the node.
076                    PacketExtensionProvider nodeParser = nodeParsers.get(node);
077                    if (nodeParser != null) {
078                        pepItem = nodeParser.parseExtension(parser);
079                    }
080                 }
081            } else if (eventType == XmlPullParser.END_TAG) {
082                if (parser.getName().equals("event")) {
083                    done = true;
084                }
085            }
086        }
087
088        return pepItem;
089    }
090}