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