1 /** 2 * $Revision$ 3 * $Date$ 4 * 5 * Copyright 2003-2007 Jive Software. 6 * 7 * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); 8 * you may not use this file except in compliance with the License. 9 * You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, software 14 * distributed under the License is distributed on an "AS IS" BASIS, 15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 * See the License for the specific language governing permissions and 17 * limitations under the License. 18 */ 19 20 package org.jivesoftware.smackx.workgroup.settings; 21 22 import org.jivesoftware.smack.packet.IQ; 23 import org.jivesoftware.smack.provider.IQProvider; 24 import org.xmlpull.v1.XmlPullParser; 25 26 import java.util.ArrayList; 27 import java.util.Collection; 28 import java.util.Iterator; 29 import java.util.List; 30 31 public class ChatSettings extends IQ { 32 33 /** 34 * Defined as image type. 35 */ 36 public static final int IMAGE_SETTINGS = 0; 37 38 /** 39 * Defined as Text settings type. 40 */ 41 public static final int TEXT_SETTINGS = 1; 42 43 /** 44 * Defined as Bot settings type. 45 */ 46 public static final int BOT_SETTINGS = 2; 47 48 private List<ChatSetting> settings; 49 private String key; 50 private int type = -1; 51 ChatSettings()52 public ChatSettings() { 53 settings = new ArrayList<ChatSetting>(); 54 } 55 ChatSettings(String key)56 public ChatSettings(String key) { 57 setKey(key); 58 } 59 setKey(String key)60 public void setKey(String key) { 61 this.key = key; 62 } 63 setType(int type)64 public void setType(int type) { 65 this.type = type; 66 } 67 addSetting(ChatSetting setting)68 public void addSetting(ChatSetting setting) { 69 settings.add(setting); 70 } 71 getSettings()72 public Collection<ChatSetting> getSettings() { 73 return settings; 74 } 75 getChatSetting(String key)76 public ChatSetting getChatSetting(String key) { 77 Collection<ChatSetting> col = getSettings(); 78 if (col != null) { 79 Iterator<ChatSetting> iter = col.iterator(); 80 while (iter.hasNext()) { 81 ChatSetting chatSetting = iter.next(); 82 if (chatSetting.getKey().equals(key)) { 83 return chatSetting; 84 } 85 } 86 } 87 return null; 88 } 89 getFirstEntry()90 public ChatSetting getFirstEntry() { 91 if (settings.size() > 0) { 92 return (ChatSetting)settings.get(0); 93 } 94 return null; 95 } 96 97 98 /** 99 * Element name of the packet extension. 100 */ 101 public static final String ELEMENT_NAME = "chat-settings"; 102 103 /** 104 * Namespace of the packet extension. 105 */ 106 public static final String NAMESPACE = "http://jivesoftware.com/protocol/workgroup"; 107 getChildElementXML()108 public String getChildElementXML() { 109 StringBuilder buf = new StringBuilder(); 110 111 buf.append("<").append(ELEMENT_NAME).append(" xmlns="); 112 buf.append('"'); 113 buf.append(NAMESPACE); 114 buf.append('"'); 115 if (key != null) { 116 buf.append(" key=\"" + key + "\""); 117 } 118 119 if (type != -1) { 120 buf.append(" type=\"" + type + "\""); 121 } 122 123 buf.append("></").append(ELEMENT_NAME).append("> "); 124 return buf.toString(); 125 } 126 127 /** 128 * Packet extension provider for AgentStatusRequest packets. 129 */ 130 public static class InternalProvider implements IQProvider { 131 parseIQ(XmlPullParser parser)132 public IQ parseIQ(XmlPullParser parser) throws Exception { 133 if (parser.getEventType() != XmlPullParser.START_TAG) { 134 throw new IllegalStateException("Parser not in proper position, or bad XML."); 135 } 136 137 ChatSettings chatSettings = new ChatSettings(); 138 139 boolean done = false; 140 while (!done) { 141 int eventType = parser.next(); 142 if ((eventType == XmlPullParser.START_TAG) && ("chat-setting".equals(parser.getName()))) { 143 chatSettings.addSetting(parseChatSetting(parser)); 144 145 } 146 else if (eventType == XmlPullParser.END_TAG && ELEMENT_NAME.equals(parser.getName())) { 147 done = true; 148 } 149 } 150 return chatSettings; 151 } 152 parseChatSetting(XmlPullParser parser)153 private ChatSetting parseChatSetting(XmlPullParser parser) throws Exception { 154 155 boolean done = false; 156 String key = null; 157 String value = null; 158 int type = 0; 159 160 while (!done) { 161 int eventType = parser.next(); 162 if ((eventType == XmlPullParser.START_TAG) && ("key".equals(parser.getName()))) { 163 key = parser.nextText(); 164 } 165 else if ((eventType == XmlPullParser.START_TAG) && ("value".equals(parser.getName()))) { 166 value = parser.nextText(); 167 } 168 else if ((eventType == XmlPullParser.START_TAG) && ("type".equals(parser.getName()))) { 169 type = Integer.parseInt(parser.nextText()); 170 } 171 else if (eventType == XmlPullParser.END_TAG && "chat-setting".equals(parser.getName())) { 172 done = true; 173 } 174 } 175 return new ChatSetting(key, value, type); 176 } 177 } 178 } 179 180