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 java.io.IOException;
  19. import java.util.ArrayList;
  20. import java.util.Collection;
  21. import java.util.Iterator;
  22. import java.util.List;

  23. import org.jivesoftware.smack.packet.IQ;
  24. import org.jivesoftware.smack.packet.IqData;
  25. import org.jivesoftware.smack.packet.XmlEnvironment;
  26. import org.jivesoftware.smack.provider.IqProvider;
  27. import org.jivesoftware.smack.xml.XmlPullParser;
  28. import org.jivesoftware.smack.xml.XmlPullParserException;

  29. public class ChatSettings extends IQ {

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

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

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

  42.     private final List<ChatSetting> settings;
  43.     private String key;
  44.     private int type = -1;

  45.     /**
  46.      * Element name of the stanza extension.
  47.      */
  48.     public static final String ELEMENT_NAME = "chat-settings";

  49.     /**
  50.      * Namespace of the stanza extension.
  51.      */
  52.     public static final String NAMESPACE = "http://jivesoftware.com/protocol/workgroup";

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

  57.     public ChatSettings(String key) {
  58.         this();
  59.         setKey(key);
  60.     }

  61.     public void setKey(String key) {
  62.         this.key = key;
  63.     }

  64.     public void setType(int type) {
  65.         this.type = type;
  66.     }

  67.     public void addSetting(ChatSetting setting) {
  68.         settings.add(setting);
  69.     }

  70.     public Collection<ChatSetting> getSettings() {
  71.         return settings;
  72.     }

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

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

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

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

  103.     /**
  104.      * Stanza extension provider for AgentStatusRequest packets.
  105.      */
  106.     public static class InternalProvider extends IqProvider<ChatSettings> {

  107.         @Override
  108.         public ChatSettings parse(XmlPullParser parser, int initialDepth, IqData iqData, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException {
  109.             if (parser.getEventType() != XmlPullParser.Event.START_ELEMENT) {
  110.                 throw new IllegalStateException("Parser not in proper position, or bad XML.");
  111.             }

  112.             ChatSettings chatSettings = new ChatSettings();

  113.             boolean done = false;
  114.             while (!done) {
  115.                 XmlPullParser.Event eventType = parser.next();
  116.                 if (eventType == XmlPullParser.Event.START_ELEMENT && "chat-setting".equals(parser.getName())) {
  117.                     chatSettings.addSetting(parseChatSetting(parser));

  118.                 }
  119.                 else if (eventType == XmlPullParser.Event.END_ELEMENT && ELEMENT_NAME.equals(parser.getName())) {
  120.                     done = true;
  121.                 }
  122.             }
  123.             return chatSettings;
  124.         }

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

  126.             boolean done = false;
  127.             String key = null;
  128.             String value = null;
  129.             int type = 0;

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