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