1 | /*
|
---|
2 | * Copyright 2002-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.taskdefs;
|
---|
18 |
|
---|
19 | import java.io.BufferedInputStream;
|
---|
20 | import java.io.ByteArrayInputStream;
|
---|
21 | import java.io.File;
|
---|
22 | import java.io.InputStream;
|
---|
23 | import java.io.FileInputStream;
|
---|
24 | import java.io.IOException;
|
---|
25 | import java.io.InputStreamReader;
|
---|
26 | import java.io.Reader;
|
---|
27 | import java.util.Properties;
|
---|
28 | import java.util.Vector;
|
---|
29 | import org.apache.tools.ant.Project;
|
---|
30 | import org.apache.tools.ant.BuildException;
|
---|
31 | import org.apache.tools.ant.Task;
|
---|
32 | import org.apache.tools.ant.filters.util.ChainReaderHelper;
|
---|
33 | import org.apache.tools.ant.types.Path;
|
---|
34 | import org.apache.tools.ant.types.Reference;
|
---|
35 | import org.apache.tools.ant.types.FilterChain;
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * Load a file's contents as Ant properties.
|
---|
39 | *
|
---|
40 | * @since Ant 1.5
|
---|
41 | * @ant.task category="utility"
|
---|
42 | */
|
---|
43 | public class LoadProperties extends Task {
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * Source file
|
---|
47 | */
|
---|
48 | private File srcFile = null;
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * Resource
|
---|
52 | */
|
---|
53 | private String resource = null;
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * Classpath
|
---|
57 | */
|
---|
58 | private Path classpath = null;
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * Holds filterchains
|
---|
62 | */
|
---|
63 | private final Vector filterChains = new Vector();
|
---|
64 |
|
---|
65 | /**
|
---|
66 | * Encoding to use for input; defaults to the platform's default encoding.
|
---|
67 | */
|
---|
68 | private String encoding = null;
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * Set the file to load.
|
---|
72 | *
|
---|
73 | * @param srcFile The new SrcFile value
|
---|
74 | */
|
---|
75 | public final void setSrcFile(final File srcFile) {
|
---|
76 | this.srcFile = srcFile;
|
---|
77 | }
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * Set the resource name of a property file to load.
|
---|
81 | *
|
---|
82 | * @param resource resource on classpath
|
---|
83 | */
|
---|
84 | public void setResource(String resource) {
|
---|
85 | this.resource = resource;
|
---|
86 | }
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * Encoding to use for input, defaults to the platform's default
|
---|
90 | * encoding. <p>
|
---|
91 | *
|
---|
92 | * For a list of possible values see
|
---|
93 | * <a href="http://java.sun.com/products/jdk/1.2/docs/guide/internat/encoding.doc.html">
|
---|
94 | * http://java.sun.com/products/jdk/1.2/docs/guide/internat/encoding.doc.html
|
---|
95 | * </a>.</p>
|
---|
96 | *
|
---|
97 | * @param encoding The new Encoding value
|
---|
98 | */
|
---|
99 | public final void setEncoding(final String encoding) {
|
---|
100 | this.encoding = encoding;
|
---|
101 | }
|
---|
102 |
|
---|
103 | /**
|
---|
104 | * Set the classpath to use when looking up a resource.
|
---|
105 | * @param classpath to add to any existing classpath
|
---|
106 | */
|
---|
107 | public void setClasspath(Path classpath) {
|
---|
108 | if (this.classpath == null) {
|
---|
109 | this.classpath = classpath;
|
---|
110 | } else {
|
---|
111 | this.classpath.append(classpath);
|
---|
112 | }
|
---|
113 | }
|
---|
114 |
|
---|
115 | /**
|
---|
116 | * Add a classpath to use when looking up a resource.
|
---|
117 | * @return The classpath to be configured
|
---|
118 | */
|
---|
119 | public Path createClasspath() {
|
---|
120 | if (this.classpath == null) {
|
---|
121 | this.classpath = new Path(getProject());
|
---|
122 | }
|
---|
123 | return this.classpath.createPath();
|
---|
124 | }
|
---|
125 |
|
---|
126 | /**
|
---|
127 | * Set the classpath to use when looking up a resource,
|
---|
128 | * given as reference to a <path> defined elsewhere
|
---|
129 | * @param r The reference value
|
---|
130 | */
|
---|
131 | public void setClasspathRef(Reference r) {
|
---|
132 | createClasspath().setRefid(r);
|
---|
133 | }
|
---|
134 |
|
---|
135 | /**
|
---|
136 | * get the classpath used by this <CODE>LoadProperties</CODE>.
|
---|
137 | * @return The classpath
|
---|
138 | */
|
---|
139 | public Path getClasspath() {
|
---|
140 | return classpath;
|
---|
141 | }
|
---|
142 |
|
---|
143 | /**
|
---|
144 | * load Ant properties from the source file or resource
|
---|
145 | *
|
---|
146 | * @exception BuildException if something goes wrong with the build
|
---|
147 | */
|
---|
148 | public final void execute() throws BuildException {
|
---|
149 | //validation
|
---|
150 | if (srcFile == null && resource == null) {
|
---|
151 | throw new BuildException(
|
---|
152 | "One of \"srcfile\" or \"resource\" is required.");
|
---|
153 | }
|
---|
154 |
|
---|
155 | BufferedInputStream bis = null;
|
---|
156 |
|
---|
157 | if (srcFile != null ) {
|
---|
158 | if (!srcFile.exists()) {
|
---|
159 | throw new BuildException("Source file does not exist.");
|
---|
160 | }
|
---|
161 |
|
---|
162 | if (!srcFile.isFile()) {
|
---|
163 | throw new BuildException("Source file is not a file.");
|
---|
164 | }
|
---|
165 |
|
---|
166 | try {
|
---|
167 | bis = new BufferedInputStream(new FileInputStream(srcFile));
|
---|
168 | } catch (IOException eyeOhEx) {
|
---|
169 | throw new BuildException(eyeOhEx);
|
---|
170 | }
|
---|
171 | } else {
|
---|
172 | ClassLoader cL = (classpath != null)
|
---|
173 | ? getProject().createClassLoader(classpath)
|
---|
174 | : LoadProperties.class.getClassLoader();
|
---|
175 |
|
---|
176 | InputStream is = (cL == null)
|
---|
177 | ? ClassLoader.getSystemResourceAsStream(resource)
|
---|
178 | : cL.getResourceAsStream(resource);
|
---|
179 |
|
---|
180 | if (is != null) {
|
---|
181 | bis = new BufferedInputStream(is);
|
---|
182 | } else { // do it like Property
|
---|
183 | log("Unable to find resource " + resource, Project.MSG_WARN);
|
---|
184 | return;
|
---|
185 | }
|
---|
186 | }
|
---|
187 |
|
---|
188 | Reader instream = null;
|
---|
189 | ByteArrayInputStream tis = null;
|
---|
190 |
|
---|
191 | try {
|
---|
192 | if (encoding == null) {
|
---|
193 | instream = new InputStreamReader(bis);
|
---|
194 | } else {
|
---|
195 | instream = new InputStreamReader(bis, encoding);
|
---|
196 | }
|
---|
197 |
|
---|
198 | ChainReaderHelper crh = new ChainReaderHelper();
|
---|
199 | crh.setPrimaryReader(instream);
|
---|
200 | crh.setFilterChains(filterChains);
|
---|
201 | crh.setProject(getProject());
|
---|
202 | instream = crh.getAssembledReader();
|
---|
203 |
|
---|
204 | String text = crh.readFully(instream);
|
---|
205 |
|
---|
206 | if (text != null) {
|
---|
207 | if (!text.endsWith("\n")) {
|
---|
208 | text = text + "\n";
|
---|
209 | }
|
---|
210 |
|
---|
211 | if (encoding == null) {
|
---|
212 | tis = new ByteArrayInputStream(text.getBytes());
|
---|
213 | } else {
|
---|
214 | tis = new ByteArrayInputStream(text.getBytes(encoding));
|
---|
215 | }
|
---|
216 | final Properties props = new Properties();
|
---|
217 | props.load(tis);
|
---|
218 |
|
---|
219 | Property propertyTask =
|
---|
220 | (Property) getProject().createTask("property");
|
---|
221 | propertyTask.setTaskName(getTaskName());
|
---|
222 | propertyTask.addProperties(props);
|
---|
223 | }
|
---|
224 |
|
---|
225 | } catch (final IOException ioe) {
|
---|
226 | final String message = "Unable to load file: " + ioe.toString();
|
---|
227 | throw new BuildException(message, ioe, getLocation());
|
---|
228 | } catch (final BuildException be) {
|
---|
229 | throw be;
|
---|
230 | } finally {
|
---|
231 | try {
|
---|
232 | if (bis != null) {
|
---|
233 | bis.close();
|
---|
234 | }
|
---|
235 | } catch (IOException ioex) {
|
---|
236 | //ignore
|
---|
237 | }
|
---|
238 | try {
|
---|
239 | if (tis != null) {
|
---|
240 | tis.close();
|
---|
241 | }
|
---|
242 | } catch (IOException ioex) {
|
---|
243 | //ignore
|
---|
244 | }
|
---|
245 | }
|
---|
246 | }
|
---|
247 |
|
---|
248 | /**
|
---|
249 | * Adds a FilterChain.
|
---|
250 | * @param filter the filter to add
|
---|
251 | */
|
---|
252 | public final void addFilterChain(FilterChain filter) {
|
---|
253 | filterChains.addElement(filter);
|
---|
254 | }
|
---|
255 |
|
---|
256 | //end class
|
---|
257 | }
|
---|