PEPProvider.java

  1. /**
  2.  *
  3.  * Copyright 2003-2007 Jive Software.
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.jivesoftware.smackx.pep.provider;

  18. import java.io.IOException;
  19. import java.util.HashMap;
  20. import java.util.Map;

  21. import org.jivesoftware.smack.SmackException;
  22. import org.jivesoftware.smack.packet.ExtensionElement;
  23. import org.jivesoftware.smack.provider.ExtensionElementProvider;
  24. import org.xmlpull.v1.XmlPullParser;
  25. import org.xmlpull.v1.XmlPullParserException;

  26. /**
  27.  *
  28.  * The PEPProvider parses incoming PEPEvent packets.
  29.  * (XEP-163 has a weird asymmetric deal: outbound PEP are <iq> + <pubsub> and inbound are <message> + <event>.
  30.  * The provider only deals with inbound, and so it only deals with <message>.
  31.  *
  32.  * Anyhoo...
  33.  *
  34.  * The way this works is that PEPxxx classes are generic <pubsub> and <message> providers, and anyone who
  35.  * wants to publish/receive PEPs, such as <tune>, <geoloc>, etc., simply need to extend PEPItem and register (here)
  36.  * a PacketExtensionProvider that knows how to parse that PEPItem extension.
  37.  *
  38.  * @author Jeff Williams
  39.  */
  40. public class PEPProvider extends ExtensionElementProvider<ExtensionElement> {

  41.     private static final Map<String, ExtensionElementProvider<?>> nodeParsers = new HashMap<String, ExtensionElementProvider<?>>();

  42.     public static void registerPEPParserExtension(String node, ExtensionElementProvider<?> pepItemParser) {
  43.         nodeParsers.put(node, pepItemParser);
  44.     }

  45.     /**
  46.      * Parses a PEPEvent packet and extracts a PEPItem from it.
  47.      * (There is only one per <event>.)
  48.      *
  49.      * @param parser the XML parser, positioned at the starting element of the extension.
  50.      * @return a PacketExtension.
  51.      * @throws IOException
  52.      * @throws XmlPullParserException
  53.      * @throws SmackException
  54.      */
  55.     @Override
  56.     public ExtensionElement parse(XmlPullParser parser, int initialDepth)
  57.                     throws Exception {
  58.         ExtensionElement pepItem = null;
  59.         boolean done = false;
  60.         while (!done) {
  61.             int eventType = parser.next();
  62.             if (eventType == XmlPullParser.START_TAG) {
  63.                 if (parser.getName().equals("event")) {
  64.                 } else if (parser.getName().equals("items")) {
  65.                     // Figure out the node for this event.
  66.                     String node = parser.getAttributeValue("", "node");
  67.                     // Get the parser for this kind of node, and if found then parse the node.
  68.                     ExtensionElementProvider<?> nodeParser = nodeParsers.get(node);
  69.                     if (nodeParser != null) {
  70.                         pepItem = nodeParser.parse(parser);
  71.                     }
  72.                  }
  73.             } else if (eventType == XmlPullParser.END_TAG) {
  74.                 if (parser.getName().equals("event")) {
  75.                     done = true;
  76.                 }
  77.             }
  78.         }

  79.         return pepItem;
  80.     }
  81. }