KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.tools.ant.Project;
21 import org.apache.tools.ant.ProjectComponent;
22 import org.apache.tools.ant.types.Path;
23 import org.apache.tools.ant.types.Reference;
24 import org.apache.tools.ant.util.ScriptRunnerBase;
25 import org.apache.tools.ant.util.ScriptRunnerHelper;
26
27
28 import java.io.File JavaDoc;
29
30 /**
31  * This is a {@link ProjectComponent} that has script support built in
32  * Use it as a foundation for scriptable things.
33  */

34 public abstract class AbstractScriptComponent extends ProjectComponent {
35     /**
36      * script runner helper
37      */

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

43     private ScriptRunnerBase runner = null;
44
45     /**
46      * Set the project.
47      * @param project the owner of this component.
48      */

49     public void setProject(Project project) {
50         super.setProject(project);
51         helper.setProjectComponent(this);
52     }
53
54     /**
55      * Get our script runner
56      * @return the runner
57      */

58     public ScriptRunnerBase getRunner() {
59         initScriptRunner();
60         return runner;
61     }
62
63     /**
64      * Load the script from an external file ; optional.
65      *
66      * @param file the file containing the script source.
67      */

68     public void setSrc(File JavaDoc file) {
69         helper.setSrc(file);
70     }
71
72     /**
73      * The script text.
74      *
75      * @param text a component of the script text to be added.
76      */

77     public void addText(String JavaDoc text) {
78         helper.addText(text);
79     }
80
81     /**
82      * Defines the manager.
83      *
84      * @param manager the scripting manager.
85      */

86     public void setManager(String JavaDoc manager) {
87         helper.setManager(manager);
88     }
89
90     /**
91      * Defines the language (required).
92      *
93      * @param language the scripting language name for the script.
94      */

95     public void setLanguage(String JavaDoc language) {
96         helper.setLanguage(language);
97     }
98
99     /**
100      * Initialize the script runner. Calls this before running the system
101      */

102     protected void initScriptRunner() {
103         if (runner != null) {
104             return;
105         }
106         helper.setProjectComponent(this);
107         runner = helper.getScriptRunner();
108     }
109     /**
110      * Set the classpath to be used when searching for classes and resources.
111      *
112      * @param classpath an Ant Path object containing the search path.
113      */

114     public void setClasspath(Path classpath) {
115         helper.setClasspath(classpath);
116     }
117
118     /**
119      * Classpath to be used when searching for classes and resources.
120      *
121      * @return an empty Path instance to be configured by Ant.
122      */

123     public Path createClasspath() {
124         return helper.createClasspath();
125     }
126
127     /**
128      * Set the classpath by reference.
129      *
130      * @param r a Reference to a Path instance to be used as the classpath
131      * value.
132      */

133     public void setClasspathRef(Reference r) {
134         helper.setClasspathRef(r);
135     }
136
137     /**
138      * Run a script
139      * @param execName name of the script
140      */

141     protected void executeScript(String JavaDoc execName) {
142         getRunner().executeScript(execName);
143     }
144 }
145
Popular Tags