KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > validator > Hierarchy


1 package org.sapia.validator;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.HashMap JavaDoc;
5 import java.util.List JavaDoc;
6 import java.util.Map JavaDoc;
7 import java.util.StringTokenizer JavaDoc;
8
9 /**
10  * This class olds a hierarchy of error message strings, organized by
11  * locale.
12  *
13  * @author Yanick Duchesne
14  * <dl>
15  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
16  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
17  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
18  * </dl>
19  */

20 class Hierarchy {
21   private ErrorMessage _msg;
22   private Hierarchy _parent;
23   private Map JavaDoc _children = new HashMap JavaDoc();
24
25   /**
26    * Constructor for Hierarchy.
27    */

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 JavaDoc[] path = split(msg.getLocale());
37
38       bind(msg, path, 0);
39     }
40   }
41
42   ErrorMessage lookup(String JavaDoc locale) {
43     if ((locale == null) || (locale.length() == 0)) {
44       return _msg;
45     }
46
47     String JavaDoc[] path = split(locale);
48
49     return lookup(path, 0);
50   }
51
52   ErrorMessage reverseLookup(String JavaDoc locale) {
53     if ((locale == null) || (locale.length() == 0)) {
54       return _msg;
55     }
56
57     String JavaDoc[] path = split(locale);
58
59     return reverselookup(path, 0);
60   }
61
62   private ErrorMessage reverselookup(String JavaDoc[] 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 JavaDoc[] 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 JavaDoc[] 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 JavaDoc[] split(String JavaDoc path) {
128     List JavaDoc parts = new ArrayList JavaDoc();
129
130     StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(path, "/");
131
132     String JavaDoc token;
133
134     while (st.hasMoreTokens()) {
135       token = st.nextToken();
136       parts.add(token);
137     }
138
139     return (String JavaDoc[]) parts.toArray(new String JavaDoc[parts.size()]);
140   }
141 }
142
Popular Tags