001/** 002 * 003 * Copyright 2016 Fernando Ramirez 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.muclight.provider; 018 019import java.io.IOException; 020import java.util.HashMap; 021import java.util.Map; 022 023import org.jivesoftware.smack.packet.IqData; 024import org.jivesoftware.smack.packet.XmlEnvironment; 025import org.jivesoftware.smack.provider.IqProvider; 026import org.jivesoftware.smack.xml.XmlPullParser; 027import org.jivesoftware.smack.xml.XmlPullParserException; 028 029import org.jivesoftware.smackx.muclight.MUCLightAffiliation; 030import org.jivesoftware.smackx.muclight.MUCLightRoomConfiguration; 031import org.jivesoftware.smackx.muclight.element.MUCLightInfoIQ; 032 033import org.jxmpp.jid.Jid; 034import org.jxmpp.jid.impl.JidCreate; 035 036/** 037 * MUC Light info IQ provider class. 038 * 039 * @author Fernando Ramirez 040 * 041 */ 042public class MUCLightInfoIQProvider extends IqProvider<MUCLightInfoIQ> { 043 044 @Override 045 public MUCLightInfoIQ parse(XmlPullParser parser, int initialDepth, IqData iqData, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException { 046 String version = null; 047 String roomName = null; 048 String subject = null; 049 Map<String, String> customConfigs = null; 050 Map<Jid, MUCLightAffiliation> occupants = new HashMap<>(); 051 052 outerloop: while (true) { 053 XmlPullParser.Event eventType = parser.next(); 054 055 if (eventType == XmlPullParser.Event.START_ELEMENT) { 056 057 if (parser.getName().equals("version")) { 058 version = parser.nextText(); 059 } 060 if (parser.getName().equals("configuration")) { 061 062 int depth = parser.getDepth(); 063 064 outerloop2: while (true) { 065 eventType = parser.next(); 066 067 if (eventType == XmlPullParser.Event.START_ELEMENT) { 068 if (parser.getName().equals("roomname")) { 069 roomName = parser.nextText(); 070 } else if (parser.getName().equals("subject")) { 071 subject = parser.nextText(); 072 } else { 073 if (customConfigs == null) { 074 customConfigs = new HashMap<>(); 075 } 076 customConfigs.put(parser.getName(), parser.nextText()); 077 } 078 079 } else if (eventType == XmlPullParser.Event.END_ELEMENT) { 080 if (parser.getDepth() == depth) { 081 break outerloop2; 082 } 083 } 084 } 085 } 086 087 if (parser.getName().equals("occupants")) { 088 occupants = iterateOccupants(parser); 089 } 090 091 } else if (eventType == XmlPullParser.Event.END_ELEMENT) { 092 if (parser.getDepth() == initialDepth) { 093 break outerloop; 094 } 095 } 096 } 097 098 return new MUCLightInfoIQ(version, new MUCLightRoomConfiguration(roomName, subject, customConfigs), occupants); 099 } 100 101 private static Map<Jid, MUCLightAffiliation> iterateOccupants(XmlPullParser parser) throws XmlPullParserException, IOException { 102 Map<Jid, MUCLightAffiliation> occupants = new HashMap<>(); 103 int depth = parser.getDepth(); 104 105 outerloop: while (true) { 106 XmlPullParser.Event eventType = parser.next(); 107 if (eventType == XmlPullParser.Event.START_ELEMENT) { 108 if (parser.getName().equals("user")) { 109 MUCLightAffiliation affiliation = MUCLightAffiliation 110 .fromString(parser.getAttributeValue("", "affiliation")); 111 occupants.put(JidCreate.from(parser.nextText()), affiliation); 112 } 113 } else if (eventType == XmlPullParser.Event.END_ELEMENT) { 114 if (parser.getDepth() == depth) { 115 break outerloop; 116 } 117 } 118 } 119 120 return occupants; 121 } 122 123}