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
21package org.jivesoftware.smackx.provider;
22
23import org.jivesoftware.smack.packet.*;
24import org.jivesoftware.smack.provider.*;
25import org.jivesoftware.smack.util.PacketParserUtils;
26import org.jivesoftware.smackx.packet.MUCOwner;
27import org.xmlpull.v1.XmlPullParser;
28
29/**
30 * The MUCOwnerProvider parses MUCOwner packets. (@see MUCOwner)
31 *
32 * @author Gaston Dombiak
33 */
34public class MUCOwnerProvider implements IQProvider {
35
36    public IQ parseIQ(XmlPullParser parser) throws Exception {
37        MUCOwner mucOwner = new MUCOwner();
38        boolean done = false;
39        while (!done) {
40            int eventType = parser.next();
41            if (eventType == XmlPullParser.START_TAG) {
42                if (parser.getName().equals("item")) {
43                    mucOwner.addItem(parseItem(parser));
44                }
45                else if (parser.getName().equals("destroy")) {
46                    mucOwner.setDestroy(parseDestroy(parser));
47                }
48                // Otherwise, it must be a packet extension.
49                else {
50                    mucOwner.addExtension(PacketParserUtils.parsePacketExtension(parser.getName(),
51                            parser.getNamespace(), parser));
52                }
53            }
54            else if (eventType == XmlPullParser.END_TAG) {
55                if (parser.getName().equals("query")) {
56                    done = true;
57                }
58            }
59        }
60
61        return mucOwner;
62    }
63
64    private MUCOwner.Item parseItem(XmlPullParser parser) throws Exception {
65        boolean done = false;
66        MUCOwner.Item item = new MUCOwner.Item(parser.getAttributeValue("", "affiliation"));
67        item.setNick(parser.getAttributeValue("", "nick"));
68        item.setRole(parser.getAttributeValue("", "role"));
69        item.setJid(parser.getAttributeValue("", "jid"));
70        while (!done) {
71            int eventType = parser.next();
72            if (eventType == XmlPullParser.START_TAG) {
73                if (parser.getName().equals("actor")) {
74                    item.setActor(parser.getAttributeValue("", "jid"));
75                }
76                if (parser.getName().equals("reason")) {
77                    item.setReason(parser.nextText());
78                }
79            }
80            else if (eventType == XmlPullParser.END_TAG) {
81                if (parser.getName().equals("item")) {
82                    done = true;
83                }
84            }
85        }
86        return item;
87    }
88
89    private MUCOwner.Destroy parseDestroy(XmlPullParser parser) throws Exception {
90        boolean done = false;
91        MUCOwner.Destroy destroy = new MUCOwner.Destroy();
92        destroy.setJid(parser.getAttributeValue("", "jid"));
93        while (!done) {
94            int eventType = parser.next();
95            if (eventType == XmlPullParser.START_TAG) {
96                if (parser.getName().equals("reason")) {
97                    destroy.setReason(parser.nextText());
98                }
99            }
100            else if (eventType == XmlPullParser.END_TAG) {
101                if (parser.getName().equals("destroy")) {
102                    done = true;
103                }
104            }
105        }
106        return destroy;
107    }
108}
109