KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > magnet > render > MagnetContext


1 package org.sapia.magnet.render;
2
3 // Import of Sun's JDK classes
4
// ---------------------------
5
import java.util.Collection JavaDoc;
6 import java.util.HashMap JavaDoc;
7 import java.util.Map JavaDoc;
8
9 // Import of Sapia's utility classes
10
// ---------------------------------
11
import org.sapia.util.text.TemplateContextIF;
12
13 // Import of Sapia's magnet classes
14
// ---------------------------------
15
import org.sapia.magnet.domain.Param;
16
17
18 /**
19  *
20  *
21  * @author Jean-Cedric Desrochers
22  *
23  * <dl>
24  * <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>
25  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
26  * <a HREF="http://www.sapia-oss.org/license.html" target="sapia-license">license page</a> at the Sapia OSS web site</dd></dt>
27  * </dl>
28  */

29 public class MagnetContext implements TemplateContextIF {
30
31   /////////////////////////////////////////////////////////////////////////////////////////
32
///////////////////////////////// INSTANCE ATTRIBUTES /////////////////////////////////
33
/////////////////////////////////////////////////////////////////////////////////////////
34

35   /** The profile name of this context. */
36   private String JavaDoc _theProfile;
37
38   /** The parent context of this context. */
39   private MagnetContext _theParent;
40
41   /** The map of parameters of this context associated with theyre param name. */
42   private Map JavaDoc _theParameters;
43
44   /////////////////////////////////////////////////////////////////////////////////////////
45
//////////////////////////////////// CONSTRUCTORS /////////////////////////////////////
46
/////////////////////////////////////////////////////////////////////////////////////////
47

48   /**
49    * Creates a new MagnetContext with the argument passed in.
50    *
51    * @param aProfile The profile name of this context.
52    */

53   public MagnetContext(String JavaDoc aProfile) {
54     _theProfile = aProfile;
55     _theParameters = new HashMap JavaDoc();
56   }
57
58   /**
59    * Creates a new MagnetContext with the argument passed in.
60    *
61    * @param aParent The parent contex of this context.
62    */

63   public MagnetContext(MagnetContext aParent) {
64     _theParent = aParent;
65     _theProfile = aParent.getProfile();
66     _theParameters = new HashMap JavaDoc();
67   }
68
69   /////////////////////////////////////////////////////////////////////////////////////////
70
////////////////////////////////// ACCESSOR METHODS ///////////////////////////////////
71
/////////////////////////////////////////////////////////////////////////////////////////
72

73   /**
74    * Returns the parent context of this magnet context.
75    *
76    * @return The parent context of this magnet context.
77    */

78   public MagnetContext getParent() {
79     return _theParent;
80   }
81
82   /**
83    * Returns the profile name of this magnet context.
84    *
85    * @return The profile name of this magnet context.
86    */

87   public String JavaDoc getProfile() {
88     return _theProfile;
89   }
90
91   /**
92    * Returns the collection of param of this context.
93    *
94    * @return The collection of <CODE>Param</CODE> objects.
95    * @see org.sapia.magnet.domain.Param
96    */

97   public Collection JavaDoc getParameters() {
98     return _theParameters.values();
99   }
100
101   /**
102    * Returns the param associated with the param name passed in.
103    *
104    * @param aName The name of the param to retrieve.
105    * @return The param object found or null if it's no found.
106    * @exception IllegalArgumentException If the name passed in is null.
107    */

108   public Param getParameterFor(String JavaDoc aName) {
109     if (aName == null) {
110       throw new IllegalArgumentException JavaDoc("The name passed in is null");
111     }
112
113     Param aParam = (Param) _theParameters.get(aName);
114     if (aParam == null && _theParent != null) {
115       aParam = _theParent.getParameterFor(aName);
116     }
117
118     return aParam;
119   }
120
121   /////////////////////////////////////////////////////////////////////////////////////////
122
/////////////////////////////////// MUTATOR METHODS ///////////////////////////////////
123
/////////////////////////////////////////////////////////////////////////////////////////
124

125   /**
126    * Changes the profile name of this context.
127    *
128    * @param aProfile The new profile name.
129    */

130   public void setProfile(String JavaDoc aProfile) {
131     _theProfile = aProfile;
132   }
133
134   /**
135    * Adds the parameter passed to this magnet context.
136    *
137    * @param aParameter The param to add.
138    * @param anOverwriteFlag Indicates if the the passed in parameter should overwrite
139    * the value of a parameter of the same name present in this context.
140    * @exception IllegalArgumentException If the paramter passed in or the
141    * name of the parameter is null.
142    */

143   public void addParameter(Param aParameter, boolean anOverwriteFlag) {
144     if (aParameter == null) {
145       throw new IllegalArgumentException JavaDoc("The parameter passed in is null");
146     } else if (aParameter.getName() == null) {
147       throw new IllegalArgumentException JavaDoc("The name of the parameter passed in is null");
148     }
149
150     if (anOverwriteFlag == true) {
151       _theParameters.put(aParameter.getName(), aParameter);
152     } else if (!_theParameters.containsKey(aParameter.getName())) {
153       _theParameters.put(aParameter.getName(), aParameter);
154     }
155   }
156
157   /**
158    * Removes the param passed in from this magnet context.
159    *
160    * @param aParameter The parameter to remove.
161    * @exception IllegalArgumentException If the paramter passed in or the
162    * name of the parameter is null.
163    */

164   public void removeParameter(Param aParameter) {
165     if (aParameter == null) {
166       throw new IllegalArgumentException JavaDoc("The parameter passed in is null");
167     } else if (aParameter.getName() == null) {
168       throw new IllegalArgumentException JavaDoc("The name of the parameter passed in is null");
169     }
170
171     _theParameters.remove(aParameter.getName());
172   }
173
174   /**
175    * Removes all the parameter from this magnet context.
176    */

177   public void clearParameters() {
178     _theParameters.clear();
179   }
180
181   /////////////////////////////////////////////////////////////////////////////////////////
182
////////////////////////////////// OVERRIDEN METHODS //////////////////////////////////
183
/////////////////////////////////////////////////////////////////////////////////////////
184

185   /**
186    * Returns a string representation of this magnet context.
187    *
188    * @return A string representation of this magnet context.
189    */

190   public String JavaDoc toString() {
191     StringBuffer JavaDoc aBuffer = new StringBuffer JavaDoc(super.toString());
192     aBuffer.append("[profile=").append(_theProfile).
193             append(" param=").append(_theParameters).
194             append(" parent=").append(_theParent).
195             append("]");
196
197     return aBuffer.toString();
198   }
199
200   /////////////////////////////////////////////////////////////////////////////////////////
201
/////////////////////////////// INTERACE IMPLEMENTATION ///////////////////////////////
202
/////////////////////////////////////////////////////////////////////////////////////////
203

204   /**
205    * Returns the value of this context for the property name passed in.
206    *
207    * @param aName The name of the property.
208    * @return The value of this context for the property name passed in.
209    */

210   public Object JavaDoc getValue(String JavaDoc aName) {
211     Param aParameter = getParameterFor(aName);
212
213     if (aParameter == null) {
214       return System.getProperty(aName);
215     } else {
216       return aParameter.getValue();
217     }
218   }
219
220   /**
221    * Puts the given value in this context; if one already exists
222    * under the given name, it is overwritten.
223    *
224    * @param name the name under which to map the given value.
225    * @param value an <code>Object</code>.
226    */

227   public void put(String JavaDoc name, Object JavaDoc value) {
228     if (!(value instanceof Param)) {
229       throw new IllegalArgumentException JavaDoc("The object to put in this magnet context is not a Param instance");
230     } else if (name == null || !name.equals(((Param) value).getName())) {
231       throw new IllegalArgumentException JavaDoc("The name of the value to add [" + name + "] is invalid");
232     }
233
234     addParameter((Param) value, true);
235   }
236 }
237
Popular Tags