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