1 package org.columba.core.context.base; 2 3 import java.io.InputStream ; 4 import java.util.Date ; 5 import java.util.Hashtable ; 6 import java.util.Iterator ; 7 import java.util.List ; 8 import java.util.Vector ; 9 10 import org.columba.core.context.base.api.IAttributeType; 11 import org.columba.core.context.base.api.IName; 12 import org.columba.core.context.base.api.IStructureType; 13 import org.columba.core.context.base.api.IStructureValue; 14 import org.columba.core.context.base.api.MULTIPLICITY; 15 import org.columba.core.context.base.api.IAttributeType.BASETYPE; 16 17 public class StructureValue implements IStructureValue { 19 20 private String name; 21 22 private String namespace; 23 24 private IStructureType type; 25 26 private final List <Object > attributeList = new Vector <Object >(); 27 28 private final Hashtable <IName, Object > attributeMap = new Hashtable <IName, Object >(); 29 30 private final Hashtable <IName, List <IStructureValue>> valueMap = new Hashtable <IName, List <IStructureValue>>(); 31 32 private IStructureValue parent; 33 34 39 public StructureValue(final String theName, final String theNamespace, 40 final IStructureType theType) { 41 this.type = theType; 42 this.name = theName; 43 this.namespace = theNamespace; 44 } 45 46 52 public StructureValue(final String theName, final String theNamespace, 53 final IStructureType theType, final IStructureValue theParent) { 54 this(theName, theNamespace, theType); 55 this.parent = theParent; 56 57 } 58 59 62 public IStructureType getType() { 63 return type; 64 } 65 66 69 public String getName() { 70 return name; 71 } 72 73 76 public String getNamespace() { 77 return namespace; 78 } 79 80 public Object getObject(final String theName, final String theNamespace) { 81 final Object obj = attributeMap.get(new Name(theName, theNamespace)); 82 if (obj == null) { 83 final IAttributeType t = getType().getAttribute(theName, 85 theNamespace); 86 return t.getDefaultValue(); 87 } 88 89 return obj; 90 } 91 92 public void setObject(final String theName, final String theNamespace, 93 final Object value) { 94 if (theName == null) { 95 throw new IllegalArgumentException ("name == null"); 96 } 97 if (theNamespace == null) { 98 throw new IllegalArgumentException ("namespace"); 99 } 100 if (value == null) { 101 throw new IllegalArgumentException ("value == null"); 102 } 103 104 final IAttributeType attrType = getType().getAttribute(theName, 105 theNamespace); 106 if (attrType == null) { 107 throw new IllegalArgumentException ("attribute type <" + theName 108 + "," + theNamespace + "> does not exist"); 109 } 110 111 final Object obj = getObject(theName, theNamespace); 113 if (obj != null) { 114 attributeMap.remove(obj); 115 attributeList.remove(obj); 116 } 117 118 attributeMap.put(new Name(theName, theNamespace), value); 119 attributeList.add(value); 120 } 121 122 125 public IStructureValue getParent() { 126 return parent; 127 } 128 129 133 public IStructureValue addChild(final String theName, 134 final String theNamespace) { 135 final IStructureType childType = getType().getChild(theName, 136 theNamespace); 137 if (childType == null) { 138 throw new IllegalArgumentException ("child structure type for <" 139 + theName + "," + theNamespace + "> does not exist"); 140 } 141 142 final IStructureValue value = new StructureValue(theName, theNamespace, 143 childType, this); 144 final List <IStructureValue> list = getChildStructureList(theName, 145 theNamespace); 146 147 if ((childType.getCardinality().equals(MULTIPLICITY.ONE_TO_ONE) || getType() 148 .getCardinality().equals(MULTIPLICITY.ZERO_TO_ONE)) 149 && (list.size() == 1)) { 150 throw new IllegalArgumentException ( 152 "multiplicity of ONE_TO_ONE or ZERO_TO_ONE doesn't allow adding more children to this structure"); 153 } 154 155 list.add(value); 156 157 return value; 158 } 159 160 165 private int getChildStructureCount(final String theName, 166 final String theNamespace) { 167 if (valueMap.containsKey(new Name(theName, theNamespace))) { 168 final List <IStructureValue> list = valueMap.get(new Name(theName, 169 theNamespace)); 170 return list.size(); 171 } 172 return 0; 173 } 174 175 179 public IStructureValue removeChild(final String theName, 180 final String theNamespace, final int index) { 181 final List <IStructureValue> list = valueMap.get(new Name(theName, 182 theNamespace)); 183 if (list == null) { 184 throw new IllegalArgumentException ("list <" + theName + "," 185 + theNamespace + "> is empty"); 186 } 187 188 final IStructureValue value = list.get(index); 189 if (value == null) { 190 throw new IllegalArgumentException ("no element at index " + index); 191 } 192 193 list.remove(index); 194 195 return value; 196 } 197 198 203 private List <IStructureValue> getChildStructureList(final String theName, 204 final String theNamespace) { 205 final int count = getChildStructureCount(theName, theNamespace); 206 207 if (count == 0) { 208 final List <IStructureValue> list = new Vector <IStructureValue>(); 210 valueMap.put(new Name(theName, theNamespace), list); 211 return list; 212 } 213 final List <IStructureValue> list = valueMap.get(new Name(theName, 214 theNamespace)); 215 return list; 216 } 217 218 @Override 219 public String toString() { 220 final StringBuilder buf = new StringBuilder (); 221 buf.append("StructureValue["); buf.append("name=" + getName()); buf.append(", namespace" + getNamespace()); buf.append(']'); 225 return buf.toString(); 226 } 227 228 231 public boolean isValid() { 232 return true; 234 } 235 236 public Iterator <IName> getAllAttributeNames() { 237 return attributeMap.keySet().iterator(); 238 } 239 240 243 public Iterator <Object > getAttributeIterator() { 244 return attributeList.listIterator(); 245 } 246 247 251 public Iterator <IStructureValue> getChildIterator(final String theName, 252 final String theNamespace) { 253 List <IStructureValue> list = valueMap.get(new Name(theName, theNamespace)); 254 if (list == null) { 255 list = new Vector <IStructureValue>(); 257 } 258 259 return list.listIterator(); 260 261 } 262 263 267 public void removeAllChildren(final String theName, final String theNamespace) { 268 valueMap.remove(new Name(theName, theNamespace)); 269 } 270 271 275 public String getString(final String theName, final String theNamespace) { 276 final IAttributeType t = getType().getAttribute(theName, theNamespace); 277 if (!t.getBaseType().equals(BASETYPE.STRING)) { 278 throw new IllegalArgumentException ("attribute <" + theName + "," 279 + theNamespace + "> is not of type String"); 280 } 281 282 return (String ) getObject(theName, theNamespace); 283 } 284 285 public void setString(final String theName, final String theNamespace, 286 final String value) { 287 final IAttributeType t = getType().getAttribute(theName, theNamespace); 288 if (!t.getBaseType().equals(BASETYPE.STRING)) { 289 throw new IllegalArgumentException ("attribute <" + theName + "," 290 + theNamespace + "> is not of type String"); 291 } 292 293 setObject(theName, theNamespace, value); 294 } 295 296 public int getInteger(final String theName, final String theNamespace) { 297 final IAttributeType t = getType().getAttribute(theName, theNamespace); 298 if (!t.getBaseType().equals(BASETYPE.INTEGER)) { 299 throw new IllegalArgumentException ("attribute <" + theName + "," 300 + theNamespace + "> is not of type Integer"); 301 } 302 303 return (Integer ) getObject(theName, theNamespace); 304 } 305 306 public void setInteger(final String theName, final String theNamespace, 307 final int value) { 308 final IAttributeType t = getType().getAttribute(theName, theNamespace); 309 if (!t.getBaseType().equals(BASETYPE.INTEGER)) { 310 throw new IllegalArgumentException ("attribute <" + theName + "," 311 + theNamespace + "> is not of type Integer"); 312 } 313 314 setObject(theName, theNamespace, value); 315 } 316 317 public Date getDate(final String theName, final String theNamespace) { 318 final IAttributeType t = getType().getAttribute(theName, theNamespace); 319 if (!t.getBaseType().equals(BASETYPE.DATE)) { 320 throw new IllegalArgumentException ("attribute <" + theName + "," 321 + theNamespace + "> is not of type Date"); 322 } 323 324 return (Date ) getObject(theName, theNamespace); 325 } 326 327 public void setDate(final String theName, final String theNamespace, 328 final Date value) { 329 final IAttributeType t = getType().getAttribute(theName, theNamespace); 330 if (!t.getBaseType().equals(BASETYPE.DATE)) { 331 throw new IllegalArgumentException ("attribute <" + theName + "," 332 + theNamespace + "> is not of type Date"); 333 } 334 335 setObject(theName, theNamespace, value); 336 } 337 338 public float getFloat(final String theName, final String theNamespace) { 339 final IAttributeType t = getType().getAttribute(theName, theNamespace); 340 if (!t.getBaseType().equals(BASETYPE.FLOAT)) { 341 throw new IllegalArgumentException ("attribute <" + theName + "," 342 + theNamespace + "> is not of type Float"); 343 } 344 345 return (Float ) getObject(theName, theNamespace); 346 } 347 348 public void setFloat(final String theName, final String theNamespace, 349 final float value) { 350 final IAttributeType t = getType().getAttribute(theName, theNamespace); 351 if (!t.getBaseType().equals(BASETYPE.FLOAT)) { 352 throw new IllegalArgumentException ("attribute <" + theName + "," 353 + theNamespace + "> is not of type Float"); 354 } 355 setObject(theName, theNamespace, value); 356 } 357 358 public double getDouble(final String theName, final String theNamespace) { 359 final IAttributeType t = getType().getAttribute(theName, theNamespace); 360 if (!t.getBaseType().equals(BASETYPE.DOUBLE)) { 361 throw new IllegalArgumentException ("attribute <" + theName + "," 362 + theNamespace + "> is not of type Double"); 363 } 364 365 return (Double ) getObject(theName, theNamespace); 366 } 367 368 public void setDouble(final String theName, final String theNamespace, 369 final double value) { 370 final IAttributeType t = getType().getAttribute(theName, theNamespace); 371 if (!t.getBaseType().equals(BASETYPE.DOUBLE)) { 372 throw new IllegalArgumentException ("attribute <" + theName + "," 373 + theNamespace + "> is not of type Double"); 374 } 375 setObject(theName, theNamespace, value); 376 } 377 378 public byte[] getByteArray(final String theName, final String theNamespace) { 379 final IAttributeType t = getType().getAttribute(theName, theNamespace); 380 if (!t.getBaseType().equals(BASETYPE.BINARY)) { 381 throw new IllegalArgumentException ("attribute <" + theName + "," 382 + theNamespace + "> is not of type binary"); 383 } 384 385 return (byte[]) getObject(theName, theNamespace); 386 } 387 388 public void setByteArray(final String theName, final String theNamespace, 389 final byte[] value) { 390 final IAttributeType t = getType().getAttribute(theName, theNamespace); 391 if (!t.getBaseType().equals(BASETYPE.BINARY)) { 392 throw new IllegalArgumentException ("attribute <" + theName + "," 393 + theNamespace + "> is not of type binary"); 394 } 395 setObject(theName, theNamespace, value); 396 } 397 398 public InputStream getInputStream(final String theName, final String theNamespace) { 399 final IAttributeType t = getType().getAttribute(theName, theNamespace); 400 if (!t.getBaseType().equals(BASETYPE.INPUTSTREAM)) { 401 throw new IllegalArgumentException ("attribute <" + theName + "," 402 + theNamespace + "> is not of type blob (binary inputstream)"); 403 } 404 405 return (InputStream ) getObject(theName, theNamespace); 406 } 407 408 public void setInputStream(final String theName, final String theNamespace, 409 final InputStream value) { 410 final IAttributeType t = getType().getAttribute(theName, theNamespace); 411 if (!t.getBaseType().equals(BASETYPE.INPUTSTREAM)) { 412 throw new IllegalArgumentException ("attribute <" + theName + "," 413 + theNamespace + "> is not of type blob (binary inputstream)"); 414 } 415 setObject(theName, theNamespace, value); 416 } 417 418 public Iterator <IName> getAllChildNames() { 419 return valueMap.keySet().iterator(); 420 } 421 422 } 423 | Popular Tags |