001/** 002 * 003 * Copyright 2003-2007 Jive Software. 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018package org.jivesoftware.smackx.workgroup.settings; 019 020import java.io.IOException; 021import java.util.ArrayList; 022import java.util.Collection; 023import java.util.Iterator; 024import java.util.List; 025 026import org.jivesoftware.smack.packet.IQ; 027import org.jivesoftware.smack.packet.IqData; 028import org.jivesoftware.smack.packet.XmlEnvironment; 029import org.jivesoftware.smack.provider.IqProvider; 030import org.jivesoftware.smack.xml.XmlPullParser; 031import org.jivesoftware.smack.xml.XmlPullParserException; 032 033public class ChatSettings extends IQ { 034 035 /** 036 * Defined as image type. 037 */ 038 public static final int IMAGE_SETTINGS = 0; 039 040 /** 041 * Defined as Text settings type. 042 */ 043 public static final int TEXT_SETTINGS = 1; 044 045 /** 046 * Defined as Bot settings type. 047 */ 048 public static final int BOT_SETTINGS = 2; 049 050 private final List<ChatSetting> settings; 051 private String key; 052 private int type = -1; 053 054 /** 055 * Element name of the stanza extension. 056 */ 057 public static final String ELEMENT_NAME = "chat-settings"; 058 059 /** 060 * Namespace of the stanza extension. 061 */ 062 public static final String NAMESPACE = "http://jivesoftware.com/protocol/workgroup"; 063 064 public ChatSettings() { 065 super(ELEMENT_NAME, NAMESPACE); 066 settings = new ArrayList<>(); 067 } 068 069 public ChatSettings(String key) { 070 this(); 071 setKey(key); 072 } 073 074 public void setKey(String key) { 075 this.key = key; 076 } 077 078 public void setType(int type) { 079 this.type = type; 080 } 081 082 public void addSetting(ChatSetting setting) { 083 settings.add(setting); 084 } 085 086 public Collection<ChatSetting> getSettings() { 087 return settings; 088 } 089 090 public ChatSetting getChatSetting(String key) { 091 Collection<ChatSetting> col = getSettings(); 092 if (col != null) { 093 Iterator<ChatSetting> iter = col.iterator(); 094 while (iter.hasNext()) { 095 ChatSetting chatSetting = iter.next(); 096 if (chatSetting.getKey().equals(key)) { 097 return chatSetting; 098 } 099 } 100 } 101 return null; 102 } 103 104 public ChatSetting getFirstEntry() { 105 if (settings.size() > 0) { 106 return settings.get(0); 107 } 108 return null; 109 } 110 111 @Override 112 protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder buf) { 113 if (key != null) { 114 buf.append(" key=\"" + key + "\""); 115 } 116 117 if (type != -1) { 118 buf.append(" type=\"" + type + "\""); 119 } 120 buf.setEmptyElement(); 121 return buf; 122 } 123 124 /** 125 * Stanza extension provider for AgentStatusRequest packets. 126 */ 127 public static class InternalProvider extends IqProvider<ChatSettings> { 128 129 @Override 130 public ChatSettings parse(XmlPullParser parser, int initialDepth, IqData iqData, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException { 131 if (parser.getEventType() != XmlPullParser.Event.START_ELEMENT) { 132 throw new IllegalStateException("Parser not in proper position, or bad XML."); 133 } 134 135 ChatSettings chatSettings = new ChatSettings(); 136 137 boolean done = false; 138 while (!done) { 139 XmlPullParser.Event eventType = parser.next(); 140 if (eventType == XmlPullParser.Event.START_ELEMENT && "chat-setting".equals(parser.getName())) { 141 chatSettings.addSetting(parseChatSetting(parser)); 142 143 } 144 else if (eventType == XmlPullParser.Event.END_ELEMENT && ELEMENT_NAME.equals(parser.getName())) { 145 done = true; 146 } 147 } 148 return chatSettings; 149 } 150 151 private static ChatSetting parseChatSetting(XmlPullParser parser) throws XmlPullParserException, IOException { 152 153 boolean done = false; 154 String key = null; 155 String value = null; 156 int type = 0; 157 158 while (!done) { 159 XmlPullParser.Event eventType = parser.next(); 160 if (eventType == XmlPullParser.Event.START_ELEMENT && "key".equals(parser.getName())) { 161 key = parser.nextText(); 162 } 163 else if (eventType == XmlPullParser.Event.START_ELEMENT && "value".equals(parser.getName())) { 164 value = parser.nextText(); 165 } 166 else if (eventType == XmlPullParser.Event.START_ELEMENT && "type".equals(parser.getName())) { 167 type = Integer.parseInt(parser.nextText()); 168 } 169 else if (eventType == XmlPullParser.Event.END_ELEMENT && "chat-setting".equals(parser.getName())) { 170 done = true; 171 } 172 } 173 return new ChatSetting(key, value, type); 174 } 175 } 176} 177