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