1 28 29 package com.idaremedia.antx.ownhelpers; 30 31 import java.util.Hashtable ; 32 import java.util.Iterator ; 33 import java.util.Map ; 34 35 import org.apache.tools.ant.Project; 36 import org.apache.tools.ant.PropertyHelper; 37 import org.apache.tools.ant.taskdefs.Property; 38 import org.apache.tools.ant.types.PropertySet; 39 40 import com.idaremedia.antx.AntX; 41 import com.idaremedia.antx.FixtureExaminer; 42 import com.idaremedia.antx.apis.Nameable; 43 import com.idaremedia.antx.apis.ProblemHandler; 44 import com.idaremedia.antx.apis.ProjectDependent; 45 import com.idaremedia.antx.helpers.GenericParameters; 46 47 58 59 public final class ScopedProperties extends Hashtable 60 implements ProjectDependent, Nameable 61 { 62 private static final String IAM_= AntX.utilities+"ScopedProperties:"; 63 64 65 71 public ScopedProperties(boolean commandProperties) 72 { 73 super(); 74 m_commandProperties = commandProperties; 75 } 76 77 78 79 84 public ScopedProperties(Project project, boolean commandProperties) 85 { 86 super(); 87 m_commandProperties = commandProperties; 88 setProject(project); 89 } 90 91 92 93 98 public void setProject(Project project) 99 { 100 m_project = project; 101 } 102 103 104 108 public Project getProject() 109 { 110 return m_project; 111 } 112 113 114 115 119 public void setName(String name) 120 { 121 m_label = name; 122 } 123 124 125 126 130 public String getName() 131 { 132 return m_label; 133 } 134 135 136 137 143 public final void swapUnderscores(boolean swapEm) 144 { 145 m_swapUnderscores = swapEm; 146 } 147 148 149 150 155 public final void install() 156 { 157 AntX.verify_(m_conduit==null,IAM_,"install- not installed"); 158 AntX.verify_(getProject()!=null,IAM_,"install- in project"); 159 160 if (!isEmpty()) { 161 PropertyHelper ph = PropertyHelper.getPropertyHelper(getProject()); 162 Conduit c = new Conduit(m_commandProperties); 163 synchronized(ph) { 164 c.setNext(ph.getNext()); 165 ph.setNext(c); 166 } 167 m_conduit = c; 168 } 169 } 170 171 172 173 179 public final void uninstall(ProblemHandler notInstalled) 180 { 181 if (!isEmpty() && m_conduit!=null) { 182 Conduit c = (Conduit)m_conduit; 183 m_conduit = null; 184 PropertyHelper ph = PropertyHelper.getPropertyHelper(getProject()); 185 synchronized(ph) { 186 PropertyHelper prevPh=null; 187 do { 188 if (ph.getNext()==c) { break; } 189 prevPh = ph; 190 ph = ph.getNext(); 191 } while(ph!=null); 192 if (ph!=null) { 193 ph.setNext(c.getNext()); 194 if (prevPh!=null) { 195 prevPh.setNext(ph); 196 } 197 } else { 198 String warning= AntX.uistrs().get("fixture.supPh.not.instald",getName()); 199 if (notInstalled!=null) { 200 notInstalled.problem(warning,Project.MSG_WARN); 201 } else { 202 getProject().log(warning,Project.MSG_WARN); 203 } 204 } 205 c.setNext(null); 206 } 207 } 208 } 209 210 211 212 217 public final Object put(Object key, Object value) 218 { 219 AntX.require_((key instanceof String ),IAM_,"put- nonzro name"); 220 String s = (String )key; 221 if (m_swapUnderscores) { 222 s = s.replace('_','.'); 223 } 224 return super.put(s,value); 225 } 226 227 228 229 233 public final void put(PropertySet ps) 234 { 235 AntX.require_(ps!=null,IAM_,"put- nonzro propertyset"); 236 Iterator itr= ps.getProperties().entrySet().iterator(); 237 while (itr.hasNext()) { 238 Map.Entry e= (Map.Entry )itr.next(); 239 super.put(e.getKey(),e.getValue()); } 241 } 242 243 244 245 250 public final void put(Property p) 251 { 252 AntX.require_(p!=null,IAM_,"put- nonzro property"); 253 String name = p.getName(); 254 String value = p.getValue(); 255 if (name!=null && value!=null) { 256 super.put(name,value); } 258 } 259 260 261 262 268 public final void put(GenericParameters params) 269 { 270 AntX.require_(params!=null,IAM_,"put- nonzro propertyset"); 271 Iterator itr= params.copyOfSimpleKeyValues(getProject(),true) 272 .entrySet().iterator(); 273 while (itr.hasNext()) { 274 Map.Entry e= (Map.Entry )itr.next(); 275 super.put(e.getKey(),e.getValue()); 276 } 277 } 278 279 280 281 285 private Object lookup(String name) 286 { 287 String s = (String )get(name); 288 if (s!=null) { 289 String actual = FixtureExaminer.findValue(getProject(),s,name); 290 if (actual!=null) { 291 s = actual; 292 } 293 } 294 return s; 295 } 296 297 298 299 309 private class Conduit extends PropertyHelper 310 { 311 private final boolean m_userOnly; 312 313 Conduit(boolean userOnly) { 314 m_userOnly= userOnly; 315 } 316 317 321 public Object getPropertyHook(String ns, String name, 322 boolean userOnly) { 323 Object o; 324 if (userOnly && !m_userOnly) { 325 o= null; 326 } else { 327 o= ScopedProperties.this.lookup(name); 328 } 329 if (o==null && getNext()!=null) { 330 o = getNext().getPropertyHook(ns,name,userOnly); 331 } 332 return o; 333 } 334 335 345 public boolean setPropertyHook(String ns, String name, Object value, 346 boolean inherited, boolean userOnly, 347 boolean isNew) { 348 349 if (ScopedProperties.this.containsKey(name)) { 350 if ((m_userOnly && (inherited || userOnly)) || 351 (!m_userOnly && !inherited && !userOnly)) { 352 ScopedProperties.this.put(name,value); 353 } 354 return true; } 356 if (getNext()!=null) { 357 return getNext().setPropertyHook(ns,name,value,inherited, 358 userOnly,isNew); 359 } 360 return false; 361 } 362 } 363 364 365 private Project m_project; 366 private boolean m_commandProperties; 367 private Object m_conduit; 368 private String m_label="n/d"; 369 private boolean m_swapUnderscores;} 371 372 373 | Popular Tags |