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 */
017package org.jivesoftware.smackx.nick.packet;
018
019import org.jivesoftware.smack.packet.ExtensionElement;
020import org.jivesoftware.smack.util.StringUtils;
021import org.jivesoftware.smack.util.XmlStringBuilder;
022
023/**
024 * A minimalistic implementation of a {@link ExtensionElement} for nicknames.
025 *
026 * @author Guus der Kinderen, guus.der.kinderen@gmail.com
027 * @see <a href="http://xmpp.org/extensions/xep-0172.html">XEP-0172: User Nickname</a>
028 */
029public class Nick implements ExtensionElement {
030
031    public static final String NAMESPACE = "http://jabber.org/protocol/nick";
032
033    public static final String ELEMENT_NAME = "nick";
034
035    private final String name;
036
037    public Nick(String name) {
038        this.name = StringUtils.requireNotNullNorEmpty(name, "Nickname must be given");
039    }
040
041    /**
042     * The value of this nickname.
043     *
044     * @return the nickname
045     */
046    public String getName() {
047        return name;
048    }
049
050    @Override
051    public String getElementName() {
052        return ELEMENT_NAME;
053    }
054
055    @Override
056    public String getNamespace() {
057        return NAMESPACE;
058    }
059
060    @Override
061    public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
062        XmlStringBuilder xml = new XmlStringBuilder(this, enclosingNamespace);
063        xml.rightAngleBracket();
064
065        xml.escape(getName());
066
067        xml.closeElement(this);
068        return xml;
069    }
070}