KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > types > optional > ScriptSelector


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 package org.apache.tools.ant.types.optional;
19
20 import java.io.File JavaDoc;
21
22 import org.apache.tools.ant.BuildException;
23 import org.apache.tools.ant.Project;
24 import org.apache.tools.ant.types.Path;
25 import org.apache.tools.ant.types.Reference;
26 import org.apache.tools.ant.types.selectors.BaseSelector;
27 import org.apache.tools.ant.util.ScriptRunnerBase;
28 import org.apache.tools.ant.util.ScriptRunnerHelper;
29
30 /**
31  * Selector that lets you run a script with selection logic inline
32  * @since Ant1.7
33  */

34 public class ScriptSelector extends BaseSelector {
35
36     /**
37      * script runner helper
38      */

39     private ScriptRunnerHelper helper = new ScriptRunnerHelper();
40
41     /**
42      * script runner
43      */

44     private ScriptRunnerBase runner;
45
46     /**
47      * fields updated for every selection
48      */

49     private File JavaDoc basedir;
50     private String JavaDoc filename;
51     private File JavaDoc file;
52
53     /**
54      * selected flag
55      */

56     private boolean selected;
57
58     /**
59      * Set the project.
60      * @param project the owner of this component.
61      */

62     public void setProject(Project project) {
63         super.setProject(project);
64         helper.setProjectComponent(this);
65     }
66
67     /**
68      * Defines the manager.
69      *
70      * @param manager the scripting manager.
71      */

72     public void setManager(String JavaDoc manager) {
73         helper.setManager(manager);
74     }
75
76     /**
77      * Defines the language (required).
78      *
79      * @param language the scripting language name for the script.
80      */

81     public void setLanguage(String JavaDoc language) {
82         helper.setLanguage(language);
83     }
84
85     /**
86      * Initialize on demand.
87      *
88      * @throws org.apache.tools.ant.BuildException
89      * if someting goes wrong
90      */

91     private void init() throws BuildException {
92         if (runner != null) {
93             return;
94         }
95         runner = helper.getScriptRunner();
96     }
97
98     /**
99      * Load the script from an external file ; optional.
100      *
101      * @param file the file containing the script source.
102      */

103     public void setSrc(File JavaDoc file) {
104         helper.setSrc(file);
105     }
106
107     /**
108      * The script text.
109      *
110      * @param text a component of the script text to be added.
111      */

112     public void addText(String JavaDoc text) {
113         helper.addText(text);
114     }
115
116     /**
117      * Set the classpath to be used when searching for classes and resources.
118      *
119      * @param classpath an Ant Path object containing the search path.
120      */

121     public void setClasspath(Path classpath) {
122         helper.setClasspath(classpath);
123     }
124
125     /**
126      * Classpath to be used when searching for classes and resources.
127      *
128      * @return an empty Path instance to be configured by Ant.
129      */

130     public Path createClasspath() {
131         return helper.createClasspath();
132     }
133
134     /**
135      * Set the classpath by reference.
136      *
137      * @param r a Reference to a Path instance to be used as the classpath
138      * value.
139      */

140     public void setClasspathRef(Reference r) {
141         helper.setClasspathRef(r);
142     }
143
144     /**
145      * Set the setbeans attribute.
146      * If this is true, <script> will create variables in the
147      * script instance for all
148      * properties, targets and references of the current project.
149      * It this is false, only the project and self variables will
150      * be set.
151      * The default is true.
152      * @param setBeans the value to set.
153      */

154     public void setSetBeans(boolean setBeans) {
155         helper.setSetBeans(setBeans);
156     }
157
158     /**
159      * Method that each selector will implement to create their selection
160      * behaviour. If there is a problem with the setup of a selector, it can
161      * throw a BuildException to indicate the problem.
162      *
163      * @param basedir A java.io.File object for the base directory
164      * @param filename The name of the file to check
165      * @param file A File object for this filename
166      *
167      * @return whether the file should be selected or not
168      */

169     public boolean isSelected(File JavaDoc basedir, String JavaDoc filename, File JavaDoc file) {
170         init();
171         setSelected(true);
172         this.file = file;
173         this.basedir = basedir;
174         this.filename = filename;
175         runner.addBean("basedir", basedir);
176         runner.addBean("filename", filename);
177         runner.addBean("file", file);
178         runner.executeScript("ant_selector");
179         return isSelected();
180     }
181
182     /**
183      * get the base directory
184      * @return the base directory
185      */

186     public File JavaDoc getBasedir() {
187         return basedir;
188     }
189
190     /**
191      * get the filename of the file
192      * @return the filename of the file that is currently been tested
193      */

194     public String JavaDoc getFilename() {
195         return filename;
196     }
197
198     /**
199      * get the file that is currently to be tested
200      * @return the file that is currently been tested
201      */

202     public File JavaDoc getFile() {
203         return file;
204     }
205
206     /**
207      * get state of selected flag
208      * @return the selected flag
209      */

210     public boolean isSelected() {
211         return selected;
212     }
213
214     /**
215      * set the selected state
216      * Intended for script use, not as an Ant attribute
217      * @param selected the selected state
218      */

219     public void setSelected(boolean selected) {
220         this.selected = selected;
221     }
222
223 }
224
Popular Tags