1 package org.sapia.magnet.render; 2 3 import java.util.Collection ; 6 import java.util.HashMap ; 7 import java.util.Map ; 8 9 import org.sapia.util.text.TemplateContextIF; 12 13 import org.sapia.magnet.domain.Param; 16 17 18 29 public class MagnetContext implements TemplateContextIF { 30 31 35 36 private String _theProfile; 37 38 39 private MagnetContext _theParent; 40 41 42 private Map _theParameters; 43 44 48 53 public MagnetContext(String aProfile) { 54 _theProfile = aProfile; 55 _theParameters = new HashMap (); 56 } 57 58 63 public MagnetContext(MagnetContext aParent) { 64 _theParent = aParent; 65 _theProfile = aParent.getProfile(); 66 _theParameters = new HashMap (); 67 } 68 69 73 78 public MagnetContext getParent() { 79 return _theParent; 80 } 81 82 87 public String getProfile() { 88 return _theProfile; 89 } 90 91 97 public Collection getParameters() { 98 return _theParameters.values(); 99 } 100 101 108 public Param getParameterFor(String aName) { 109 if (aName == null) { 110 throw new IllegalArgumentException ("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 125 130 public void setProfile(String aProfile) { 131 _theProfile = aProfile; 132 } 133 134 143 public void addParameter(Param aParameter, boolean anOverwriteFlag) { 144 if (aParameter == null) { 145 throw new IllegalArgumentException ("The parameter passed in is null"); 146 } else if (aParameter.getName() == null) { 147 throw new IllegalArgumentException ("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 164 public void removeParameter(Param aParameter) { 165 if (aParameter == null) { 166 throw new IllegalArgumentException ("The parameter passed in is null"); 167 } else if (aParameter.getName() == null) { 168 throw new IllegalArgumentException ("The name of the parameter passed in is null"); 169 } 170 171 _theParameters.remove(aParameter.getName()); 172 } 173 174 177 public void clearParameters() { 178 _theParameters.clear(); 179 } 180 181 185 190 public String toString() { 191 StringBuffer aBuffer = new StringBuffer (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 204 210 public Object getValue(String 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 227 public void put(String name, Object value) { 228 if (!(value instanceof Param)) { 229 throw new IllegalArgumentException ("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 ("The name of the value to add [" + name + "] is invalid"); 232 } 233 234 addParameter((Param) value, true); 235 } 236 } 237 | Popular Tags |