KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > types > selectors > ExtendSelector


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18
19 package org.apache.tools.ant.types.selectors;
20
21 import java.io.File JavaDoc;
22 import java.util.Vector JavaDoc;
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 /**
32  * Selector that selects files by forwarding the request on to other classes.
33  *
34  * @since 1.5
35  */

36 public class ExtendSelector extends BaseSelector {
37
38     private String JavaDoc classname = null;
39     private FileSelector dynselector = null;
40     private Vector JavaDoc paramVec = new Vector JavaDoc();
41     private Path classpath = null;
42
43     /**
44      * Default constructor.
45      */

46     public ExtendSelector() {
47     }
48
49     /**
50      * Sets the classname of the custom selector.
51      *
52      * @param classname is the class which implements this selector
53      */

54     public void setClassname(String JavaDoc classname) {
55         this.classname = classname;
56     }
57
58     /**
59      * Instantiates the identified custom selector class.
60      */

61     public void selectorCreate() {
62         if (classname != null && classname.length() > 0) {
63             try {
64                 Class JavaDoc 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 JavaDoc cnfexcept) {
78                 setError("Selector " + classname
79                     + " not initialized, no such class");
80             } catch (InstantiationException JavaDoc iexcept) {
81                 setError("Selector " + classname
82                     + " not initialized, could not create class");
83             } catch (IllegalAccessException JavaDoc 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     /**
93      * Create new parameters to pass to custom selector.
94      *
95      * @param p The new Parameter object
96      */

97     public void addParam(Parameter p) {
98         paramVec.addElement(p);
99     }
100
101
102     /**
103      * Set the classpath to load the classname specified using an attribute.
104      * @param classpath the classpath to use
105      */

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     /**
118      * Specify the classpath to use to load the Selector (nested element).
119      * @return a classpath to be configured
120      */

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     /**
132      * Get the classpath
133      * @return the classpath
134      */

135     public final Path getClasspath() {
136         return classpath;
137     }
138
139     /**
140      * Set the classpath to use for loading a custom selector by using
141      * a reference.
142      * @param r a reference to the classpath
143      */

144     public void setClasspathref(Reference r) {
145         if (isReference()) {
146             throw tooManyAttributes();
147         }
148         createClasspath().setRefid(r);
149     }
150
151     /**
152      * These are errors specific to ExtendSelector only. If there are
153      * errors in the custom selector, it should throw a BuildException
154      * when isSelected() is called.
155      */

156     public void verifySettings() {
157         // Creation is done here rather than in isSelected() because some
158
// containers may do a validation pass before running isSelected(),
159
// but we need to check for the existence of the created class.
160
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     /**
176      * Allows the custom selector to choose whether to select a file. This
177      * is also where the Parameters are passed to the custom selector,
178      * since we know we must have them all by now. And since we must know
179      * both classpath and classname, creating the class is deferred to here
180      * as well.
181      * @param basedir The the base directory.
182      * @param filename The name of the file to check.
183      * @param file A File object for this filename.
184      * @return whether the file should be selected or not.
185      * @exception BuildException if an error occurs.
186      */

187     public boolean isSelected(File JavaDoc basedir, String JavaDoc filename, File JavaDoc 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             // We know that dynselector must be non-null if no error message
194
((ExtendFileSelector) dynselector).setParameters(paramArray);
195         }
196         return dynselector.isSelected(basedir, filename, file);
197     }
198
199 }
200
201
Popular Tags