Nick.java

  1. /**
  2.  *
  3.  * Copyright 2003-2007 Jive Software.
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smackx.nick.packet;

  18. import java.io.IOException;

  19. import org.jivesoftware.smack.packet.ExtensionElement;
  20. import org.jivesoftware.smack.provider.ExtensionElementProvider;
  21. import org.xmlpull.v1.XmlPullParser;
  22. import org.xmlpull.v1.XmlPullParserException;

  23. /**
  24.  * A minimalistic implementation of a {@link ExtensionElement} for nicknames.
  25.  *
  26.  * @author Guus der Kinderen, guus.der.kinderen@gmail.com
  27.  * @see <a href="http://xmpp.org/extensions/xep-0172.html">XEP-0172: User Nickname</a>
  28.  */
  29. public class Nick implements ExtensionElement {

  30.     public static final String NAMESPACE = "http://jabber.org/protocol/nick";

  31.     public static final String ELEMENT_NAME = "nick";

  32.     private String name = null;

  33.     public Nick(String name) {
  34.         this.name = name;
  35.     }

  36.     /**
  37.      * The value of this nickname
  38.      *
  39.      * @return the nickname
  40.      */
  41.     public String getName() {
  42.         return name;
  43.     }

  44.     /**
  45.      * Sets the value of this nickname
  46.      *
  47.      * @param name
  48.      *            the name to set
  49.      */
  50.     public void setName(String name) {
  51.         this.name = name;
  52.     }

  53.     /*
  54.      * (non-Javadoc)
  55.      *
  56.      * @see org.jivesoftware.smack.packet.PacketExtension#getElementName()
  57.      */
  58.     public String getElementName() {
  59.         return ELEMENT_NAME;
  60.     }

  61.     /*
  62.      * (non-Javadoc)
  63.      *
  64.      * @see org.jivesoftware.smack.packet.PacketExtension#getNamespace()
  65.      */
  66.     public String getNamespace() {
  67.         return NAMESPACE;
  68.     }

  69.     /*
  70.      * (non-Javadoc)
  71.      *
  72.      * @see org.jivesoftware.smack.packet.PacketExtension#toXML()
  73.      */
  74.     public String toXML() {
  75.         final StringBuilder buf = new StringBuilder();

  76.         buf.append("<").append(ELEMENT_NAME).append(" xmlns=\"").append(
  77.                 NAMESPACE).append("\">");
  78.         buf.append(getName());
  79.         buf.append("</").append(ELEMENT_NAME).append('>');

  80.         return buf.toString();
  81.     }

  82.     public static class Provider extends ExtensionElementProvider<Nick> {

  83.         @Override
  84.         public Nick parse(XmlPullParser parser, int initialDepth)
  85.                         throws XmlPullParserException, IOException {

  86.             parser.next();
  87.             final String name = parser.getText();

  88.             // Advance to end of extension.
  89.             while(parser.getEventType() != XmlPullParser.END_TAG) {
  90.                 parser.next();
  91.             }

  92.             return new Nick(name);
  93.         }
  94.     }
  95. }