"SfR Fresh" - the SfR Freeware/Shareware Archive

Member "wormscan-1.6.1-src/net/websoup/utility/FilenameResolver.java" of archive wormscan-1.6.1-src.tar.gz:


As a special service "SfR Fresh" has tried to format the requested source page into HTML format using source code syntax highlighting with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. That can be also achieved for any archive member file by clicking within an archive contents listing on the first character of the file(path) respectively on the according byte size field.
    1 package net.websoup.utility;
    2 
    3 /*
    4 *    FilenameResolver is free software; you can redistribute it and/or
    5 *    modify it under the terms of the GNU Library General Public License
    6 *    as published by the Free Software Foundation; either version 2
    7 *    of the License, or (at your option) any later version.
    8 *
    9 *    This program is distributed in the hope that it will be useful,
   10 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
   11 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   12 *    GNU Library General Public License for more details.
   13 *
   14 *    You should have received a copy of the GNU Library General Public License
   15 *    along with this program; if not, write to the Free Software
   16 *    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
   17 */
   18 
   19 import java.io.File;
   20 import java.io.FilenameFilter;
   21 import java.util.StringTokenizer;
   22 import java.util.Vector;
   23 
   24 import org.apache.oro.text.perl.Perl5Util;
   25 import org.apache.oro.text.regex.Perl5Matcher;
   26 import org.apache.oro.text.regex.Perl5Compiler;
   27 import org.apache.oro.text.regex.Pattern;
   28 import org.apache.oro.text.regex.MatchResult;
   29 import org.apache.oro.text.regex.Perl5Substitution;
   30 import org.apache.oro.text.regex.Util;
   31 
   32 /**
   33  * Resolves lists of filenames with wildcards into complete paths (absolute or relative)
   34  * Copyright:    Copyright (c) 2001 Andriy Rozeluk <arozeluk@websoup.net>
   35  * @author Andriy Rozeluk
   36  * @version 1.0
   37  */
   38 public class FilenameResolver {
   39   private static Perl5Compiler compiler;
   40   private static Perl5Substitution escapesSubstitution;
   41   private static Perl5Substitution enclosedSubstitution;
   42   private static Perl5Substitution wildcardSubstitution;
   43   private static Pattern escapesPattern;
   44   private static Pattern enclosedPattern;
   45   private static Pattern wildcardPattern;
   46 
   47   /**
   48    * Initialize patterns to convert filename and wildcards into valid RE expression
   49    */
   50   static {
   51     compiler = new Perl5Compiler();
   52     try {
   53       escapesPattern = compiler.compile( "([\\[\\]\\{\\}\\^\\$\\#]){1,1}" );
   54       escapesSubstitution = new Perl5Substitution( "\\$1" );
   55       enclosedPattern = compiler.compile( "([.+]){1,1}" );
   56       enclosedSubstitution = new Perl5Substitution( "[$1]" );
   57       wildcardPattern = compiler.compile( "([\\*|\\?]){1,1}" );
   58       wildcardSubstitution = new Perl5Substitution( ".$1" );
   59     }
   60     catch ( Exception e ){
   61       System.err.println( "FilenameResolver patterns are wrong: " + e.getMessage() );
   62     }
   63   }
   64 
   65   /**
   66    * Filter out files in a directory that match the wildcard
   67    */
   68   class FilenameResolver$Filter implements FilenameFilter {
   69     private Perl5Matcher matcher;
   70     private Pattern filepattern;
   71     private boolean patternOK = true;
   72 
   73     public FilenameResolver$Filter( String filename ) {
   74       matcher = new Perl5Matcher();
   75       String temp = Util.substitute( matcher, escapesPattern, escapesSubstitution, filename, Util.SUBSTITUTE_ALL );
   76       temp = Util.substitute( matcher, enclosedPattern, enclosedSubstitution, filename, Util.SUBSTITUTE_ALL );
   77       temp = Util.substitute( matcher, wildcardPattern, wildcardSubstitution, filename, Util.SUBSTITUTE_ALL );
   78 
   79 //      System.out.println( "FilenameResolver Filename: " + filename );
   80 //      System.out.println( "FilenameResolver Pattern: " + temp );
   81 
   82       try {
   83         filepattern = compiler.compile( temp );
   84       }
   85       catch( Exception e ){
   86         System.out.println( "Generated FilenameResolver pattern bad: " + e.getMessage() );
   87         patternOK = false;
   88       }
   89     }
   90 
   91     public boolean accept( File dir, String name ){
   92       if( !patternOK ) return false;
   93       return matcher.matches( name, filepattern );
   94     }
   95   }
   96 
   97   /**
   98    * Expand a path/filename with wildcards to valid files
   99    */
  100   private String[] solveWildcards( String path, String filename ){
  101     File file = new File( path );
  102     String[] potentialFiles = file.list( new FilenameResolver$Filter( filename ) );
  103     return potentialFiles;
  104   }
  105 
  106   /**
  107    * Given a string containing one or more filename paths, return an array of absolute
  108    * paths to all files that match. Filenames (though not directory names at this time) may
  109    * contain wildcard characters * and ?. Multiple filenames may be separated by a space.
  110    */
  111   public String[] resolve( String pathnames ){
  112     Vector matchingFilenames = new Vector();
  113     StringTokenizer st = new StringTokenizer( pathnames, " " );
  114     while( st.hasMoreTokens() ){
  115       String fulltoken = st.nextToken();
  116       String path, filename;
  117       int lastIndex = fulltoken.lastIndexOf( File.separator );
  118       if ( lastIndex == -1 ){
  119         path = "." + File.separator;
  120       }
  121       else {
  122         path = fulltoken.substring( 0, lastIndex + 1 );
  123       }
  124       filename = fulltoken.substring( lastIndex + 1 );
  125 
  126       String[] results = solveWildcards( path, filename );
  127       for( int i = 0; i < results.length; i++ ){
  128         //System.out.println( "FilenameResolver matched " + results[i] );
  129         matchingFilenames.add( path + results[i] );
  130       }
  131     }
  132     return (String[])( matchingFilenames.toArray( new String[ matchingFilenames.size() ] ));
  133   }
  134 }