1 18 19 package org.apache.tools.ant.types.resources.selectors; 20 21 import org.apache.tools.ant.Project; 22 import org.apache.tools.ant.ProjectHelper; 23 import org.apache.tools.ant.BuildException; 24 import org.apache.tools.ant.ComponentHelper; 25 import org.apache.tools.ant.AntTypeDefinition; 26 import org.apache.tools.ant.types.Resource; 27 28 32 public class InstanceOf implements ResourceSelector { 33 private static final String ONE_ONLY = "Exactly one of class|type must be set."; 34 35 private Project project; 36 private Class clazz; 37 private String type; 38 private String uri; 39 40 44 public void setProject(Project p) { 45 project = p; 46 } 47 48 52 public void setClass(Class c) { 53 if (clazz != null) { 54 throw new BuildException("The class attribute has already been set."); 55 } 56 clazz = c; 57 } 58 59 63 public void setType(String s) { 64 type = s; 65 } 66 67 71 public void setURI(String u) { 72 uri = u; 73 } 74 75 79 public Class getCheckClass() { 80 return clazz; 81 } 82 83 87 public String getType() { 88 return type; 89 } 90 91 95 public String getURI() { 96 return uri; 97 } 98 99 105 public boolean isSelected(Resource r) { 106 if ((clazz == null) == (type == null)) { 107 throw new BuildException(ONE_ONLY); 108 } 109 Class c = clazz; 110 if (type != null) { 111 if (project == null) { 112 throw new BuildException( 113 "No project set for InstanceOf ResourceSelector; " 114 + "the type attribute is invalid."); 115 } 116 AntTypeDefinition d = ComponentHelper.getComponentHelper( 117 project).getDefinition(ProjectHelper.genComponentName(uri, type)); 118 if (d == null) { 119 throw new BuildException("type " + type + " not found."); 120 } 121 try { 122 c = d.innerGetTypeClass(); 123 } catch (ClassNotFoundException e) { 124 throw new BuildException(e); 125 } 126 } 127 return c.isAssignableFrom(r.getClass()); 128 } 129 130 } 131 | Popular Tags |