KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thaiopensource > validate > mns > ContextMap


1 package com.thaiopensource.validate.mns;
2
3 import com.thaiopensource.xml.util.Name;
4
5 import java.util.Vector JavaDoc;
6 import java.util.Hashtable JavaDoc;
7
8 class ContextMap {
9   private Object JavaDoc rootValue;
10   private Object JavaDoc otherValue;
11   private final Hashtable JavaDoc nameTable = new Hashtable JavaDoc();
12
13   Object JavaDoc get(Vector JavaDoc context) {
14     return get(context, context.size());
15   }
16
17   boolean put(boolean isRoot, Vector JavaDoc names, Object JavaDoc value) {
18     return put(isRoot, names, names.size(), value);
19   }
20
21   private Object JavaDoc get(Vector JavaDoc context, int len) {
22     if (len > 0) {
23       ContextMap nestedMap = (ContextMap)nameTable.get(context.elementAt(len - 1));
24       if (nestedMap != null) {
25         Object JavaDoc value = nestedMap.get(context, len - 1);
26         if (value != null)
27           return value;
28       }
29     }
30     if (rootValue != null && len == 0)
31       return rootValue;
32     return otherValue;
33   }
34
35   private boolean put(boolean isRoot, Vector JavaDoc names, int len, Object JavaDoc value) {
36     if (len == 0) {
37       if (isRoot) {
38         if (rootValue != null)
39           return false;
40         rootValue = value;
41       }
42       else {
43         if (otherValue != null)
44           return false;
45         otherValue = value;
46       }
47       return true;
48     }
49     else {
50       Name name = (Name)names.elementAt(len - 1);
51       ContextMap nestedMap = (ContextMap)nameTable.get(name);
52       if (nestedMap == null) {
53         nestedMap = new ContextMap();
54         nameTable.put(name, nestedMap);
55       }
56       return nestedMap.put(isRoot, names, len - 1, value);
57     }
58   }
59 }
60
Popular Tags