1 | /*
|
---|
2 | * Copyright 2003-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.optional;
|
---|
18 |
|
---|
19 | import org.apache.tools.ant.filters.TokenFilter;
|
---|
20 | import java.io.File;
|
---|
21 | import org.apache.tools.ant.BuildException;
|
---|
22 | import org.apache.tools.ant.util.ScriptRunner;
|
---|
23 |
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * Most of this is CAP (Cut And Paste) from the Script task
|
---|
27 | * ScriptFilter class, implements TokenFilter.Filter
|
---|
28 | * for scripts to use.
|
---|
29 | * This provides the same beans as the Script Task
|
---|
30 | * to a script.
|
---|
31 | * The script is meant to use get self.token and
|
---|
32 | * set self.token in the reply.
|
---|
33 | *
|
---|
34 | *
|
---|
35 | * @since Ant 1.6
|
---|
36 | */
|
---|
37 | public class ScriptFilter extends TokenFilter.ChainableReaderFilter {
|
---|
38 | /** Has this object been initialized ? */
|
---|
39 | private boolean initialized = false;
|
---|
40 | /** the token used by the script */
|
---|
41 | private String token;
|
---|
42 |
|
---|
43 | private ScriptRunner runner = new ScriptRunner();
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * Defines the language (required).
|
---|
47 | *
|
---|
48 | * @param language the scripting language name for the script.
|
---|
49 | */
|
---|
50 | public void setLanguage(String language) {
|
---|
51 | runner.setLanguage(language);
|
---|
52 | }
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * Initialize.
|
---|
56 | *
|
---|
57 | * @exception BuildException if someting goes wrong
|
---|
58 | */
|
---|
59 | private void init() throws BuildException {
|
---|
60 | if (initialized) {
|
---|
61 | return;
|
---|
62 | }
|
---|
63 | initialized = true;
|
---|
64 |
|
---|
65 | runner.addBeans(getProject().getProperties());
|
---|
66 | runner.addBeans(getProject().getUserProperties());
|
---|
67 | runner.addBeans(getProject().getTargets());
|
---|
68 | runner.addBeans(getProject().getReferences());
|
---|
69 |
|
---|
70 | runner.addBean("project", getProject());
|
---|
71 | runner.addBean("self", this);
|
---|
72 | }
|
---|
73 |
|
---|
74 | /**
|
---|
75 | * The current token
|
---|
76 | *
|
---|
77 | * @param token the string filtered by the script
|
---|
78 | */
|
---|
79 | public void setToken(String token) {
|
---|
80 | this.token = token;
|
---|
81 | }
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * The current token
|
---|
85 | *
|
---|
86 | * @return the string filtered by the script
|
---|
87 | */
|
---|
88 | public String getToken() {
|
---|
89 | return token;
|
---|
90 | }
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * Called filter the token.
|
---|
94 | * This sets the token in this object, calls
|
---|
95 | * the script and returns the token.
|
---|
96 | *
|
---|
97 | * @param token the token to be filtered
|
---|
98 | * @return the filtered token
|
---|
99 | */
|
---|
100 | public String filter(String token) {
|
---|
101 | init();
|
---|
102 | setToken(token);
|
---|
103 | runner.executeScript("<ANT-Filter>");
|
---|
104 | return getToken();
|
---|
105 | }
|
---|
106 |
|
---|
107 | /**
|
---|
108 | * Load the script from an external file ; optional.
|
---|
109 | *
|
---|
110 | * @param file the file containing the script source.
|
---|
111 | */
|
---|
112 | public void setSrc(File file) {
|
---|
113 | runner.setSrc(file);
|
---|
114 | }
|
---|
115 |
|
---|
116 | /**
|
---|
117 | * The script text.
|
---|
118 | *
|
---|
119 | * @param text a component of the script text to be added.
|
---|
120 | */
|
---|
121 | public void addText(String text) {
|
---|
122 | runner.addText(text);
|
---|
123 | }
|
---|
124 | }
|
---|