searchpath.java

来自「plugin for eclipse」· Java 代码 · 共 118 行

JAVA
118
字号
package isis.commons.fs;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Properties;


/**
 * A list of paths.
 * @author sallai
 */
public class SearchPath extends ArrayList {
	
	private static final long serialVersionUID = 1L;

	/**
	 * Load path list from a file in the classpath.
	 * @param fn file name
	 * @return the path list
	 * @throws FileNotFoundException
	 */
	public static SearchPath load(String name) throws IOException {
		InputStream is = Loader.getStream(name);
		if(is != null) {
			return readObject(is);
		} else {
			return null;
		}
	}

	/**
	 * Restore SearchPath object from a stream.
	 * @param is input stream
	 * @return SearchPath object
	 * @throws IOException
	 */
	public static SearchPath readObject(InputStream is) throws IOException {
		Properties p = new Properties();
		p.load(is);
		
		String spl = p.getProperty("searchPathList");
		is.close();
		
		return readObject(spl);
	}

	public static SearchPath readObject(String spl) throws IOException {
		SearchPath rval = new SearchPath();
		if(spl!=null) {
			rval.addAll(Arrays.asList(spl.split(";")));
		}
		
		return rval;
	}

	
	/**
	 * Searches for a file in the path list.
	 * @param filePath file to search for
	 * @return the file
	 * @throws FileNotFoundException
	 */
    public File findFile(String filePath) throws FileNotFoundException {
        File f;    	
        Iterator i = this.iterator();
        while (i.hasNext()) {
            String pathPrefix = (String)i.next();

            String fullFilePath = pathPrefix+"/"+filePath;
            
            int lastSlashIdx = fullFilePath.lastIndexOf('/');            
            if(fullFilePath.lastIndexOf('\\')>lastSlashIdx) {
            	lastSlashIdx = fullFilePath.lastIndexOf('\\');
            }
            
            String preloadFilePath = fullFilePath.substring(0, lastSlashIdx) 
            	+ "/." + fullFilePath.substring(lastSlashIdx+1, fullFilePath.length());
            
           
            if ((f = new File(preloadFilePath)).exists())
            	return f;         

            if ((f = new File(fullFilePath)).exists())
            	return f;         
        }
        throw new FileNotFoundException("File not found: "+filePath+ " (search path is "+ this.toString()+")");    	
    }
 
    /**
     * Searches for a file in the path list.
     * @param filePath the file name
     * @return the canonical path of the file
     * @throws IOException
     */
    public CanonicalPath getCanonicalPath(String filePath) throws IOException {
    	return new CanonicalPath(findFile(filePath).getCanonicalPath());
    }

    public void addAllWithPrefix(SearchPath sp, String prefix) {

		if(sp==null) return;
	
		if(prefix==null) {
			prefix="";
		}
		
		Iterator i = sp.iterator();
		while (i.hasNext()) {
			String p = (String) i.next();
			this.add(prefix + p);
		}
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?