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