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.

LoadFile.java 8.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 2001-2002 The Apache Software Foundation. All rights
  5. * reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if
  20. * any, must include the following acknowlegement:
  21. * "This product includes software developed by the
  22. * Apache Software Foundation (http://www.apache.org/)."
  23. * Alternately, this acknowlegement may appear in the software itself,
  24. * if and wherever such third-party acknowlegements normally appear.
  25. *
  26. * 4. The names "The Jakarta Project", "Ant", and "Apache Software
  27. * Foundation" must not be used to endorse or promote products derived
  28. * from this software without prior written permission. For written
  29. * permission, please contact apache@apache.org.
  30. *
  31. * 5. Products derived from this software may not be called "Apache"
  32. * nor may "Apache" appear in their names without prior written
  33. * permission of the Apache Group.
  34. *
  35. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46. * SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This software consists of voluntary contributions made by many
  50. * individuals on behalf of the Apache Software Foundation. For more
  51. * information on the Apache Software Foundation, please see
  52. * <http://www.apache.org/>.
  53. */
  54. package org.apache.tools.ant.taskdefs;
  55. import org.apache.tools.ant.Task;
  56. import org.apache.tools.ant.Project;
  57. import org.apache.tools.ant.BuildException;
  58. import java.io.*;
  59. /**
  60. * Load a file into a property
  61. *
  62. * @author Steve Loughran
  63. *
  64. * @ant:task category="utility"
  65. */
  66. public class LoadFile extends Task {
  67. /**
  68. * source file, usually null
  69. */
  70. private File srcFile = null;
  71. /**
  72. * what to do when it goes pear-shaped
  73. */
  74. private boolean failOnError = true;
  75. /**
  76. * Encoding to use for filenames, defaults to the platform's default
  77. * encoding.
  78. */
  79. private String encoding = null;
  80. /**
  81. * name of property
  82. */
  83. private String property = null;
  84. /** flag to control if we flatten the file or no'
  85. *
  86. */
  87. private boolean makeOneLine=false;
  88. /**
  89. * flag to control whether props get evaluated or not
  90. */
  91. private boolean evaluateProperties=false;
  92. /**
  93. * Encoding to use for filenames, defaults to the platform's default
  94. * encoding. <p>
  95. *
  96. * For a list of possible values see <a href="http://java.sun.com/products/jdk/1.2/docs/guide/internat/encoding.doc.html">
  97. * http://java.sun.com/products/jdk/1.2/docs/guide/internat/encoding.doc.html
  98. * </a>.</p>
  99. *
  100. * @param encoding The new Encoding value
  101. */
  102. public void setEncoding(String encoding) {
  103. this.encoding = encoding;
  104. }
  105. /**
  106. * Sets the Property attribute of the LoadFile object
  107. *
  108. * @param property The new Property value
  109. */
  110. public void setProperty(String property) {
  111. this.property = property;
  112. }
  113. /**
  114. * Sets the srcfile attribute.
  115. *
  116. * @param srcFile The new SrcFile value
  117. */
  118. public void setSrcFile(File srcFile) {
  119. this.srcFile = srcFile;
  120. }
  121. /**
  122. * Sets the Failonerror attribute of the LoadFile object
  123. *
  124. * @param fail The new Failonerror value
  125. */
  126. public void setFailonerror(boolean fail) {
  127. failOnError = fail;
  128. }
  129. /**
  130. * setter to flatten the file to a single line
  131. * @since 1.6
  132. */
  133. public void setMakeOneLine(boolean makeOneLine) {
  134. this.makeOneLine=makeOneLine;
  135. }
  136. /**
  137. * setter to eval properties.
  138. * @since 1.6
  139. */
  140. public void setEvaluateProperties(boolean evaluateProperties) {
  141. this.evaluateProperties=evaluateProperties;
  142. }
  143. /**
  144. * read in a source file to a property
  145. *
  146. * @exception BuildException if something goes wrong with the build
  147. */
  148. public void execute()
  149. throws BuildException {
  150. //validation
  151. if (srcFile == null) {
  152. throw new BuildException("source file not defined");
  153. }
  154. if (property == null) {
  155. throw new BuildException("output property not defined");
  156. }
  157. FileInputStream fis = null;
  158. BufferedInputStream bis = null;
  159. Reader instream = null;
  160. log("loading "+srcFile+" into property "+property,Project.MSG_VERBOSE);
  161. try {
  162. long len = srcFile.length();
  163. log("file size = "+len,Project.MSG_DEBUG);
  164. //discard most of really big files
  165. if (len > Integer.MAX_VALUE) {
  166. log("this file is far to big to load completely");
  167. }
  168. int size=(int) len;
  169. char[] buffer = new char[size];
  170. //open up the file
  171. fis = new FileInputStream(srcFile);
  172. bis = new BufferedInputStream(fis);
  173. if (encoding == null) {
  174. instream = new InputStreamReader(bis);
  175. }
  176. else {
  177. instream = new InputStreamReader(bis, encoding);
  178. }
  179. instream.read(buffer);
  180. String text = new String(buffer);
  181. if (makeOneLine) {
  182. text=stripLineBreaks(text);
  183. }
  184. if(evaluateProperties) {
  185. text = project.replaceProperties(text);
  186. }
  187. project.setNewProperty(property, text);
  188. log("loaded "+buffer.length+" characters",Project.MSG_VERBOSE);
  189. log(property+" := "+text,Project.MSG_DEBUG);
  190. } catch (IOException ioe) {
  191. String message = "Unable to load file: " + ioe.toString();
  192. if (failOnError) {
  193. throw new BuildException(message, ioe, location);
  194. }
  195. else {
  196. log(message, Project.MSG_ERR);
  197. }
  198. } finally {
  199. try {
  200. if (fis != null) {
  201. fis.close();
  202. }
  203. } catch (IOException ioex) {
  204. }
  205. }
  206. }
  207. /**
  208. * strip out all line breaks from a string.
  209. * @param source source
  210. * This implementation always duplicates the string; it is nominally possible to probe
  211. * the string first looking for any line breaks before bothering to do a copy. But we assume if
  212. * the option is requested, then line breaks are probably in the source string.
  213. */
  214. protected String stripLineBreaks(String source) {
  215. //Linebreaks. What do to on funny IBM mainframes with odd line endings?
  216. String linebreaks="\r\n";
  217. int len=source.length();
  218. StringBuffer dest=new StringBuffer(len);
  219. for(int i=0;i<len;++i) {
  220. char ch=source.charAt(i);
  221. if(linebreaks.indexOf(ch)==-1) {
  222. dest.append(ch);
  223. }
  224. }
  225. return new String(dest);
  226. }
  227. //end class
  228. }