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