KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > magnet > domain > Parameters


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

30 public class Parameters extends AbstractRenderable {
31
32   /////////////////////////////////////////////////////////////////////////////////////////
33
///////////////////////////////// INSTANCE ATTRIBUTES /////////////////////////////////
34
/////////////////////////////////////////////////////////////////////////////////////////
35

36   /** The profile name associated with this parameters. */
37   private String JavaDoc _theProfile;
38
39   /** The list of params of this parameters. */
40   private List JavaDoc _theParams;
41
42   /** The map of params identified with the name of each param. */
43   private Map JavaDoc _theParamsByName;
44
45   /////////////////////////////////////////////////////////////////////////////////////////
46
//////////////////////////////////// CONSTRUCTORS /////////////////////////////////////
47
/////////////////////////////////////////////////////////////////////////////////////////
48

49   /**
50    * Creates a new Parameters instance.
51    */

52   public Parameters() {
53     _theParams = new ArrayList JavaDoc();
54     _theParamsByName = new HashMap JavaDoc();
55   }
56
57   /**
58    * Creates a new Parameters instance with the argument passed in.
59    *
60    * @param aProfile The profile name of this parameters.
61    */

62   public Parameters(String JavaDoc aProfile) {
63     _theParams = new ArrayList JavaDoc();
64     _theParamsByName = new HashMap JavaDoc();
65     _theProfile = aProfile;
66   }
67
68   /////////////////////////////////////////////////////////////////////////////////////////
69
////////////////////////////////// ACCESSOR METHODS ///////////////////////////////////
70
/////////////////////////////////////////////////////////////////////////////////////////
71

72   /**
73    * Returns the profile name of this parameters.
74    *
75    * @return The profile name of this parameters.
76    */

77   public String JavaDoc getProfile() {
78     return _theProfile;
79   }
80
81   /**
82    * Returns the collection of param objects.
83    *
84    * @return The collection of param objects.
85    */

86   public Collection JavaDoc getParams() {
87     return _theParams;
88   }
89
90   /**
91    * Return the parameter identified by the name passed in.
92    *
93    * @param aName The name of the parameter to retrieve.
94    * @return The parameter identified by the name or null if no parameter is
95    * found with the name passed in.
96    * @exception IllegalArgumentException If the name passed in is null.
97    */

98   public Param getParam(String JavaDoc aName) {
99     if (aName == null) {
100       throw new IllegalArgumentException JavaDoc("The name passed in is null");
101     }
102
103     return (Param) _theParamsByName.get(aName);
104   }
105
106   /////////////////////////////////////////////////////////////////////////////////////////
107
/////////////////////////////////// MUTATOR METHODS ///////////////////////////////////
108
/////////////////////////////////////////////////////////////////////////////////////////
109

110   /**
111    * Changes the profile name of this parameters.
112    *
113    * @param aProfile The new profile name.
114    */

115   public void setProfile(String JavaDoc aProfile) {
116     _theProfile = aProfile;
117   }
118
119   /**
120    * Adds the parameter passed to this parameters collection.
121    *
122    * @param aParam The parameter to add.
123    * @exception IllegalArgumentException If the paramter passed in or the
124    * name of the parameter is null.
125    */

126   public void addParam(Param aParam) {
127     if (aParam == null) {
128       throw new IllegalArgumentException JavaDoc("The parameter passed in is null");
129     } else if (aParam.getName() == null) {
130       throw new IllegalArgumentException JavaDoc("The name of the parameter passed in is null");
131     }
132
133     Param anOldParam = (Param) _theParamsByName.get(aParam.getName());
134     if (anOldParam != null) {
135       _theParams.remove(anOldParam);
136     }
137     
138     _theParams.add(aParam);
139     _theParamsByName.put(aParam.getName(), aParam);
140   }
141
142   /**
143    * Removes the parameter passed in from this parameters.
144    *
145    * @param aParam The parameter to remove.
146    * @exception IllegalArgumentException If the paramter passed in or the
147    * name of the parameter is null.
148    */

149   public void removeParam(Param aParam) {
150     if (aParam == null) {
151       throw new IllegalArgumentException JavaDoc("The parameter passed in is null");
152     } else if (aParam.getName() == null) {
153       throw new IllegalArgumentException JavaDoc("The name of the parameter passed in is null");
154     }
155
156     _theParams.remove(aParam);
157     _theParamsByName.remove(aParam.getName());
158   }
159
160   /**
161    * Removes all the parameter from this parameters.
162    */

163   public void clearParams() {
164     _theParams.clear();
165     _theParamsByName.clear();
166   }
167
168   /////////////////////////////////////////////////////////////////////////////////////////
169
////////////////////////////////// OVERRIDEN METHODS //////////////////////////////////
170
/////////////////////////////////////////////////////////////////////////////////////////
171

172   /**
173    * Renders this objects to the magnet context passed in.
174    *
175    * @param aContext The magnet context to use.
176    * @exception RenderingException If an error occurs while rendering this object.
177    */

178   public void render(MagnetContext aContext) throws RenderingException {
179     if (_theProfile == null ||
180         (aContext.getProfile() != null && _theProfile.equals(aContext.getProfile()))) {
181
182       for (Iterator JavaDoc it = _theParams.iterator(); it.hasNext(); ) {
183         Param aParam = (Param) it.next();
184       
185         // Validate if we render the param or not
186
if ((aParam.getIfDefine() == null ||
187                 aContext.getValue(aParam.getIfDefine()) != null) &&
188             (aParam.getUnlessDefine() == null ||
189                 aContext.getValue(aParam.getUnlessDefine()) == null)) {
190
191           // Render the parameter
192
aParam.render(aContext);
193           
194           // Add the param in the right context
195
if (aParam.getScope().equals(Param.SCOPE_MAGNET)) {
196             aContext.addParameter(aParam, (_theProfile != null));
197           } else if (aParam.getScope().equals(Param.SCOPE_SYSTEM)) {
198             System.setProperty(aParam.getName(), aParam.getValue());
199           } else {
200             StringBuffer JavaDoc aBuffer = new StringBuffer JavaDoc();
201             aBuffer.append("The param ").append(aParam.getName()).
202                     append(" has an invalid scope: ").append(aParam.getScope()).
203                     append(". The scope must either be ").append(Param.SCOPE_MAGNET).
204                     append(" or ").append(Param.SCOPE_SYSTEM);
205             throw new RenderingException(aBuffer.toString());
206           }
207         }
208       }
209     }
210   }
211
212   /**
213    * Returns a string representation of this parameters.
214    *
215    * @return A string representation of this parameters.
216    */

217   public String JavaDoc toString() {
218     StringBuffer JavaDoc aBuffer = new StringBuffer JavaDoc(super.toString());
219     aBuffer.append("[profile=").append(_theProfile).
220             append(" params=").append(_theParams).
221             append("]");
222
223     return aBuffer.toString();
224   }
225 }
226
Popular Tags