1 18 19 package org.apache.tools.ant.types; 20 21 import java.util.Stack ; 22 23 import org.apache.tools.ant.Project; 24 import org.apache.tools.ant.BuildException; 25 import org.apache.tools.ant.ComponentHelper; 26 import org.apache.tools.ant.ProjectComponent; 27 import org.apache.tools.ant.util.IdentityStack; 28 29 40 public abstract class DataType extends ProjectComponent implements Cloneable { 41 43 50 protected Reference ref; 51 52 65 protected boolean checked = true; 66 68 72 public boolean isReference() { 73 return ref != null; 74 } 75 76 85 public void setRefid(final Reference ref) { 86 this.ref = ref; 87 checked = false; 88 } 89 90 94 protected String getDataTypeName() { 95 return ComponentHelper.getElementName(getProject(), this, true); 96 } 97 98 102 protected void dieOnCircularReference() { 103 dieOnCircularReference(getProject()); 104 } 105 106 111 protected void dieOnCircularReference(Project p) { 112 if (checked || !isReference()) { 113 return; 114 } 115 dieOnCircularReference(new IdentityStack(this), p); 116 } 117 118 137 protected void dieOnCircularReference(final Stack stack, 138 final Project project) 139 throws BuildException { 140 141 if (checked || !isReference()) { 142 return; 143 } 144 Object o = ref.getReferencedObject(project); 145 146 if (o instanceof DataType) { 147 IdentityStack id = IdentityStack.getInstance(stack); 148 149 if (id.contains(o)) { 150 throw circularReference(); 151 } else { 152 id.push(o); 153 ((DataType) o).dieOnCircularReference(id, project); 154 id.pop(); 155 } 156 } 157 checked = true; 158 } 159 160 169 public static void invokeCircularReferenceCheck(DataType dt, Stack stk, 170 Project p) { 171 dt.dieOnCircularReference(stk, p); 172 } 173 174 181 protected Object getCheckedRef() { 182 return getCheckedRef(getProject()); 183 } 184 185 193 protected Object getCheckedRef(Project p) { 194 return getCheckedRef(getClass(), getDataTypeName(), p); 195 } 196 197 206 protected Object getCheckedRef(final Class requiredClass, 207 final String dataTypeName) { 208 return getCheckedRef(requiredClass, dataTypeName, getProject()); 209 } 210 211 223 protected Object getCheckedRef(final Class requiredClass, 224 final String dataTypeName, final Project project) { 225 if (project == null) { 226 throw new BuildException("No Project specified"); 227 } 228 dieOnCircularReference(project); 229 Object o = ref.getReferencedObject(project); 230 if (!(requiredClass.isAssignableFrom(o.getClass()))) { 231 log("Class " + o.getClass() + " is not a subclass of " + requiredClass, 232 Project.MSG_VERBOSE); 233 String msg = ref.getRefId() + " doesn\'t denote a " + dataTypeName; 234 throw new BuildException(msg); 235 } 236 return o; 237 } 238 239 244 protected BuildException tooManyAttributes() { 245 return new BuildException("You must not specify more than one " 246 + "attribute when using refid"); 247 } 248 249 254 protected BuildException noChildrenAllowed() { 255 return new BuildException("You must not specify nested elements " 256 + "when using refid"); 257 } 258 259 264 protected BuildException circularReference() { 265 return new BuildException("This data type contains a circular " 266 + "reference."); 267 } 268 269 273 protected boolean isChecked() { 274 return checked; 275 } 276 277 281 protected void setChecked(final boolean checked) { 282 this.checked = checked; 283 } 284 285 289 public Reference getRefid() { 290 return ref; 291 } 292 293 298 protected void checkAttributesAllowed() { 299 if (isReference()) { 300 throw tooManyAttributes(); 301 } 302 } 303 304 309 protected void checkChildrenAllowed() { 310 if (isReference()) { 311 throw noChildrenAllowed(); 312 } 313 } 314 315 319 public String toString() { 320 String d = getDescription(); 321 return d == null ? getDataTypeName() : getDataTypeName() + " " + d; 322 } 323 324 329 public Object clone() throws CloneNotSupportedException { 330 DataType dt = (DataType) super.clone(); 331 dt.setDescription(getDescription()); 332 if (getRefid() != null) { 333 dt.setRefid(getRefid()); 334 } 335 dt.setChecked(isChecked()); 336 return dt; 337 } 338 } 339 340 | Popular Tags |