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