1 28 29 package com.idaremedia.antx.solo; 30 31 import java.lang.reflect.Method ; 32 33 import org.apache.tools.ant.BuildException; 34 import org.apache.tools.ant.Location; 35 import org.apache.tools.ant.Project; 36 37 import com.idaremedia.antx.AntX; 38 import com.idaremedia.antx.AssertableTask; 39 import com.idaremedia.antx.apis.AntLibFriendly; 40 import com.idaremedia.antx.helpers.Empties; 41 import com.idaremedia.antx.parameters.FeedbackLevel; 42 43 69 70 public final class CopyReferenceTask extends AssertableTask 71 implements AntLibFriendly 72 { 73 76 public CopyReferenceTask() 77 { 78 super(AntX.nopackage); 79 } 80 81 82 86 public void setRefId(String srcRefId) 87 { 88 require_(srcRefId!=null, "setRefId- nonzro id"); 89 m_srcRefId = srcRefId; 90 91 if (m_dstRefId==null) { 92 m_dstRefId= srcRefId; 93 } 94 } 95 96 97 101 public String getRefId() 102 { 103 return m_srcRefId; 104 } 105 106 107 112 public void setToRefId(String dstRefId) 113 { 114 require_(dstRefId!=null, "setRefToId- nonzro id"); 115 m_dstRefId = dstRefId; 116 } 117 118 119 124 public String getToRefId() 125 { 126 return m_dstRefId; 127 } 128 129 130 138 public void setHaltIfExists(boolean halt) 139 { 140 m_haltIfExists = halt; 141 } 142 143 144 149 public boolean isHaltIfExists() 150 { 151 return m_haltIfExists; 152 } 153 154 155 162 public void setOverwrite(boolean allowOverwrite) 163 { 164 m_allowOverwrite = allowOverwrite; 165 } 166 167 168 173 public boolean willAllowOverwrite() 174 { 175 return m_allowOverwrite; 176 } 177 178 179 180 186 public void setShallow(boolean shallowCopy) 187 { 188 m_shallowCopy = shallowCopy; 189 } 190 191 192 193 198 public boolean willCopyShallow() 199 { 200 return m_shallowCopy; 201 } 202 203 204 212 public void setFeedback(String level) 213 { 214 require_(level!=null,"setFeedback- nonzro level"); 215 FeedbackLevel fbl = FeedbackLevel.from(level); 216 if (fbl==null) { 217 String e = getAntXMsg("task.illegal.param.value", 218 getTaskName(), level,"feedback"); 219 log(e, Project.MSG_ERR); 220 throw new BuildException(e, getLocation()); 221 } 222 m_fbLevel = fbl; 223 } 224 225 226 227 232 public final FeedbackLevel getFeedbackLevel() 233 { 234 return m_fbLevel; 235 } 236 237 238 239 246 public void execute() 247 { 248 verifyCanExecute_("execute"); 249 250 final Project P = getProject(); 251 final boolean quiet = FeedbackLevel.isQuietish(m_fbLevel,true); 252 String src_refid = getRefId(); 253 String dst_refid = getToRefId(); 254 255 if (src_refid.equals(dst_refid)) { 257 String ermsg = uistrs().get("task.needs.this.attr",getTaskName(),"torefid"); 258 log(ermsg,Project.MSG_ERR); 259 throw new BuildException(ermsg,getLocation()); 260 } 261 262 if (P.getReference(dst_refid)!=null) { 264 String msg = uistrs().get("task.warn.refid.exists",dst_refid); 265 if (isHaltIfExists()) { 266 log(msg,Project.MSG_ERR); 267 throw new BuildException(msg,getLocation()); 268 } 269 if (!willAllowOverwrite()) { 270 if (!quiet && m_fbLevel!=FeedbackLevel.NORMAL) { 271 log(msg,Project.MSG_VERBOSE); 272 } 273 return; 274 } 275 if (!quiet) { 276 log(msg,Project.MSG_WARN); 277 } 278 } 279 280 Object src = P.getReference(src_refid); 283 Object dst = null; 284 try { 285 if (willCopyShallow()) { 286 dst = src; 287 } else if (src instanceof String ) { dst = new String ((String )src); 289 } else { 290 Method cloneit = src.getClass().getMethod("clone", new Class [0]); 291 dst = cloneit.invoke(src,Empties.EMPTY_CLASS_ARRAY); 292 } 293 } catch (Exception anyX) { 294 String ermsg = uistrs().get("task.bad.refid",src_refid, 295 "PUBLIC "+Cloneable .class.getName(), 296 src.getClass().getName()); 297 log(ermsg,Project.MSG_ERR); 298 throw new BuildException(ermsg,anyX,getLocation()); 299 } 300 301 log("Copying reference "+src_refid+" to "+dst_refid,Project.MSG_DEBUG); 302 if (quiet) { 303 P.getReferences().put(dst_refid, dst); 304 } else { 305 P.addReference(dst_refid, dst); 306 } 307 308 } 309 310 311 316 public void verifyIsDefined() 317 { 318 if (getRefId()==null) { 319 String ermsg = uistrs().get("task.needs.this.attr",getTaskName(),"refid"); 320 log(ermsg,Project.MSG_ERR); 321 throw new BuildException(ermsg,getLocation()); 322 } 323 } 324 325 326 335 public void verifyExists(Project theProject, Location theLocation, Class ofKind) 336 { 337 verifyIsDefined(); 338 339 if (theProject==null) { 340 theProject = getProject(); 341 if (theProject==null) { 342 throw new IllegalStateException ("verifyExists- no project"); 343 } 344 } 345 if (theLocation==null) { 346 theLocation = getLocation(); 347 } 348 349 String error = null; 350 351 String refid = getRefId(); 352 353 Object o = theProject.getReference(refid); 354 if (o==null) { 355 error = uistrs().get("task.missing.refid",refid); 356 } else if (ofKind!=null && !ofKind.isAssignableFrom(o.getClass())) { 357 if ((ofKind==Cloneable .class) && (o instanceof String )) { 358 return; } 360 error = uistrs().get("task.bad.refid",refid,ofKind.getName(), 361 o.getClass().getName()); 362 } 363 if (error!=null) { 364 log(error,Project.MSG_ERR); 365 throw new BuildException(error,theLocation); 366 } 367 } 368 369 370 375 protected void verifyCanExecute_(String calr) 376 { 377 verifyInProject_(calr); 378 379 Class ofKind = Cloneable .class; 380 if (willCopyShallow()) { 381 ofKind = null; 382 } 383 verifyExists(null,null,ofKind); 384 } 385 386 private String m_srcRefId,m_dstRefId; 387 private boolean m_haltIfExists; private boolean m_allowOverwrite=true; private boolean m_shallowCopy; private FeedbackLevel m_fbLevel=FeedbackLevel.NORMAL;} 392 393 394 | Popular Tags |