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