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