ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject cvs commit: jakarta-ant/src/testcases/org/apache/tools/ant/taskdefs/optional ReplaceRegExpTest.java
Date Mon, 23 Sep 2002 10:58:17 GMT
bodewig     2002/09/23 03:58:17

  Modified:    .        Tag: ANT_15_BRANCH WHATSNEW
               src/etc/testcases/taskdefs/optional Tag: ANT_15_BRANCH
                        replaceregexp.xml
               src/main/org/apache/tools/ant/taskdefs/optional Tag:
                        ANT_15_BRANCH ReplaceRegExp.java
               src/testcases/org/apache/tools/ant/taskdefs/optional Tag:
                        ANT_15_BRANCH ReplaceRegExpTest.java
  Added:       src/etc/testcases/taskdefs/optional Tag: ANT_15_BRANCH
                        replaceregexp2.properties
                        replaceregexp2.result.properties
  Log:
  Fix <replaceregexp> linefeed behavior at end of file, merge from HEAD.
  
  Revision  Changes    Path
  No                   revision
  
  
  No                   revision
  
  
  1.263.2.78 +3 -0      jakarta-ant/WHATSNEW
  
  Index: WHATSNEW
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/WHATSNEW,v
  retrieving revision 1.263.2.77
  retrieving revision 1.263.2.78
  diff -u -r1.263.2.77 -r1.263.2.78
  --- WHATSNEW	13 Sep 2002 10:13:40 -0000	1.263.2.77
  +++ WHATSNEW	23 Sep 2002 10:58:16 -0000	1.263.2.78
  @@ -55,6 +55,9 @@
     be a single empty argument.  Use <arg value="''"/> if you need the
     quotes literally.
   
  +* <replaceregexp> could append a newline character at the end of the
  +  file.
  +
   Other changes:
   --------------
   
  
  
  
  No                   revision
  
  
  No                   revision
  
  
  1.3.2.1   +18 -0     jakarta-ant/src/etc/testcases/taskdefs/optional/replaceregexp.xml
  
  Index: replaceregexp.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/etc/testcases/taskdefs/optional/replaceregexp.xml,v
  retrieving revision 1.3
  retrieving revision 1.3.2.1
  diff -u -r1.3 -r1.3.2.1
  --- replaceregexp.xml	19 Feb 2002 16:48:47 -0000	1.3
  +++ replaceregexp.xml	23 Sep 2002 10:58:16 -0000	1.3.2.1
  @@ -8,7 +8,25 @@
       <copy file="replaceregexp.properties" tofile="test.properties" />
     </target>
   
  +  <target name="setup-nl">
  +    <copy file="replaceregexp2.properties" tofile="test.properties" />
  +  </target>
  +
     <target name="testReplace" depends="setup">
  +    <replaceregexp file="test.properties" byline="true">
  +      <regexp pattern="Old(.*)=(.*)" />
  +      <substitution expression="NewProp=\1\2" />
  +    </replaceregexp>
  +  </target>
  +
  +  <target name="testDontAddNewline1" depends="setup-nl">
  +    <replaceregexp file="test.properties" byline="false">
  +      <regexp pattern="Old(.*)=(.*)" />
  +      <substitution expression="NewProp=\1\2" />
  +    </replaceregexp>
  +  </target>
  +
  +  <target name="testDontAddNewline2" depends="setup-nl">
       <replaceregexp file="test.properties" byline="true">
         <regexp pattern="Old(.*)=(.*)" />
         <substitution expression="NewProp=\1\2" />
  
  
  
  No                   revision
  
  
  No                   revision
  
  
  1.1.2.1   +0 -0      jakarta-ant/src/etc/testcases/taskdefs/optional/replaceregexp2.properties
  
  Index: replaceregexp2.properties
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/etc/testcases/taskdefs/optional/replaceregexp2.properties,v
  retrieving revision 1.1
  retrieving revision 1.1.2.1
  diff -u -r1.1 -r1.1.2.1
  
  
  
  1.1.2.1   +0 -0      jakarta-ant/src/etc/testcases/taskdefs/optional/replaceregexp2.result.properties
  
  Index: replaceregexp2.result.properties
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/etc/testcases/taskdefs/optional/replaceregexp2.result.properties,v
  retrieving revision 1.1
  retrieving revision 1.1.2.1
  diff -u -r1.1 -r1.1.2.1
  
  
  
  No                   revision
  
  
  No                   revision
  
  
  1.9.2.6   +83 -25    jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/ReplaceRegExp.java
  
  Index: ReplaceRegExp.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/ReplaceRegExp.java,v
  retrieving revision 1.9.2.5
  retrieving revision 1.9.2.6
  diff -u -r1.9.2.5 -r1.9.2.6
  --- ReplaceRegExp.java	24 Jun 2002 02:28:10 -0000	1.9.2.5
  +++ ReplaceRegExp.java	23 Sep 2002 10:58:17 -0000	1.9.2.6
  @@ -53,25 +53,23 @@
    */
   package org.apache.tools.ant.taskdefs.optional;
   
  -import org.apache.tools.ant.BuildException;
  -import org.apache.tools.ant.DirectoryScanner;
  -import org.apache.tools.ant.Project;
  -import org.apache.tools.ant.Task;
  -import org.apache.tools.ant.util.regexp.Regexp;
  -import org.apache.tools.ant.types.RegularExpression;
  -import org.apache.tools.ant.types.Substitution;
  -import org.apache.tools.ant.types.FileSet;
  -import org.apache.tools.ant.util.FileUtils;
   import java.io.BufferedReader;
   import java.io.BufferedWriter;
   import java.io.File;
   import java.io.FileReader;
   import java.io.FileWriter;
   import java.io.IOException;
  -import java.io.LineNumberReader;
   import java.io.PrintWriter;
  -
   import java.util.Vector;
  +import org.apache.tools.ant.BuildException;
  +import org.apache.tools.ant.DirectoryScanner;
  +import org.apache.tools.ant.Project;
  +import org.apache.tools.ant.Task;
  +import org.apache.tools.ant.types.FileSet;
  +import org.apache.tools.ant.types.RegularExpression;
  +import org.apache.tools.ant.types.Substitution;
  +import org.apache.tools.ant.util.FileUtils;
  +import org.apache.tools.ant.util.regexp.Regexp;
   
   /**
    * Performs regular expression string replacements in a text
  @@ -285,10 +283,11 @@
                                  String input,
                                  int options) {
           String res = input;
  -        Regexp regexp = r.getRegexp(project);
  +        Regexp regexp = r.getRegexp(getProject());
   
           if (regexp.matches(input, options)) {
  -            res = regexp.substitute(input, s.getExpression(project), options);
  +            res = regexp.substitute(input, s.getExpression(getProject()), 
  +                                    options);
           }
   
           return res;
  @@ -314,8 +313,8 @@
   
               boolean changes = false;
   
  -            log("Replacing pattern '" + regex.getPattern(project) + 
  -                "' with '" + subs.getExpression(project) +
  +            log("Replacing pattern '" + regex.getPattern(getProject()) +
  +                "' with '" + subs.getExpression(getProject()) +
                   "' in '" + f.getPath() + "'" +
                   (byline ? " by line" : "") +
                   (flags.length() > 0 ? " with flags: '" + flags + "'" : "") +
  @@ -323,18 +322,77 @@
                   Project.MSG_VERBOSE);
   
               if (byline) {
  -                LineNumberReader lnr = new LineNumberReader(br);
  +                StringBuffer linebuf = new StringBuffer();
                   String line = null;
  -
  -                while ((line = lnr.readLine()) != null) {
  -                    String res = doReplace(regex, subs, line, options);
  -
  -                    if (!res.equals(line)) {
  -                        changes = true;
  +                String res = null;
  +                int c;
  +                boolean hasCR = false;
  +
  +                do {
  +                    c = br.read();
  +
  +                    if (c == '\r') {
  +                        if (hasCR) {
  +                            // second CR -> EOL + possibly empty line
  +                            line = linebuf.toString();
  +                            res  = doReplace(regex, subs, line, options);
  +
  +                            if (!res.equals(line)) {
  +                                changes = true;
  +                            }
  +
  +                            pw.print(res);
  +                            pw.print('\r');
  +
  +                            linebuf.setLength(0);
  +                            // hasCR is still true (for the second one)
  +                        } else {
  +                            // first CR in this line
  +                            hasCR = true;
  +                        }
                       }
  +                    else if (c == '\n') {
  +                        // LF -> EOL
  +                        line = linebuf.toString();
  +                        res  = doReplace(regex, subs, line, options);
  +
  +                        if (!res.equals(line)) {
  +                            changes = true;
  +                        }
  +
  +                        pw.print(res);
  +                        if (hasCR) {
  +                            pw.print('\r');
  +                            hasCR = false;
  +                        }
  +                        pw.print('\n');
  +
  +                        linebuf.setLength(0);
  +                    } else { // any other char
  +                        if ((hasCR) || (c < 0)) {
  +                            // Mac-style linebreak or EOF (or both)
  +                            line = linebuf.toString();
  +                            res  = doReplace(regex, subs, line, options);
  +
  +                            if (!res.equals(line)) {
  +                                changes = true;
  +                            }
  +
  +                            pw.print(res);
  +                            if (hasCR) {
  +                                pw.print('\r');
  +                                hasCR = false;
  +                            }
  +
  +                            linebuf.setLength(0);
  +                        }
  +
  +                        if (c >= 0) {
  +                            linebuf.append((char) c);
  +                        }
  +                    }
  +                } while (c >= 0);
   
  -                    pw.println(res);
  -                }
                   pw.flush();
               } else {
                   int flen = (int) f.length();
  @@ -355,7 +413,7 @@
                       changes = true;
                   }
   
  -                pw.println(res);
  +                pw.print(res);
                   pw.flush();
               }
   
  
  
  
  No                   revision
  
  
  No                   revision
  
  
  1.2.2.1   +20 -2     jakarta-ant/src/testcases/org/apache/tools/ant/taskdefs/optional/ReplaceRegExpTest.java
  
  Index: ReplaceRegExpTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/testcases/org/apache/tools/ant/taskdefs/optional/ReplaceRegExpTest.java,v
  retrieving revision 1.2
  retrieving revision 1.2.2.1
  diff -u -r1.2 -r1.2.2.1
  --- ReplaceRegExpTest.java	14 Nov 2001 12:25:31 -0000	1.2
  +++ ReplaceRegExpTest.java	23 Sep 2002 10:58:17 -0000	1.2.2.1
  @@ -1,7 +1,7 @@
   /*
    *  The Apache Software License, Version 1.1
    *
  - *  Copyright (c) 2001 The Apache Software Foundation.  All rights
  + *  Copyright (c) 2001-2002 The Apache Software Foundation.  All rights
    *  reserved.
    *
    *  Redistribution and use in source and binary forms, with or without
  @@ -54,8 +54,10 @@
   package org.apache.tools.ant.taskdefs.optional;
   
   import org.apache.tools.ant.BuildFileTest;
  +import org.apache.tools.ant.util.FileUtils;
   
   import java.util.Properties;
  +import java.io.File;
   import java.io.FileInputStream;
   import java.io.IOException;
   
  @@ -109,6 +111,22 @@
   
           assertNull(after.get("OldAbc"));
           assertEquals("AbcDef", after.get("NewProp"));
  +    }
  +
  +    public void testDontAddNewline1() throws IOException {
  +        executeTarget("testDontAddNewline1");
  +        assertTrue("Files match",
  +                   FileUtils.newFileUtils()
  +                   .contentEquals(new File("src/etc/testcases/taskdefs/optional/test.properties"),
  +                                  new File("src/etc/testcases/taskdefs/optional/replaceregexp2.result.properties")));
  +    }
  +
  +    public void testDontAddNewline2() throws IOException {
  +        executeTarget("testDontAddNewline2");
  +        assertTrue("Files match",
  +                   FileUtils.newFileUtils()
  +                   .contentEquals(new File("src/etc/testcases/taskdefs/optional/test.properties"),
  +                                  new File("src/etc/testcases/taskdefs/optional/replaceregexp2.result.properties")));
       }
   
   }// ReplaceRegExpTest
  
  
  

--
To unsubscribe, e-mail:   <mailto:ant-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:ant-dev-help@jakarta.apache.org>


Mime
View raw message