001/**
002 *
003 * Copyright 2005 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 */
017package org.jivesoftware.smackx.sharedgroups.packet;
018
019import org.jivesoftware.smack.packet.IQ;
020import org.jivesoftware.smack.provider.IQProvider;
021import org.xmlpull.v1.XmlPullParser;
022
023import java.util.ArrayList;
024import java.util.List;
025
026/**
027 * IQ packet used for discovering the user's shared groups and for getting the answer back
028 * from the server.<p>
029 *
030 * Important note: This functionality is not part of the XMPP spec and it will only work
031 * with Wildfire.
032 *
033 * @author Gaston Dombiak
034 */
035public class SharedGroupsInfo extends IQ {
036
037    private List<String> groups = new ArrayList<String>();
038
039    /**
040     * Returns a collection with the shared group names returned from the server.
041     *
042     * @return collection with the shared group names returned from the server.
043     */
044    public List<String> getGroups() {
045        return groups;
046    }
047
048    public String getChildElementXML() {
049        StringBuilder buf = new StringBuilder();
050        buf.append("<sharedgroup xmlns=\"http://www.jivesoftware.org/protocol/sharedgroup\">");
051        for (String group : groups) {
052            buf.append("<group>").append(group).append("</group>");
053        }
054        buf.append("</sharedgroup>");
055        return buf.toString();
056    }
057
058    /**
059     * Internal Search service Provider.
060     */
061    public static class Provider implements IQProvider {
062
063        /**
064         * Provider Constructor.
065         */
066        public Provider() {
067            super();
068        }
069
070        public IQ parseIQ(XmlPullParser parser) throws Exception {
071            SharedGroupsInfo groupsInfo = new SharedGroupsInfo();
072
073            boolean done = false;
074            while (!done) {
075                int eventType = parser.next();
076                if (eventType == XmlPullParser.START_TAG && parser.getName().equals("group")) {
077                    groupsInfo.getGroups().add(parser.nextText());
078                }
079                else if (eventType == XmlPullParser.END_TAG) {
080                    if (parser.getName().equals("sharedgroup")) {
081                        done = true;
082                    }
083                }
084            }
085            return groupsInfo;
086        }
087    }
088}