1. /*
  2. * Copyright 2001-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.File;
  20. import java.io.FileInputStream;
  21. import java.io.IOException;
  22. import java.io.InputStreamReader;
  23. import java.io.Reader;
  24. import java.util.Vector;
  25. import org.apache.tools.ant.BuildException;
  26. import org.apache.tools.ant.Project;
  27. import org.apache.tools.ant.Task;
  28. import org.apache.tools.ant.filters.util.ChainReaderHelper;
  29. import org.apache.tools.ant.types.FilterChain;
  30. /**
  31. * Load a file into a property
  32. *
  33. * @since Ant 1.5
  34. * @ant.task category="utility"
  35. */
  36. public final class LoadFile extends Task {
  37. /**
  38. * source file, usually null
  39. */
  40. private File srcFile = null;
  41. /**
  42. * what to do when it goes pear-shaped
  43. */
  44. private boolean failOnError = true;
  45. /**
  46. * Encoding to use for filenames, defaults to the platform's default
  47. * encoding.
  48. */
  49. private String encoding = null;
  50. /**
  51. * name of property
  52. */
  53. private String property = 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
  60. * encoding. <p>
  61. *
  62. * For a list of possible values see
  63. * <a href="http://java.sun.com/products/jdk/1.2/docs/guide/internat/encoding.doc.html">
  64. * http://java.sun.com/products/jdk/1.2/docs/guide/internat/encoding.doc.html
  65. * </a>.</p>
  66. *
  67. * @param encoding The new Encoding value
  68. */
  69. public final void setEncoding(final String encoding) {
  70. this.encoding = encoding;
  71. }
  72. /**
  73. * Property name to save to.
  74. *
  75. * @param property The new Property value
  76. */
  77. public final void setProperty(final String property) {
  78. this.property = property;
  79. }
  80. /**
  81. * Sets the file to load.
  82. *
  83. * @param srcFile The new SrcFile value
  84. */
  85. public final void setSrcFile(final File srcFile) {
  86. this.srcFile = srcFile;
  87. }
  88. /**
  89. * If true, fail on load error.
  90. *
  91. * @param fail The new Failonerror value
  92. */
  93. public final void setFailonerror(final boolean fail) {
  94. failOnError = fail;
  95. }
  96. /**
  97. * read in a source file to a property
  98. *
  99. * @exception BuildException if something goes wrong with the build
  100. */
  101. public final void execute()
  102. throws BuildException {
  103. //validation
  104. if (srcFile == null) {
  105. throw new BuildException("source file not defined");
  106. }
  107. if (property == null) {
  108. throw new BuildException("output property not defined");
  109. }
  110. FileInputStream fis = null;
  111. BufferedInputStream bis = null;
  112. Reader instream = null;
  113. log("loading " + srcFile + " into property " + property,
  114. Project.MSG_VERBOSE);
  115. try {
  116. final long len = srcFile.length();
  117. log("file size = " + len, Project.MSG_DEBUG);
  118. //discard most of really big files
  119. final int size = (int) len;
  120. //open up the file
  121. fis = new FileInputStream(srcFile);
  122. bis = new BufferedInputStream(fis);
  123. if (encoding == null) {
  124. instream = new InputStreamReader(bis);
  125. } else {
  126. instream = new InputStreamReader(bis, encoding);
  127. }
  128. String text = "";
  129. if (size != 0) {
  130. ChainReaderHelper crh = new ChainReaderHelper();
  131. crh.setBufferSize(size);
  132. crh.setPrimaryReader(instream);
  133. crh.setFilterChains(filterChains);
  134. crh.setProject(getProject());
  135. instream = crh.getAssembledReader();
  136. text = crh.readFully(instream);
  137. }
  138. if (text != null) {
  139. if (text.length() > 0) {
  140. getProject().setNewProperty(property, text);
  141. log("loaded " + text.length() + " characters",
  142. Project.MSG_VERBOSE);
  143. log(property + " := " + text, Project.MSG_DEBUG);
  144. }
  145. }
  146. } catch (final IOException ioe) {
  147. final String message = "Unable to load file: " + ioe.toString();
  148. if (failOnError) {
  149. throw new BuildException(message, ioe, getLocation());
  150. } else {
  151. log(message, Project.MSG_ERR);
  152. }
  153. } catch (final BuildException be) {
  154. if (failOnError) {
  155. throw be;
  156. } else {
  157. log(be.getMessage(), Project.MSG_ERR);
  158. }
  159. } finally {
  160. try {
  161. if (fis != null) {
  162. fis.close();
  163. }
  164. } catch (IOException ioex) {
  165. //ignore
  166. }
  167. }
  168. }
  169. /**
  170. * Add the FilterChain element.
  171. */
  172. public final void addFilterChain(FilterChain filter) {
  173. filterChains.addElement(filter);
  174. }
  175. //end class
  176. }