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.Jid;
030
031public class UserID implements ExtensionElement {
032
033    /**
034     * Element name of the stanza extension.
035     */
036    public static final String ELEMENT_NAME = "user";
037
038    /**
039     * Namespace of the stanza extension.
040     */
041    public static final String NAMESPACE = "http://jivesoftware.com/protocol/workgroup";
042
043    private final Jid userID;
044
045    public UserID(Jid userID) {
046        this.userID = userID;
047    }
048
049    public Jid getUserID() {
050        return this.userID;
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(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
065        StringBuilder buf = new StringBuilder();
066
067        buf.append('<').append(ELEMENT_NAME).append(" xmlns=\"").append(NAMESPACE).append("\" ");
068        buf.append("id=\"").append(this.getUserID());
069        buf.append("\"/>");
070
071        return buf.toString();
072    }
073
074    public static class Provider extends ExtensionElementProvider<UserID> {
075
076        @Override
077        public UserID parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment)
078                        throws XmlPullParserException, IOException {
079            Jid userID = ParserUtils.getJidAttribute(parser, "id");
080
081            // Advance to end of extension.
082            parser.next();
083
084            return new UserID(userID);
085        }
086    }
087}