AgentInfo.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.workgroup.packet;

  18. import java.io.IOException;

  19. import org.jivesoftware.smack.packet.IQ;
  20. import org.jivesoftware.smack.packet.IqData;
  21. import org.jivesoftware.smack.packet.XmlEnvironment;
  22. import org.jivesoftware.smack.provider.IqProvider;
  23. import org.jivesoftware.smack.xml.XmlPullParser;
  24. import org.jivesoftware.smack.xml.XmlPullParserException;

  25. /**
  26.  * IQ stanza for retrieving and changing the Agent personal information.
  27.  */
  28. public class AgentInfo extends IQ {

  29.     /**
  30.     * Element name of the stanza extension.
  31.     */
  32.    public static final String ELEMENT_NAME = "agent-info";

  33.    /**
  34.     * Namespace of the stanza extension.
  35.     */
  36.    public static final String NAMESPACE = "http://jivesoftware.com/protocol/workgroup";

  37.     private String jid;
  38.     private String name;

  39.     public AgentInfo() {
  40.         super(ELEMENT_NAME, NAMESPACE);
  41.     }

  42.     /**
  43.      * Returns the Agent's jid.
  44.      *
  45.      * @return the Agent's jid.
  46.      */
  47.     public String getJid() {
  48.         return jid;
  49.     }

  50.     /**
  51.      * Sets the Agent's jid.
  52.      *
  53.      * @param jid the jid of the agent.
  54.      */
  55.     public void setJid(String jid) {
  56.         this.jid = jid;
  57.     }

  58.     /**
  59.      * Returns the Agent's name. The name of the agent may be different than the user's name.
  60.      * This property may be shown in the webchat client.
  61.      *
  62.      * @return the Agent's name.
  63.      */
  64.     public String getName() {
  65.         return name;
  66.     }

  67.     /**
  68.      * Sets the Agent's name. The name of the agent may be different than the user's name.
  69.      * This property may be shown in the webchat client.
  70.      *
  71.      * @param name the new name of the agent.
  72.      */
  73.     public void setName(String name) {
  74.         this.name = name;
  75.     }

  76.     @Override
  77.     protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder buf) {
  78.         buf.rightAngleBracket();

  79.         if (jid != null) {
  80.             buf.append("<jid>").append(getJid()).append("</jid>");
  81.         }
  82.         if (name != null) {
  83.             buf.append("<name>").append(getName()).append("</name>");
  84.         }

  85.         return buf;
  86.     }

  87.     /**
  88.      * An IQProvider for AgentInfo packets.
  89.      *
  90.      * @author Gaston Dombiak
  91.      */
  92.     public static class Provider extends IqProvider<AgentInfo> {

  93.         @Override
  94.         public AgentInfo parse(XmlPullParser parser, int initialDepth, IqData iqData, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException {
  95.             AgentInfo answer = new AgentInfo();

  96.             boolean done = false;
  97.             while (!done) {
  98.                 XmlPullParser.Event eventType = parser.next();
  99.                 if (eventType == XmlPullParser.Event.START_ELEMENT) {
  100.                     if (parser.getName().equals("jid")) {
  101.                         answer.setJid(parser.nextText());
  102.                     }
  103.                     else if (parser.getName().equals("name")) {
  104.                         answer.setName(parser.nextText());
  105.                     }
  106.                 }
  107.                 else if (eventType == XmlPullParser.Event.END_ELEMENT) {
  108.                     if (parser.getName().equals(ELEMENT_NAME)) {
  109.                         done = true;
  110.                     }
  111.                 }
  112.             }

  113.             return answer;
  114.         }
  115.     }
  116. }