SharedGroupsInfo.java

  1. /**
  2.  *
  3.  * Copyright 2005 Jive Software.
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smackx.sharedgroups.packet;

  18. import java.io.IOException;
  19. import java.util.ArrayList;
  20. import java.util.List;

  21. import org.jivesoftware.smack.packet.IQ;
  22. import org.jivesoftware.smack.packet.IqData;
  23. import org.jivesoftware.smack.packet.XmlEnvironment;
  24. import org.jivesoftware.smack.provider.IqProvider;
  25. import org.jivesoftware.smack.xml.XmlPullParser;
  26. import org.jivesoftware.smack.xml.XmlPullParserException;

  27. /**
  28.  * IQ stanza used for discovering the user's shared groups and for getting the answer back
  29.  * from the server.<p>
  30.  *
  31.  * Important note: This functionality is not part of the XMPP spec and it will only work
  32.  * with Wildfire.
  33.  *
  34.  * @author Gaston Dombiak
  35.  */
  36. public class SharedGroupsInfo extends IQ {

  37.     public static final String ELEMENT = "sharedgroup";
  38.     public static final String NAMESPACE = "http://www.jivesoftware.org/protocol/sharedgroup";

  39.     private final List<String> groups = new ArrayList<>();

  40.     public SharedGroupsInfo() {
  41.         super(ELEMENT, NAMESPACE);
  42.     }

  43.     /**
  44.      * Returns a collection with the shared group names returned from the server.
  45.      *
  46.      * @return collection with the shared group names returned from the server.
  47.      */
  48.     public List<String> getGroups() {
  49.         return groups;
  50.     }

  51.     @Override
  52.     protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder buf) {
  53.         buf.rightAngleBracket();
  54.         for (String group : groups) {
  55.             buf.element("group", group);
  56.         }
  57.         return buf;
  58.     }

  59.     /**
  60.      * Internal Search service Provider.
  61.      */
  62.     public static class Provider extends IqProvider<SharedGroupsInfo> {

  63.         @Override
  64.         public SharedGroupsInfo parse(XmlPullParser parser, int initialDepth, IqData iqData, XmlEnvironment xmlEnvironment)
  65.                         throws XmlPullParserException, IOException {
  66.             SharedGroupsInfo groupsInfo = new SharedGroupsInfo();

  67.             boolean done = false;
  68.             while (!done) {
  69.                 XmlPullParser.Event eventType = parser.next();
  70.                 if (eventType == XmlPullParser.Event.START_ELEMENT && parser.getName().equals("group")) {
  71.                     groupsInfo.getGroups().add(parser.nextText());
  72.                 }
  73.                 else if (eventType == XmlPullParser.Event.END_ELEMENT) {
  74.                     if (parser.getName().equals("sharedgroup")) {
  75.                         done = true;
  76.                     }
  77.                 }
  78.             }
  79.             return groupsInfo;
  80.         }
  81.     }
  82. }