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