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 */
017package org.jivesoftware.smackx.workgroup.packet;
018
019import java.io.IOException;
020
021import org.jivesoftware.smack.packet.IQ;
022import org.jivesoftware.smack.packet.IqData;
023import org.jivesoftware.smack.packet.XmlEnvironment;
024import org.jivesoftware.smack.provider.IqProvider;
025import org.jivesoftware.smack.xml.XmlPullParser;
026import org.jivesoftware.smack.xml.XmlPullParserException;
027
028import org.jxmpp.JxmppContext;
029
030public class MonitorPacket extends IQ {
031
032    private String sessionID;
033
034    private boolean isMonitor;
035
036    public boolean isMonitor() {
037        return isMonitor;
038    }
039
040    public void setMonitor(boolean monitor) {
041        isMonitor = monitor;
042    }
043
044    public String getSessionID() {
045        return sessionID;
046    }
047
048    public void setSessionID(String sessionID) {
049        this.sessionID = sessionID;
050    }
051
052    /**
053     * Element name of the stanza extension.
054     */
055    public static final String ELEMENT_NAME = "monitor";
056
057    /**
058     * Namespace of the stanza extension.
059     */
060    public static final String NAMESPACE = "http://jivesoftware.com/protocol/workgroup";
061
062    public MonitorPacket() {
063        super(ELEMENT_NAME, NAMESPACE);
064    }
065
066    @Override
067    protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder buf) {
068        buf.rightAngleBracket();
069
070        if (sessionID != null) {
071            buf.append("<makeOwner sessionID=\"" + sessionID + "\"></makeOwner>");
072        }
073
074        return buf;
075    }
076
077
078    /**
079     * Stanza extension provider for Monitor Packets.
080     */
081    public static class InternalProvider extends IqProvider<MonitorPacket> {
082
083        @Override
084        public MonitorPacket parse(XmlPullParser parser, int initialDepth, IqData iqData, XmlEnvironment xmlEnvironment, JxmppContext jxmppContext) throws XmlPullParserException, IOException {
085            MonitorPacket packet = new MonitorPacket();
086
087            boolean done = false;
088
089
090            while (!done) {
091                XmlPullParser.Event eventType = parser.next();
092                if (eventType == XmlPullParser.Event.START_ELEMENT && "isMonitor".equals(parser.getName())) {
093                    String value = parser.nextText();
094                    if ("false".equalsIgnoreCase(value)) {
095                        packet.setMonitor(false);
096                    }
097                    else {
098                        packet.setMonitor(true);
099                    }
100                }
101                else if (eventType == XmlPullParser.Event.END_ELEMENT && "monitor".equals(parser.getName())) {
102                    done = true;
103                }
104            }
105
106            return packet;
107        }
108    }
109}