001/** 002 * 003 * Copyright 2003-2007 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 */ 017 018package org.jivesoftware.smackx.muc.provider; 019 020import java.io.IOException; 021 022import org.jivesoftware.smack.packet.XmlEnvironment; 023import org.jivesoftware.smack.provider.ExtensionElementProvider; 024import org.jivesoftware.smack.util.ParserUtils; 025import org.jivesoftware.smack.xml.XmlPullParser; 026import org.jivesoftware.smack.xml.XmlPullParserException; 027 028import org.jivesoftware.smackx.muc.packet.MUCUser; 029 030import org.jxmpp.jid.EntityBareJid; 031import org.jxmpp.jid.EntityJid; 032 033/** 034 * The MUCUserProvider parses packets with extended presence information about 035 * roles and affiliations. 036 * 037 * @author Gaston Dombiak 038 */ 039public class MUCUserProvider extends ExtensionElementProvider<MUCUser> { 040 041 /** 042 * Parses a MUCUser stanza (extension sub-packet). 043 * 044 * @param parser the XML parser, positioned at the starting element of the extension. 045 * @return a PacketExtension. 046 * @throws IOException if an I/O error occurred. 047 * @throws XmlPullParserException if an error in the XML parser occurred. 048 */ 049 @Override 050 public MUCUser parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException { 051 MUCUser mucUser = new MUCUser(); 052 outerloop: while (true) { 053 switch (parser.next()) { 054 case START_ELEMENT: 055 switch (parser.getName()) { 056 case "invite": 057 mucUser.setInvite(parseInvite(parser)); 058 break; 059 case "item": 060 mucUser.setItem(MUCParserUtils.parseItem(parser)); 061 break; 062 case "password": 063 mucUser.setPassword(parser.nextText()); 064 break; 065 case "status": 066 String statusString = parser.getAttributeValue("", "code"); 067 mucUser.addStatusCode(MUCUser.Status.create(statusString)); 068 break; 069 case "decline": 070 mucUser.setDecline(parseDecline(parser)); 071 break; 072 case "destroy": 073 mucUser.setDestroy(MUCParserUtils.parseDestroy(parser)); 074 break; 075 } 076 break; 077 case END_ELEMENT: 078 if (parser.getDepth() == initialDepth) { 079 break outerloop; 080 } 081 break; 082 default: 083 // Catch all for incomplete switch (MissingCasesInEnumSwitch) statement. 084 break; 085 } 086 } 087 088 return mucUser; 089 } 090 091 private static MUCUser.Invite parseInvite(XmlPullParser parser) throws XmlPullParserException, IOException { 092 String reason = null; 093 EntityBareJid to = ParserUtils.getBareJidAttribute(parser, "to"); 094 EntityJid from = ParserUtils.getEntityJidAttribute(parser, "from"); 095 096 outerloop: while (true) { 097 XmlPullParser.Event eventType = parser.next(); 098 if (eventType == XmlPullParser.Event.START_ELEMENT) { 099 if (parser.getName().equals("reason")) { 100 reason = parser.nextText(); 101 } 102 } 103 else if (eventType == XmlPullParser.Event.END_ELEMENT) { 104 if (parser.getName().equals("invite")) { 105 break outerloop; 106 } 107 } 108 } 109 return new MUCUser.Invite(reason, from, to); 110 } 111 112 private static MUCUser.Decline parseDecline(XmlPullParser parser) throws XmlPullParserException, IOException { 113 String reason = null; 114 EntityBareJid to = ParserUtils.getBareJidAttribute(parser, "to"); 115 EntityBareJid from = ParserUtils.getBareJidAttribute(parser, "from"); 116 117 outerloop: while (true) { 118 XmlPullParser.Event eventType = parser.next(); 119 if (eventType == XmlPullParser.Event.START_ELEMENT) { 120 if (parser.getName().equals("reason")) { 121 reason = parser.nextText(); 122 } 123 } 124 else if (eventType == XmlPullParser.Event.END_ELEMENT) { 125 if (parser.getName().equals("decline")) { 126 break outerloop; 127 } 128 } 129 } 130 return new MUCUser.Decline(reason, from, to); 131 } 132}