1 /** 2 * $RCSfile$ 3 * $Revision$ 4 * $Date$ 5 * 6 * Copyright 2003-2007 Jive Software. 7 * 8 * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); 9 * you may not use this file except in compliance with the License. 10 * You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, software 15 * distributed under the License is distributed on an "AS IS" BASIS, 16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 * See the License for the specific language governing permissions and 18 * limitations under the License. 19 */ 20 21 package org.jivesoftware.smackx.provider; 22 23 import org.jivesoftware.smack.packet.*; 24 import org.jivesoftware.smack.provider.*; 25 import org.jivesoftware.smackx.packet.*; 26 import org.xmlpull.v1.XmlPullParser; 27 28 /** 29 * The MUCUserProvider parses packets with extended presence information about 30 * roles and affiliations. 31 * 32 * @author Gaston Dombiak 33 */ 34 public class MUCUserProvider implements PacketExtensionProvider { 35 36 /** 37 * Creates a new MUCUserProvider. 38 * ProviderManager requires that every PacketExtensionProvider has a public, no-argument 39 * constructor 40 */ 41 public MUCUserProvider() { 42 } 43 44 /** 45 * Parses a MUCUser packet (extension sub-packet). 46 * 47 * @param parser the XML parser, positioned at the starting element of the extension. 48 * @return a PacketExtension. 49 * @throws Exception if a parsing error occurs. 50 */ 51 public PacketExtension parseExtension(XmlPullParser parser) throws Exception { 52 MUCUser mucUser = new MUCUser(); 53 boolean done = false; 54 while (!done) { 55 int eventType = parser.next(); 56 if (eventType == XmlPullParser.START_TAG) { 57 if (parser.getName().equals("invite")) { 58 mucUser.setInvite(parseInvite(parser)); 59 } 60 if (parser.getName().equals("item")) { 61 mucUser.setItem(parseItem(parser)); 62 } 63 if (parser.getName().equals("password")) { 64 mucUser.setPassword(parser.nextText()); 65 } 66 if (parser.getName().equals("status")) { 67 mucUser.setStatus(new MUCUser.Status(parser.getAttributeValue("", "code"))); 68 } 69 if (parser.getName().equals("decline")) { 70 mucUser.setDecline(parseDecline(parser)); 71 } 72 if (parser.getName().equals("destroy")) { 73 mucUser.setDestroy(parseDestroy(parser)); 74 } 75 } 76 else if (eventType == XmlPullParser.END_TAG) { 77 if (parser.getName().equals("x")) { 78 done = true; 79 } 80 } 81 } 82 83 return mucUser; 84 } 85 86 private MUCUser.Item parseItem(XmlPullParser parser) throws Exception { 87 boolean done = false; 88 MUCUser.Item item = 89 new MUCUser.Item( 90 parser.getAttributeValue("", "affiliation"), 91 parser.getAttributeValue("", "role")); 92 item.setNick(parser.getAttributeValue("", "nick")); 93 item.setJid(parser.getAttributeValue("", "jid")); 94 while (!done) { 95 int eventType = parser.next(); 96 if (eventType == XmlPullParser.START_TAG) { 97 if (parser.getName().equals("actor")) { 98 item.setActor(parser.getAttributeValue("", "jid")); 99 } 100 if (parser.getName().equals("reason")) { 101 item.setReason(parser.nextText()); 102 } 103 } 104 else if (eventType == XmlPullParser.END_TAG) { 105 if (parser.getName().equals("item")) { 106 done = true; 107 } 108 } 109 } 110 return item; 111 } 112 113 private MUCUser.Invite parseInvite(XmlPullParser parser) throws Exception { 114 boolean done = false; 115 MUCUser.Invite invite = new MUCUser.Invite(); 116 invite.setFrom(parser.getAttributeValue("", "from")); 117 invite.setTo(parser.getAttributeValue("", "to")); 118 while (!done) { 119 int eventType = parser.next(); 120 if (eventType == XmlPullParser.START_TAG) { 121 if (parser.getName().equals("reason")) { 122 invite.setReason(parser.nextText()); 123 } 124 } 125 else if (eventType == XmlPullParser.END_TAG) { 126 if (parser.getName().equals("invite")) { 127 done = true; 128 } 129 } 130 } 131 return invite; 132 } 133 134 private MUCUser.Decline parseDecline(XmlPullParser parser) throws Exception { 135 boolean done = false; 136 MUCUser.Decline decline = new MUCUser.Decline(); 137 decline.setFrom(parser.getAttributeValue("", "from")); 138 decline.setTo(parser.getAttributeValue("", "to")); 139 while (!done) { 140 int eventType = parser.next(); 141 if (eventType == XmlPullParser.START_TAG) { 142 if (parser.getName().equals("reason")) { 143 decline.setReason(parser.nextText()); 144 } 145 } 146 else if (eventType == XmlPullParser.END_TAG) { 147 if (parser.getName().equals("decline")) { 148 done = true; 149 } 150 } 151 } 152 return decline; 153 } 154 155 private MUCUser.Destroy parseDestroy(XmlPullParser parser) throws Exception { 156 boolean done = false; 157 MUCUser.Destroy destroy = new MUCUser.Destroy(); 158 destroy.setJid(parser.getAttributeValue("", "jid")); 159 while (!done) { 160 int eventType = parser.next(); 161 if (eventType == XmlPullParser.START_TAG) { 162 if (parser.getName().equals("reason")) { 163 destroy.setReason(parser.nextText()); 164 } 165 } 166 else if (eventType == XmlPullParser.END_TAG) { 167 if (parser.getName().equals("destroy")) { 168 done = true; 169 } 170 } 171 } 172 return destroy; 173 } 174 } 175