Home | History | Annotate | Download | only in packet
      1 /**
      2  * $Revision$
      3  * $Date$
      4  *
      5  * Copyright 2003-2007 Jive Software.
      6  *
      7  * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
      8  * you may not use this file except in compliance with the License.
      9  * You may obtain a copy of the License at
     10  *
     11  *     http://www.apache.org/licenses/LICENSE-2.0
     12  *
     13  * Unless required by applicable law or agreed to in writing, software
     14  * distributed under the License is distributed on an "AS IS" BASIS,
     15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     16  * See the License for the specific language governing permissions and
     17  * limitations under the License.
     18  */
     19 package org.jivesoftware.smackx.packet;
     20 
     21 import org.jivesoftware.smack.packet.PacketExtension;
     22 import org.jivesoftware.smack.provider.PacketExtensionProvider;
     23 import org.xmlpull.v1.XmlPullParser;
     24 
     25 /**
     26  * A minimalistic implementation of a {@link PacketExtension} for nicknames.
     27  *
     28  * @author Guus der Kinderen, guus.der.kinderen (at) gmail.com
     29  * @see <a href="http://xmpp.org/extensions/xep-0172.html">XEP-0172: User Nickname</a>
     30  */
     31 public class Nick implements PacketExtension {
     32 
     33 	public static final String NAMESPACE = "http://jabber.org/protocol/nick";
     34 
     35 	public static final String ELEMENT_NAME = "nick";
     36 
     37 	private String name = null;
     38 
     39 	public Nick(String name) {
     40 		this.name = name;
     41 	}
     42 
     43 	/**
     44 	 * The value of this nickname
     45 	 *
     46 	 * @return the nickname
     47 	 */
     48 	public String getName() {
     49 		return name;
     50 	}
     51 
     52 	/**
     53 	 * Sets the value of this nickname
     54 	 *
     55 	 * @param name
     56 	 *            the name to set
     57 	 */
     58 	public void setName(String name) {
     59 		this.name = name;
     60 	}
     61 
     62 	/*
     63 	 * (non-Javadoc)
     64 	 *
     65 	 * @see org.jivesoftware.smack.packet.PacketExtension#getElementName()
     66 	 */
     67 	public String getElementName() {
     68 		return ELEMENT_NAME;
     69 	}
     70 
     71 	/*
     72 	 * (non-Javadoc)
     73 	 *
     74 	 * @see org.jivesoftware.smack.packet.PacketExtension#getNamespace()
     75 	 */
     76 	public String getNamespace() {
     77 		return NAMESPACE;
     78 	}
     79 
     80 	/*
     81 	 * (non-Javadoc)
     82 	 *
     83 	 * @see org.jivesoftware.smack.packet.PacketExtension#toXML()
     84 	 */
     85 	public String toXML() {
     86 		final StringBuilder buf = new StringBuilder();
     87 
     88 		buf.append("<").append(ELEMENT_NAME).append(" xmlns=\"").append(
     89 				NAMESPACE).append("\">");
     90 		buf.append(getName());
     91 		buf.append("</").append(ELEMENT_NAME).append('>');
     92 
     93 		return buf.toString();
     94 	}
     95 
     96 	public static class Provider implements PacketExtensionProvider {
     97 
     98 		public PacketExtension parseExtension(XmlPullParser parser)
     99 				throws Exception {
    100 
    101             parser.next();
    102 			final String name = parser.getText();
    103 
    104 			// Advance to end of extension.
    105 			while(parser.getEventType() != XmlPullParser.END_TAG) {
    106 				parser.next();
    107 			}
    108 
    109 			return new Nick(name);
    110 		}
    111 	}
    112 }
    113