KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.sapia.validator;
2
3 import java.util.HashMap JavaDoc;
4 import java.util.Locale JavaDoc;
5 import java.util.Map JavaDoc;
6 import java.util.Stack JavaDoc;
7
8 /**
9  * This class models the execution context of <code>Validatable</code>
10  * instances. An instance of this class is not thread-safe.
11  *
12  * @author Yanick Duchesne
13  * <dl>
14  * <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>
15  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
16  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
17  * </dl>
18  */

19 public class ValidationContext {
20   private Stack JavaDoc _stack = new Stack JavaDoc();
21   private Vlad _config;
22   private Locale JavaDoc _locale;
23   private Status _stat;
24   private Map JavaDoc _globals;
25   private Map JavaDoc _contextMap = new HashMap JavaDoc();
26   /**
27    * Constructor for ValidationContext.
28    */

29   public ValidationContext(Map JavaDoc globals,
30                            Object JavaDoc toValidate,
31                            Vlad cfg,
32                            Locale JavaDoc locale) {
33     this(globals, toValidate, cfg, locale, null);
34   }
35   
36   /**
37    * Constructor for ValidationContext with contextual object map.
38    */

39   public ValidationContext(Map JavaDoc globals,
40                            Object JavaDoc toValidate,
41                            Vlad cfg,
42                            Locale JavaDoc locale,
43                            Map JavaDoc contextMap) {
44      _stack.push(toValidate);
45      _config = cfg;
46      _stat = new Status(this);
47      _locale = locale;
48      _globals = globals;
49      if (contextMap != null) {
50        _contextMap.putAll(contextMap);
51      }
52   }
53
54   /**
55    * Returns the current object on this context's stack.
56    *
57    * @return an <code>Object</code>.
58    */

59   public Object JavaDoc get() {
60     return _stack.peek();
61   }
62
63   /**
64    * Pushes the given object on this context's stack.
65    *
66    * @param toValidate an <code>Object</code> to validate.
67    */

68   public void push(Object JavaDoc toValidate) {
69     _stack.push(toValidate);
70   }
71
72   /**
73    * Pops the current object from this context's stack.
74    *
75    * @return an <code>Object</code>.
76    */

77   public Object JavaDoc pop() {
78     return _stack.pop();
79   }
80   
81   /**
82    * @param name the name of a global value.
83    *
84    * @see Vlad#getGlobal(String)
85    */

86   public Object JavaDoc getGlobal(String JavaDoc name){
87     return _globals.get(name);
88   }
89
90   /**
91    * This method first attempts looking up in the map of this instance; if no
92    * object is found, it resorts to the global map.
93    *
94    * @param name the name of the desired value.
95    * @return the <code>Object</code> corresponding to the given name,
96    * or <code>null</code> if no such object exists.
97    */

98   public Object JavaDoc get(String JavaDoc name){
99     Object JavaDoc val = _contextMap.get(name);
100     if(val == null){
101       return getGlobal(name);
102     }
103     return val;
104   }
105
106   /**
107    * Returns this instance's validator.
108    *
109    * @return a <code>Vlad</code>.
110    */

111   public Vlad getConfig() {
112     return _config;
113   }
114
115   /**
116    * Returns this instance's <code>Locale</code>.
117    *
118    * @return a <code>Locale</code>.
119    */

120   public Locale JavaDoc getLocale() {
121     return _locale;
122   }
123
124   /**
125    * Returns this instance's execution status.
126    *
127    * @return a <code>Status</code>.
128    */

129   public Status getStatus() {
130     return _stat;
131   }
132 }
133
Popular Tags