1 28 29 package com.idaremedia.antx; 30 31 import java.util.Iterator ; 32 import java.util.Map ; 33 import java.util.Stack ; 34 35 import org.apache.tools.ant.BuildException; 36 import org.apache.tools.ant.Project; 37 import org.apache.tools.ant.types.DataType; 38 import org.apache.tools.ant.types.Reference; 39 40 import com.idaremedia.apis.UIStringManager; 41 42 import com.idaremedia.antx.apis.ProjectDependent; 43 import com.idaremedia.antx.helpers.Strings; 44 import com.idaremedia.antx.helpers.Tk; 45 46 56 57 public abstract class AssertableDataType extends DataType 58 implements ProjectDependent 59 { 60 63 protected AssertableDataType() 64 { 65 super(); 66 Iam_=""; 67 } 68 69 70 74 protected AssertableDataType(String iam) 75 { 76 super(); 77 Iam_= Tk.cvlabelFrom(iam); 78 } 79 80 81 84 protected final String cvlabel_() 85 { 86 return Iam_; 87 } 88 89 90 95 public final UIStringManager uistrs() 96 { 97 return AntX.uistrs(); 98 } 99 100 101 107 protected final void require_(boolean c, String msg) 108 { 109 if (!c) { 110 String ermsg = uistrs().get("cv.require",Iam_,msg); 111 log(ermsg, Project.MSG_ERR); 112 throw new IllegalArgumentException (ermsg); 113 } 114 } 115 116 117 124 protected final void ensure_(boolean c, String msg) 125 { 126 if (!c) { 127 String ermsg = uistrs().get("cv.ensure",Iam_,msg); 128 log(ermsg, Project.MSG_ERR); 129 throw new IllegalStateException (ermsg); 130 } 131 } 132 133 134 141 protected final void verify_(boolean c, String msg) 142 { 143 if (!c) { 144 String ermsg = uistrs().get("cv.verify",Iam_,msg); 145 log(ermsg, Project.MSG_ERR); 146 throw new IllegalStateException (ermsg); 147 } 148 } 149 150 151 157 protected final void unexpected_(Throwable t, String msg) 158 { 159 String ermsg = uistrs().get("cv.unexpected",Iam_,msg,t); 160 log(ermsg, Project.MSG_WARN); 161 } 162 163 164 165 169 protected final void verifyInProject_(String calr) 170 { 171 if (getProject()==null) { 172 String ermsg = uistrs().get("cv.verifyInP",Iam_,calr); 173 log(ermsg, Project.MSG_ERR); 174 throw new IllegalStateException (ermsg); 175 } 176 } 177 178 179 180 183 public String getId() 184 { 185 return getClass().getName()+"@"+ 186 String.valueOf(System.identityHashCode(this)); 187 } 188 189 190 191 192 197 public void setRefid(Reference r) 198 throws BuildException 199 { 200 require_(r!=null,"setRef- nonzro ref"); 201 if (isIndependentlyEdited()) { 202 throw tooManyAttributes(); 203 } 204 super.setRefid(r); 205 } 206 207 208 209 214 protected void edited(String what) 215 { 216 m_attrsEdited++; 217 } 218 219 223 protected final void edited() 224 { 225 edited(""); 226 } 227 228 229 233 protected final boolean isIndependentlyEdited() 234 { 235 return m_attrsEdited>0; 236 } 237 238 239 240 249 public final Object getReferencedObject(Project theProject, String refid, 250 Class requiredClass) 251 { 252 require_(refid!=null,"getRefObj- nonzro id"); 253 if (theProject==null) { 254 theProject= getProject(); 255 verify_(theProject!=null,"getRefObj- hav project"); 256 } 257 258 Object o = theProject.getReference(refid); 259 String error = null; 260 261 if (o==null) { 262 error = uistrs().get("task.missing.refid", refid); 263 } 264 else if (!requiredClass.isInstance(o)) { 265 error = uistrs().get("task.bad.refid", refid, 266 requiredClass.getName(), 267 o.getClass().getName()); 268 } 269 if (error!=null) { 270 log(error,Project.MSG_ERR); 271 throw new BuildException(error); 272 } 273 return o; 274 } 275 276 277 278 282 protected Object getCheckedRef(Class requiredClass, String typeName) 283 { 284 if (!isChecked()) { 285 Stack stk = new Stack (); 286 stk.push(this); 287 dieOnCircularReference(stk,getProject()); 288 } 289 return getReferencedObject(getProject(),getRefid().getRefId(),requiredClass); 290 } 291 292 293 294 302 public synchronized String getTypeName() 303 { 304 if (m_typeName==null) { 305 verifyInProject_("typeName"); 306 Class myClass = getClass(); 307 Iterator itr= getProject().getDataTypeDefinitions(). 308 entrySet().iterator(); while (itr.hasNext()) { 310 Map.Entry mE= (Map.Entry )itr.next(); 311 if (myClass==mE.getValue()) { 312 m_typeName = (String )mE.getKey(); 313 break; 314 } 315 } 316 } 317 return m_typeName==null ? Strings.NONE : m_typeName; 318 } 319 320 321 private int m_attrsEdited; private final String Iam_; 323 private String m_typeName;} 325 326 327 | Popular Tags |