KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > context > base > StructureValue


1 package org.columba.core.context.base;
2
3 import java.io.InputStream JavaDoc;
4 import java.util.Date JavaDoc;
5 import java.util.Hashtable JavaDoc;
6 import java.util.Iterator JavaDoc;
7 import java.util.List JavaDoc;
8 import java.util.Vector JavaDoc;
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 // TODO: multiplicity checks
18
public class StructureValue implements IStructureValue {
19
20     private String JavaDoc name;
21
22     private String JavaDoc namespace;
23
24     private IStructureType type;
25
26     private final List JavaDoc<Object JavaDoc> attributeList = new Vector JavaDoc<Object JavaDoc>();
27
28     private final Hashtable JavaDoc<IName, Object JavaDoc> attributeMap = new Hashtable JavaDoc<IName, Object JavaDoc>();
29
30     private final Hashtable JavaDoc<IName, List JavaDoc<IStructureValue>> valueMap = new Hashtable JavaDoc<IName, List JavaDoc<IStructureValue>>();
31
32     private IStructureValue parent;
33
34     /**
35      * @param theName
36      * @param theNamespace
37      * @param theType
38      */

39     public StructureValue(final String JavaDoc theName, final String JavaDoc theNamespace,
40             final IStructureType theType) {
41         this.type = theType;
42         this.name = theName;
43         this.namespace = theNamespace;
44     }
45
46     /**
47      * @param theName
48      * @param theNamespace
49      * @param theType
50      * @param theParent
51      */

52     public StructureValue(final String JavaDoc theName, final String JavaDoc theNamespace,
53             final IStructureType theType, final IStructureValue theParent) {
54         this(theName, theNamespace, theType);
55         this.parent = theParent;
56
57     }
58
59     /**
60      * @see org.columba.core.context.base.api.IStructureValue#getType()
61      */

62     public IStructureType getType() {
63         return type;
64     }
65
66     /**
67      * @see org.columba.core.context.base.api.IStructureValue#getName()
68      */

69     public String JavaDoc getName() {
70         return name;
71     }
72
73     /**
74      * @see org.columba.core.context.base.api.IStructureValue#getNamespace()
75      */

76     public String JavaDoc getNamespace() {
77         return namespace;
78     }
79
80     public Object JavaDoc getObject(final String JavaDoc theName, final String JavaDoc theNamespace) {
81         final Object JavaDoc obj = attributeMap.get(new Name(theName, theNamespace));
82         if (obj == null) {
83             // check if default value exists
84
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 JavaDoc theName, final String JavaDoc theNamespace,
93             final Object JavaDoc value) {
94         if (theName == null) {
95             throw new IllegalArgumentException JavaDoc("name == null");
96         }
97         if (theNamespace == null) {
98             throw new IllegalArgumentException JavaDoc("namespace");
99         }
100         if (value == null) {
101             throw new IllegalArgumentException JavaDoc("value == null");
102         }
103
104         final IAttributeType attrType = getType().getAttribute(theName,
105                 theNamespace);
106         if (attrType == null) {
107             throw new IllegalArgumentException JavaDoc("attribute type <" + theName
108                     + "," + theNamespace + "> does not exist");
109         }
110
111         // remove old
112
final Object JavaDoc 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     /**
123      * @see org.columba.core.context.base.api.IStructureValue#getParent()
124      */

125     public IStructureValue getParent() {
126         return parent;
127     }
128
129     /**
130      * @see org.columba.core.context.base.api.IStructureValue#addChild(java.lang.String,
131      * java.lang.String)
132      */

133     public IStructureValue addChild(final String JavaDoc theName,
134             final String JavaDoc theNamespace) {
135         final IStructureType childType = getType().getChild(theName,
136                 theNamespace);
137         if (childType == null) {
138             throw new IllegalArgumentException JavaDoc("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 JavaDoc<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             // contains already a single element
151
throw new IllegalArgumentException JavaDoc(
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     /**
161      * @param theName
162      * @param theNamespace
163      * @return
164      */

165     private int getChildStructureCount(final String JavaDoc theName,
166             final String JavaDoc theNamespace) {
167         if (valueMap.containsKey(new Name(theName, theNamespace))) {
168             final List JavaDoc<IStructureValue> list = valueMap.get(new Name(theName,
169                     theNamespace));
170             return list.size();
171         }
172         return 0;
173     }
174
175     /**
176      * @see org.columba.core.context.base.api.IStructureValue#removeChild(java.lang.String,
177      * java.lang.String, int)
178      */

179     public IStructureValue removeChild(final String JavaDoc theName,
180             final String JavaDoc theNamespace, final int index) {
181         final List JavaDoc<IStructureValue> list = valueMap.get(new Name(theName,
182                 theNamespace));
183         if (list == null) {
184             throw new IllegalArgumentException JavaDoc("list <" + theName + ","
185                     + theNamespace + "> is empty");
186         }
187
188         final IStructureValue value = list.get(index);
189         if (value == null) {
190             throw new IllegalArgumentException JavaDoc("no element at index " + index);
191         }
192
193         list.remove(index);
194
195         return value;
196     }
197
198     /**
199      * @param theName
200      * @param theNamespace
201      * @return
202      */

203     private List JavaDoc<IStructureValue> getChildStructureList(final String JavaDoc theName,
204             final String JavaDoc theNamespace) {
205         final int count = getChildStructureCount(theName, theNamespace);
206
207         if (count == 0) {
208             // create empty list
209
final List JavaDoc<IStructureValue> list = new Vector JavaDoc<IStructureValue>();
210             valueMap.put(new Name(theName, theNamespace), list);
211             return list;
212         }
213         final List JavaDoc<IStructureValue> list = valueMap.get(new Name(theName,
214                 theNamespace));
215         return list;
216     }
217
218     @Override JavaDoc
219     public String JavaDoc toString() {
220         final StringBuilder JavaDoc buf = new StringBuilder JavaDoc();
221         buf.append("StructureValue["); //$NON-NLS-1$
222
buf.append("name=" + getName()); //$NON-NLS-1$
223
buf.append(", namespace" + getNamespace()); //$NON-NLS-1$
224
buf.append(']');
225         return buf.toString();
226     }
227
228     /**
229      * @see org.columba.core.context.base.api.IStructureValue#isValid()
230      */

231     public boolean isValid() {
232         // TODO implement validation
233
return true;
234     }
235
236     public Iterator JavaDoc<IName> getAllAttributeNames() {
237         return attributeMap.keySet().iterator();
238     }
239
240     /**
241      * @see org.columba.core.context.base.api.IStructureValue#getAttributeIterator()
242      */

243     public Iterator JavaDoc<Object JavaDoc> getAttributeIterator() {
244         return attributeList.listIterator();
245     }
246
247     /**
248      * @see org.columba.core.context.base.api.IStructureValue#getChildIterator(java.lang.String,
249      * java.lang.String)
250      */

251     public Iterator JavaDoc<IStructureValue> getChildIterator(final String JavaDoc theName,
252             final String JavaDoc theNamespace) {
253         List JavaDoc<IStructureValue> list = valueMap.get(new Name(theName, theNamespace));
254         if (list == null) {
255             // create empty structure value
256
list = new Vector JavaDoc<IStructureValue>();
257         }
258
259         return list.listIterator();
260
261     }
262
263     /**
264      * @see org.columba.core.context.base.api.IStructureValue#removeAllChildren(java.lang.String,
265      * java.lang.String)
266      */

267     public void removeAllChildren(final String JavaDoc theName, final String JavaDoc theNamespace) {
268         valueMap.remove(new Name(theName, theNamespace));
269     }
270
271     /**
272      * @see org.columba.core.context.base.api.IStructureValue#getString(java.lang.String,
273      * java.lang.String)
274      */

275     public String JavaDoc getString(final String JavaDoc theName, final String JavaDoc theNamespace) {
276         final IAttributeType t = getType().getAttribute(theName, theNamespace);
277         if (!t.getBaseType().equals(BASETYPE.STRING)) {
278             throw new IllegalArgumentException JavaDoc("attribute <" + theName + ","
279                     + theNamespace + "> is not of type String");
280         }
281
282         return (String JavaDoc) getObject(theName, theNamespace);
283     }
284
285     public void setString(final String JavaDoc theName, final String JavaDoc theNamespace,
286             final String JavaDoc value) {
287         final IAttributeType t = getType().getAttribute(theName, theNamespace);
288         if (!t.getBaseType().equals(BASETYPE.STRING)) {
289             throw new IllegalArgumentException JavaDoc("attribute <" + theName + ","
290                     + theNamespace + "> is not of type String");
291         }
292
293         setObject(theName, theNamespace, value);
294     }
295
296     public int getInteger(final String JavaDoc theName, final String JavaDoc theNamespace) {
297         final IAttributeType t = getType().getAttribute(theName, theNamespace);
298         if (!t.getBaseType().equals(BASETYPE.INTEGER)) {
299             throw new IllegalArgumentException JavaDoc("attribute <" + theName + ","
300                     + theNamespace + "> is not of type Integer");
301         }
302
303         return (Integer JavaDoc) getObject(theName, theNamespace);
304     }
305
306     public void setInteger(final String JavaDoc theName, final String JavaDoc theNamespace,
307             final int value) {
308         final IAttributeType t = getType().getAttribute(theName, theNamespace);
309         if (!t.getBaseType().equals(BASETYPE.INTEGER)) {
310             throw new IllegalArgumentException JavaDoc("attribute <" + theName + ","
311                     + theNamespace + "> is not of type Integer");
312         }
313
314         setObject(theName, theNamespace, value);
315     }
316
317     public Date JavaDoc getDate(final String JavaDoc theName, final String JavaDoc theNamespace) {
318         final IAttributeType t = getType().getAttribute(theName, theNamespace);
319         if (!t.getBaseType().equals(BASETYPE.DATE)) {
320             throw new IllegalArgumentException JavaDoc("attribute <" + theName + ","
321                     + theNamespace + "> is not of type Date");
322         }
323
324         return (Date JavaDoc) getObject(theName, theNamespace);
325     }
326
327     public void setDate(final String JavaDoc theName, final String JavaDoc theNamespace,
328             final Date JavaDoc value) {
329         final IAttributeType t = getType().getAttribute(theName, theNamespace);
330         if (!t.getBaseType().equals(BASETYPE.DATE)) {
331             throw new IllegalArgumentException JavaDoc("attribute <" + theName + ","
332                     + theNamespace + "> is not of type Date");
333         }
334
335         setObject(theName, theNamespace, value);
336     }
337
338     public float getFloat(final String JavaDoc theName, final String JavaDoc theNamespace) {
339         final IAttributeType t = getType().getAttribute(theName, theNamespace);
340         if (!t.getBaseType().equals(BASETYPE.FLOAT)) {
341             throw new IllegalArgumentException JavaDoc("attribute <" + theName + ","
342                     + theNamespace + "> is not of type Float");
343         }
344
345         return (Float JavaDoc) getObject(theName, theNamespace);
346     }
347
348     public void setFloat(final String JavaDoc theName, final String JavaDoc theNamespace,
349             final float value) {
350         final IAttributeType t = getType().getAttribute(theName, theNamespace);
351         if (!t.getBaseType().equals(BASETYPE.FLOAT)) {
352             throw new IllegalArgumentException JavaDoc("attribute <" + theName + ","
353                     + theNamespace + "> is not of type Float");
354         }
355         setObject(theName, theNamespace, value);
356     }
357
358     public double getDouble(final String JavaDoc theName, final String JavaDoc theNamespace) {
359         final IAttributeType t = getType().getAttribute(theName, theNamespace);
360         if (!t.getBaseType().equals(BASETYPE.DOUBLE)) {
361             throw new IllegalArgumentException JavaDoc("attribute <" + theName + ","
362                     + theNamespace + "> is not of type Double");
363         }
364
365         return (Double JavaDoc) getObject(theName, theNamespace);
366     }
367
368     public void setDouble(final String JavaDoc theName, final String JavaDoc theNamespace,
369             final double value) {
370         final IAttributeType t = getType().getAttribute(theName, theNamespace);
371         if (!t.getBaseType().equals(BASETYPE.DOUBLE)) {
372             throw new IllegalArgumentException JavaDoc("attribute <" + theName + ","
373                     + theNamespace + "> is not of type Double");
374         }
375         setObject(theName, theNamespace, value);
376     }
377
378     public byte[] getByteArray(final String JavaDoc theName, final String JavaDoc theNamespace) {
379         final IAttributeType t = getType().getAttribute(theName, theNamespace);
380         if (!t.getBaseType().equals(BASETYPE.BINARY)) {
381             throw new IllegalArgumentException JavaDoc("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 JavaDoc theName, final String JavaDoc theNamespace,
389             final byte[] value) {
390         final IAttributeType t = getType().getAttribute(theName, theNamespace);
391         if (!t.getBaseType().equals(BASETYPE.BINARY)) {
392             throw new IllegalArgumentException JavaDoc("attribute <" + theName + ","
393                     + theNamespace + "> is not of type binary");
394         }
395         setObject(theName, theNamespace, value);
396     }
397
398     public InputStream JavaDoc getInputStream(final String JavaDoc theName, final String JavaDoc theNamespace) {
399         final IAttributeType t = getType().getAttribute(theName, theNamespace);
400         if (!t.getBaseType().equals(BASETYPE.INPUTSTREAM)) {
401             throw new IllegalArgumentException JavaDoc("attribute <" + theName + ","
402                     + theNamespace + "> is not of type blob (binary inputstream)");
403         }
404
405         return (InputStream JavaDoc) getObject(theName, theNamespace);
406     }
407
408     public void setInputStream(final String JavaDoc theName, final String JavaDoc theNamespace,
409             final InputStream JavaDoc value) {
410         final IAttributeType t = getType().getAttribute(theName, theNamespace);
411         if (!t.getBaseType().equals(BASETYPE.INPUTSTREAM)) {
412             throw new IllegalArgumentException JavaDoc("attribute <" + theName + ","
413                     + theNamespace + "> is not of type blob (binary inputstream)");
414         }
415         setObject(theName, theNamespace, value);
416     }
417
418     public Iterator JavaDoc<IName> getAllChildNames() {
419         return valueMap.keySet().iterator();
420     }
421
422 }
423
Popular Tags