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 020 021import java.io.IOException; 022 023import org.jivesoftware.smack.provider.ExtensionElementProvider; 024import org.jivesoftware.smackx.muc.packet.MUCUser; 025import org.xmlpull.v1.XmlPullParser; 026import org.xmlpull.v1.XmlPullParserException; 027 028/** 029 * The MUCUserProvider parses packets with extended presence information about 030 * roles and affiliations. 031 * 032 * @author Gaston Dombiak 033 */ 034public class MUCUserProvider extends ExtensionElementProvider<MUCUser> { 035 036 /** 037 * Parses a MUCUser stanza(/packet) (extension sub-packet). 038 * 039 * @param parser the XML parser, positioned at the starting element of the extension. 040 * @return a PacketExtension. 041 * @throws IOException 042 * @throws XmlPullParserException 043 */ 044 @Override 045 public MUCUser parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException { 046 MUCUser mucUser = new MUCUser(); 047 outerloop: while (true) { 048 switch (parser.next()) { 049 case XmlPullParser.START_TAG: 050 switch (parser.getName()) { 051 case "invite": 052 mucUser.setInvite(parseInvite(parser)); 053 break; 054 case "item": 055 mucUser.setItem(MUCParserUtils.parseItem(parser)); 056 break; 057 case "password": 058 mucUser.setPassword(parser.nextText()); 059 break; 060 case "status": 061 String statusString = parser.getAttributeValue("", "code"); 062 mucUser.addStatusCode(MUCUser.Status.create(statusString)); 063 break; 064 case "decline": 065 mucUser.setDecline(parseDecline(parser)); 066 break; 067 case "destroy": 068 mucUser.setDestroy(MUCParserUtils.parseDestroy(parser)); 069 break; 070 } 071 break; 072 case XmlPullParser.END_TAG: 073 if (parser.getDepth() == initialDepth) { 074 break outerloop; 075 } 076 break; 077 } 078 } 079 080 return mucUser; 081 } 082 083 private static MUCUser.Invite parseInvite(XmlPullParser parser) throws XmlPullParserException, IOException { 084 boolean done = false; 085 MUCUser.Invite invite = new MUCUser.Invite(); 086 invite.setFrom(parser.getAttributeValue("", "from")); 087 invite.setTo(parser.getAttributeValue("", "to")); 088 while (!done) { 089 int eventType = parser.next(); 090 if (eventType == XmlPullParser.START_TAG) { 091 if (parser.getName().equals("reason")) { 092 invite.setReason(parser.nextText()); 093 } 094 } 095 else if (eventType == XmlPullParser.END_TAG) { 096 if (parser.getName().equals("invite")) { 097 done = true; 098 } 099 } 100 } 101 return invite; 102 } 103 104 private static MUCUser.Decline parseDecline(XmlPullParser parser) throws XmlPullParserException, IOException { 105 boolean done = false; 106 MUCUser.Decline decline = new MUCUser.Decline(); 107 decline.setFrom(parser.getAttributeValue("", "from")); 108 decline.setTo(parser.getAttributeValue("", "to")); 109 while (!done) { 110 int eventType = parser.next(); 111 if (eventType == XmlPullParser.START_TAG) { 112 if (parser.getName().equals("reason")) { 113 decline.setReason(parser.nextText()); 114 } 115 } 116 else if (eventType == XmlPullParser.END_TAG) { 117 if (parser.getName().equals("decline")) { 118 done = true; 119 } 120 } 121 } 122 return decline; 123 } 124}