MUCLightInfoIQProvider.java

  1. /**
  2.  *
  3.  * Copyright 2016 Fernando Ramirez
  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.muclight.provider;

  18. import java.io.IOException;
  19. import java.util.HashMap;

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

  25. import org.jivesoftware.smackx.muclight.MUCLightAffiliation;
  26. import org.jivesoftware.smackx.muclight.MUCLightRoomConfiguration;
  27. import org.jivesoftware.smackx.muclight.element.MUCLightInfoIQ;

  28. import org.jxmpp.jid.Jid;
  29. import org.jxmpp.jid.impl.JidCreate;

  30. /**
  31.  * MUC Light info IQ provider class.
  32.  *
  33.  * @author Fernando Ramirez
  34.  *
  35.  */
  36. public class MUCLightInfoIQProvider extends IqProvider<MUCLightInfoIQ> {

  37.     @Override
  38.     public MUCLightInfoIQ parse(XmlPullParser parser, int initialDepth, IqData iqData, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException {
  39.         String version = null;
  40.         String roomName = null;
  41.         String subject = null;
  42.         HashMap<String, String> customConfigs = null;
  43.         HashMap<Jid, MUCLightAffiliation> occupants = new HashMap<>();

  44.         outerloop: while (true) {
  45.             XmlPullParser.Event eventType = parser.next();

  46.             if (eventType == XmlPullParser.Event.START_ELEMENT) {

  47.                 if (parser.getName().equals("version")) {
  48.                     version = parser.nextText();
  49.                 }
  50.                 if (parser.getName().equals("configuration")) {

  51.                     int depth = parser.getDepth();

  52.                     outerloop2: while (true) {
  53.                         eventType = parser.next();

  54.                         if (eventType == XmlPullParser.Event.START_ELEMENT) {
  55.                             if (parser.getName().equals("roomname")) {
  56.                                 roomName = parser.nextText();
  57.                             } else if (parser.getName().equals("subject")) {
  58.                                 subject = parser.nextText();
  59.                             } else {
  60.                                 if (customConfigs == null) {
  61.                                     customConfigs = new HashMap<>();
  62.                                 }
  63.                                 customConfigs.put(parser.getName(), parser.nextText());
  64.                             }

  65.                         } else if (eventType == XmlPullParser.Event.END_ELEMENT) {
  66.                             if (parser.getDepth() == depth) {
  67.                                 break outerloop2;
  68.                             }
  69.                         }
  70.                     }
  71.                 }

  72.                 if (parser.getName().equals("occupants")) {
  73.                     occupants = iterateOccupants(parser);
  74.                 }

  75.             } else if (eventType == XmlPullParser.Event.END_ELEMENT) {
  76.                 if (parser.getDepth() == initialDepth) {
  77.                     break outerloop;
  78.                 }
  79.             }
  80.         }

  81.         return new MUCLightInfoIQ(version, new MUCLightRoomConfiguration(roomName, subject, customConfigs), occupants);
  82.     }

  83.     private static HashMap<Jid, MUCLightAffiliation> iterateOccupants(XmlPullParser parser) throws XmlPullParserException, IOException {
  84.         HashMap<Jid, MUCLightAffiliation> occupants = new HashMap<>();
  85.         int depth = parser.getDepth();

  86.         outerloop: while (true) {
  87.             XmlPullParser.Event eventType = parser.next();
  88.             if (eventType == XmlPullParser.Event.START_ELEMENT) {
  89.                 if (parser.getName().equals("user")) {
  90.                     MUCLightAffiliation affiliation = MUCLightAffiliation
  91.                             .fromString(parser.getAttributeValue("", "affiliation"));
  92.                     occupants.put(JidCreate.from(parser.nextText()), affiliation);
  93.                 }
  94.             } else if (eventType == XmlPullParser.Event.END_ELEMENT) {
  95.                 if (parser.getDepth() == depth) {
  96.                     break outerloop;
  97.                 }
  98.             }
  99.         }

  100.         return occupants;
  101.     }

  102. }