1 2 package org.ksoap2.serialization; 3 4 import java.util.Vector; 5 6 public class AttributeContainer implements HasAttributes{ 7 protected Vector attributes = new Vector(); 8 9 /** 10 * Places AttributeInfo of desired attribute into a designated AttributeInfo object 11 * 12 * @param index index of desired attribute 13 * @param attributeInfo designated retainer of desired attribute 14 */ getAttributeInfo(int index, AttributeInfo attributeInfo)15 public void getAttributeInfo(int index, AttributeInfo attributeInfo) { 16 AttributeInfo p = (AttributeInfo) attributes.elementAt(index); 17 attributeInfo.name = p.name; 18 attributeInfo.namespace = p.namespace; 19 attributeInfo.flags = p.flags; 20 attributeInfo.type = p.type; 21 attributeInfo.elementType = p.elementType; 22 attributeInfo.value = p.getValue(); 23 } 24 25 /** 26 * Get the attribute at the given index 27 */ getAttribute(int index)28 public Object getAttribute(int index) { 29 return ((AttributeInfo) attributes.elementAt(index)).getValue(); 30 } 31 32 /** 33 * Get the attribute's toString value. 34 */ getAttributeAsString(int index)35 public String getAttributeAsString(int index) { 36 AttributeInfo attributeInfo = (AttributeInfo) attributes.elementAt(index); 37 return attributeInfo.getValue().toString(); 38 } 39 40 /** 41 * Get the attribute with the given name 42 * 43 * @throws RuntimeException if the attribute does not exist 44 */ getAttribute(String name)45 public Object getAttribute(String name) { 46 Integer i = attributeIndex(name); 47 if (i != null) { 48 return getAttribute(i.intValue()); 49 } else { 50 throw new RuntimeException("illegal property: " + name); 51 } 52 } 53 54 /** 55 * Get the attribute with the given name 56 * 57 * @throws RuntimeException if the attribute does not exist 58 */ getAttribute(String namespace,String name)59 public Object getAttribute(String namespace,String name) { 60 Integer i = attributeIndex(namespace,name); 61 if (i != null) { 62 return getAttribute(i.intValue()); 63 } else { 64 throw new RuntimeException("illegal property: " + name); 65 } 66 } 67 68 /** 69 * Get the toString value of the attribute with the given name. 70 * 71 * @throws RuntimeException if the attribute does not exist 72 */ getAttributeAsString(String name)73 public String getAttributeAsString(String name) { 74 Integer i = attributeIndex(name); 75 if (i != null) { 76 return getAttribute(i.intValue()).toString(); 77 } else { 78 throw new RuntimeException("illegal property: " + name); 79 } 80 } 81 82 /** 83 * Get the toString value of the attribute with the given name. 84 * 85 * @throws RuntimeException if the attribute does not exist 86 */ getAttributeAsString(String namespace,String name)87 public String getAttributeAsString(String namespace,String name) { 88 Integer i = attributeIndex(namespace,name); 89 if (i != null) { 90 return getAttribute(i.intValue()).toString(); 91 } else { 92 throw new RuntimeException("illegal property: " + name); 93 } 94 } 95 /** 96 * Knows whether the given attribute exists 97 */ hasAttribute(final String name)98 public boolean hasAttribute(final String name) { 99 if (attributeIndex(name) != null) { 100 return true; 101 } else { 102 return false; 103 } 104 } 105 106 /** 107 * Knows whether the given attribute exists 108 */ hasAttribute(final String namespace,final String name)109 public boolean hasAttribute(final String namespace,final String name) { 110 if (attributeIndex(namespace,name) != null) { 111 return true; 112 } else { 113 return false; 114 } 115 } 116 /** 117 * Get an attribute without chance of throwing an exception 118 * 119 * @param name the name of the attribute to retrieve 120 * @return the value of the attribute if it exists; {@code null} if it does not exist 121 */ getAttributeSafely(String name)122 public Object getAttributeSafely(String name) { 123 Integer i = attributeIndex(name); 124 if (i != null) { 125 return getAttribute(i.intValue()); 126 } else { 127 return null; 128 } 129 } 130 131 /** 132 * Get an attribute without chance of throwing an exception 133 * 134 * @param name the name of the attribute to retrieve 135 * @return the value of the attribute if it exists; {@code null} if it does not exist 136 */ getAttributeSafely(String namespace,String name)137 public Object getAttributeSafely(String namespace,String name) { 138 Integer i = attributeIndex(namespace,name); 139 if (i != null) { 140 return getAttribute(i.intValue()); 141 } else { 142 return null; 143 } 144 } 145 146 /** 147 * Get an attributes' toString value without chance of throwing an 148 * exception. 149 150 * @param name 151 * @return the value of the attribute,s toString method if it exists; "" 152 * if it does not exist 153 */ getAttributeSafelyAsString(String name)154 public Object getAttributeSafelyAsString(String name) { 155 Integer i = attributeIndex(name); 156 if (i != null) { 157 return getAttribute(i.intValue()).toString(); 158 } else { 159 return ""; 160 } 161 } 162 163 /** 164 * Get an attributes' toString value without chance of throwing an 165 * exception. 166 167 * @param name 168 * @return the value of the attribute,s toString method if it exists; "" 169 * if it does not exist 170 */ getAttributeSafelyAsString(String namespace,String name)171 public Object getAttributeSafelyAsString(String namespace,String name) { 172 Integer i = attributeIndex(namespace,name); 173 if (i != null) { 174 return getAttribute(i.intValue()).toString(); 175 } else { 176 return ""; 177 } 178 } 179 attributeIndex(String name)180 private Integer attributeIndex(String name) { 181 for (int i = 0; i < attributes.size(); i++) { 182 if (name.equals(((AttributeInfo) attributes.elementAt(i)).getName())) { 183 return new Integer(i); 184 } 185 } 186 return null; 187 } 188 attributeIndex(String namespace,String name)189 private Integer attributeIndex(String namespace,String name) { 190 for (int i = 0; i < attributes.size(); i++) { 191 AttributeInfo attrInfo=(AttributeInfo) attributes.elementAt(i); 192 if (name.equals(attrInfo.getName()) && namespace.equals(attrInfo.getNamespace())) { 193 return new Integer(i); 194 } 195 } 196 return null; 197 } 198 199 /** 200 * Returns the number of attributes 201 * 202 * @return the number of attributes 203 */ getAttributeCount()204 public int getAttributeCount() { 205 return attributes.size(); 206 } 207 208 /** 209 * Checks that the two objects have identical sets of attributes. 210 * 211 * @param other 212 * @return {@code true} of the attrubte sets are equal, {@code false} otherwise. 213 */ attributesAreEqual(AttributeContainer other)214 protected boolean attributesAreEqual(AttributeContainer other) { 215 int numAttributes = getAttributeCount(); 216 if (numAttributes != other.getAttributeCount()) { 217 return false; 218 } 219 220 for (int attribIndex = 0; attribIndex < numAttributes; attribIndex++) { 221 AttributeInfo thisAttrib = (AttributeInfo) this.attributes.elementAt(attribIndex); 222 Object thisAttribValue = thisAttrib.getValue(); 223 if (!other.hasAttribute(thisAttrib.getName())) { 224 return false; 225 } 226 Object otherAttribValue = other.getAttributeSafely(thisAttrib.getName()); 227 if (!thisAttribValue.equals(otherAttribValue)) { 228 return false; 229 } 230 } 231 return true; 232 } 233 234 /** 235 * Adds a attribute (parameter) to the object. 236 * 237 * @param name The name of the attribute 238 * @param value the value of the attribute 239 * @return {@code this} object. 240 */ addAttribute(String name, Object value)241 public void addAttribute(String name, Object value) { 242 addAttribute(null,name,value); 243 } 244 245 /** 246 * Adds a attribute (parameter) to the object. 247 * 248 * @param namespace The namespace of the attribute 249 * @param name The name of the attribute 250 * @param value the value of the attribute 251 * @return {@code this} object. 252 */ addAttribute(String namespace,String name, Object value)253 public void addAttribute(String namespace,String name, Object value) { 254 AttributeInfo attributeInfo = new AttributeInfo(); 255 attributeInfo.name = name; 256 attributeInfo.namespace = namespace; 257 attributeInfo.type = value == null ? PropertyInfo.OBJECT_CLASS : value.getClass(); 258 attributeInfo.value = value; 259 addAttribute(attributeInfo); 260 } 261 /** 262 * Add an attribute if the value is not null. 263 * @param name 264 * @param value 265 */ addAttributeIfValue(String name, Object value)266 public void addAttributeIfValue(String name, Object value) { 267 if (value != null) { 268 addAttribute(name, value); 269 } 270 } 271 272 /** 273 * Add an attribute if the value is not null. 274 * @param namespace The namespace of the attribute 275 * @param name 276 * @param value 277 */ addAttributeIfValue(String namespace,String name, Object value)278 public void addAttributeIfValue(String namespace,String name, Object value) { 279 if (value != null) { 280 addAttribute(namespace,name, value); 281 } 282 } 283 284 /** 285 * Add a new attribute by providing an {@link AttributeInfo} object. {@code AttributeInfo} 286 * contains all data about the attribute, including name and value.} 287 * 288 * @param attributeInfo the {@code AttributeInfo} object to add. 289 * @return {@code this} object. 290 */ addAttribute(AttributeInfo attributeInfo)291 public void addAttribute(AttributeInfo attributeInfo) { 292 attributes.addElement(attributeInfo); 293 } 294 295 /** 296 * Add an attributeInfo if its value is not null. 297 * @param attributeInfo 298 */ addAttributeIfValue(AttributeInfo attributeInfo)299 public void addAttributeIfValue(AttributeInfo attributeInfo) { 300 if (attributeInfo.value != null) { 301 attributes.addElement(attributeInfo); 302 } 303 } 304 305 setAttribute(AttributeInfo info)306 public void setAttribute(AttributeInfo info) { 307 308 309 } 310 311 getAttribute(int index, AttributeInfo info)312 public void getAttribute(int index, AttributeInfo info) { 313 314 315 } 316 317 } 318