KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > LoadProperties


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.taskdefs;
19
20 import java.io.BufferedInputStream JavaDoc;
21 import java.io.ByteArrayInputStream JavaDoc;
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.InputStreamReader JavaDoc;
25 import java.io.Reader JavaDoc;
26 import java.util.Properties JavaDoc;
27 import java.util.Vector JavaDoc;
28 import org.apache.tools.ant.Project;
29 import org.apache.tools.ant.BuildException;
30 import org.apache.tools.ant.Task;
31 import org.apache.tools.ant.filters.util.ChainReaderHelper;
32 import org.apache.tools.ant.types.Path;
33 import org.apache.tools.ant.types.Reference;
34 import org.apache.tools.ant.types.Resource;
35 import org.apache.tools.ant.types.ResourceCollection;
36 import org.apache.tools.ant.types.FilterChain;
37 import org.apache.tools.ant.types.resources.FileResource;
38 import org.apache.tools.ant.types.resources.JavaResource;
39 import org.apache.tools.ant.util.FileUtils;
40
41 /**
42  * Load a file's contents as Ant properties.
43  *
44  * @since Ant 1.5
45  * @ant.task category="utility"
46  */

47 public class LoadProperties extends Task {
48
49     /**
50      * Source resource.
51      */

52     private Resource src = null;
53
54     /**
55      * Holds filterchains
56      */

57     private final Vector JavaDoc filterChains = new Vector JavaDoc();
58
59     /**
60      * Encoding to use for input; defaults to the platform's default encoding.
61      */

62     private String JavaDoc encoding = null;
63
64     /**
65      * Set the file to load.
66      *
67      * @param srcFile The new SrcFile value
68      */

69     public final void setSrcFile(final File JavaDoc srcFile) {
70         addConfigured(new FileResource(srcFile));
71     }
72
73     /**
74      * Set the resource name of a property file to load.
75      *
76      * @param resource resource on classpath
77      */

78     public void setResource(String JavaDoc resource) {
79         assertSrcIsJavaResource();
80         ((JavaResource) src).setName(resource);
81     }
82
83     /**
84      * Encoding to use for input, defaults to the platform's default
85      * encoding. <p>
86      *
87      * For a list of possible values see
88      * <a HREF="http://java.sun.com/j2se/1.5.0/docs/guide/intl/encoding.doc.html">
89      * http://java.sun.com/j2se/1.5.0/docs/guide/intl/encoding.doc.html
90      * </a>.</p>
91      *
92      * @param encoding The new Encoding value
93      */

94     public final void setEncoding(final String JavaDoc encoding) {
95         this.encoding = encoding;
96     }
97
98     /**
99      * Set the classpath to use when looking up a resource.
100      * @param classpath to add to any existing classpath
101      */

102     public void setClasspath(Path classpath) {
103         assertSrcIsJavaResource();
104         ((JavaResource) src).setClasspath(classpath);
105     }
106
107     /**
108      * Add a classpath to use when looking up a resource.
109      * @return The classpath to be configured
110      */

111     public Path createClasspath() {
112         assertSrcIsJavaResource();
113         return ((JavaResource) src).createClasspath();
114     }
115
116     /**
117      * Set the classpath to use when looking up a resource,
118      * given as reference to a &lt;path&gt; defined elsewhere
119      * @param r The reference value
120      */

121     public void setClasspathRef(Reference r) {
122         assertSrcIsJavaResource();
123         ((JavaResource) src).setClasspathRef(r);
124     }
125
126     /**
127      * get the classpath used by this <code>LoadProperties</code>.
128      * @return The classpath
129      */

130     public Path getClasspath() {
131         assertSrcIsJavaResource();
132         return ((JavaResource) src).getClasspath();
133     }
134
135     /**
136      * load Ant properties from the source file or resource
137      *
138      * @exception BuildException if something goes wrong with the build
139      */

140     public final void execute() throws BuildException {
141         //validation
142
if (src == null) {
143             throw new BuildException("A source resource is required.");
144         }
145         if (!src.isExists()) {
146             if (src instanceof JavaResource) {
147                 // dreaded backwards compatibility
148
log("Unable to find resource " + src, Project.MSG_WARN);
149                 return;
150             }
151             throw new BuildException("Source resource does not exist: " + src);
152         }
153
154         BufferedInputStream JavaDoc bis = null;
155         Reader JavaDoc instream = null;
156         ByteArrayInputStream JavaDoc tis = null;
157
158         try {
159             bis = new BufferedInputStream JavaDoc(src.getInputStream());
160             if (encoding == null) {
161                 instream = new InputStreamReader JavaDoc(bis);
162             } else {
163                 instream = new InputStreamReader JavaDoc(bis, encoding);
164             }
165
166             ChainReaderHelper crh = new ChainReaderHelper();
167             crh.setPrimaryReader(instream);
168             crh.setFilterChains(filterChains);
169             crh.setProject(getProject());
170             instream = crh.getAssembledReader();
171
172             String JavaDoc text = crh.readFully(instream);
173
174             if (text != null) {
175                 if (!text.endsWith("\n")) {
176                     text = text + "\n";
177                 }
178
179                 if (encoding == null) {
180                     tis = new ByteArrayInputStream JavaDoc(text.getBytes());
181                 } else {
182                     tis = new ByteArrayInputStream JavaDoc(text.getBytes(encoding));
183                 }
184                 final Properties JavaDoc props = new Properties JavaDoc();
185                 props.load(tis);
186
187                 Property propertyTask = new Property();
188                 propertyTask.bindToOwner(this);
189                 propertyTask.addProperties(props);
190             }
191
192         } catch (final IOException JavaDoc ioe) {
193             final String JavaDoc message = "Unable to load file: " + ioe.toString();
194             throw new BuildException(message, ioe, getLocation());
195         } finally {
196             FileUtils.close(bis);
197             FileUtils.close(tis);
198         }
199     }
200
201     /**
202      * Adds a FilterChain.
203      * @param filter the filter to add
204      */

205     public final void addFilterChain(FilterChain filter) {
206         filterChains.addElement(filter);
207     }
208
209     /**
210      * Set the source resource.
211      * @param a the resource to load as a single element Resource collection.
212      * @since Ant 1.7
213      */

214     public void addConfigured(ResourceCollection a) {
215         if (src != null) {
216             throw new BuildException("only a single source is supported");
217         }
218         if (a.size() != 1) {
219             throw new BuildException("only single argument resource collections"
220                                      + " are supported");
221         }
222         src = (Resource) a.iterator().next();
223     }
224
225     private void assertSrcIsJavaResource() {
226         if (src == null) {
227             src = new JavaResource();
228             src.setProject(getProject());
229         } else if (!(src instanceof JavaResource)) {
230             throw new BuildException("expected a java resource as source");
231         }
232     }
233 }
234
Popular Tags