KickJava   Java API By Example, From Geeks To Geeks.

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


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.optional;
20
21 import org.apache.tools.ant.filters.TokenFilter;
22 import java.io.File JavaDoc;
23 import org.apache.tools.ant.BuildException;
24 import org.apache.tools.ant.Project;
25 import org.apache.tools.ant.types.Path;
26 import org.apache.tools.ant.types.Reference;
27 import org.apache.tools.ant.util.ScriptRunnerBase;
28 import org.apache.tools.ant.util.ScriptRunnerHelper;
29
30 /**
31  * Most of this is CAP (Cut And Paste) from the Script task
32  * ScriptFilter class, implements TokenFilter.Filter
33  * for scripts to use.
34  * This provides the same beans as the Script Task
35  * to a script.
36  * The script is meant to use get self.token and
37  * set self.token in the reply.
38  *
39  * @since Ant 1.6
40  */

41 public class ScriptFilter extends TokenFilter.ChainableReaderFilter {
42     /** script runner helper */
43     private ScriptRunnerHelper helper = new ScriptRunnerHelper();
44
45     /** script runner. */
46     private ScriptRunnerBase runner = null;
47
48     /** the token used by the script */
49     private String JavaDoc token;
50
51     /**
52      * Set the project.
53      * @param project the owner of this component.
54      */

55     public void setProject(Project project) {
56         super.setProject(project);
57         helper.setProjectComponent(this);
58     }
59
60     /**
61      * Defines the language (required).
62      *
63      * @param language the scripting language name for the script.
64      */

65     public void setLanguage(String JavaDoc language) {
66         helper.setLanguage(language);
67     }
68
69     /**
70      * Initialize.
71      *
72      * @exception BuildException if someting goes wrong
73      */

74     private void init() throws BuildException {
75         if (runner != null) {
76             return;
77         }
78         runner = helper.getScriptRunner();
79     }
80
81     /**
82      * The current token
83      *
84      * @param token the string filtered by the script
85      */

86     public void setToken(String JavaDoc token) {
87         this.token = token;
88     }
89
90     /**
91      * The current token
92      *
93      * @return the string filtered by the script
94      */

95     public String JavaDoc getToken() {
96         return token;
97     }
98
99     /**
100      * Called filter the token.
101      * This sets the token in this object, calls
102      * the script and returns the token.
103      *
104      * @param token the token to be filtered
105      * @return the filtered token
106      */

107     public String JavaDoc filter(String JavaDoc token) {
108         init();
109         setToken(token);
110         runner.executeScript("ant_filter");
111         return getToken();
112     }
113
114     /**
115      * Load the script from an external file ; optional.
116      *
117      * @param file the file containing the script source.
118      */

119     public void setSrc(File JavaDoc file) {
120         helper.setSrc(file);
121     }
122
123     /**
124      * The script text.
125      *
126      * @param text a component of the script text to be added.
127      */

128     public void addText(String JavaDoc text) {
129         helper.addText(text);
130     }
131
132     /**
133      * Defines the manager.
134      *
135      * @param manager the scripting manager.
136      */

137     public void setManager(String JavaDoc manager) {
138         helper.setManager(manager);
139     }
140     /**
141      * Set the classpath to be used when searching for classes and resources.
142      *
143      * @param classpath an Ant Path object containing the search path.
144      */

145     public void setClasspath(Path classpath) {
146         helper.setClasspath(classpath);
147     }
148
149     /**
150      * Classpath to be used when searching for classes and resources.
151      *
152      * @return an empty Path instance to be configured by Ant.
153      */

154     public Path createClasspath() {
155         return helper.createClasspath();
156     }
157
158     /**
159      * Set the classpath by reference.
160      *
161      * @param r a Reference to a Path instance to be used as the classpath
162      * value.
163      */

164     public void setClasspathRef(Reference r) {
165         helper.setClasspathRef(r);
166     }
167
168 }
169
Popular Tags