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 org.jivesoftware.smack.packet.IQ;
  19. import org.jivesoftware.smack.provider.IQProvider;
  20. import org.xmlpull.v1.XmlPullParser;
  21. import org.xmlpull.v1.XmlPullParserException;

  22. import java.io.IOException;
  23. import java.util.ArrayList;
  24. import java.util.List;

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

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

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

  38.     public SharedGroupsInfo() {
  39.         super(ELEMENT, NAMESPACE);
  40.     }

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

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

  57.     /**
  58.      * Internal Search service Provider.
  59.      */
  60.     public static class Provider extends IQProvider<SharedGroupsInfo> {

  61.         @Override
  62.         public SharedGroupsInfo parse(XmlPullParser parser, int initialDepth)
  63.                         throws XmlPullParserException, IOException {
  64.             SharedGroupsInfo groupsInfo = new SharedGroupsInfo();

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