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