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