1 package org.sapia.validator; 2 3 import java.util.ArrayList ; 4 import java.util.HashMap ; 5 import java.util.List ; 6 import java.util.Map ; 7 import java.util.StringTokenizer ; 8 9 20 class Hierarchy { 21 private ErrorMessage _msg; 22 private Hierarchy _parent; 23 private Map _children = new HashMap (); 24 25 28 Hierarchy(Hierarchy parent) { 29 _parent = parent; 30 } 31 32 void bind(ErrorMessage msg) { 33 if (msg.getLocale() == null) { 34 _msg = msg; 35 } else { 36 String [] path = split(msg.getLocale()); 37 38 bind(msg, path, 0); 39 } 40 } 41 42 ErrorMessage lookup(String locale) { 43 if ((locale == null) || (locale.length() == 0)) { 44 return _msg; 45 } 46 47 String [] path = split(locale); 48 49 return lookup(path, 0); 50 } 51 52 ErrorMessage reverseLookup(String locale) { 53 if ((locale == null) || (locale.length() == 0)) { 54 return _msg; 55 } 56 57 String [] path = split(locale); 58 59 return reverselookup(path, 0); 60 } 61 62 private ErrorMessage reverselookup(String [] path, int index) { 63 if (index >= path.length) { 64 if ((_msg == null) && (_parent != null)) { 65 return _parent.reverse(); 66 } 67 68 return _msg; 69 } 70 71 Hierarchy h = (Hierarchy) _children.get(path[index]); 72 if (h == null) { 73 if(_msg != null){ 74 return _msg; 75 } 76 else if (_parent != null) { 77 return _parent.reverse(); 78 } 79 return null; 80 } else { 81 return h.reverselookup(path, ++index); 82 } 83 } 84 85 private ErrorMessage reverse() { 86 if (_msg != null) { 87 return _msg; 88 } else if (_parent != null) { 89 return _parent.reverse(); 90 } else { 91 return null; 92 } 93 } 94 95 private ErrorMessage lookup(String [] path, int index) { 96 if (index >= path.length) { 97 return _msg; 98 } 99 100 Hierarchy h = (Hierarchy) _children.get(path[index]); 101 102 if (h == null) { 103 return null; 104 } else { 105 return h.lookup(path, ++index); 106 } 107 } 108 109 private void bind(ErrorMessage msg, String [] path, int index) { 110 if (index >= path.length) { 111 _msg = msg; 112 } else { 113 Hierarchy h = (Hierarchy) _children.get(path[index]); 114 if (h == null) { 115 h = new Hierarchy(this); 116 _children.put(path[index], h); 117 } 118 119 h.bind(msg, path, ++index); 120 } 121 } 122 123 public ErrorMessage getMessage() { 124 return _msg; 125 } 126 127 private static String [] split(String path) { 128 List parts = new ArrayList (); 129 130 StringTokenizer st = new StringTokenizer (path, "/"); 131 132 String token; 133 134 while (st.hasMoreTokens()) { 135 token = st.nextToken(); 136 parts.add(token); 137 } 138 139 return (String []) parts.toArray(new String [parts.size()]); 140 } 141 } 142
| Popular Tags
|