From e75231b54caea20b8fa2ded6f40fa2ab335989f6 Mon Sep 17 00:00:00 2001
From: Sam Ruby
Date: Sat, 12 Feb 2000 16:59:26 +0000
Subject: [PATCH] Add a task to convert a text file to local OS conventions.
Can also be used to adjust tabs and spaces. See documentation for details.
git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@267602 13f79535-47bb-0310-9956-ffa450edef68
---
build.xml | 2 +
docs/index.html | 120 +++++-
.../apache/tools/ant/taskdefs/FixCRLF.java | 364 ++++++++++++++++++
.../tools/ant/taskdefs/defaults.properties | 1 +
4 files changed, 486 insertions(+), 1 deletion(-)
create mode 100644 src/main/org/apache/tools/ant/taskdefs/FixCRLF.java
diff --git a/build.xml b/build.xml
index ec7daff90..59609a816 100644
--- a/build.xml
+++ b/build.xml
@@ -69,6 +69,8 @@
+
+
diff --git a/docs/index.html b/docs/index.html
index e731343c7..05ec42e9b 100644
--- a/docs/index.html
+++ b/docs/index.html
@@ -14,8 +14,9 @@
James Duncan Davison (duncan@x180.com)
Arnout J. Kuiper (ajkuiper@wxs.nl)
Stefano Mazzocchi (stefano@apache.org)
+ Sam Ruby (rubys@us.ibm.com)
-Version 1.0.4 - 2000/02/10
+Version 1.0.5 - 2000/02/12
Table of Contents
@@ -430,6 +431,7 @@ but excludes all "*.gif" files from the copy.
Expand
Get
GZip
+ FixCRLF
Jar
Java
Javac
@@ -901,6 +903,122 @@ archive with http/ftp.
/>
+
+Description
+Adjusts a text file to local.
+The basedir attribute is the reference directory from where to jar.
+Parameters
+
+
+ Attribute |
+ Description |
+ Required |
+
+
+ srcDir |
+ Where to find the files to be fixed up. |
+ Yes |
+
+
+ destDir |
+ Where to place the corrected files. Defaults to
+ srcDir (replacing the original file) |
+ No |
+
+
+ includes |
+ comma separated list of patterns of files that must be
+ included. All files are included when omitted. |
+ No |
+
+
+ excludes |
+ comma separated list of patterns of files that must be
+ excluded. No files (except default excludes) are excluded when omitted. |
+ No |
+
+
+ cr |
+ Specifies how carriage return (CR) characters are to
+ be handled. Valid values for this property are:
+
+ - add: ensure that there is a CR before every LF
+
- asis: leave CR characters alone
+
- remove: remove all CR characters
+
+ Default is based on the platform on which you are running this task.
+ For Unix platforms, the default is remove. For DOS based systems
+ (including Windows), the default is add.
+
+ Note: Unless this property is specified as "asis", extra CR characters
+ which do not preceed a LF will be removed.
+ |
+ No |
+
+
+ tab |
+ Specifies how tab characters are to be handled. Valid
+ values for this property are:
+
+ - add: convert sequences of spaces which span a tab stop to tabs
+
- asis: leave tab and space characters alone
+
- remove: convert tabs to spaces
+
+ Default for this parameter is "asis".
+
+ Note: Unless this property is specified as "asis", extra spaces and
+ tabs after the last non-whitespace character on the line will be removed.
+ |
+ No |
+
+
+ eof |
+ Specifies how DOS end of file (control-Z) characters are
+ to be handled. Valid values for this property are:
+
+ - add: ensure that there is an EOF character at the end of the file
+
- asis: leave EOF characters alone
+
- remove: remove any EOF character found at the end
+
+ Default is based on the platform on which you are running this task.
+ For Unix platforms, the default is remove. For DOS based systems
+ (including Windows), the default is asis.
+ |
+ No |
+
+
+Examples
+ <fixcrlf srcdir="${src}"
+ cr="remove" eof="remove"
+ includes="**/*.sh"
+ />
+Removes carriage return and eof characters from the shell scripts. Tabs and
+spaces are left as is.
+
<fixcrlf srcdir="${src}"
+ cr="add"
+ includes="**/*.bat"
+ />
+Ensures that there are carriage return characters prior to evey line feed.
+Tabs and spaces are left as is.
+EOF characters are left alone if run on
+DOS systems, and are removed if run on Unix systems.
+ <fixcrlf srcdir="${src}"
+ tabs="add"
+ includes="**/Makefile"
+ />
+Adds or removes CR characters to match local OS conventions, and
+converts spaces to tabs when appropriate. EOF characters are left alone if
+run on DOS systems, and are removed if run on Unix systems.
+Many versions of make require tabs prior to commands.
+ <fixcrlf srcdir="${src}"
+ tabs="remove"
+ includes="**/README*"
+ />
+Adds or removes CR characters to match local OS conventions, and
+converts all tabs to spaces. EOF characters are left alone if run on
+DOS systems, and are removed if run on Unix systems.
+You never know what editor a user will use to browse README's.
+
Description
Jars a set of files.
diff --git a/src/main/org/apache/tools/ant/taskdefs/FixCRLF.java b/src/main/org/apache/tools/ant/taskdefs/FixCRLF.java
new file mode 100644
index 000000000..733ef3db3
--- /dev/null
+++ b/src/main/org/apache/tools/ant/taskdefs/FixCRLF.java
@@ -0,0 +1,364 @@
+/*
+ * The Apache Software License, Version 1.1
+ *
+ * Copyright (c) 1999 The Apache Software Foundation. All rights
+ * reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * 3. The end-user documentation included with the redistribution, if
+ * any, must include the following acknowlegement:
+ * "This product includes software developed by the
+ * Apache Software Foundation (http://www.apache.org/)."
+ * Alternately, this acknowlegement may appear in the software itself,
+ * if and wherever such third-party acknowlegements normally appear.
+ *
+ * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
+ * Foundation" must not be used to endorse or promote products derived
+ * from this software without prior written permission. For written
+ * permission, please contact apache@apache.org.
+ *
+ * 5. Products derived from this software may not be called "Apache"
+ * nor may "Apache" appear in their names without prior written
+ * permission of the Apache Group.
+ *
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
+ * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation. For more
+ * information on the Apache Software Foundation, please see
+ * .
+ */
+
+package org.apache.tools.ant.taskdefs;
+
+import org.apache.tools.ant.*;
+import java.io.*;
+import java.util.*;
+import java.text.*;
+
+/**
+ * Task to convert text source files to local OS formatting conventions, as
+ * well as repair text files damaged by misconfigured or misguided editors or
+ * file transfer programs.
+ *
+ * This task can take the following arguments:
+ *
+ * - srcdir
+ *
- destdir
+ *
- include
+ *
- exclude
+ *
- cr
+ *
- tab
+ *
- eof
+ *
+ * Of these arguments, only sourcedir is required.
+ *
+ * When this task executes, it will scan the srcdir based on the include
+ * and exclude properties.
+ *
+ * Warning: do not run on binary or carefully formatted files.
+ * this may sound obvious, but if you don't specify asis, presume that
+ * your files are going to be modified. If you want tabs to be fixed,
+ * whitespace characters may be added or removed as necessary. Similarly,
+ * for CR's - in fact cr="add" can result in cr characters being removed.
+ * (to handle cases where other programs have converted CRLF into CRCRLF).
+ *
+ * @author Sam Ruby rubys@us.ibm.com
+ */
+
+public class FixCRLF extends MatchingTask {
+
+ private int addcr; // cr: -1 => remove, 0 => asis, +1 => add
+ private int addtab; // tab: -1 => remove, 0 => asis, +1 => add
+ private int ctrlz; // eof: -1 => remove, 0 => asis, +1 => add
+
+ private File srcDir;
+ private File destDir = null;
+
+ /**
+ * Defaults the properties based on the system type.
+ *
- Unix: cr="remove" tab="asis" eof="remove"
+ *
- DOS: cr="add" tab="asis" eof="asis"
+ */
+ public FixCRLF() {
+ if (System.getProperty("path.separator").equals(":")) {
+ addcr = -1; // remove
+ ctrlz = -1; // remove
+ } else {
+ addcr = +1; // add
+ ctrlz = 0; // asis
+ }
+ }
+
+ /**
+ * Set the source dir to find the source text files.
+ *
+ * @param srcDirName name of the source directory.
+ */
+ public void setSrcdir(String srcDirName) {
+ srcDir = project.resolveFile(srcDirName);
+ }
+
+ /**
+ * Set the destination where the fixed files should be placed.
+ * Default is to replace the original file.
+ *
+ * @param destDirName name of the destination directory.
+ */
+ public void setDestdir(String destDirName) {
+ destDir = project.resolveFile(destDirName);
+ }
+
+ /**
+ * Specify how carriage return (CR) charaters are to be handled
+ *
+ * @param option valid values:
+ *
+ * - add: ensure that there is a CR before every LF
+ *
- asis: leave CR characters alone
+ *
- remove: remove all CR characters
+ *
+ */
+ public void setCr(String option) {
+ if (option.equals("remove")) {
+ addcr = -1;
+ } else if (option.equals("asis")) {
+ addcr = 0;
+ } else if (option.equals("add")) {
+ addcr = +1;
+ } else {
+ throw new BuildException("Invalid option: " + option );
+ }
+ }
+
+ /**
+ * Specify how tab charaters are to be handled
+ *
+ * @param option valid values:
+ *
+ * - add: convert sequences of spaces which span a tab stop to tabs
+ *
- asis: leave tab and space characters alone
+ *
- remove: convert tabs to spaces
+ *
+ */
+ public void setTab(String option) {
+ if (option.equals("remove")) {
+ addtab = -1;
+ } else if (option.equals("asis")) {
+ addtab = 0;
+ } else if (option.equals("add")) {
+ addtab = +1;
+ } else {
+ throw new BuildException("Invalid option: " + option );
+ }
+ }
+
+ /**
+ * Specify how DOS EOF (control-z) charaters are to be handled
+ *
+ * @param option valid values:
+ *
+ * - add: ensure that there is an eof at the end of the file
+ *
- asis: leave eof characters alone
+ *
- remove: remove any eof character found at the end
+ *
+ */
+ public void setEof(String option) {
+ if (option.equals("remove")) {
+ ctrlz = -1;
+ } else if (option.equals("asis")) {
+ ctrlz = 0;
+ } else if (option.equals("add")) {
+ ctrlz = +1;
+ } else {
+ throw new BuildException("Invalid option: " + option );
+ }
+ }
+
+ /**
+ * Executes the task.
+ */
+ public void execute() throws BuildException {
+ // first off, make sure that we've got a srcdir and destdir
+
+ if (srcDir == null) {
+ throw new BuildException("srcdir attribute must be set!");
+ }
+ if (!srcDir.exists()) {
+ throw new BuildException("srcdir does not exist!");
+ }
+ if (!srcDir.isDirectory()) {
+ throw new BuildException("srcdir is not a directory!");
+ }
+ if (destDir != null) {
+ if (!destDir.exists()) {
+ throw new BuildException("destdir does not exist!");
+ }
+ if (!destDir.isDirectory()) {
+ throw new BuildException("destdir is not a directory!");
+ }
+ }
+
+ // log options used
+ project.log("options:" +
+ " cr=" + (addcr==-1 ? "add" : addcr==0 ? "asis" : "remove") +
+ " tab=" + (addtab==-1 ? "add" : addtab==0 ? "asis" : "remove") +
+ " eof=" + (ctrlz==-1 ? "add" : ctrlz==0 ? "asis" : "remove"),
+ "fixcrlf", project.MSG_VERBOSE);
+
+ DirectoryScanner ds = super.getDirectoryScanner(srcDir);
+ String[] files = ds.getIncludedFiles();
+try {
+
+ for (int i = 0; i < files.length; i++) {
+ File srcFile = new File(srcDir, files[i]);
+
+ // read the contents of the file
+ int count = (int)srcFile.length();
+ byte indata[] = new byte[count];
+ try {
+ FileInputStream inStream = new FileInputStream(srcFile);
+ inStream.read(indata);
+ inStream.close();
+ } catch (IOException e) {
+ throw new BuildException(e);
+ }
+
+ // count the number of cr, lf, and tab characters
+ int cr = 0;
+ int lf = 0;
+ int tab = 0;
+
+ for (int k=0; k0) && (indata[count-1] == 0x1A));
+
+ // log stats (before fixes)
+ project.log(srcFile + ": size=" + count + " cr=" + cr +
+ " lf=" + lf + " tab=" + tab + " eof=" + eof,
+ "fixcrlf", project.MSG_VERBOSE);
+
+ // determine the output buffer size (slightly pessimisticly)
+ int outsize = count;
+ if (addcr != 0) outsize-=cr;
+ if (addcr == +1) outsize+=lf;
+ if (addtab == -1) outsize+=tab*7;
+ if (ctrlz == +1) outsize+=1;
+
+ // copy the data
+ byte outdata[] = new byte[outsize];
+ int o = 0; // output offset
+ int line = o; // beginning of line
+ int col = 0; // desired column
+
+ for (int k=0; k0 && o+12 && outdata[o-1]==0x0A && outdata[o-2]==0x1A) o--;
+ if (o>1 && outdata[o-1]==0x1A) o--;
+ }
+
+ // output the data
+ try {
+ File destFile = srcFile;
+ if (destDir != null) destFile = new File(destDir, files[i]);
+ FileOutputStream outStream = new FileOutputStream(destFile);
+ outStream.write(outdata,0,o);
+ outStream.close();
+ } catch (IOException e) {
+ throw new BuildException(e);
+ }
+
+ } /* end for */
+} catch (Exception e) {e.printStackTrace(); throw new BuildException(e); }
+ }
+}
diff --git a/src/main/org/apache/tools/ant/taskdefs/defaults.properties b/src/main/org/apache/tools/ant/taskdefs/defaults.properties
index f668642fd..66e2bc65d 100644
--- a/src/main/org/apache/tools/ant/taskdefs/defaults.properties
+++ b/src/main/org/apache/tools/ant/taskdefs/defaults.properties
@@ -25,6 +25,7 @@ ant=org.apache.tools.ant.taskdefs.Ant
exec=org.apache.tools.ant.taskdefs.Exec
tar=org.apache.tools.ant.taskdefs.Tar
available=org.apache.tools.ant.taskdefs.Available
+fixcrlf=org.apache.tools.ant.taskdefs.FixCRLF
# deprecated ant tasks (kept for back compatibility)
javadoc2=org.apache.tools.ant.taskdefs.Javadoc