Home | History | Annotate | Download | only in provider
      1 /**
      2  * $RCSfile: PEPProvider.java,v $
      3  * $Revision: 1.2 $
      4  * $Date: 2007/11/06 02:05:09 $
      5  *
      6  * Copyright 2003-2007 Jive Software.
      7  *
      8  * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
      9  * you may not use this file except in compliance with the License.
     10  * You may obtain a copy of the License at
     11  *
     12  *     http://www.apache.org/licenses/LICENSE-2.0
     13  *
     14  * Unless required by applicable law or agreed to in writing, software
     15  * distributed under the License is distributed on an "AS IS" BASIS,
     16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     17  * See the License for the specific language governing permissions and
     18  * limitations under the License.
     19  */
     20 
     21 package org.jivesoftware.smackx.provider;
     22 
     23 import java.util.HashMap;
     24 import java.util.Map;
     25 
     26 import org.jivesoftware.smack.packet.PacketExtension;
     27 import org.jivesoftware.smack.provider.PacketExtensionProvider;
     28 import org.xmlpull.v1.XmlPullParser;
     29 
     30 /**
     31  *
     32  * The PEPProvider parses incoming PEPEvent packets.
     33  * (XEP-163 has a weird asymmetric deal: outbound PEP are <iq> + <pubsub> and inbound are <message> + <event>.
     34  * The provider only deals with inbound, and so it only deals with <message>.
     35  *
     36  * Anyhoo...
     37  *
     38  * The way this works is that PEPxxx classes are generic <pubsub> and <message> providers, and anyone who
     39  * wants to publish/receive PEPs, such as <tune>, <geoloc>, etc., simply need to extend PEPItem and register (here)
     40  * a PacketExtensionProvider that knows how to parse that PEPItem extension.
     41  *
     42  * @author Jeff Williams
     43  */
     44 public class PEPProvider implements PacketExtensionProvider {
     45 
     46     Map<String, PacketExtensionProvider> nodeParsers = new HashMap<String, PacketExtensionProvider>();
     47     PacketExtension pepItem;
     48 
     49     /**
     50      * Creates a new PEPProvider.
     51      * ProviderManager requires that every PacketExtensionProvider has a public, no-argument constructor
     52      */
     53     public PEPProvider() {
     54     }
     55 
     56     public void registerPEPParserExtension(String node, PacketExtensionProvider pepItemParser) {
     57         nodeParsers.put(node, pepItemParser);
     58     }
     59 
     60     /**
     61      * Parses a PEPEvent packet and extracts a PEPItem from it.
     62      * (There is only one per <event>.)
     63      *
     64      * @param parser the XML parser, positioned at the starting element of the extension.
     65      * @return a PacketExtension.
     66      * @throws Exception if a parsing error occurs.
     67      */
     68     public PacketExtension parseExtension(XmlPullParser parser) throws Exception {
     69 
     70         boolean done = false;
     71         while (!done) {
     72             int eventType = parser.next();
     73             if (eventType == XmlPullParser.START_TAG) {
     74                 if (parser.getName().equals("event")) {
     75                 } else if (parser.getName().equals("items")) {
     76                     // Figure out the node for this event.
     77                     String node = parser.getAttributeValue("", "node");
     78                     // Get the parser for this kind of node, and if found then parse the node.
     79                     PacketExtensionProvider nodeParser = nodeParsers.get(node);
     80                     if (nodeParser != null) {
     81                         pepItem = nodeParser.parseExtension(parser);
     82                     }
     83                  }
     84             } else if (eventType == XmlPullParser.END_TAG) {
     85                 if (parser.getName().equals("event")) {
     86                     done = true;
     87                 }
     88             }
     89         }
     90 
     91         return pepItem;
     92     }
     93 }
     94