You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ant.apache.org by bo...@locus.apache.org on 2000/11/16 18:05:42 UTC

cvs commit: jakarta-ant/src/testcases/org/apache/tools/ant/util GlobPatternMapperTest.java

bodewig     00/11/16 09:05:41

  Modified:    src/main/org/apache/tools/ant/taskdefs Javac.java
               src/main/org/apache/tools/ant/util SourceFileScanner.java
               src/testcases/org/apache/tools/ant ProjectTest.java
  Added:       src/main/org/apache/tools/ant/util GlobPatternMapper.java
               src/testcases/org/apache/tools/ant/util
                        GlobPatternMapperTest.java
  Log:
  New FileNameMapper that does wildcard replacements like DOS's
  ren *.foo *.bar. Use it in javac (more to come).
  
  Revision  Changes    Path
  1.57      +15 -36    jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Javac.java
  
  Index: Javac.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Javac.java,v
  retrieving revision 1.56
  retrieving revision 1.57
  diff -u -r1.56 -r1.57
  --- Javac.java	2000/10/27 14:59:26	1.56
  +++ Javac.java	2000/11/16 17:05:34	1.57
  @@ -58,6 +58,7 @@
   import org.apache.tools.ant.DirectoryScanner;
   import org.apache.tools.ant.Project;
   import org.apache.tools.ant.types.*;
  +import org.apache.tools.ant.util.*;
   
   import java.lang.reflect.Method;
   import java.lang.reflect.Constructor;
  @@ -89,6 +90,7 @@
    *
    * @author James Davidson <a href="mailto:duncan@x180.com">duncan@x180.com</a>
    * @author Robin Green <a href="mailto:greenrd@hotmail.com">greenrd@hotmail.com</a>
  + * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a> 
    */
   
   public class Javac extends MatchingTask {
  @@ -114,7 +116,7 @@
       private Path extdirs;
       private static String lSep = System.getProperty("line.separator");
   
  -    protected Vector compileList = new Vector();
  +    protected File[] compileList = new File[0];
   
       /**
        * Create a nested <src ...> element for multiple source path
  @@ -322,10 +324,10 @@
               }
           }
   
  -        if (compileList.size() > 0) {
  -            log("Compiling " + compileList.size() + 
  +        if (compileList.length > 0) {
  +            log("Compiling " + compileList.length + 
                   " source file"
  -                + (compileList.size() == 1 ? "" : "s")
  +                + (compileList.length == 1 ? "" : "s")
                   + (destDir != null ? " to " + destDir : ""));
   
               if (compiler.equalsIgnoreCase("classic")) {
  @@ -347,7 +349,7 @@
        * Clear the list of files to be compiled and copied.. 
        */
       protected void resetFileLists() {
  -        compileList.removeAllElements();
  +        compileList = new File[0];
       }
   
       /**
  @@ -356,33 +358,11 @@
        */
   
       protected void scanDir(File srcDir, File destDir, String files[]) {
  -
  -        long now = (new Date()).getTime();
  -
  -        for (int i = 0; i < files.length; i++) {
  -            File srcFile = new File(srcDir, files[i]);
  -            if (files[i].endsWith(".java")) {
  -                File classFile = new File(destDir, files[i].substring(0,
  -                                          files[i].indexOf(".java")) + ".class");
  -
  -                if (srcFile.lastModified() > now) {
  -                    log("Warning: file modified in the future: " +
  -                        files[i], Project.MSG_WARN);
  -                }
  -
  -                if (!classFile.exists() || srcFile.lastModified() > classFile.lastModified()) {
  -                    if (!classFile.exists()) {
  -                        log("Compiling " + srcFile.getPath() + " because class file " 
  -                                + classFile.getPath() + " does not exist", Project.MSG_DEBUG);
  -                    }
  -                    else {
  -                        log("Compiling " + srcFile.getPath() + " because it is out of date with respect to " 
  -                                + classFile.getPath(), Project.MSG_DEBUG);
  -                    }                                                        
  -                    compileList.addElement(srcFile.getAbsolutePath());
  -                }
  -            }
  -        }
  +        GlobPatternMapper m = new GlobPatternMapper();
  +        m.setFrom("*.java");
  +        m.setTo("*.class");
  +        SourceFileScanner sfs = new SourceFileScanner(this);
  +        compileList = sfs.restrictAsFiles(files, srcDir, destDir, m);
       }
   
       // XXX
  @@ -619,16 +599,15 @@
               Project.MSG_VERBOSE);
   
           StringBuffer niceSourceList = new StringBuffer("File");
  -        if (compileList.size() != 1) {
  +        if (compileList.length != 1) {
               niceSourceList.append("s");
           }
           niceSourceList.append(" to be compiled:");
   
           niceSourceList.append(lSep);
   
  -        Enumeration enum = compileList.elements();
  -        while (enum.hasMoreElements()) {
  -            String arg = (String)enum.nextElement();
  +        for (int i=0; i < compileList.length; i++) {
  +            String arg = compileList[i].getAbsolutePath();
               cmd.createArgument().setValue(arg);
               niceSourceList.append("    " + arg + lSep);
           }
  
  
  
  1.4       +1 -1      jakarta-ant/src/main/org/apache/tools/ant/util/SourceFileScanner.java
  
  Index: SourceFileScanner.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/util/SourceFileScanner.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- SourceFileScanner.java	2000/11/16 09:57:47	1.3
  +++ SourceFileScanner.java	2000/11/16 17:05:40	1.4
  @@ -160,7 +160,7 @@
        * absolute).
        */
       public File[] restrictAsFiles(String[] files, File srcDir, File destDir,
  -                             FileNameMapper mapper) {
  +                                  FileNameMapper mapper) {
           String[] res = restrict(files, srcDir, destDir, mapper);
           File[] result = new File[res.length];
           for (int i=0; i<res.length; i++) {
  
  
  
  1.1                  jakarta-ant/src/main/org/apache/tools/ant/util/GlobPatternMapper.java
  
  Index: GlobPatternMapper.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2000 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
   * <http://www.apache.org/>.
   */
  
  package org.apache.tools.ant.util;
  
  /**
   * Implementation of FileNameMapper that does simple wildcard pattern
   * replacements.
   *
   * <p>This does simple translations like *.foo -> *.bar where the
   * prefix to .foo will be left unchanged. It only handles a single *
   * character, use regular expressions for more complicated
   * situations.</p>
   *
   * <p>This is one of the more useful Mappers, it is used by javac for
   * example.</p>
   *
   * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a>
   */
  public class GlobPatternMapper implements FileNameMapper {
      /**
       * Part of &quot;from&quot; pattern before the *.
       */
      protected String fromPrefix = null;
  
      /**
       * Part of &quot;from&quot; pattern after the *.
       */
      protected String fromPostfix = null;
      
      /**
       * Length of the prefix (&quot;from&quot; pattern).
       */
      protected int prefixLength;
  
      /**
       * Length of the postfix (&quot;from&quot; pattern).
       */
      protected int postfixLength;
  
      /**
       * Part of &quot;to&quot; pattern before the *.
       */
      protected String toPrefix = null;
  
      /**
       * Part of &quot;to&quot; pattern after the *.
       */
      protected String toPostfix = null;
      
      /**
       * Sets the &quot;from&quot; pattern. Required.
       */
      public void setFrom(String from) {
          int index = from.lastIndexOf("*");
          if (index == -1) {
              fromPrefix = from;
              fromPostfix = "";
          } else {
              fromPrefix = from.substring(0, index);
              fromPostfix = from.substring(index+1);
          }
          prefixLength = fromPrefix.length();
          postfixLength = fromPostfix.length();
      }
  
      /**
       * Sets the &quot;to&quot; pattern. Required.
       */
      public void setTo(String to) {
          int index = to.lastIndexOf("*");
          if (index == -1) {
              toPrefix = to;
              toPostfix = "";
          } else {
              toPrefix = to.substring(0, index);
              toPostfix = to.substring(index+1);
          }
      }
  
      /**
       * Returns null if the source file name doesn't match the
       * &quot;from&quot; pattern, an one-element array containing the
       * translated file otherwise.
       */
      public String[] mapFileName(String sourceFileName) {
          if (fromPrefix == null 
              || !sourceFileName.startsWith(fromPrefix) 
              || !sourceFileName.endsWith(fromPostfix)) {
              return null;
          }
          return new String[] {toPrefix 
                                   + extractVariablePart(sourceFileName)
                                   + toPostfix};
      }
  
      /**
       * Returns the part of the given string that matches the * in the
       * &quot;from&quot; pattern.
       */
      protected String extractVariablePart(String name) {
          return name.substring(prefixLength,
                                name.length() - postfixLength);
      }
  }
  
  
  
  1.2       +1 -1      jakarta-ant/src/testcases/org/apache/tools/ant/ProjectTest.java
  
  Index: ProjectTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/testcases/org/apache/tools/ant/ProjectTest.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ProjectTest.java	2000/09/04 12:20:12	1.1
  +++ ProjectTest.java	2000/11/16 17:05:40	1.2
  @@ -63,7 +63,7 @@
   /**
    * Very limited test class for Project. Waiting to be extended.
    *
  - * @author Stefan Bodewig <a href="mailto:stefan.bodewig@megabit.net">stefan.bodewig@megabit.net</a> 
  + * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a> 
    */
   public class ProjectTest extends TestCase {
   
  
  
  
  1.1                  jakarta-ant/src/testcases/org/apache/tools/ant/util/GlobPatternMapperTest.java
  
  Index: GlobPatternMapperTest.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2000 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
   * <http://www.apache.org/>.
   */
  
  package org.apache.tools.ant.util;
  
  import junit.framework.Test;
  import junit.framework.TestCase;
  import junit.framework.TestSuite;
  
  /**
   * Tests for org.apache.tools.ant.util;GlobPatternMapper.
   *
   * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a> 
   */
  public class GlobPatternMapperTest extends TestCase {
  
      public GlobPatternMapperTest(String name) {
          super(name);
      }
  
      public void testNoPatternAtAll() {
          GlobPatternMapper m = new GlobPatternMapper();
          m.setFrom("foobar");
          m.setTo("baz");
          assertNull("Shouldn\'t match foobar", m.mapFileName("plonk"));
          String[] result = m.mapFileName("foobar");
          assertNotNull("Should match foobar", result);
          assertEquals("only one result for foobar", 1, result.length);
          assertEquals("baz", result[0]);
      }
  
      public void testPostfixOnly() {
          GlobPatternMapper m = new GlobPatternMapper();
          m.setFrom("*foo");
          m.setTo("*plonk");
          assertNull("Shouldn\'t match *foo", m.mapFileName("bar.baz"));
          String[] result = m.mapFileName("bar.foo");
          assertNotNull("Should match *.foo", result);
          assertEquals("only one result for bar.foo", 1, result.length);
          assertEquals("bar.plonk", result[0]);
  
          // Try a silly case
          m.setTo("foo*");
          result = m.mapFileName("bar.foo");
          assertEquals("foobar.", result[0]);
      }
  
      public void testPrefixOnly() {
          GlobPatternMapper m = new GlobPatternMapper();
          m.setFrom("foo*");
          m.setTo("plonk*");
          assertNull("Shouldn\'t match foo*", m.mapFileName("bar.baz"));
          String[] result = m.mapFileName("foo.bar");
          assertNotNull("Should match foo*", result);
          assertEquals("only one result for foo.bar", 1, result.length);
          assertEquals("plonk.bar", result[0]);
  
          // Try a silly case
          m.setTo("*foo");
          result = m.mapFileName("foo.bar");
          assertEquals(".barfoo", result[0]);
      }
  
      public void testPreAndPostfix() {
          GlobPatternMapper m = new GlobPatternMapper();
          m.setFrom("foo*bar");
          m.setTo("plonk*pling");
          assertNull("Shouldn\'t match foo*bar", m.mapFileName("bar.baz"));
          String[] result = m.mapFileName("foo.bar");
          assertNotNull("Should match foo*bar", result);
          assertEquals("only one result for foo.bar", 1, result.length);
          assertEquals("plonk.pling", result[0]);
  
          // and a little longer
          result = m.mapFileName("foo.baz.bar");
          assertNotNull("Should match foo*bar", result);
          assertEquals("only one result for foo.baz.bar", 1, result.length);
          assertEquals("plonk.baz.pling", result[0]);
  
          // and a little shorter
          result = m.mapFileName("foobar");
          assertNotNull("Should match foo*bar", result);
          assertEquals("only one result for foobar", 1, result.length);
          assertEquals("plonkpling", result[0]);
      }
  }