commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bay...@apache.org
Subject cvs commit: jakarta-commons/io/xdocs tasks.xml
Date Fri, 17 Sep 2004 00:14:49 GMT
bayard      2004/09/16 17:14:49

  Modified:    io/xdocs tasks.xml
  Added:       io/src/java/org/apache/commons/io/find FileFinder.java
                        FindEvent.java FindListener.java Finder.java
                        FinderException.java FindingFilter.java
                        WildcardUtils.java
               io/src/test/find-data/can_read file
               io/src/test/find-data/can_write file
               io/src/test/find-data/empty file
               io/src/test/find-data/name file
               io/src/test/find-data/path/dir file
               io/src/test/find-data/regex file.1 file.2 file.3
               io/src/test/find-data/size file
               io/src/test/find-data/type file
               io/src/test/find-data/type/dir file
               io/src/test/org/apache/commons/io/find FileFinderTest.java
                        WildcardUtilsTest.java
  Log:
  added finding subsystem to IO along with WildcardUtils
  
  Revision  Changes    Path
  1.1                  jakarta-commons/io/src/java/org/apache/commons/io/find/FileFinder.java
  
  Index: FileFinder.java
  ===================================================================
  /*
   * Copyright 2001-2004 The Apache Software Foundation.
   * 
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   * 
   *      http://www.apache.org/licenses/LICENSE-2.0
   * 
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.commons.io.find;
  
  import java.io.File;
  import java.util.List;
  import java.util.LinkedList;
  import java.util.Iterator;
  import java.util.Map;
  
  /**
   * Finds Files in a file system.
   *
   * Informs FindListeners whenever a Find is made, and returns the 
   * finds to the user.
   */
  public class FileFinder implements Finder {
  
      // helper methods to handle options in String->whatever
      private static int toInt(Object obj) {
          if(obj == null) {
              return 0;
          } else
          if(obj instanceof Number) {
              return ((Number)obj).intValue();
          } else {
              String str = obj.toString();
              try {
                  return Integer.parseInt(str.toString());
              } catch(NumberFormatException nfe) {
                  throw new IllegalArgumentException("String argument "+str+" must be parseable
as an integer.  ");
              }
          }
      }
      private static boolean toBoolean(Object obj) {
          if(obj == null) {
              return false;
          } else
          if(obj instanceof Boolean) {
              return ((Boolean)obj).booleanValue();
          } else
          if(obj instanceof Number) {
              return ((Number)obj).intValue() != 0;
          } else {
              String str = obj.toString();
              return new Boolean(str).booleanValue();
          }
      }
  
      private List findListeners;
  
      /**
       * Find all files in the specified directory.
       */
      public File[] find(File directory) {
          return find(directory, new java.util.HashMap());
      }
  
      // add maxdepth and mindepth somehow
      public File[] find(File directory, Map options) {
          notifyDirectoryStarted(directory);
  
          boolean depthFirst = toBoolean(options.get(Finder.DEPTH));
  
          // to implement
          int maxDepth = toInt(options.get(Finder.MAXDEPTH));
          int minDepth = toInt(options.get(Finder.MINDEPTH));
          boolean ignoreHiddenDirs = toBoolean(options.get(Finder.IGNORE_HIDDEN_DIRS));
  
          FindingFilter filter = new FindingFilter(options);
          List list = find(directory, filter, depthFirst);
          if(filter.accept(directory)) {
              if(depthFirst) {
                  list.add( directory );
              } else {
                  list.add( 0, directory );
              }
          }
          File[] files = (File[]) list.toArray(new File[0]);
          notifyDirectoryFinished(directory, files);
          return files;
      }
  
      private List find(File directory, FindingFilter filter, boolean depthFirst) {
  
          // we can't use listFiles(filter) here, directories don't work correctly
          File[] list = directory.listFiles();
  
          if (list == null) {
              return null;
          }
  
          List retlist = new LinkedList();
          int sz = list.length;
  
          for (int i = 0; i < sz; i++) {
              File tmp = list[i];
              if(!depthFirst && filter.accept(tmp)) {
                  retlist.add(tmp);
                  notifyFileFound(directory,tmp);
              }
              if (tmp.isDirectory()) {
                  notifyDirectoryStarted(tmp);
                  List sublist = find(tmp, filter, depthFirst);
                  int subsz = sublist.size();
                  for (int j = 0; j < subsz; j++) {
                      retlist.add(sublist.get(j));
                  }
                  notifyDirectoryFinished(tmp, (File[]) sublist.toArray(new File[0]));
              }
              if(depthFirst && filter.accept(tmp)) {
                  retlist.add(tmp);
                  notifyFileFound(directory,tmp);
              }
          }
  
          return retlist;
      }
      
      /**
       * Add a FindListener.
       */
      public void addFindListener(FindListener fl) {
          if(findListeners == null) {
              findListeners = new LinkedList();
          }
          findListeners.add(fl);
      }
  
      /**
       * Remove a FindListener.
       */
      public void removeFindListener(FindListener fl) {
          if(findListeners != null) {
              findListeners.remove(fl);
          }
      }
  
      /**
       * Notify all FindListeners that a directory is being started.
       */
      public void notifyDirectoryStarted(File directory) {
          if(!directory.isDirectory()) {
              return;
          }
          if(findListeners != null) {
              FindEvent fe = new FindEvent(this,"directoryStarted",directory);
              Iterator itr = findListeners.iterator();
              while(itr.hasNext()) {
                  FindListener findListener = (FindListener)itr.next();
                  findListener.directoryStarted( fe );
              }
          }
      }
  
      /**
       * Notify all FindListeners that a directory has been finished.
       * Supplying the filenames that have been found.
       */
      public void notifyDirectoryFinished(File directory, File[] files) {
          if(!directory.isDirectory()) {
              return;
          }
          if(findListeners != null) {
              FindEvent fe = new FindEvent(this,"directoryFinished",directory,files);
              Iterator itr = findListeners.iterator();
              while(itr.hasNext()) {
                  FindListener findListener = (FindListener)itr.next();
                  findListener.directoryFinished( fe );
              }
          }
      }
  
      /**
       * Notify FindListeners that a file has been found.
       */
      public void notifyFileFound(File directory, File file) {
          if(file.isDirectory()) {
              return;
          }
          if(findListeners != null) {
              FindEvent fe = new FindEvent(this,"fileFound",directory,file);
              Iterator itr = findListeners.iterator();
              while(itr.hasNext()) {
                  FindListener findListener = (FindListener)itr.next();
                  findListener.fileFound( fe );
              }
          }
      }
      
  }
  
  
  
  1.1                  jakarta-commons/io/src/java/org/apache/commons/io/find/FindEvent.java
  
  Index: FindEvent.java
  ===================================================================
  /*
   * Copyright 2001-2004 The Apache Software Foundation.
   * 
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   * 
   *      http://www.apache.org/licenses/LICENSE-2.0
   * 
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.commons.io.find;
  
  import java.io.File;
  
  /**
   * Event upon which notification is made to a FindListener.
   * It contains references to the Finder and the Directory in which 
   * the event occured.
   * Depending on the particular occasion, it may also contain 
   * a set of files or a file.
   */
  public class FindEvent {
  
      private File directory;
      private Finder finder;
      private File file;
      private File[] files;
      private String type;
  
      public FindEvent(Finder finder, String type, File directory) {
          this.finder = finder;
          this.directory = directory;
          this.type = type;
      }
      
      public FindEvent(Finder finder, String type, File directory, File file) {
          this.finder = finder;
          this.directory = directory;
          this.file = file;
          this.type = type;
      }
      
      public FindEvent(Finder finder, String type, File directory, File[] files) {
          this.finder = finder;
          this.directory = directory;
          this.files = files;
          this.type = type;
      }
  
      public File getDirectory() {
          return this.directory;
      }
      
      public Finder getFinder() {
          return this.finder;
      }
      
      /**
       * File found.
       */
      public File getFile() {
          return this.file;
      }
      
      /**
       * Files found in a directory.
       */
      public File[] getFiles() {
          return this.files;
      }
  
      public String getType() {
          return this.type;
      }
  
      public String toString() {
          String str = "FindEvent - "+this.type+"; dir="+this.directory+", file="+this.file;
          if(this.files != null) {
              str += ", files="+java.util.Arrays.asList(this.files);
          }
          return str;
      }
  
  }
  
  
  
  1.1                  jakarta-commons/io/src/java/org/apache/commons/io/find/FindListener.java
  
  Index: FindListener.java
  ===================================================================
  /*
   * Copyright 2001-2004 The Apache Software Foundation.
   * 
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   * 
   *      http://www.apache.org/licenses/LICENSE-2.0
   * 
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.commons.io.find;
  
  public interface FindListener {
  
      /**
       * A directory has begun to be looked at by a Finder.
       */
      public void directoryStarted(FindEvent findEvent);
  
      /**
       * A directory has been finished. The FindEvent will 
       * contain an array of the filenames found.
       */
      public void directoryFinished(FindEvent findEvent);
  
      /**
       * A file has been found. The FindEvent will contain the 
       * filename found.
       */
      public void fileFound(FindEvent findEvent);
  //    public void entryFound(FindEvent findEvent);
  
  }
  
  
  
  1.1                  jakarta-commons/io/src/java/org/apache/commons/io/find/Finder.java
  
  Index: Finder.java
  ===================================================================
  /*
   * Copyright 2001-2004 The Apache Software Foundation.
   * 
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   * 
   *      http://www.apache.org/licenses/LICENSE-2.0
   * 
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.commons.io.find;
  
  import java.io.File;
  import java.util.Map;
  
  /**
   * A Finder of Files. Though the structure the files are in is 
   * unspecified.
   */
  public interface Finder {
  
      // MODIFIER
      public static final String NOT = "NOT_";
  
      // OPTIONS
      public static final String DAYSTART = "DAYSTART";
      public static final String DEPTH = "DEPTH";
      public static final String MAXDEPTH = "MAXDEPTH";
      public static final String MINDEPTH = "MINDEPTH";
      public static final String IGNORE_HIDDEN_DIRS = "IGNORE_HIDDEN_DIRS";
  
      // Time based tests
      public static final String MIN = "MIN";
      public static final String NEWER = "NEWER";
      public static final String TIME = "TIME";
  
      // size based tests
      public static final String EMPTY = "EMPTY";
      public static final String SIZE = "SIZE";
  
      // name based tests
      public static final String NAME = "NAME";
      public static final String INAME = "INAME";
      public static final String PATH = "PATH";
      public static final String IPATH = "IPATH";
      public static final String REGEX = "REGEX";
      public static final String IREGEX = "IREGEX";
  
      // type of file
      public static final String TYPE = "TYPE";      // supports 'd' and 'f'
      public static final String HIDDEN = "HIDDEN";
  
      // permission replacements
      public static final String CAN_READ = "CAN_READ";
      public static final String CAN_WRITE = "CAN_WRITE";
  
      public void addFindListener(FindListener fl);
      public void removeFindListener(FindListener fl);
  
      public File[] find(File root);
      public File[] find(File root, Map options);
  
  }
  
  
  
  1.1                  jakarta-commons/io/src/java/org/apache/commons/io/find/FinderException.java
  
  Index: FinderException.java
  ===================================================================
  /*
   * Copyright 2001-2004 The Apache Software Foundation.
   * 
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   * 
   *      http://www.apache.org/licenses/LICENSE-2.0
   * 
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  package org.apache.commons.io.find;
  
  /**
   * Something went wrong in the finding.
   */
  public class FinderException extends RuntimeException {
  
      public FinderException() {
          super();
      }
  
      public FinderException(String msg) {
          super(msg);
      }
  
      public FinderException(Throwable t) {
          super(t);
      }
  
      public FinderException(String msg, Throwable t) {
          super(msg,t);
      }
  
  }
  
  
  
  1.1                  jakarta-commons/io/src/java/org/apache/commons/io/find/FindingFilter.java
  
  Index: FindingFilter.java
  ===================================================================
  /*
   * Copyright 2001-2004 The Apache Software Foundation.
   * 
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   * 
   *      http://www.apache.org/licenses/LICENSE-2.0
   * 
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.commons.io.find;
  
  import java.util.Collection;
  import java.io.File;
  import java.io.FileFilter;
  import java.util.Map;
  import java.util.List;
  import java.util.LinkedList;
  import java.util.Iterator;
  import java.util.regex.Pattern;
  import java.util.regex.Matcher;
  
  /**
   * This is where most of the find functionality occurs. Nearly every option 
   * to find is mapped to a FileFilter, which are then chained together inside 
   * this class. 
   */
  public class FindingFilter implements FileFilter {
  
      private Map options;
      private List filters = new LinkedList();
      private boolean daystart;
  
      public FindingFilter(Map options) {
          this.options = options;
          Collection entries = options.entrySet();
          Iterator itr = entries.iterator();
          while(itr.hasNext()) {
              Map.Entry entry = (Map.Entry)itr.next();
              if( entry.getKey().equals(Finder.DAYSTART) ) {
                  this.daystart = true; 
                  continue;
              }
              // knows that the key is a String
              filters.add( createFilter(entry.getKey().toString(), entry.getValue()) );
          }
      }
  
      private FileFilter createFilter(String option, Object argument) {
  
          boolean invert = false;
          if( option.startsWith(Finder.NOT) ) {
              invert = true;
              // knows that option is a String. Bad. Needs an Enum?
              option = option.substring(Finder.NOT.length());
          }
          if( option.equals(Finder.MIN) ) {
              return new MinFilter(option, argument, invert, this);
          }
          if( option.equals(Finder.NEWER) ) {
              return new NewerFilter(option, argument, invert);
          }
          if( option.equals(Finder.TIME) ) {
              return new TimeFilter(option, argument, invert, this);
          }
          if( option.equals(Finder.EMPTY) ) {
              return new EmptyFilter(option, argument, invert);
          }
          if( option.equals(Finder.SIZE) ) {
              return new SizeFilter(option, argument, invert);
          }
          if( option.equals(Finder.NAME) ) {
              return new NameFilter(option, argument, invert, false);
          }
          if( option.equals(Finder.INAME) ) {
              return new NameFilter(option, argument, invert, true);
          }
          if( option.equals(Finder.PATH) ) {
              return new PathFilter(option, argument, invert, false);
          }
          if( option.equals(Finder.IPATH) ) {
              return new PathFilter(option, argument, invert, true);
          }
          if( option.equals(Finder.REGEX) ) {
              return new RegexFilter(option, argument, invert, false);
          }
          if( option.equals(Finder.IREGEX) ) {
              return new RegexFilter(option, argument, invert, true);
          }
          if( option.equals(Finder.TYPE) ) {
              return new TypeFilter(option, argument, invert);
          }
          if( option.equals(Finder.HIDDEN) ) {
              return new HiddenFilter(option, argument, invert);
          }
          if( option.equals(Finder.CAN_READ) ) {
              return new CanReadFilter(option, argument, invert);
          }
          if( option.equals(Finder.CAN_WRITE) ) {
              return new CanWriteFilter(option, argument, invert);
          }
          return null;
      }
  
      public boolean accept(File file) {
          Iterator itr = filters.iterator();
          while(itr.hasNext()) {
              FileFilter filter = (FileFilter) itr.next();
              if(filter == null) {
                  continue;
              }
              boolean result = filter.accept(file);
              if(result == false) {
                  return false;
              }
          }
          return true;
      }
  
      public boolean isDaystartConfigured() {
          return this.daystart;
      }
  
      // helper method to make the inverting easier.
      // possibly the Filters should be inner classes.
      // possibly there should be an abstract FindFilter class.
      static boolean invert(boolean invert, boolean answer) {
          if(invert) {
              answer = !answer;
          }
          return answer;
      }
  
  }
  
      // need to implement the daystart bits
      class MinFilter implements FileFilter {
          private Object option;
          private boolean invert;
          private int argument;
          private FindingFilter parent;
          public MinFilter(Object option, Object argument, boolean invert, FindingFilter parent)
{
              this.option = option;
              this.invert = invert;
              try {
                  this.argument = Integer.parseInt(argument.toString());
              } catch(NumberFormatException nfe) {
                  throw new IllegalArgumentException("Argument "+argument+" must be an integer.
 ");
              }
              this.parent = parent;
          }
          public boolean accept(File file) {
              boolean daystart = this.parent.isDaystartConfigured();
              return FindingFilter.invert( this.invert,  file.lastModified() > System.currentTimeMillis()
- this.argument * 60000 );
          }
      }
  
      class NewerFilter implements FileFilter {
          private Object option;
          private boolean invert;
          private File argument;
          public NewerFilter(Object option, Object argument, boolean invert) {
              this.option = option;
              this.invert = invert;
              this.argument = new File(argument.toString());
          }
          public boolean accept(File file) {
              return FindingFilter.invert( this.invert,  file.lastModified() > this.argument.lastModified()
);
          }
      }
  
      // implement daystart
      class TimeFilter implements FileFilter {
          private Object option;
          private boolean invert;
          private int argument;
          private FindingFilter parent;
          public TimeFilter(Object option, Object argument, boolean invert, FindingFilter
parent) {
              this.option = option;
              this.invert = invert;
              try {
                  this.argument = Integer.parseInt(argument.toString());
              } catch(NumberFormatException nfe) {
                  throw new IllegalArgumentException("Argument "+argument+" must be an integer.
 ");
              }
              this.parent = parent;
          }
          public boolean accept(File file) {
              boolean daystart = this.parent.isDaystartConfigured();
              return FindingFilter.invert( this.invert,  file.lastModified() > System.currentTimeMillis()
- this.argument * 60000*60*24 );
          }
      }
  
      class EmptyFilter implements FileFilter {
          private Object option;
          private boolean invert;
          private boolean argument;
          public EmptyFilter(Object option, Object argument, boolean invert) {
              this.option = option;
              this.invert = invert;
              this.argument = new Boolean(argument.toString()).booleanValue();
          }
          public boolean accept(File file) {
              return FindingFilter.invert( this.invert,  (file.length() == 0) == this.argument
);
          }
      }
  
      // needs to handle +5 for > 5 and -5 for < 5. Also needs 
      // to handle k, m, g, as suffixes.
      class SizeFilter implements FileFilter {
          private Object option;
          private boolean invert;
          private int argument;
          public SizeFilter(Object option, Object argument, boolean invert) {
              this.option = option;
              this.invert = invert;
              try {
                  this.argument = Integer.parseInt(argument.toString());
              } catch(NumberFormatException nfe) {
                  throw new IllegalArgumentException("Argument "+argument+" must be an integer.
 ");
              }
          }
          public boolean accept(File file) {
              return FindingFilter.invert( this.invert,  (int)(file.length()/512) == this.argument
);
          }
      }
  
      class NameFilter implements FileFilter {
          private Object option;
          private boolean invert;
          private Object argument;
          private boolean ignoreCase;
          public NameFilter(Object option, Object argument, boolean invert, boolean ignoreCase)
{
              this.option = option;
              this.invert = invert;
              this.argument = argument;
              this.ignoreCase = ignoreCase;
          }
          public boolean accept(File file) {
              if(this.ignoreCase) {
                  return FindingFilter.invert( this.invert,  WildcardUtils.match(file.getName().toLowerCase(),
this.argument.toString().toLowerCase()) );
              } else {
                  return FindingFilter.invert( this.invert,  WildcardUtils.match(file.getName(),
this.argument.toString()) );
              }
          }
      }
  
      class PathFilter implements FileFilter {
          private Object option;
          private boolean invert;
          private Object argument;
          private boolean ignoreCase;
          public PathFilter(Object option, Object argument, boolean invert, boolean ignoreCase)
{
              this.option = option;
              this.invert = invert;
              this.argument = argument;
              this.ignoreCase = ignoreCase;
          }
          public boolean accept(File file) {
              if(this.ignoreCase) {
                  return FindingFilter.invert( this.invert,  WildcardUtils.match(file.getPath().toLowerCase(),
this.argument.toString().toLowerCase()) );
              } else {
                  return FindingFilter.invert( this.invert,  WildcardUtils.match(file.getPath(),
this.argument.toString()) );
              }
          }
      }
  
      class RegexFilter implements FileFilter {
          private Object option;
          private boolean invert;
          private Object argument;
          private boolean ignoreCase;
          public RegexFilter(Object option, Object argument, boolean invert, boolean ignoreCase)
{
              this.option = option;
              this.invert = invert;
              this.argument = argument;
              this.ignoreCase = ignoreCase;
          }
          public boolean accept(File file) {
              if(this.ignoreCase) {
                  Pattern pattern = Pattern.compile(this.argument.toString(), Pattern.CASE_INSENSITIVE);
                  Matcher matcher = pattern.matcher(file.getPath());
                  return FindingFilter.invert( this.invert,  matcher.matches() );
              } else {
                  return FindingFilter.invert( this.invert,  file.getPath().matches(this.argument.toString())
);
              }
          }
      }
  
      class TypeFilter implements FileFilter {
          private Object option;
          private boolean invert;
          private Object argument;
          public TypeFilter(Object option, Object argument, boolean invert) {
              this.option = option;
              this.invert = invert;
              if(!"d".equals(argument) && !"f".equals(argument)) {
                  throw new IllegalArgumentException("Type option must be 'f' or 'd'. ");
              }
              this.argument = argument;
          }
          public boolean accept(File file) {
              if("d".equals(argument)) {
                  return FindingFilter.invert( this.invert,  file.isDirectory() );
              } else
              if("f".equals(argument)) {
                  return FindingFilter.invert( this.invert,  !file.isDirectory() );
              } else {
                  throw new IllegalArgumentException("Type option must be 'f' or 'd'. ");
              }
          }
      }
  
      class HiddenFilter implements FileFilter {
          private Object option;
          private boolean invert;
          private boolean argument;
          public HiddenFilter(Object option, Object argument, boolean invert) {
              this.option = option;
              this.invert = invert;
              this.argument = new Boolean(argument.toString()).booleanValue();
          }
          public boolean accept(File file) {
              return FindingFilter.invert( this.invert,  file.isHidden() == this.argument
);
          }
      }
  
      class CanReadFilter implements FileFilter {
          private Object option;
          private boolean invert;
          private boolean argument;
          public CanReadFilter(Object option, Object argument, boolean invert) {
              this.option = option;
              this.invert = invert;
              this.argument = new Boolean(argument.toString()).booleanValue();
          }
          public boolean accept(File file) {
              return FindingFilter.invert( this.invert,  file.canRead() == this.argument );
          }
      }
  
      class CanWriteFilter implements FileFilter {
          private Object option;
          private boolean invert;
          private boolean argument;
          public CanWriteFilter(Object option, Object argument, boolean invert) {
              this.option = option;
              this.invert = invert;
              this.argument = new Boolean(argument.toString()).booleanValue();
          }
          public boolean accept(File file) {
              return FindingFilter.invert( this.invert,  file.canWrite() == this.argument
);
      }
  }
  
  
  
  
  1.1                  jakarta-commons/io/src/java/org/apache/commons/io/find/WildcardUtils.java
  
  Index: WildcardUtils.java
  ===================================================================
  /*
   * Copyright 2001-2004 The Apache Software Foundation.
   * 
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   * 
   *      http://www.apache.org/licenses/LICENSE-2.0
   * 
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.commons.io.find;
  
  import java.util.ArrayList;
  
  /**
   * An implementation of Wildcard logic, as seen on command lines 
   * on UNIX and DOS.
   */
  public class WildcardUtils {
  
      /**
       * See if a particular piece of text, often a filename, 
       * matches to a specified wildcard. 
       */
      public static boolean match(String text, String wildcard) {
          // split wildcard on ? and *
          // for each element of the array, find a matching block in text
          // earliest matching block counts
          String[] wcs = splitOnTokens(wildcard);
          int textIdx = 0;
          for(int i=0; i<wcs.length; i++) {
              if(textIdx == text.length()) {
                  if("*".equals(wcs[i])) {
                      return true;
                  }
                  return wcs[i].length() == 0;
              }
  
              if("?".equals(wcs[i])) {
                  textIdx++;
              } else
              if("*".equals(wcs[i])) {
                  int nextIdx = i+1;
                  if(nextIdx == wcs.length) {
                      return true;
                  }
                  int restartIdx = text.indexOf(wcs[nextIdx], textIdx);
                  if(restartIdx == -1) {
                      return false;
                  } else {
                      textIdx = restartIdx;
                  }
              } else {
                  if(!text.startsWith(wcs[i], textIdx)) {
                      return false;
                  } else {
                      textIdx += wcs[i].length();
                  }
              }
          }
  
          return true;
      }
  
      // package level so a unit test may run on this
      static String[] splitOnTokens(String text) {
          char[] array = text.toCharArray();
          if(text.indexOf("?") == -1 && text.indexOf("*") == -1) {
              return new String[] { text };
          }
  
          ArrayList list = new ArrayList();
          StringBuffer buffer = new StringBuffer();
          for(int i=0; i<array.length; i++) {
              if(array[i] == '?' || array[i] == '*') {
                  if(buffer.length() != 0) {
                     list.add(buffer.toString());
                     buffer.setLength(0);
                  }
                  list.add(new String( new char[] { array[i] } ));
              } else {
                  buffer.append(array[i]);
              }
          }
          if(buffer.length() != 0) {
              list.add(buffer.toString());
          }
  
          return (String[]) list.toArray(new String[0]);
      }
          
  
  }
  
  
  
  1.1                  jakarta-commons/io/src/test/find-data/can_read/file
  
  Index: file
  ===================================================================
  file
  
  
  
  1.1                  jakarta-commons/io/src/test/find-data/can_write/file
  
  Index: file
  ===================================================================
  file
  
  
  
  1.1                  jakarta-commons/io/src/test/find-data/empty/file
  
  	<<Binary file>>
  
  
  1.1                  jakarta-commons/io/src/test/find-data/name/file
  
  Index: file
  ===================================================================
  file
  
  
  
  1.1                  jakarta-commons/io/src/test/find-data/path/dir/file
  
  Index: file
  ===================================================================
  file
  
  
  
  1.1                  jakarta-commons/io/src/test/find-data/regex/file.1
  
  Index: file.1
  ===================================================================
  file.1
  
  
  
  1.1                  jakarta-commons/io/src/test/find-data/regex/file.2
  
  Index: file.2
  ===================================================================
  file.2
  
  
  
  1.1                  jakarta-commons/io/src/test/find-data/regex/file.3
  
  Index: file.3
  ===================================================================
  file.3
  
  
  
  1.1                  jakarta-commons/io/src/test/find-data/size/file
  
  Index: file
  ===================================================================
  1234567890
  1234567890
  1234567890
  1234567890
  1234567890
  1234567890
  1234567890
  1234567890
  1234567890
  1234567890
  1234567890
  1234567890
  1234567890
  1234567890
  1234567890
  1234567890
  1234567890
  1234567890
  1234567890
  1234567890
  1234567890
  1234567890
  1234567890
  1234567890
  1234567890
  1234567890
  1234567890
  1234567890
  1234567890
  1234567890
  1234567890
  1234567890
  1234567890
  1234567890
  1234567890
  1234567890
  1234567890
  1234567890
  1234567890
  1234567890
  1234567890
  1234567890
  1234567890
  1234567890
  1234567890
  1234567890
  12345
  
  
  
  1.1                  jakarta-commons/io/src/test/find-data/type/file
  
  Index: file
  ===================================================================
  file
  
  
  
  1.1                  jakarta-commons/io/src/test/find-data/type/dir/file
  
  Index: file
  ===================================================================
  file
  
  
  
  1.1                  jakarta-commons/io/src/test/org/apache/commons/io/find/FileFinderTest.java
  
  Index: FileFinderTest.java
  ===================================================================
  /*
   * Copyright 2001-2004 The Apache Software Foundation.
   * 
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   * 
   *      http://www.apache.org/licenses/LICENSE-2.0
   * 
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.commons.io.find;
  
  import junit.framework.Test;
  import junit.framework.TestCase;
  import junit.framework.TestSuite;
  import junit.textui.TestRunner;
  
  import java.io.IOException;
  import java.io.File;
  
  import java.util.HashMap;
  
  public class FileFinderTest extends TestCase {
  
      private HashMap options;
      private FileFinder finder;
      private String dirStr = "src/test/find-data/";
      private File dir = new File(dirStr);
  
      public FileFinderTest(String name) {
          super(name);
      }
  
      public void setUp() {
          finder = new FileFinder();
          options = new HashMap();
          options.put(Finder.NOT+Finder.PATH, "*.svn*");
          // uncomment for debugging
  //        finder.addFindListener( new DebugListener() );
      }
  
      //-----------------------------------------------------------------------
      // To test: 
      // find(File, Map)
  
      public void testFindName() {
          options.put(Finder.NAME, "file");
          File[] files = finder.find(new File(dir, "name"), options);
          assertEquals(1, files.length);
      }
  
      public void testFindIName() {
          options.put(Finder.INAME, "FiLe");
          File[] files = finder.find(new File(dir, "name"), options);
          assertEquals(1, files.length);
      }
  
      public void testFindPath() {
          options.put(Finder.PATH, dirStr+"path/dir/file");
          File[] files = finder.find(new File(dir, "path"), options);
          assertEquals(1, files.length);
      }
  
      public void testFindIPath() {
          options.put(Finder.IPATH, dirStr+"PAth/dIR/fILe");
          File[] files = finder.find(new File(dir, "path"), options);
          assertEquals(1, files.length);
      }
  
      public void testFindNotPath() {
          options.put(Finder.NOT+Finder.PATH, dirStr+"*");
          File[] files = finder.find(new File(dir, "path"), options);
          assertEquals(0, files.length);
      }
  
      public void testFindRegex() {
          options.put(Finder.REGEX, dirStr+"regex/f.*");
          File[] files = finder.find(new File(dir, "regex"), options);
          assertEquals(3, files.length);
      }
  
      public void testFindIRegex() {
          options.put(Finder.IREGEX, dirStr+"REgeX/F.*");
          File[] files = finder.find(new File(dir, "regex"), options);
          assertEquals(3, files.length);
      }
  
      public void testFindEmpty() {
          options.put(Finder.EMPTY, "true");
          File[] files = finder.find(new File(dir, "empty"), options);
          assertEquals(1, files.length);
      }
  
      public void testFindSize() {
          options.put(Finder.SIZE, "1");
          File[] files = finder.find(new File(dir, "size"), options);
          assertEquals(1, files.length);
      }
  
      public void testFindTypeF() {
          options.put(Finder.TYPE, "f");
          File[] files = finder.find(new File(dir, "type"), options);
          assertEquals(1, files.length);
      }
  
      public void testFindTypeD() {
          options.put(Finder.TYPE, "d");
          File[] files = finder.find(new File(dir, "type"), options);
          assertEquals(2, files.length);
      }
  
      public void testCanReadTrue() {
          options.put(Finder.CAN_WRITE, "true");
          File[] files = finder.find(new File(dir, "can_write"), options);
          assertEquals(2, files.length);
      }
  
      public void testCanWriteTrue() {
          options.put(Finder.CAN_READ, "true");
          File[] files = finder.find(new File(dir, "can_read"), options);
          assertEquals(2, files.length);
      }
  
      public void testCanReadFalse() {
          options.put(Finder.CAN_WRITE, "false");
          File[] files = finder.find(new File(dir, "can_write"), options);
          assertEquals(0, files.length);
      }
  
      public void testCanWriteFalse() {
          options.put(Finder.CAN_READ, "false");
          File[] files = finder.find(new File(dir, "can_read"), options);
          assertEquals(0, files.length);
      }
  
  }
  
  class DebugListener implements FindListener {
      public void fileFound(FindEvent fe) {
          System.out.println("EVENT: "+fe);
      }
      public void directoryStarted(FindEvent fe) {
          System.out.println("EVENT: "+fe);
      }
      public void directoryFinished(FindEvent fe) {
          System.out.println("EVENT: "+fe);
      }
  }
  
  
  
  1.1                  jakarta-commons/io/src/test/org/apache/commons/io/find/WildcardUtilsTest.java
  
  Index: WildcardUtilsTest.java
  ===================================================================
  /*
   * Copyright 2001-2004 The Apache Software Foundation.
   * 
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   * 
   *      http://www.apache.org/licenses/LICENSE-2.0
   * 
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.commons.io.find;
  
  import junit.framework.Test;
  import junit.framework.TestCase;
  import junit.framework.TestSuite;
  import junit.textui.TestRunner;
  
  public class WildcardUtilsTest extends TestCase {
  
      public WildcardUtilsTest(String name) {
          super(name);
      }
  
      //-----------------------------------------------------------------------
      // To test: 
      //   WildcardUtils.match(String,String)
  
      public void testMatch() {
          assertTrue( WildcardUtils.match("Foo", "Foo") );
          assertTrue( WildcardUtils.match("", "") );
          assertTrue( WildcardUtils.match("Foo", "Fo*") );
          assertTrue( WildcardUtils.match("Foo", "Fo?") );
          assertTrue( WildcardUtils.match("Foo Bar and Catflap", "Fo*") );
          assertTrue( WildcardUtils.match("New Bookmarks", "N?w ?o?k??r?s") );
          assertFalse( WildcardUtils.match("Foo", "Bar") );
          assertTrue( WildcardUtils.match("Foo Bar Foo", "F*o Bar*") );
          assertTrue( WildcardUtils.match("Adobe Acrobat Installer", "Ad*er") );
          assertTrue( WildcardUtils.match("Foo", "*Foo") );
          assertTrue( WildcardUtils.match("Foo", "Foo*") );
      }
  
      public void testSplitOnTokens() {
          assertArrayEquals( new String[] { "Ad", "*", "er" }, WildcardUtils.splitOnTokens("Ad*er")
);
          assertArrayEquals( new String[] { "Ad", "?", "er" }, WildcardUtils.splitOnTokens("Ad?er")
);
          assertArrayEquals( new String[] { "Test", "*", "?", "One" }, WildcardUtils.splitOnTokens("Test*?One")
);
          assertArrayEquals( new String[] { "*", "*", "*", "*" }, WildcardUtils.splitOnTokens("****")
);
          assertArrayEquals( new String[] { "*", "?", "?", "*" }, WildcardUtils.splitOnTokens("*??*")
);
          assertArrayEquals( new String[] { "*", "?", "?", "*" }, WildcardUtils.splitOnTokens("*??*")
);
          assertArrayEquals( new String[] { "h", "?", "?", "*" }, WildcardUtils.splitOnTokens("h??*")
);
          assertArrayEquals( new String[] { "" }, WildcardUtils.splitOnTokens("") );
      }
  
      private void assertArrayEquals(Object[] a1, Object[] a2) {
          assertEquals(a1.length, a2.length);
          for(int i=0; i<a1.length; i++) {
              assertEquals(a1[i], a2[i]);
          }
      }
  
  }
  
  
  
  1.28      +1 -2      jakarta-commons/io/xdocs/tasks.xml
  
  Index: tasks.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/io/xdocs/tasks.xml,v
  retrieving revision 1.27
  retrieving revision 1.28
  diff -u -r1.27 -r1.28
  --- tasks.xml	12 Mar 2004 22:34:05 -0000	1.27
  +++ tasks.xml	17 Sep 2004 00:14:49 -0000	1.28
  @@ -21,8 +21,7 @@
           <li>JoinReader/ConcatReader. One in GenJava, one submitted to Bayard</li>
           <li>FormattedWriter, when it writes out values it uses Format objects to
output them. </li>
           <li>FixedWidthReader. Reads in files with a known width, ie) mainframe like.
</li>
  -        <li>Finder system. Generic code to find things in things. So FileFinder,

  -            ClassFinder, ZipFinder. Probably too big for IO</li>
  +        <li>Finder system. Currently takes a Map for options; it needs to have the
ability to set methods on the Finder. </li>
           <li>Add an additional ByteArrayOutputStream variant that saves content
               in memory just as the original but starts to write to a temp file when
               a given number of bytes is surpassed. This is to reduce the memory
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message