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 */
017
018package org.jivesoftware.smackx.workgroup.packet;
019
020import java.io.IOException;
021
022import org.jivesoftware.smack.packet.ExtensionElement;
023import org.jivesoftware.smack.provider.ExtensionElementProvider;
024import org.xmlpull.v1.XmlPullParser;
025import org.xmlpull.v1.XmlPullParserException;
026
027/**
028 * A stanza(/packet) extension that contains information about the user and agent in a
029 * workgroup chat. The stanza(/packet) extension is attached to group chat invitations.
030 */
031public class WorkgroupInformation implements ExtensionElement {
032
033    /**
034     * Element name of the stanza(/packet) extension.
035     */
036    public static final String ELEMENT_NAME = "workgroup";
037
038    /**
039     * Namespace of the stanza(/packet) extension.
040     */
041    public static final String NAMESPACE = "http://jabber.org/protocol/workgroup";
042
043    private String workgroupJID;
044
045    public WorkgroupInformation(String workgroupJID){
046        this.workgroupJID = workgroupJID;
047    }
048
049    public String getWorkgroupJID() {
050        return workgroupJID;
051    }
052
053    @Override
054    public String getElementName() {
055        return ELEMENT_NAME;
056    }
057
058    @Override
059    public String getNamespace() {
060        return NAMESPACE;
061    }
062
063    @Override
064    public String toXML() {
065        StringBuilder buf = new StringBuilder();
066
067        buf.append('<').append(ELEMENT_NAME);
068        buf.append(" jid=\"").append(getWorkgroupJID()).append('"');
069        buf.append(" xmlns=\"").append(NAMESPACE).append("\" />");
070
071        return buf.toString();
072    }
073
074    public static class Provider extends ExtensionElementProvider<WorkgroupInformation> {
075
076        /**
077         * PacketExtensionProvider implementation.
078         * @throws IOException 
079         * @throws XmlPullParserException 
080         */
081        @Override
082        public WorkgroupInformation parse(XmlPullParser parser,
083                        int initialDepth) throws XmlPullParserException,
084                        IOException {
085            String workgroupJID = parser.getAttributeValue("", "jid");
086
087            // since this is a start and end tag, and we arrive on the start, this should guarantee
088            //      we leave on the end
089            parser.next();
090
091            return new WorkgroupInformation(workgroupJID);
092        }
093    }
094}