1 18 19 package org.apache.tools.ant.types.selectors; 20 21 import java.io.File ; 22 import java.util.Vector ; 23 24 import org.apache.tools.ant.Project; 25 import org.apache.tools.ant.AntClassLoader; 26 import org.apache.tools.ant.BuildException; 27 import org.apache.tools.ant.types.Parameter; 28 import org.apache.tools.ant.types.Path; 29 import org.apache.tools.ant.types.Reference; 30 31 36 public class ExtendSelector extends BaseSelector { 37 38 private String classname = null; 39 private FileSelector dynselector = null; 40 private Vector paramVec = new Vector (); 41 private Path classpath = null; 42 43 46 public ExtendSelector() { 47 } 48 49 54 public void setClassname(String classname) { 55 this.classname = classname; 56 } 57 58 61 public void selectorCreate() { 62 if (classname != null && classname.length() > 0) { 63 try { 64 Class c = null; 65 if (classpath == null) { 66 c = Class.forName(classname); 67 } else { 68 AntClassLoader al 69 = getProject().createClassLoader(classpath); 70 c = Class.forName(classname, true, al); 71 } 72 dynselector = (FileSelector) c.newInstance(); 73 final Project p = getProject(); 74 if (p != null) { 75 p.setProjectReference(dynselector); 76 } 77 } catch (ClassNotFoundException cnfexcept) { 78 setError("Selector " + classname 79 + " not initialized, no such class"); 80 } catch (InstantiationException iexcept) { 81 setError("Selector " + classname 82 + " not initialized, could not create class"); 83 } catch (IllegalAccessException iaexcept) { 84 setError("Selector " + classname 85 + " not initialized, class not accessible"); 86 } 87 } else { 88 setError("There is no classname specified"); 89 } 90 } 91 92 97 public void addParam(Parameter p) { 98 paramVec.addElement(p); 99 } 100 101 102 106 public final void setClasspath(Path classpath) { 107 if (isReference()) { 108 throw tooManyAttributes(); 109 } 110 if (this.classpath == null) { 111 this.classpath = classpath; 112 } else { 113 this.classpath.append(classpath); 114 } 115 } 116 117 121 public final Path createClasspath() { 122 if (isReference()) { 123 throw noChildrenAllowed(); 124 } 125 if (this.classpath == null) { 126 this.classpath = new Path(getProject()); 127 } 128 return this.classpath.createPath(); 129 } 130 131 135 public final Path getClasspath() { 136 return classpath; 137 } 138 139 144 public void setClasspathref(Reference r) { 145 if (isReference()) { 146 throw tooManyAttributes(); 147 } 148 createClasspath().setRefid(r); 149 } 150 151 156 public void verifySettings() { 157 if (dynselector == null) { 161 selectorCreate(); 162 } 163 if (classname == null || classname.length() < 1) { 164 setError("The classname attribute is required"); 165 } else if (dynselector == null) { 166 setError("Internal Error: The custom selector was not created"); 167 } else if (!(dynselector instanceof ExtendFileSelector) 168 && (paramVec.size() > 0)) { 169 setError("Cannot set parameters on custom selector that does not " 170 + "implement ExtendFileSelector"); 171 } 172 } 173 174 175 187 public boolean isSelected(File basedir, String filename, File file) 188 throws BuildException { 189 validate(); 190 if (paramVec.size() > 0 && dynselector instanceof ExtendFileSelector) { 191 Parameter[] paramArray = new Parameter[paramVec.size()]; 192 paramVec.copyInto(paramArray); 193 ((ExtendFileSelector) dynselector).setParameters(paramArray); 195 } 196 return dynselector.isSelected(basedir, filename, file); 197 } 198 199 } 200 201 | Popular Tags |