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 java.io.IOException;
020
021import org.jivesoftware.smack.packet.ExtensionElement;
022import org.jivesoftware.smack.provider.ExtensionElementProvider;
023import org.xmlpull.v1.XmlPullParser;
024import org.xmlpull.v1.XmlPullParserException;
025
026/**
027 * A minimalistic implementation of a {@link ExtensionElement} for nicknames.
028 * 
029 * @author Guus der Kinderen, guus.der.kinderen@gmail.com
030 * @see <a href="http://xmpp.org/extensions/xep-0172.html">XEP-0172: User Nickname</a>
031 */
032public class Nick implements ExtensionElement {
033
034    public static final String NAMESPACE = "http://jabber.org/protocol/nick";
035
036    public static final String ELEMENT_NAME = "nick";
037
038    private String name = null;
039
040    public Nick(String name) {
041        this.name = name;
042    }
043
044    /**
045     * The value of this nickname.
046     * 
047     * @return the nickname
048     */
049    public String getName() {
050        return name;
051    }
052
053    /**
054     * Sets the value of this nickname.
055     * 
056     * @param name
057     *            the name to set
058     */
059    public void setName(String name) {
060        this.name = name;
061    }
062
063    /*
064     * (non-Javadoc)
065     * 
066     * @see org.jivesoftware.smack.packet.PacketExtension#getElementName()
067     */
068    @Override
069    public String getElementName() {
070        return ELEMENT_NAME;
071    }
072
073    /*
074     * (non-Javadoc)
075     * 
076     * @see org.jivesoftware.smack.packet.PacketExtension#getNamespace()
077     */
078    @Override
079    public String getNamespace() {
080        return NAMESPACE;
081    }
082
083    /*
084     * (non-Javadoc)
085     * 
086     * @see org.jivesoftware.smack.packet.PacketExtension#toXML()
087     */
088    @Override
089    public String toXML() {
090        final StringBuilder buf = new StringBuilder();
091
092        buf.append('<').append(ELEMENT_NAME).append(" xmlns=\"").append(
093                NAMESPACE).append("\">");
094        buf.append(getName());
095        buf.append("</").append(ELEMENT_NAME).append('>');
096
097        return buf.toString();
098    }
099
100    public static class Provider extends ExtensionElementProvider<Nick> {
101
102        @Override
103        public Nick parse(XmlPullParser parser, int initialDepth)
104                        throws XmlPullParserException, IOException {
105            final String name = parser.nextText();
106
107            return new Nick(name);
108        }
109    }
110}