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.PacketExtension;
020import org.jivesoftware.smack.provider.PacketExtensionProvider;
021import org.xmlpull.v1.XmlPullParser;
022
023/**
024 * A minimalistic implementation of a {@link PacketExtension} 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 PacketExtension {
030
031        public static final String NAMESPACE = "http://jabber.org/protocol/nick";
032
033        public static final String ELEMENT_NAME = "nick";
034
035        private String name = null;
036
037        public Nick(String name) {
038                this.name = name;
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        /**
051         * Sets the value of this nickname
052         * 
053         * @param name
054         *            the name to set
055         */
056        public void setName(String name) {
057                this.name = name;
058        }
059
060        /*
061         * (non-Javadoc)
062         * 
063         * @see org.jivesoftware.smack.packet.PacketExtension#getElementName()
064         */
065        public String getElementName() {
066                return ELEMENT_NAME;
067        }
068
069        /*
070         * (non-Javadoc)
071         * 
072         * @see org.jivesoftware.smack.packet.PacketExtension#getNamespace()
073         */
074        public String getNamespace() {
075                return NAMESPACE;
076        }
077
078        /*
079         * (non-Javadoc)
080         * 
081         * @see org.jivesoftware.smack.packet.PacketExtension#toXML()
082         */
083        public String toXML() {
084                final StringBuilder buf = new StringBuilder();
085
086                buf.append("<").append(ELEMENT_NAME).append(" xmlns=\"").append(
087                                NAMESPACE).append("\">");
088                buf.append(getName());
089                buf.append("</").append(ELEMENT_NAME).append('>');
090
091                return buf.toString();
092        }
093
094        public static class Provider implements PacketExtensionProvider {
095
096                public PacketExtension parseExtension(XmlPullParser parser)
097                                throws Exception {
098                        
099            parser.next();
100                        final String name = parser.getText();
101
102                        // Advance to end of extension.
103                        while(parser.getEventType() != XmlPullParser.END_TAG) {
104                                parser.next();
105                        }
106
107                        return new Nick(name);
108                }
109        }
110}