001 package railo.commons.io; 002 003 import java.io.File; 004 import java.net.MalformedURLException; 005 import java.net.URL; 006 007 /** 008 * Helper methods for file objects 009 */ 010 public final class FileUtil { 011 /** 012 * Field <code>FILE_SEPERATOR</code> 013 */ 014 public static final char FILE_SEPERATOR=File.separatorChar; 015 /** 016 * Field <code>FILE_ANTI_SEPERATOR</code> 017 */ 018 public static final char FILE_ANTI_SEPERATOR=(FILE_SEPERATOR=='/')?'\\':'/'; 019 020 /** 021 * Field <code>TYPE_DIR</code> 022 */ 023 public static final short TYPE_DIR=0; 024 025 /** 026 * Field <code>TYPE_FILE</code> 027 */ 028 public static final short TYPE_FILE=1; 029 030 /** 031 * Field <code>LEVEL_FILE</code> 032 */ 033 public static final short LEVEL_FILE=0; 034 /** 035 * Field <code>LEVEL_PARENT_FILE</code> 036 */ 037 public static final short LEVEL_PARENT_FILE=1; 038 /** 039 * Field <code>LEVEL_GRAND_PARENT_FILE</code> 040 */ 041 public static final short LEVEL_GRAND_PARENT_FILE=2; 042 043 /** 044 * create a file from path 045 * @param path 046 * @return new File Object 047 */ 048 public static File toFile(String path) { 049 return new File(path.replace(FILE_ANTI_SEPERATOR,FILE_SEPERATOR)); 050 } 051 052 /** 053 * create a File from parent file and string 054 * @param parent 055 * @param path 056 * @return new File Object 057 */ 058 public static File toFile(File parent, String path) { 059 return new File(parent,path.replace(FILE_ANTI_SEPERATOR,FILE_SEPERATOR)); 060 } 061 062 /** 063 * create a File from parent file and string 064 * @param parent 065 * @param path 066 * @return new File Object 067 */ 068 public static File toFile(String parent, String path) { 069 return new File( 070 parent.replace(FILE_ANTI_SEPERATOR,FILE_SEPERATOR), 071 path.replace(FILE_ANTI_SEPERATOR,FILE_SEPERATOR)); 072 } 073 074 /* * 075 * create a file object from a file object (parent) and realpath, in difference to the same constructor 076 * of the File Object this method ignore the diffrent path seperators on the different plattforms 077 * @param parent 078 * @param realpath 079 * @return new FIle Object matching on arguments 080 * / 081 public static File toFile2(File parent, String realpath) { 082 realpath=realpath.replace(FILE_ANTI_SEPERATOR,FILE_SEPERATOR); 083 while(realpath.startsWith("../")) { 084 parent=parent.getParentFile(); 085 realpath=realpath.substring(3); 086 } 087 if(realpath.startsWith("./")) realpath=realpath.substring(2); 088 089 return FileUtil.toFile(parent,realpath); 090 }*/ 091 092 093 094 095 096 097 /** 098 * translate a URL to a File Object 099 * @param url 100 * @return matching file object 101 * @throws MalformedURLException 102 */ 103 public static final File URLToFile(URL url) throws MalformedURLException { 104 if (!"file".equals(url.getProtocol())) 105 throw new MalformedURLException("URL protocol must be 'file'."); 106 return new File(URIToFilename(url.getFile())); 107 } 108 109 /** 110 * Fixes a platform dependent filename to standard URI form. 111 * @param str The string to fix. 112 * @return Returns the fixed URI string. 113 */ 114 public static final String URIToFilename(String str) { 115 // Windows fix 116 if (str.length() >= 3) { 117 if (str.charAt(0) == '/' && str.charAt(2) == ':') { 118 char ch1 = Character.toUpperCase(str.charAt(1)); 119 if (ch1 >= 'A' && ch1 <= 'Z') str = str.substring(1); 120 } 121 } 122 // handle platform dependent strings 123 str = str.replace('/', java.io.File.separatorChar); 124 return str; 125 } 126 127 128 129 130 }