1 28 29 package com.idaremedia.antx.fixture.helpers; 30 31 import java.util.Map ; 32 33 import org.apache.tools.ant.BuildException; 34 import org.apache.tools.ant.ComponentHelper; 35 import org.apache.tools.ant.DynamicElementNS; 36 import org.apache.tools.ant.Project; 37 import org.apache.tools.ant.ProjectHelper; 38 import org.apache.tools.ant.Task; 39 40 import com.idaremedia.antx.AntX; 41 import com.idaremedia.antx.AssertableTask; 42 import com.idaremedia.antx.FixtureExaminer; 43 import com.idaremedia.antx.helpers.Tk; 44 import com.idaremedia.antx.parameters.FeedbackLevel; 45 46 47 73 74 public final class DataDefTask extends AssertableTask implements DynamicElementNS 75 { 76 79 public DataDefTask() 80 { 81 super(AntX.nopackage); 82 } 83 84 85 89 94 public void setName(String dstRefId) 95 { 96 require_(dstRefId!=null, "setName- nonzro refid"); 97 m_dstRefId = dstRefId; 98 } 99 100 101 102 106 public final void setToId(String dstRefId) 107 { 108 setName(dstRefId); 109 } 110 111 112 113 118 public String getToRefId() 119 { 120 return m_dstRefId; 121 } 122 123 124 125 130 public void setSetId(boolean setIt) 131 { 132 m_setImplId = setIt; 133 } 134 135 136 137 143 public void setTellProperty(String property) 144 { 145 require_(property!=null,"setProperty- nonzro property"); 146 m_announceProperty = property; 147 } 148 149 150 151 155 public final String getTellProperty() 156 { 157 return m_announceProperty; 158 } 159 160 161 162 170 public void setHaltIfExists(boolean halt) 171 { 172 m_haltIfExists = halt; 173 } 174 175 176 177 182 public boolean isHaltIfExists() 183 { 184 return m_haltIfExists; 185 } 186 187 188 189 196 public void setOverwrite(boolean allowOverwrite) 197 { 198 m_allowOverwrite = allowOverwrite; 199 } 200 201 202 203 208 public boolean willAllowOverwrite() 209 { 210 return m_allowOverwrite; 211 } 212 213 214 215 222 public void setFeedback(String level) 223 { 224 require_(level!=null,"setFeedback- nonzro level"); 225 FeedbackLevel fbl = FeedbackLevel.from(level); 226 if (fbl==null) { 227 String e = getAntXMsg("task.illegal.param.value", 228 getTaskName(), level, "feedback"); 229 log(e, Project.MSG_ERR); 230 throw new BuildException(e, getLocation()); 231 } 232 m_fbLevel = fbl; 233 } 234 235 236 237 241 public final FeedbackLevel getFeedbackLevel() 242 { 243 return m_fbLevel; 244 } 245 246 247 251 252 256 public final Object createDynamicElement(String uri, String localName, 257 String qName) 258 { 259 if (m_dataImpl!=null) { 260 String ermsg = uistrs().get("taskset.only.one.specialtask", 261 qName, m_dataQName); 262 log(ermsg,Project.MSG_ERR); 263 throw new BuildException(ermsg,getLocation()); 264 } 265 266 Project P= getProject(); 267 ComponentHelper ch = ComponentHelper.getComponentHelper(P); 268 String fullname = localName; 269 if (!localName.equals(qName)) { 270 fullname = ProjectHelper.genComponentName(uri,localName); 271 } 272 273 Object o= ch.createComponent(fullname); 274 if (o!=null) { 275 P.setProjectReference(o); 276 if (o instanceof Task) { 277 Task t = (Task)o; 278 t.setOwningTarget(getOwningTarget()); 279 t.setTaskName(qName); 280 t.setTaskType(fullname); 281 t.init(); 282 } 283 m_dataQName = qName; 284 m_dataImpl = o; 285 } 286 287 return m_dataImpl; 288 } 289 290 291 295 296 300 public void execute() 301 { 302 verifyCanExecute_("exec"); 303 304 final Project P = getProject(); 305 boolean quiet = FeedbackLevel.isQuietish(m_fbLevel,true,true); 306 307 Object there = FixtureExaminer.trueReference(P,m_dstRefId); 308 if (there==FixtureExaminer.PENDING_REFERENCE) { 309 quiet = true; 310 there = null; 311 } 312 if (there!=null) { 313 String msg = uistrs().get("task.warn.refid.exists",m_dstRefId); 314 if (isHaltIfExists()) { 315 log(msg,Project.MSG_ERR); 316 throw new BuildException(msg,getLocation()); 317 } 318 if (!willAllowOverwrite()) { 319 if (!quiet) { 320 log(msg,Project.MSG_VERBOSE); 321 } 322 return; 323 } 324 if (!quiet) { 325 log(msg,Project.MSG_WARN); 326 } 327 } 328 329 if (m_setImplId) { 330 tryToSetId(m_dataImpl); 331 } 332 333 if (m_dataImpl instanceof Task) { 334 ((Task)m_dataImpl).perform(); 335 } 336 337 Map refs = P.getReferences(); 338 if (quiet) { 339 refs.put(m_dstRefId,m_dataImpl); 340 } else { 341 P.addReference(m_dstRefId,m_dataImpl); 342 } 343 344 if (m_announceProperty!=null) { 345 checkIfProperty_(m_announceProperty,true); 346 P.setNewProperty(m_announceProperty,"true"); 347 } 348 } 349 350 351 352 357 public void verifyIsDefined() 358 { 359 if (getToRefId()==null) { 360 String ermsg = uistrs().get("task.needs.this.attr", 361 getTaskName(),"name"); 362 log(ermsg,Project.MSG_ERR); 363 throw new BuildException(ermsg,getLocation()); 364 } 365 if (m_dataImpl==null) { 366 String ermsg = uistrs().get("task.needs.oneof.these.nested", 367 getTaskName(),"<any datatype>"); 368 log(ermsg,Project.MSG_ERR); 369 throw new BuildException(ermsg,getLocation()); 370 } 371 } 372 373 374 379 protected void verifyCanExecute_(String calr) 380 { 381 verifyInProject_(calr); 382 verifyIsDefined(); 383 } 384 385 386 387 392 private void tryToSetId(Object instance) 393 { 394 try { 395 Tk.invokeit(instance, "setId", new Class []{String .class}, 396 new String []{getToRefId()}); 397 } catch(Exception reflectX) { 398 399 } 400 } 401 402 403 private String m_dstRefId; 404 private String m_announceProperty; 405 private boolean m_haltIfExists; private boolean m_allowOverwrite=true; private boolean m_setImplId; private FeedbackLevel m_fbLevel=FeedbackLevel.NORMAL; private Object m_dataImpl; 410 private String m_dataQName; 411 412 } 413 414 | Popular Tags |