Home | History | Annotate | Download | only in provider
      1 /**
      2  * $RCSfile$
      3  * $Revision$
      4  * $Date$
      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.ArrayList;
     24 
     25 import org.jivesoftware.smack.packet.PacketExtension;
     26 import org.jivesoftware.smack.provider.PacketExtensionProvider;
     27 import org.jivesoftware.smackx.*;
     28 import org.jivesoftware.smackx.packet.*;
     29 import org.xmlpull.v1.XmlPullParser;
     30 
     31 /**
     32  *
     33  * The RosterExchangeProvider parses RosterExchange packets.
     34  *
     35  * @author Gaston Dombiak
     36  */
     37 public class RosterExchangeProvider implements PacketExtensionProvider {
     38 
     39     /**
     40      * Creates a new RosterExchangeProvider.
     41      * ProviderManager requires that every PacketExtensionProvider has a public, no-argument constructor
     42      */
     43     public RosterExchangeProvider() {
     44     }
     45 
     46     /**
     47      * Parses a RosterExchange packet (extension sub-packet).
     48      *
     49      * @param parser the XML parser, positioned at the starting element of the extension.
     50      * @return a PacketExtension.
     51      * @throws Exception if a parsing error occurs.
     52      */
     53     public PacketExtension parseExtension(XmlPullParser parser) throws Exception {
     54 
     55         RosterExchange rosterExchange = new RosterExchange();
     56         boolean done = false;
     57         RemoteRosterEntry remoteRosterEntry = null;
     58 		String jid = "";
     59 		String name = "";
     60 		ArrayList<String> groupsName = new ArrayList<String>();
     61         while (!done) {
     62             int eventType = parser.next();
     63             if (eventType == XmlPullParser.START_TAG) {
     64                 if (parser.getName().equals("item")) {
     65                 	// Reset this variable since they are optional for each item
     66 					groupsName = new ArrayList<String>();
     67 					// Initialize the variables from the parsed XML
     68                     jid = parser.getAttributeValue("", "jid");
     69                     name = parser.getAttributeValue("", "name");
     70                 }
     71                 if (parser.getName().equals("group")) {
     72 					groupsName.add(parser.nextText());
     73                 }
     74             } else if (eventType == XmlPullParser.END_TAG) {
     75                 if (parser.getName().equals("item")) {
     76 					// Create packet.
     77 					remoteRosterEntry = new RemoteRosterEntry(jid, name, (String[]) groupsName.toArray(new String[groupsName.size()]));
     78                     rosterExchange.addRosterEntry(remoteRosterEntry);
     79                 }
     80                 if (parser.getName().equals("x")) {
     81                     done = true;
     82                 }
     83             }
     84         }
     85 
     86         return rosterExchange;
     87 
     88     }
     89 
     90 }
     91