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 javax.xml.namespace.QName;
023
024import org.jivesoftware.smack.packet.ExtensionElement;
025import org.jivesoftware.smack.packet.XmlEnvironment;
026import org.jivesoftware.smack.provider.ExtensionElementProvider;
027import org.jivesoftware.smack.util.ParserUtils;
028import org.jivesoftware.smack.xml.XmlPullParser;
029import org.jivesoftware.smack.xml.XmlPullParserException;
030
031import org.jxmpp.jid.Jid;
032
033public class UserID implements ExtensionElement {
034
035    /**
036     * Element name of the stanza extension.
037     */
038    public static final String ELEMENT_NAME = "user";
039
040    /**
041     * Namespace of the stanza extension.
042     */
043    public static final String NAMESPACE = "http://jivesoftware.com/protocol/workgroup";
044
045    public static final QName QNAME = new QName(NAMESPACE, ELEMENT_NAME);
046
047    private final Jid userID;
048
049    public UserID(Jid userID) {
050        this.userID = userID;
051    }
052
053    public Jid getUserID() {
054        return this.userID;
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).append(" xmlns=\"").append(NAMESPACE).append("\" ");
072        buf.append("id=\"").append(this.getUserID());
073        buf.append("\"/>");
074
075        return buf.toString();
076    }
077
078    public static class Provider extends ExtensionElementProvider<UserID> {
079
080        @Override
081        public UserID parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment)
082                        throws XmlPullParserException, IOException {
083            Jid userID = ParserUtils.getJidAttribute(parser, "id");
084
085            // Advance to end of extension.
086            parser.next();
087
088            return new UserID(userID);
089        }
090    }
091}