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 org.jivesoftware.smack.packet.IQ;
020import org.jivesoftware.smack.provider.IQProvider;
021import org.xmlpull.v1.XmlPullParser;
022
023public class MonitorPacket extends IQ {
024
025    private String sessionID;
026
027    private boolean isMonitor;
028
029    public boolean isMonitor() {
030        return isMonitor;
031    }
032
033    public void setMonitor(boolean monitor) {
034        isMonitor = monitor;
035    }
036
037    public String getSessionID() {
038        return sessionID;
039    }
040
041    public void setSessionID(String sessionID) {
042        this.sessionID = sessionID;
043    }
044
045    /**
046     * Element name of the packet extension.
047     */
048    public static final String ELEMENT_NAME = "monitor";
049
050    /**
051     * Namespace of the packet extension.
052     */
053    public static final String NAMESPACE = "http://jivesoftware.com/protocol/workgroup";
054
055    public String getElementName() {
056        return ELEMENT_NAME;
057    }
058
059    public String getNamespace() {
060        return NAMESPACE;
061    }
062
063    public String getChildElementXML() {
064        StringBuilder buf = new StringBuilder();
065
066        buf.append("<").append(ELEMENT_NAME).append(" xmlns=");
067        buf.append('"');
068        buf.append(NAMESPACE);
069        buf.append('"');
070        buf.append(">");
071        if (sessionID != null) {
072            buf.append("<makeOwner sessionID=\""+sessionID+"\"></makeOwner>");
073        }
074        buf.append("</").append(ELEMENT_NAME).append("> ");
075        return buf.toString();
076    }
077
078
079    /**
080     * Packet extension provider for Monitor Packets.
081     */
082    public static class InternalProvider implements IQProvider {
083
084        public IQ parseIQ(XmlPullParser parser) throws Exception {
085            if (parser.getEventType() != XmlPullParser.START_TAG) {
086                throw new IllegalStateException("Parser not in proper position, or bad XML.");
087            }
088
089            MonitorPacket packet = new MonitorPacket();
090
091            boolean done = false;
092
093
094            while (!done) {
095                int eventType = parser.next();
096                if ((eventType == XmlPullParser.START_TAG) && ("isMonitor".equals(parser.getName()))) {
097                    String value = parser.nextText();
098                    if ("false".equalsIgnoreCase(value)) {
099                        packet.setMonitor(false);
100                    }
101                    else {
102                        packet.setMonitor(true);
103                    }
104                }
105                else if (eventType == XmlPullParser.END_TAG && "monitor".equals(parser.getName())) {
106                    done = true;
107                }
108            }
109
110            return packet;
111        }
112    }
113}