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;
025import org.jxmpp.jid.Jid;
026import org.xmlpull.v1.XmlPullParser;
027import org.xmlpull.v1.XmlPullParserException;
028
029public class UserID implements ExtensionElement {
030
031    /**
032     * Element name of the stanza(/packet) extension.
033     */
034    public static final String ELEMENT_NAME = "user";
035
036    /**
037     * Namespace of the stanza(/packet) extension.
038     */
039    public static final String NAMESPACE = "http://jivesoftware.com/protocol/workgroup";
040
041    private Jid userID;
042
043    public UserID(Jid userID) {
044        this.userID = userID;
045    }
046
047    public Jid getUserID() {
048        return this.userID;
049    }
050
051    @Override
052    public String getElementName() {
053        return ELEMENT_NAME;
054    }
055
056    @Override
057    public String getNamespace() {
058        return NAMESPACE;
059    }
060
061    @Override
062    public String toXML() {
063        StringBuilder buf = new StringBuilder();
064
065        buf.append('<').append(ELEMENT_NAME).append(" xmlns=\"").append(NAMESPACE).append("\" ");
066        buf.append("id=\"").append(this.getUserID());
067        buf.append("\"/>");
068
069        return buf.toString();
070    }
071
072    public static class Provider extends ExtensionElementProvider<UserID> {
073
074        @Override
075        public UserID parse(XmlPullParser parser, int initialDepth)
076                        throws XmlPullParserException, IOException {
077            Jid userID = ParserUtils.getJidAttribute(parser, "id");
078
079            // Advance to end of extension.
080            parser.next();
081
082            return new UserID(userID);
083        }
084    }
085}