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