001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.tools.ant.taskdefs;
019:
020: import java.io.BufferedInputStream;
021: import java.io.ByteArrayInputStream;
022: import java.io.File;
023: import java.io.IOException;
024: import java.io.InputStreamReader;
025: import java.io.Reader;
026: import java.util.Properties;
027: import java.util.Vector;
028: import org.apache.tools.ant.Project;
029: import org.apache.tools.ant.BuildException;
030: import org.apache.tools.ant.Task;
031: import org.apache.tools.ant.filters.util.ChainReaderHelper;
032: import org.apache.tools.ant.types.Path;
033: import org.apache.tools.ant.types.Reference;
034: import org.apache.tools.ant.types.Resource;
035: import org.apache.tools.ant.types.ResourceCollection;
036: import org.apache.tools.ant.types.FilterChain;
037: import org.apache.tools.ant.types.resources.FileResource;
038: import org.apache.tools.ant.types.resources.JavaResource;
039: import org.apache.tools.ant.util.FileUtils;
040:
041: /**
042: * Load a file's contents as Ant properties.
043: *
044: * @since Ant 1.5
045: * @ant.task category="utility"
046: */
047: public class LoadProperties extends Task {
048:
049: /**
050: * Source resource.
051: */
052: private Resource src = null;
053:
054: /**
055: * Holds filterchains
056: */
057: private final Vector filterChains = new Vector();
058:
059: /**
060: * Encoding to use for input; defaults to the platform's default encoding.
061: */
062: private String encoding = null;
063:
064: /**
065: * Set the file to load.
066: *
067: * @param srcFile The new SrcFile value
068: */
069: public final void setSrcFile(final File srcFile) {
070: addConfigured(new FileResource(srcFile));
071: }
072:
073: /**
074: * Set the resource name of a property file to load.
075: *
076: * @param resource resource on classpath
077: */
078: public void setResource(String resource) {
079: assertSrcIsJavaResource();
080: ((JavaResource) src).setName(resource);
081: }
082:
083: /**
084: * Encoding to use for input, defaults to the platform's default
085: * encoding. <p>
086: *
087: * For a list of possible values see
088: * <a href="http://java.sun.com/j2se/1.5.0/docs/guide/intl/encoding.doc.html">
089: * http://java.sun.com/j2se/1.5.0/docs/guide/intl/encoding.doc.html
090: * </a>.</p>
091: *
092: * @param encoding The new Encoding value
093: */
094: public final void setEncoding(final String encoding) {
095: this .encoding = encoding;
096: }
097:
098: /**
099: * 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 <path> 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: "
152: + src);
153: }
154:
155: BufferedInputStream bis = null;
156: Reader instream = null;
157: ByteArrayInputStream tis = null;
158:
159: try {
160: bis = new BufferedInputStream(src.getInputStream());
161: if (encoding == null) {
162: instream = new InputStreamReader(bis);
163: } else {
164: instream = new InputStreamReader(bis, encoding);
165: }
166:
167: ChainReaderHelper crh = new ChainReaderHelper();
168: crh.setPrimaryReader(instream);
169: crh.setFilterChains(filterChains);
170: crh.setProject(getProject());
171: instream = crh.getAssembledReader();
172:
173: String text = crh.readFully(instream);
174:
175: if (text != null) {
176: if (!text.endsWith("\n")) {
177: text = text + "\n";
178: }
179:
180: if (encoding == null) {
181: tis = new ByteArrayInputStream(text.getBytes());
182: } else {
183: tis = new ByteArrayInputStream(text
184: .getBytes(encoding));
185: }
186: final Properties props = new Properties();
187: props.load(tis);
188:
189: Property propertyTask = new Property();
190: propertyTask.bindToOwner(this );
191: propertyTask.addProperties(props);
192: }
193:
194: } catch (final IOException ioe) {
195: final String message = "Unable to load file: "
196: + ioe.toString();
197: throw new BuildException(message, ioe, getLocation());
198: } finally {
199: FileUtils.close(bis);
200: FileUtils.close(tis);
201: }
202: }
203:
204: /**
205: * Adds a FilterChain.
206: * @param filter the filter to add
207: */
208: public final void addFilterChain(FilterChain filter) {
209: filterChains.addElement(filter);
210: }
211:
212: /**
213: * Set the source resource.
214: * @param a the resource to load as a single element Resource collection.
215: * @since Ant 1.7
216: */
217: public void addConfigured(ResourceCollection a) {
218: if (src != null) {
219: throw new BuildException(
220: "only a single source is supported");
221: }
222: if (a.size() != 1) {
223: throw new BuildException(
224: "only single argument resource collections"
225: + " are supported");
226: }
227: src = (Resource) a.iterator().next();
228: }
229:
230: private void assertSrcIsJavaResource() {
231: if (src == null) {
232: src = new JavaResource();
233: src.setProject(getProject());
234: } else if (!(src instanceof JavaResource)) {
235: throw new BuildException(
236: "expected a java resource as source");
237: }
238: }
239: }
|