1 package com.thaiopensource.validate.mns; 2 3 import com.thaiopensource.xml.util.Name; 4 5 import java.util.Vector ; 6 import java.util.Hashtable ; 7 8 class ContextMap { 9 private Object rootValue; 10 private Object otherValue; 11 private final Hashtable nameTable = new Hashtable (); 12 13 Object get(Vector context) { 14 return get(context, context.size()); 15 } 16 17 boolean put(boolean isRoot, Vector names, Object value) { 18 return put(isRoot, names, names.size(), value); 19 } 20 21 private Object get(Vector context, int len) { 22 if (len > 0) { 23 ContextMap nestedMap = (ContextMap)nameTable.get(context.elementAt(len - 1)); 24 if (nestedMap != null) { 25 Object 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 names, int len, Object 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 |