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.

FixCRLF.java 13 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 1999 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", "Tomcat", 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.*;
  56. import java.io.*;
  57. import java.util.*;
  58. import java.text.*;
  59. /**
  60. * Task to convert text source files to local OS formatting conventions, as
  61. * well as repair text files damaged by misconfigured or misguided editors or
  62. * file transfer programs.
  63. * <p>
  64. * This task can take the following arguments:
  65. * <ul>
  66. * <li>srcdir
  67. * <li>destdir
  68. * <li>include
  69. * <li>exclude
  70. * <li>cr
  71. * <li>tab
  72. * <li>eof
  73. * </ul>
  74. * Of these arguments, only <b>sourcedir</b> is required.
  75. * <p>
  76. * When this task executes, it will scan the srcdir based on the include
  77. * and exclude properties.
  78. * <p>
  79. * <em>Warning:</em> do not run on binary or carefully formatted files.
  80. * this may sound obvious, but if you don't specify asis, presume that
  81. * your files are going to be modified. If you want tabs to be fixed,
  82. * whitespace characters may be added or removed as necessary. Similarly,
  83. * for CR's - in fact cr="add" can result in cr characters being removed.
  84. * (to handle cases where other programs have converted CRLF into CRCRLF).
  85. *
  86. * @author Sam Ruby <a href="mailto:rubys@us.ibm.com">rubys@us.ibm.com</a>
  87. */
  88. public class FixCRLF extends MatchingTask {
  89. private int addcr; // cr: -1 => remove, 0 => asis, +1 => add
  90. private int addtab; // tab: -1 => remove, 0 => asis, +1 => add
  91. private int ctrlz; // eof: -1 => remove, 0 => asis, +1 => add
  92. private File srcDir;
  93. private File destDir = null;
  94. /**
  95. * Defaults the properties based on the system type.
  96. * <ul><li>Unix: cr="remove" tab="asis" eof="remove"
  97. * <li>DOS: cr="add" tab="asis" eof="asis"</ul>
  98. */
  99. public FixCRLF() {
  100. if (System.getProperty("path.separator").equals(":")) {
  101. addcr = -1; // remove
  102. ctrlz = -1; // remove
  103. } else {
  104. addcr = +1; // add
  105. ctrlz = 0; // asis
  106. }
  107. }
  108. /**
  109. * Set the source dir to find the source text files.
  110. *
  111. * @param srcDirName name of the source directory.
  112. */
  113. public void setSrcdir(String srcDirName) {
  114. srcDir = project.resolveFile(srcDirName);
  115. }
  116. /**
  117. * Set the destination where the fixed files should be placed.
  118. * Default is to replace the original file.
  119. *
  120. * @param destDirName name of the destination directory.
  121. */
  122. public void setDestdir(String destDirName) {
  123. destDir = project.resolveFile(destDirName);
  124. }
  125. /**
  126. * Specify how carriage return (CR) charaters are to be handled
  127. *
  128. * @param option valid values:
  129. * <ul>
  130. * <li>add: ensure that there is a CR before every LF
  131. * <li>asis: leave CR characters alone
  132. * <li>remove: remove all CR characters
  133. * </ul>
  134. */
  135. public void setCr(String option) {
  136. if (option.equals("remove")) {
  137. addcr = -1;
  138. } else if (option.equals("asis")) {
  139. addcr = 0;
  140. } else if (option.equals("add")) {
  141. addcr = +1;
  142. } else {
  143. throw new BuildException("Invalid option: " + option );
  144. }
  145. }
  146. /**
  147. * Specify how tab charaters are to be handled
  148. *
  149. * @param option valid values:
  150. * <ul>
  151. * <li>add: convert sequences of spaces which span a tab stop to tabs
  152. * <li>asis: leave tab and space characters alone
  153. * <li>remove: convert tabs to spaces
  154. * </ul>
  155. */
  156. public void setTab(String option) {
  157. if (option.equals("remove")) {
  158. addtab = -1;
  159. } else if (option.equals("asis")) {
  160. addtab = 0;
  161. } else if (option.equals("add")) {
  162. addtab = +1;
  163. } else {
  164. throw new BuildException("Invalid option: " + option );
  165. }
  166. }
  167. /**
  168. * Specify how DOS EOF (control-z) charaters are to be handled
  169. *
  170. * @param option valid values:
  171. * <ul>
  172. * <li>add: ensure that there is an eof at the end of the file
  173. * <li>asis: leave eof characters alone
  174. * <li>remove: remove any eof character found at the end
  175. * </ul>
  176. */
  177. public void setEof(String option) {
  178. if (option.equals("remove")) {
  179. ctrlz = -1;
  180. } else if (option.equals("asis")) {
  181. ctrlz = 0;
  182. } else if (option.equals("add")) {
  183. ctrlz = +1;
  184. } else {
  185. throw new BuildException("Invalid option: " + option );
  186. }
  187. }
  188. /**
  189. * Executes the task.
  190. */
  191. public void execute() throws BuildException {
  192. // first off, make sure that we've got a srcdir and destdir
  193. if (srcDir == null) {
  194. throw new BuildException("srcdir attribute must be set!");
  195. }
  196. if (!srcDir.exists()) {
  197. throw new BuildException("srcdir does not exist!");
  198. }
  199. if (!srcDir.isDirectory()) {
  200. throw new BuildException("srcdir is not a directory!");
  201. }
  202. if (destDir != null) {
  203. if (!destDir.exists()) {
  204. throw new BuildException("destdir does not exist!");
  205. }
  206. if (!destDir.isDirectory()) {
  207. throw new BuildException("destdir is not a directory!");
  208. }
  209. }
  210. // log options used
  211. project.log("options:" +
  212. " cr=" + (addcr==-1 ? "add" : addcr==0 ? "asis" : "remove") +
  213. " tab=" + (addtab==-1 ? "add" : addtab==0 ? "asis" : "remove") +
  214. " eof=" + (ctrlz==-1 ? "add" : ctrlz==0 ? "asis" : "remove"),
  215. "fixcrlf", project.MSG_VERBOSE);
  216. DirectoryScanner ds = super.getDirectoryScanner(srcDir);
  217. String[] files = ds.getIncludedFiles();
  218. for (int i = 0; i < files.length; i++) {
  219. File srcFile = new File(srcDir, files[i]);
  220. // read the contents of the file
  221. int count = (int)srcFile.length();
  222. byte indata[] = new byte[count];
  223. try {
  224. FileInputStream inStream = new FileInputStream(srcFile);
  225. inStream.read(indata);
  226. inStream.close();
  227. } catch (IOException e) {
  228. throw new BuildException(e);
  229. }
  230. // count the number of cr, lf, and tab characters
  231. int cr = 0;
  232. int lf = 0;
  233. int tab = 0;
  234. for (int k=0; k<count; k++) {
  235. byte c = indata[k];
  236. if (c == '\r') cr++;
  237. if (c == '\n') lf++;
  238. if (c == '\t') tab++;
  239. }
  240. // check for trailing eof
  241. boolean eof = ((count>0) && (indata[count-1] == 0x1A));
  242. // log stats (before fixes)
  243. project.log(srcFile + ": size=" + count + " cr=" + cr +
  244. " lf=" + lf + " tab=" + tab + " eof=" + eof,
  245. "fixcrlf", project.MSG_VERBOSE);
  246. // determine the output buffer size (slightly pessimisticly)
  247. int outsize = count;
  248. if (addcr != 0) outsize-=cr;
  249. if (addcr == +1) outsize+=lf;
  250. if (addtab == -1) outsize+=tab*7;
  251. if (ctrlz == +1) outsize+=1;
  252. // copy the data
  253. byte outdata[] = new byte[outsize];
  254. int o = 0; // output offset
  255. int line = o; // beginning of line
  256. int col = 0; // desired column
  257. for (int k=0; k<count; k++) {
  258. switch (indata[k]) {
  259. case (byte)' ':
  260. // advance column
  261. if (addtab == 0) outdata[o++]=(byte)' ';
  262. col++;
  263. break;
  264. case (byte)'\t':
  265. if (addtab == 0) {
  266. // treat like any other character
  267. outdata[o++]=(byte)'\t';
  268. col++;
  269. } else {
  270. // advance column to next tab stop
  271. col = (col|7)+1;
  272. }
  273. break;
  274. case (byte)'\r':
  275. if (addcr == 0) {
  276. // treat like any other character
  277. outdata[o++]=(byte)'\r';
  278. col++;
  279. }
  280. break;
  281. case (byte)'\n':
  282. // start a new line (optional CR followed by LF)
  283. if (addcr == +1) outdata[o++]=(byte)'\r';
  284. outdata[o++]=(byte)'\n';
  285. line=o;
  286. col=0;
  287. break;
  288. default:
  289. // add tabs if two or more spaces are required
  290. if (addtab>0 && o+1<line+col) {
  291. // determine logical column
  292. int diff=o-line;
  293. // add tabs until this column would be passed
  294. // note: the start of line is adjusted to match
  295. while ((diff|7)<col) {
  296. outdata[o++]=(byte)'\t';
  297. line-=7-(diff&7);
  298. diff=o-line;
  299. };
  300. };
  301. // space out to desired column
  302. while (o<line+col) outdata[o++]=(byte)' ';
  303. // append desired character
  304. outdata[o++]=indata[k];
  305. col++;
  306. }
  307. }
  308. // add or remove an eof character as required
  309. if (ctrlz == +1) {
  310. if (outdata[o-1]!=0x1A) outdata[o++]=0x1A;
  311. } else if (ctrlz == -1) {
  312. if (o>2 && outdata[o-1]==0x0A && outdata[o-2]==0x1A) o--;
  313. if (o>1 && outdata[o-1]==0x1A) o--;
  314. }
  315. // output the data
  316. try {
  317. File destFile = srcFile;
  318. if (destDir != null) destFile = new File(destDir, files[i]);
  319. FileOutputStream outStream = new FileOutputStream(destFile);
  320. outStream.write(outdata,0,o);
  321. outStream.close();
  322. } catch (IOException e) {
  323. throw new BuildException(e);
  324. }
  325. } /* end for */
  326. }
  327. }