5.2 Search Engine

<..static String searchFile( file )..>
 public static String searchFile( String file ){
    String key = ((ii_scriptDir == null)? "" : ii_scriptDir )
                 + "!" + file;
    String result = (String) registry.get( key );
    if( result == null ){
       for(int i=0; i<2; i++){
          if( trace ){
             log.println( "Searching: " + file );
          }
          if( (new File(file)).exists() ){
             result = ( file.indexOf(slash) == -1 )?
                          (System.getProperty("user.dir") + slash + file)
                         :
                          file;
          }
          else {
             if( ii_scriptDir != null ){
                <.search file in script directories.>
             }
             if( result == null ){
                <.search file in classpath/xtpipes/lib.>
          }  }
          if( result != null ){ break; }
          file =  new File(file).getName();
       }
       if( result != null ){
         result = FileInfo.cleanPath(result);
         registry.put(key, result);
       }
    }
    if( trace ){
       if( result == null ){
          log.println(
             "Directory paths from xtpipes command line option -i: "
                                             + ii_scriptDir );
       } else { log.println( "Found: " + result + "\n" ); }
       log.flush();
    }
    return result;
 }
 -_-_-

<..search file in script directories..>
 int k = scriptPaths.length;
 while( k>0 ){
   k--;
   if( trace ){
     log.println( "Searching: " + file
                    + ", recursively in directory: " + scriptPaths[k] );
   }
   result = searchDirectory( new File(scriptPaths[k]), file);
   if( result != null ){ break; }
 }
 String s = ii_scriptDir + file;
 if( (new File( s )).exists() ){ result = s; }
 -_-_-

<..search file in classpath/xtpipes/lib..>
 int k = classPaths.length;
 String toFile = "xtpipes" + slash + "lib" + slash + file;
 while( k>0 ){
   k--;
   String s =  classPaths[k] + toFile;
   if( trace ){ log.println( "Searching: " + s ); }
   if( new File(s).exists() ){ result = s; break; }
 }
 -_-_-

<..static String searchDirectory(dir, file)..>
 static String searchDirectory(File dir, String file) {
     String result = null;
     if( dir.isDirectory() ){
        String [] children = dir.list();
        for (int i=0; i<children.length; i++) {
           result = searchDirectory( new File(dir,children[i]), file);
           if( result != null ) { break; }
        }
     } else {
        String s = dir.toString();
        if( s.equals(file) || s.endsWith(slash + file) ){
            result = s;
         }
      }
      return result;
 }
 -_-_-