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