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.xroster.provider; 019 020import java.io.IOException; 021import java.util.ArrayList; 022 023import org.jivesoftware.smack.provider.ExtensionElementProvider; 024import org.jivesoftware.smackx.xroster.RemoteRosterEntry; 025import org.jivesoftware.smackx.xroster.packet.RosterExchange; 026import org.xmlpull.v1.XmlPullParser; 027import org.xmlpull.v1.XmlPullParserException; 028 029/** 030 * 031 * The RosterExchangeProvider parses RosterExchange packets. 032 * 033 * @author Gaston Dombiak 034 */ 035public class RosterExchangeProvider extends ExtensionElementProvider<RosterExchange> { 036 037 /** 038 * Parses a RosterExchange stanza(/packet) (extension sub-packet). 039 * 040 * @param parser the XML parser, positioned at the starting element of the extension. 041 * @return a PacketExtension. 042 * @throws IOException 043 * @throws XmlPullParserException 044 */ 045 @Override 046 public RosterExchange parse(XmlPullParser parser, int initialDepth) 047 throws XmlPullParserException, IOException { 048 049 RosterExchange rosterExchange = new RosterExchange(); 050 boolean done = false; 051 RemoteRosterEntry remoteRosterEntry = null; 052 String jid = ""; 053 String name = ""; 054 ArrayList<String> groupsName = new ArrayList<String>(); 055 while (!done) { 056 int eventType = parser.next(); 057 if (eventType == XmlPullParser.START_TAG) { 058 if (parser.getName().equals("item")) { 059 // Reset this variable since they are optional for each item 060 groupsName = new ArrayList<String>(); 061 // Initialize the variables from the parsed XML 062 jid = parser.getAttributeValue("", "jid"); 063 name = parser.getAttributeValue("", "name"); 064 } 065 if (parser.getName().equals("group")) { 066 groupsName.add(parser.nextText()); 067 } 068 } else if (eventType == XmlPullParser.END_TAG) { 069 if (parser.getName().equals("item")) { 070 // Create packet. 071 remoteRosterEntry = new RemoteRosterEntry(jid, name, (String[]) groupsName.toArray(new String[groupsName.size()])); 072 rosterExchange.addRosterEntry(remoteRosterEntry); 073 } 074 if (parser.getName().equals("x")) { 075 done = true; 076 } 077 } 078 } 079 080 return rosterExchange; 081 082 } 083 084}