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 public String getElementName() { 069 return ELEMENT_NAME; 070 } 071 072 /* 073 * (non-Javadoc) 074 * 075 * @see org.jivesoftware.smack.packet.PacketExtension#getNamespace() 076 */ 077 public String getNamespace() { 078 return NAMESPACE; 079 } 080 081 /* 082 * (non-Javadoc) 083 * 084 * @see org.jivesoftware.smack.packet.PacketExtension#toXML() 085 */ 086 public String toXML() { 087 final StringBuilder buf = new StringBuilder(); 088 089 buf.append("<").append(ELEMENT_NAME).append(" xmlns=\"").append( 090 NAMESPACE).append("\">"); 091 buf.append(getName()); 092 buf.append("</").append(ELEMENT_NAME).append('>'); 093 094 return buf.toString(); 095 } 096 097 public static class Provider extends ExtensionElementProvider<Nick> { 098 099 @Override 100 public Nick parse(XmlPullParser parser, int initialDepth) 101 throws XmlPullParserException, IOException { 102 103 parser.next(); 104 final String name = parser.getText(); 105 106 // Advance to end of extension. 107 while(parser.getEventType() != XmlPullParser.END_TAG) { 108 parser.next(); 109 } 110 111 return new Nick(name); 112 } 113 } 114}