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