You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

LoadProperties.java 7.4 kB

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