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.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. import java.io.BufferedInputStream;
  20. import java.io.ByteArrayInputStream;
  21. import java.io.File;
  22. import java.io.IOException;
  23. import java.io.InputStreamReader;
  24. import java.io.Reader;
  25. import java.util.Properties;
  26. import java.util.Vector;
  27. import org.apache.tools.ant.Project;
  28. import org.apache.tools.ant.BuildException;
  29. import org.apache.tools.ant.Task;
  30. import org.apache.tools.ant.filters.util.ChainReaderHelper;
  31. import org.apache.tools.ant.types.Path;
  32. import org.apache.tools.ant.types.Reference;
  33. import org.apache.tools.ant.types.Resource;
  34. import org.apache.tools.ant.types.ResourceCollection;
  35. import org.apache.tools.ant.types.FilterChain;
  36. import org.apache.tools.ant.types.resources.FileResource;
  37. import org.apache.tools.ant.types.resources.JavaResource;
  38. import org.apache.tools.ant.util.FileUtils;
  39. import org.apache.tools.ant.util.ResourceUtils;
  40. /**
  41. * Load a file's contents as Ant properties.
  42. *
  43. * @since Ant 1.5
  44. * @ant.task category="utility"
  45. */
  46. public class LoadProperties extends Task {
  47. /**
  48. * Source resource.
  49. */
  50. private Resource src = null;
  51. /**
  52. * Holds filterchains
  53. */
  54. private final Vector filterChains = new Vector();
  55. /**
  56. * Encoding to use for input; defaults to the platform's default encoding.
  57. */
  58. private String encoding = null;
  59. /**
  60. * Prefix for loaded properties.
  61. */
  62. private String prefix = null;
  63. private boolean prefixValues = true;
  64. /**
  65. * Set the file to load.
  66. *
  67. * @param srcFile The new SrcFile value
  68. */
  69. public final void setSrcFile(final File srcFile) {
  70. addConfigured(new FileResource(srcFile));
  71. }
  72. /**
  73. * Set the resource name of a property file to load.
  74. *
  75. * @param resource resource on classpath
  76. */
  77. public void setResource(String resource) {
  78. getRequiredJavaResource().setName(resource);
  79. }
  80. /**
  81. * Encoding to use for input, defaults to the platform's default
  82. * encoding. <p>
  83. *
  84. * For a list of possible values see
  85. * <a href="http://java.sun.com/j2se/1.5.0/docs/guide/intl/encoding.doc.html">
  86. * http://java.sun.com/j2se/1.5.0/docs/guide/intl/encoding.doc.html
  87. * </a>.</p>
  88. *
  89. * @param encoding The new Encoding value
  90. */
  91. public final void setEncoding(final String encoding) {
  92. this.encoding = encoding;
  93. }
  94. /**
  95. * Set the classpath to use when looking up a resource.
  96. * @param classpath to add to any existing classpath
  97. */
  98. public void setClasspath(Path classpath) {
  99. getRequiredJavaResource().setClasspath(classpath);
  100. }
  101. /**
  102. * Add a classpath to use when looking up a resource.
  103. * @return The classpath to be configured
  104. */
  105. public Path createClasspath() {
  106. return getRequiredJavaResource().createClasspath();
  107. }
  108. /**
  109. * Set the classpath to use when looking up a resource,
  110. * given as reference to a &lt;path&gt; defined elsewhere
  111. * @param r The reference value
  112. */
  113. public void setClasspathRef(Reference r) {
  114. getRequiredJavaResource().setClasspathRef(r);
  115. }
  116. /**
  117. * get the classpath used by this <code>LoadProperties</code>.
  118. * @return The classpath
  119. */
  120. public Path getClasspath() {
  121. return getRequiredJavaResource().getClasspath();
  122. }
  123. /**
  124. * Set the prefix to load these properties under.
  125. * @param prefix to set
  126. */
  127. public void setPrefix(String prefix) {
  128. this.prefix = prefix;
  129. }
  130. /**
  131. * Whether to apply the prefix when expanding properties on the
  132. * right hand side of a properties file as well.
  133. *
  134. * @since Ant 1.8.2
  135. */
  136. public void setPrefixValues(boolean b) {
  137. prefixValues = b;
  138. }
  139. /**
  140. * load Ant properties from the source file or resource
  141. *
  142. * @exception BuildException if something goes wrong with the build
  143. */
  144. public final void execute() throws BuildException {
  145. //validation
  146. if (src == null) {
  147. throw new BuildException("A source resource is required.");
  148. }
  149. if (!src.isExists()) {
  150. if (src instanceof JavaResource) {
  151. // dreaded backwards compatibility
  152. log("Unable to find resource " + src, Project.MSG_WARN);
  153. return;
  154. }
  155. throw new BuildException("Source resource does not exist: " + src);
  156. }
  157. BufferedInputStream bis = null;
  158. Reader instream = null;
  159. ByteArrayInputStream tis = null;
  160. try {
  161. bis = new BufferedInputStream(src.getInputStream());
  162. if (encoding == null) {
  163. instream = new InputStreamReader(bis);
  164. } else {
  165. instream = new InputStreamReader(bis, encoding);
  166. }
  167. ChainReaderHelper crh = new ChainReaderHelper();
  168. crh.setPrimaryReader(instream);
  169. crh.setFilterChains(filterChains);
  170. crh.setProject(getProject());
  171. instream = crh.getAssembledReader();
  172. String text = crh.readFully(instream);
  173. if (text != null && text.length() != 0) {
  174. if (!text.endsWith("\n")) {
  175. text = text + "\n";
  176. }
  177. tis = new ByteArrayInputStream(text.getBytes(ResourceUtils.ISO_8859_1));
  178. final Properties props = new Properties();
  179. props.load(tis);
  180. Property propertyTask = new Property();
  181. propertyTask.bindToOwner(this);
  182. propertyTask.setPrefix(prefix);
  183. propertyTask.setPrefixValues(prefixValues);
  184. propertyTask.addProperties(props);
  185. }
  186. } catch (final IOException ioe) {
  187. throw new BuildException("Unable to load file: " + ioe, ioe, getLocation());
  188. } finally {
  189. FileUtils.close(bis);
  190. FileUtils.close(tis);
  191. }
  192. }
  193. /**
  194. * Adds a FilterChain.
  195. * @param filter the filter to add
  196. */
  197. public final void addFilterChain(FilterChain filter) {
  198. filterChains.addElement(filter);
  199. }
  200. /**
  201. * Set the source resource.
  202. * @param a the resource to load as a single element Resource collection.
  203. * @since Ant 1.7
  204. */
  205. public synchronized void addConfigured(ResourceCollection a) {
  206. if (src != null) {
  207. throw new BuildException("only a single source is supported");
  208. }
  209. if (a.size() != 1) {
  210. throw new BuildException(
  211. "only single-element resource collections are supported");
  212. }
  213. src = a.iterator().next();
  214. }
  215. private synchronized JavaResource getRequiredJavaResource() {
  216. if (src == null) {
  217. src = new JavaResource();
  218. src.setProject(getProject());
  219. } else if (!(src instanceof JavaResource)) {
  220. throw new BuildException("expected a java resource as source");
  221. }
  222. return (JavaResource) src;
  223. }
  224. }