1 28 31 package org.jruby.util; 32 33 import java.io.File ; 34 import java.io.FileFilter ; 35 import java.io.FilenameFilter ; 36 import java.io.IOException ; 37 38 43 public class JRubyFile extends File { 44 private static final long serialVersionUID = 435364547567567L; 45 46 public static JRubyFile create(String cwd, String pathname) { 47 File internal = new File (pathname); 48 if(!internal.isAbsolute()) { 49 internal = new File (cwd,pathname); 50 if(!internal.isAbsolute()) { 51 throw new IllegalArgumentException ("Neither current working directory ("+cwd+") nor pathname ("+pathname+") led to an absolute path"); 52 } 53 } 54 return new JRubyFile(internal); 55 } 56 57 public static String getFileProperty(String property) { 58 String value = System.getProperty(property); 59 60 return value.replace(File.separatorChar, '/'); 61 } 62 63 private JRubyFile(File file) { 64 this(file.getAbsolutePath()); 65 } 66 67 private JRubyFile(String filename) { 68 super(filename); 69 } 70 71 public String getAbsolutePath() { 72 return super.getAbsolutePath().replace(File.separatorChar, '/'); 73 } 74 75 public String getCanonicalPath() throws IOException { 76 return super.getCanonicalPath().replace(File.separatorChar, '/'); 77 } 78 79 public String getPath() { 80 return super.getPath().replace(File.separatorChar, '/'); 81 } 82 83 public String toString() { 84 return super.toString().replace(File.separatorChar, '/'); 85 } 86 87 public File getAbsoluteFile() { 88 return new JRubyFile(getAbsolutePath()); 89 } 90 91 public File getCanonicalFile() throws IOException { 92 return new JRubyFile(getCanonicalPath()); 93 } 94 95 public String getParent() { 96 return super.getParent().replace(File.separatorChar, '/'); 97 } 98 99 public File getParentFile() { 100 return new JRubyFile(getParent()); 101 } 102 103 public static File [] listRoots() { 104 File [] roots = File.listRoots(); 105 JRubyFile[] smartRoots = new JRubyFile[roots.length]; 106 for(int i = 0, j = roots.length; i < j; i++) { 107 smartRoots[i] = new JRubyFile(roots[i].getPath()); 108 } 109 return smartRoots; 110 } 111 112 public static File createTempFile(String prefix, String suffix, File directory) throws IOException { 113 return new JRubyFile(File.createTempFile(prefix, suffix,directory)); 114 } 115 116 public static File createTempFile(String prefix, String suffix) throws IOException { 117 return new JRubyFile(File.createTempFile(prefix, suffix)); 118 } 119 120 public String [] list(FilenameFilter filter) { 121 String [] files = super.list(filter); 122 if (files == null) { 123 return null; 124 } 125 126 String [] smartFiles = new String [files.length]; 127 for (int i = 0; i < files.length; i++) { 128 smartFiles[i] = files[i].replace(File.separatorChar, '/'); 129 } 130 return smartFiles; 131 } 132 133 public File [] listFiles() { 134 File [] files = super.listFiles(); 135 if (files == null) { 136 return null; 137 } 138 139 JRubyFile[] smartFiles = new JRubyFile[files.length]; 140 for (int i = 0, j = files.length; i < j; i++) { 141 smartFiles[i] = create(super.getAbsolutePath(),files[i].getPath()); 142 } 143 return smartFiles; 144 } 145 146 public File [] listFiles(final FileFilter filter) { 147 final File [] files = super.listFiles(filter); 148 if (files == null) { 149 return null; 150 } 151 152 JRubyFile[] smartFiles = new JRubyFile[files.length]; 153 for (int i = 0,j = files.length; i < j; i++) { 154 smartFiles[i] = create(super.getAbsolutePath(),files[i].getPath()); 155 } 156 return smartFiles; 157 } 158 159 public File [] listFiles(final FilenameFilter filter) { 160 final File [] files = super.listFiles(filter); 161 if (files == null) { 162 return null; 163 } 164 165 JRubyFile[] smartFiles = new JRubyFile[files.length]; 166 for (int i = 0,j = files.length; i < j; i++) { 167 smartFiles[i] = create(super.getAbsolutePath(),files[i].getPath()); 168 } 169 return smartFiles; 170 } 171 } 172 | Popular Tags |