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