KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > util > text > MapContext


1 package org.sapia.util.text;
2
3 import java.util.Map JavaDoc;
4
5
6 /**
7  * A template context that tries to resolve properties from provided properties (in the
8  * form of a <code>Map</code>, and which can hold a reference to an "ancestor" context. This
9  * pattern allows to hierarchically organize template contexts in the from of a
10  * chain of responsability.
11  * <p>
12  * The following code creates a <code>MapContext</code> that resolves values from
13  * a <code>Map</code> or from the system properties.
14  *
15  * <pre>
16  * Map someValues = new HashMap();
17  * // here init the map with some data...
18  * MapContext ctx = new MapContext(map, new SystemContext(), false);
19  * ...
20  * </pre>
21  *
22  * @author Yanick Duchesne
23  * 2003-04-08
24  *
25  * <dl>
26  * <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>
27  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
28  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
29  * </dl>
30  */

31 public class MapContext implements TemplateContextIF {
32   private Map JavaDoc _props;
33   private TemplateContextIF _ancestor;
34   private boolean _ancestorFirst;
35
36   /**
37    * Creates an instance of this class.
38    *
39    * @param a <code>Map</code> instance that will be used to resolve values.
40    * @param ancestor a <code>TemplateContextIF</code> instance to which the
41    * lookup can eventually be delegated.
42    * @param ancestorFirst if <code>true</code>, indicates that this instance
43    * should first try to delegate the value lookup to its ancestor prior to trying
44    * itself - the opposite applies if false.
45    */

46   public MapContext(Map JavaDoc props, TemplateContextIF ancestor, boolean ancestorFirst) {
47     _props = props;
48     _ancestor = ancestor;
49     _ancestorFirst = ancestorFirst;
50   }
51
52   /**
53    * @see org.sapia.util.text.TemplateContextIF#getValue(String)
54    */

55   public Object JavaDoc getValue(String JavaDoc aName) {
56     Object JavaDoc value;
57
58     if (_ancestorFirst) {
59       value = fromAncestor(aName);
60
61       if (value == null) {
62         value = _props.get(aName);
63       }
64     } else {
65       value = _props.get(aName);
66
67       if (value == null) {
68         value = fromAncestor(aName);
69       }
70     }
71
72     return value;
73   }
74
75   /**
76    * @see org.sapia.util.text.TemplateContextIF#put(String, Object)
77    */

78   public void put(String JavaDoc name, Object JavaDoc value) {
79     _props.put(name, value);
80   }
81
82   protected Object JavaDoc fromAncestor(String JavaDoc aName) {
83     if (_ancestor != null) {
84       return _ancestor.getValue(aName);
85     }
86
87     return null;
88   }
89 }
90
Popular Tags