1. /*
  2. * Copyright 2002-2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. package org.apache.tools.ant.types.selectors;
  18. import java.io.File;
  19. import java.util.Vector;
  20. import org.apache.tools.ant.Project;
  21. import org.apache.tools.ant.AntClassLoader;
  22. import org.apache.tools.ant.BuildException;
  23. import org.apache.tools.ant.types.Parameter;
  24. import org.apache.tools.ant.types.Path;
  25. import org.apache.tools.ant.types.Reference;
  26. /**
  27. * Selector that selects files by forwarding the request on to other classes.
  28. *
  29. * @since 1.5
  30. */
  31. public class ExtendSelector extends BaseSelector {
  32. private String classname = null;
  33. private FileSelector dynselector = null;
  34. private Vector paramVec = new Vector();
  35. private Path classpath = null;
  36. /**
  37. * Default constructor.
  38. */
  39. public ExtendSelector() {
  40. }
  41. /**
  42. * Sets the classname of the custom selector.
  43. *
  44. * @param classname is the class which implements this selector
  45. */
  46. public void setClassname(String classname) {
  47. this.classname = classname;
  48. }
  49. /**
  50. * Instantiates the identified custom selector class.
  51. */
  52. public void selectorCreate() {
  53. if (classname != null && classname.length() > 0) {
  54. try {
  55. Class c = null;
  56. if (classpath == null) {
  57. c = Class.forName(classname);
  58. } else {
  59. AntClassLoader al
  60. = getProject().createClassLoader(classpath);
  61. c = Class.forName(classname, true, al);
  62. }
  63. dynselector = (FileSelector) c.newInstance();
  64. final Project project = getProject();
  65. if (project != null) {
  66. project.setProjectReference(dynselector);
  67. }
  68. } catch (ClassNotFoundException cnfexcept) {
  69. setError("Selector " + classname
  70. + " not initialized, no such class");
  71. } catch (InstantiationException iexcept) {
  72. setError("Selector " + classname
  73. + " not initialized, could not create class");
  74. } catch (IllegalAccessException iaexcept) {
  75. setError("Selector " + classname
  76. + " not initialized, class not accessible");
  77. }
  78. } else {
  79. setError("There is no classname specified");
  80. }
  81. }
  82. /**
  83. * Create new parameters to pass to custom selector.
  84. *
  85. * @param p The new Parameter object
  86. */
  87. public void addParam(Parameter p) {
  88. paramVec.addElement(p);
  89. }
  90. /**
  91. * Set the classpath to load the classname specified using an attribute.
  92. * @param classpath the classpath to use
  93. */
  94. public final void setClasspath(Path classpath) {
  95. if (isReference()) {
  96. throw tooManyAttributes();
  97. }
  98. if (this.classpath == null) {
  99. this.classpath = classpath;
  100. } else {
  101. this.classpath.append(classpath);
  102. }
  103. }
  104. /**
  105. * Specify the classpath to use to load the Selector (nested element).
  106. * @return a classpath to be configured
  107. */
  108. public final Path createClasspath() {
  109. if (isReference()) {
  110. throw noChildrenAllowed();
  111. }
  112. if (this.classpath == null) {
  113. this.classpath = new Path(getProject());
  114. }
  115. return this.classpath.createPath();
  116. }
  117. /**
  118. * Get the classpath
  119. * @return the classpath
  120. */
  121. public final Path getClasspath() {
  122. return classpath;
  123. }
  124. /**
  125. * Set the classpath to use for loading a custom selector by using
  126. * a reference.
  127. * @param r a reference to the classpath
  128. */
  129. public void setClasspathref(Reference r) {
  130. if (isReference()) {
  131. throw tooManyAttributes();
  132. }
  133. createClasspath().setRefid(r);
  134. }
  135. /**
  136. * These are errors specific to ExtendSelector only. If there are
  137. * errors in the custom selector, it should throw a BuildException
  138. * when isSelected() is called.
  139. */
  140. public void verifySettings() {
  141. // Creation is done here rather than in isSelected() because some
  142. // containers may do a validation pass before running isSelected(),
  143. // but we need to check for the existence of the created class.
  144. if (dynselector == null) {
  145. selectorCreate();
  146. }
  147. if (classname == null || classname.length() < 1) {
  148. setError("The classname attribute is required");
  149. } else if (dynselector == null) {
  150. setError("Internal Error: The custom selector was not created");
  151. } else if (!(dynselector instanceof ExtendFileSelector)
  152. && (paramVec.size() > 0)) {
  153. setError("Cannot set parameters on custom selector that does not "
  154. + "implement ExtendFileSelector");
  155. }
  156. }
  157. /**
  158. * Allows the custom selector to choose whether to select a file. This
  159. * is also where the Parameters are passed to the custom selector,
  160. * since we know we must have them all by now. And since we must know
  161. * both classpath and classname, creating the class is deferred to here
  162. * as well.
  163. *
  164. * @exception BuildException if an error occurs
  165. */
  166. public boolean isSelected(File basedir, String filename, File file)
  167. throws BuildException {
  168. validate();
  169. if (paramVec.size() > 0 && dynselector instanceof ExtendFileSelector) {
  170. Parameter[] paramArray = new Parameter[paramVec.size()];
  171. paramVec.copyInto(paramArray);
  172. // We know that dynselector must be non-null if no error message
  173. ((ExtendFileSelector) dynselector).setParameters(paramArray);
  174. }
  175. return dynselector.isSelected(basedir, filename, file);
  176. }
  177. }