ChatSettings.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.settings;

  18. import org.jivesoftware.smack.packet.IQ;
  19. import org.jivesoftware.smack.provider.IQProvider;
  20. import org.xmlpull.v1.XmlPullParser;
  21. import org.xmlpull.v1.XmlPullParserException;

  22. import java.io.IOException;
  23. import java.util.ArrayList;
  24. import java.util.Collection;
  25. import java.util.Iterator;
  26. import java.util.List;

  27. public class ChatSettings extends IQ {

  28.     /**
  29.      * Defined as image type.
  30.      */
  31.     public static final int IMAGE_SETTINGS = 0;

  32.     /**
  33.      * Defined as Text settings type.
  34.      */
  35.     public static final int TEXT_SETTINGS = 1;

  36.     /**
  37.      * Defined as Bot settings type.
  38.      */
  39.     public static final int BOT_SETTINGS = 2;

  40.     private List<ChatSetting> settings;
  41.     private String key;
  42.     private int type = -1;

  43.     /**
  44.      * Element name of the packet extension.
  45.      */
  46.     public static final String ELEMENT_NAME = "chat-settings";

  47.     /**
  48.      * Namespace of the packet extension.
  49.      */
  50.     public static final String NAMESPACE = "http://jivesoftware.com/protocol/workgroup";

  51.     public ChatSettings() {
  52.         super(ELEMENT_NAME, NAMESPACE);
  53.         settings = new ArrayList<ChatSetting>();
  54.     }

  55.     public ChatSettings(String key) {
  56.         this();
  57.         setKey(key);
  58.     }

  59.     public void setKey(String key) {
  60.         this.key = key;
  61.     }

  62.     public void setType(int type) {
  63.         this.type = type;
  64.     }

  65.     public void addSetting(ChatSetting setting) {
  66.         settings.add(setting);
  67.     }

  68.     public Collection<ChatSetting> getSettings() {
  69.         return settings;
  70.     }

  71.     public ChatSetting getChatSetting(String key) {
  72.         Collection<ChatSetting> col = getSettings();
  73.         if (col != null) {
  74.             Iterator<ChatSetting> iter = col.iterator();
  75.             while (iter.hasNext()) {
  76.                 ChatSetting chatSetting = iter.next();
  77.                 if (chatSetting.getKey().equals(key)) {
  78.                     return chatSetting;
  79.                 }
  80.             }
  81.         }
  82.         return null;
  83.     }

  84.     public ChatSetting getFirstEntry() {
  85.         if (settings.size() > 0) {
  86.             return (ChatSetting)settings.get(0);
  87.         }
  88.         return null;
  89.     }

  90.     @Override
  91.     protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder buf) {
  92.         if (key != null) {
  93.             buf.append(" key=\"" + key + "\"");
  94.         }

  95.         if (type != -1) {
  96.             buf.append(" type=\"" + type + "\"");
  97.         }
  98.         buf.setEmptyElement();
  99.         return buf;
  100.     }

  101.     /**
  102.      * Packet extension provider for AgentStatusRequest packets.
  103.      */
  104.     public static class InternalProvider extends IQProvider<ChatSettings> {

  105.         @Override
  106.         public ChatSettings parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException {
  107.             if (parser.getEventType() != XmlPullParser.START_TAG) {
  108.                 throw new IllegalStateException("Parser not in proper position, or bad XML.");
  109.             }

  110.             ChatSettings chatSettings = new ChatSettings();

  111.             boolean done = false;
  112.             while (!done) {
  113.                 int eventType = parser.next();
  114.                 if ((eventType == XmlPullParser.START_TAG) && ("chat-setting".equals(parser.getName()))) {
  115.                     chatSettings.addSetting(parseChatSetting(parser));

  116.                 }
  117.                 else if (eventType == XmlPullParser.END_TAG && ELEMENT_NAME.equals(parser.getName())) {
  118.                     done = true;
  119.                 }
  120.             }
  121.             return chatSettings;
  122.         }

  123.         private ChatSetting parseChatSetting(XmlPullParser parser) throws XmlPullParserException, IOException {

  124.             boolean done = false;
  125.             String key = null;
  126.             String value = null;
  127.             int type = 0;

  128.             while (!done) {
  129.                 int eventType = parser.next();
  130.                 if ((eventType == XmlPullParser.START_TAG) && ("key".equals(parser.getName()))) {
  131.                     key = parser.nextText();
  132.                 }
  133.                 else if ((eventType == XmlPullParser.START_TAG) && ("value".equals(parser.getName()))) {
  134.                     value = parser.nextText();
  135.                 }
  136.                 else if ((eventType == XmlPullParser.START_TAG) && ("type".equals(parser.getName()))) {
  137.                     type = Integer.parseInt(parser.nextText());
  138.                 }
  139.                 else if (eventType == XmlPullParser.END_TAG && "chat-setting".equals(parser.getName())) {
  140.                     done = true;
  141.                 }
  142.             }
  143.             return new ChatSetting(key, value, type);
  144.         }
  145.     }
  146. }