1 jls.loader.provide('jls.io.File'); 2 3 jls.loader.requireLibrary('jls_io'); 4 5 jls.io.File = jls.lang.Class.create( /** @lends jls.io.File.prototype */ 6 { 7 /** 8 * Creates a file. 9 * 10 * @param {jls.io.File} [parent] The parent file. 11 * @param {String} child The file pathname. 12 * @constructs 13 * @class This class represents a file system entry. 14 */ 15 initialize : function(parent, child) { 16 if (! child) { 17 child = parent; 18 parent = undefined; 19 } 20 if (typeof child != 'string') { 21 throw new jls.lang.Exception('Invalid File argument type (' + (typeof child) + ')'); 22 } 23 if (parent) { 24 if (parent instanceof jls.io.File) { 25 this.path = jls.io.File.concat(parent.getPath(), jls.io.File.normalize(child)); 26 } else if (typeof parent == 'string') { 27 this.path = jls.io.File.normalize(jls.io.File.concat(parent, child)); 28 } 29 } else { 30 this.path = jls.io.File.normalize(child); 31 } 32 }, 33 /** 34 * Removes the file entry. 35 * 36 * @returns {Boolean} true if the entry is removed. 37 */ 38 remove : function() { 39 return _native.io.delete(this.path); 40 }, 41 /** 42 * Tests whether this file exists. 43 * 44 * @returns {Boolean} true if the file exists. 45 */ 46 exists : function() { 47 return _native.io.exists(this.path); 48 }, 49 /** 50 * Returns the name of this file. This is the last name in the pathname's name sequence. 51 * 52 * @returns {String} The name of this file. 53 */ 54 getName : function() { 55 var index = this.path.lastIndexOf(jls.io.File.separator); 56 if (index > 0) { 57 return this.path.substr(index + 1); 58 } else { 59 return this.path; 60 } 61 }, 62 /** 63 * Returns the extension of this file. 64 * 65 * @returns {String} The extension of this file. 66 */ 67 getExtension : function() { 68 var index = this.path.lastIndexOf('.'); 69 if (index > 0) { 70 return this.path.substr(index + 1); 71 } else { 72 return ''; 73 } 74 }, 75 /** 76 * Returns the path of this file. 77 * 78 * @returns {String} The path of this file. 79 */ 80 getPath : function() { 81 return this.path; 82 }, 83 getParent : function() { 84 return this.getParentFile().getPath(); 85 }, 86 getParentFile : function() { 87 if (this.isAbsolute()) { 88 if (jls.io.File.isRoot(this.path)) { 89 return null; 90 } 91 return new jls.io.File(this.path.substring(0, this.path.lastIndexOf(jls.io.File.separator))); 92 } 93 throw new jls.lang.Exception('Not supported'); 94 }, 95 /** 96 * Tests whether the file path is absolute. 97 * 98 * @returns {Boolean} true if the file path is absolute. 99 */ 100 isAbsolute : function() { 101 return this.path.startsWith(jls.io.File.separator) || (this.path.indexOf(':\\') == 1); 102 }, 103 /** 104 * Tests whether the file entry is a directory. 105 * 106 * @returns {Boolean} true if the file entry is a directory. 107 */ 108 isDirectory : function() { 109 return _native.io.getType(this.path) == _native.io.TYPE_DIRECTORY; 110 }, 111 /** 112 * Tests whether the file entry is a file. 113 * 114 * @returns {Boolean} true if the file entry is a file. 115 */ 116 isFile : function() { 117 return _native.io.getType(this.path) == _native.io.TYPE_FILE; 118 }, 119 isHidden : function() { 120 throw new jls.lang.Exception('Not supported'); 121 return false; 122 }, 123 lastModified : function() { 124 return _native.io.getModifyTime(this.path); 125 }, 126 /** 127 * Returns the length of the file entry represented by this file. 128 * 129 * @returns {Number} The length of the file entry represented by this file. 130 */ 131 length : function() { 132 return _native.io.getSize(this.path); 133 }, 134 /** 135 * Returns an array of strings naming the file system entries in the directory represented by this file. 136 * 137 * @returns {Array} An array of strings naming the file system entries. 138 */ 139 list : function() { 140 return _native.io.list(this.path); 141 }, 142 /** 143 * Returns an array of files in the directory represented by this file. 144 * 145 * @returns {Array} An array of files. 146 */ 147 listFiles : function() { 148 var l = this.list(); 149 var lf = []; 150 for (var i = 0; i < l.length; i++) { 151 lf.push(new jls.io.File(this, l[i])); 152 } 153 return lf; 154 }, 155 getAbsoluteFile : function() { 156 throw new jls.lang.Exception('Not supported'); 157 return false; 158 }, 159 /** 160 * Returns the absolute path of this file. 161 * 162 * @returns {String} The absolute path of this file. 163 */ 164 getAbsolutePath : function() { 165 if (this.isAbsolute()) { 166 return this.getPath(); 167 } else { 168 return jls.io.File.concat(jls.io.File.userDir, this.getPath()); 169 } 170 }, 171 /** 172 * Creates the directory named by this file. 173 * 174 * @returns {Boolean} true if the directory is created. 175 */ 176 mkdir : function() { 177 return _native.io.mkDir(this.path); 178 }, 179 mkdirs : function() { 180 throw new jls.lang.Exception('Not supported'); 181 return false; 182 }, 183 /** 184 * Renames this file. 185 * 186 * @param {String} newPath The new path for this file. 187 * @returns {Boolean} true if this file is renamed. 188 */ 189 renameTo : function(newPath) { 190 return _native.io.rename(this.path, newPath); 191 }, 192 setLastModified : function() { 193 throw new jls.lang.Exception('Not supported'); 194 return false; 195 } 196 }); 197 198 199 Object.extend(jls.io.File, /** @lends jls.io.File */ 200 { 201 isRoot : function(path) { 202 return (path != null) && ((path == '/') || ((path.length == 3) && path.endsWith(':\\'))); 203 }, 204 /** 205 * Concats to path parts and then returns the resulting path. 206 * 207 * @param {String} a The first path part to concat. 208 * @param {String} b The last path part to concat. 209 * @returns {String} The concated path. 210 */ 211 concat : function(a, b) { 212 return a + jls.io.File.separator + b; 213 }, 214 /** 215 * Normalizes a path. 216 * 217 * @param {String} path The path to normalize. 218 * @returns {String} The normalized path. 219 */ 220 normalize : function(path) { 221 var npath = path.replace(/[\\\/][\\\/]*/g, jls.io.File.separator); 222 if ((! jls.io.File.isRoot(path)) && (npath.length > 0) && (npath.endsWith(jls.io.File.separator))) { 223 npath = npath.substr(0, npath.length - 1 - jls.io.File.separator.length); 224 } 225 return npath; 226 }, 227 createTempFile : function() { 228 throw new jls.lang.Exception('Not supported'); 229 return false; 230 }, 231 /** 232 * Returns an array of file system roots. 233 * 234 * @returns {Array} An array of files denoting the available filesystem roots. 235 */ 236 listRoots : function() { 237 var list = []; 238 var roots = _native.io.listRoots(); 239 for (var i = 0; i < roots.length; i++) { 240 list.push(new jls.io.File(roots[i])); 241 } 242 return list; 243 }, 244 userDir : _native.core.properties['user.dir'], 245 pathSeparator : _native.core.properties['path.separator'], 246 separator : _native.core.properties['file.separator'] 247 }); 248 249