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