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