1 28 29 package com.idaremedia.antx.flowcontrol.call; 30 31 import java.util.Iterator ; 32 import java.util.List ; 33 import java.util.Map ; 34 35 import org.apache.tools.ant.BuildException; 36 import org.apache.tools.ant.Project; 37 import org.apache.tools.ant.Target; 38 import org.apache.tools.ant.Task; 39 import org.apache.tools.ant.UnknownElement; 40 import org.apache.tools.ant.taskdefs.Ant; 41 import org.apache.tools.ant.taskdefs.Property; 42 import org.apache.tools.ant.types.PropertySet; 43 44 import com.idaremedia.antx.AntX; 45 import com.idaremedia.antx.AntXFixture; 46 import com.idaremedia.antx.helpers.InnerNameValuePair; 47 import com.idaremedia.antx.ownhelpers.ProjectDependentSkeleton; 48 import com.idaremedia.antx.ownhelpers.ScopedProperties; 49 import com.idaremedia.antx.ownhelpers.TaskExaminer; 50 51 63 64 public final class MacroInstanceCaller extends ProjectDependentSkeleton 65 implements TargetCaller, ConfigurableCaller 66 { 67 private static final String IAM_= AntX.flow+"MacroInstanceCaller"; 68 69 70 76 public MacroInstanceCaller() 77 { 78 } 79 80 81 87 public MacroInstanceCaller(CallerTask task) 88 { 89 setCaller(task); 90 } 91 92 93 94 100 public void setCaller(Task task) 101 { 102 AntX.require_(task!=null,IAM_,"setCalr- nonzro task"); 103 Target parent = task.getOwningTarget(); 104 if (parent!=null) { 105 setTarget(parent.getName()); 106 } 107 setProject(task.getProject()); 108 m_owningTask = task; 109 } 110 111 112 113 116 public void setTarget(String targetName) 117 { 118 m_targetName = targetName; 119 } 120 121 122 125 public String getTargetName() 126 { 127 return m_targetName; 128 } 129 130 131 132 136 public final void setStepName(String stepName) 137 { 138 throw new UnsupportedOperationException (); 139 } 140 141 142 145 public final String getStepName() 146 { 147 return null; 148 } 149 150 151 152 155 public Property createProperty() 156 { 157 Property p = new Property(); 158 p.setProject(getProject()); 159 if (m_plist==null) { 160 m_plist = AntXFixture.newList(); 161 } 162 m_plist.add(p); 163 return p; 164 } 165 166 167 168 171 public PropertySet createPropertySet() 172 { 173 PropertySet p = new PropertySet(); p.setProject(getProject()); 175 if (m_plist==null) { 176 m_plist = AntXFixture.newList(); 177 } 178 m_plist.add(p); 179 return p; 180 } 181 182 183 184 188 public Ant.Reference createReference() 189 { 190 throw new UnsupportedOperationException (); 191 } 192 193 194 195 198 public InnerNameValuePair createAttribute() 199 { 200 InnerNameValuePair a = new InnerNameValuePair(); 201 if (m_plist==null) { 202 m_plist = AntXFixture.newList(); 203 } 204 m_plist.add(a); 205 return a; 206 } 207 208 209 210 215 private Map installOverlay() 216 { 217 Project P= getProjectNoNull(); 218 Map attrs = null; 219 if (m_plist!=null) { 220 m_overlay = new ScopedProperties(P,true); 221 Iterator itr= m_plist.iterator(); 222 while (itr.hasNext()) { 223 Object o = itr.next(); 224 if (o instanceof InnerNameValuePair) { 225 InnerNameValuePair nv = (InnerNameValuePair)o; 226 nv.verifyNamed(); 227 if (attrs==null) { 228 attrs = AntXFixture.newMap(); 229 } 230 attrs.put(nv.getName(), nv.getValue(P,true)); 231 } 232 else if (o instanceof Property) { 233 m_overlay.put((Property)o); 234 } 235 else { 236 m_overlay.put((PropertySet)o); 237 } 238 } 239 m_overlay.install(); 240 } 241 return attrs; 242 } 243 244 245 246 249 private void uninstallOverlay() 250 { 251 if (m_overlay!=null) { 252 m_overlay.uninstall(null); 253 m_overlay=null; 254 } 255 } 256 257 258 259 260 266 public void run() 267 throws BuildException 268 { 269 Map attrs = installOverlay(); 270 try { 271 UnknownElement launch = TaskExaminer.newUEProxy 272 (getTargetName(),attrs,(String )null,getProject()); 273 launch.setOwningTarget(m_owningTask.getOwningTarget()); 274 launch.perform(); 275 } finally { 276 uninstallOverlay(); 277 } 278 } 279 280 281 282 290 public final void run(String property, String value) 291 throws BuildException 292 { 293 Property p= this.createProperty(); 294 p.setName(property); 295 p.setValue(value); 296 run(); 297 } 298 299 300 301 306 public final void run(Map properties) 307 throws BuildException 308 { 309 if (!properties.isEmpty()) { 310 Iterator itr = properties.entrySet().iterator(); 311 while (itr.hasNext()) { 312 Map.Entry item = (Map.Entry )itr.next(); 313 Property p= this.createProperty(); 314 p.setName(item.getKey().toString()); 315 p.setValue(item.getValue().toString()); 316 } 317 } 318 run(); 319 } 320 321 322 323 328 public final void run(TargetCaller.Prep prep) 329 throws BuildException 330 { 331 prep.prepare(this); 332 run(); 333 } 334 335 336 private String m_targetName; 337 private List m_plist; 338 private Task m_owningTask; 339 private ScopedProperties m_overlay;} 341 342 343 | Popular Tags |