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 throw new jls.lang.Exception('Not supported'); 85 return false; 86 }, 87 getParentFile : function() { 88 throw new jls.lang.Exception('Not supported'); 89 return false; 90 }, 91 /** 92 * Tests whether the file path is absolute. 93 * 94 * @returns {Boolean} true if the file path is absolute. 95 */ 96 isAbsolute : function() { 97 return this.path.startsWith(jls.io.File.separator) || (this.path.indexOf(':\\') == 1); 98 }, 99 /** 100 * Tests whether the file entry is a directory. 101 * 102 * @returns {Boolean} true if the file entry is a directory. 103 */ 104 isDirectory : function() { 105 return _native.io.getType(this.path) == _native.io.TYPE_DIRECTORY; 106 }, 107 /** 108 * Tests whether the file entry is a file. 109 * 110 * @returns {Boolean} true if the file entry is a file. 111 */ 112 isFile : function() { 113 return _native.io.getType(this.path) == _native.io.TYPE_FILE; 114 }, 115 isHidden : function() { 116 throw new jls.lang.Exception('Not supported'); 117 return false; 118 }, 119 lastModified : function() { 120 return _native.io.getModifyTime(this.path); 121 }, 122 /** 123 * Returns the length of the file entry represented by this file. 124 * 125 * @returns {Number} The length of the file entry represented by this file. 126 */ 127 length : function() { 128 return _native.io.getSize(this.path); 129 }, 130 /** 131 * Returns an array of strings naming the file system entries in the directory represented by this file. 132 * 133 * @returns {Array} An array of strings naming the file system entries. 134 */ 135 list : function() { 136 return _native.io.list(this.path); 137 }, 138 /** 139 * Returns an array of files in the directory represented by this file. 140 * 141 * @returns {Array} An array of files. 142 */ 143 listFiles : function() { 144 var l = this.list(); 145 var lf = []; 146 for (var i = 0; i < l.length; i++) { 147 lf.push(new jls.io.File(this, l[i])); 148 } 149 return lf; 150 }, 151 getAbsoluteFile : function() { 152 throw new jls.lang.Exception('Not supported'); 153 return false; 154 }, 155 /** 156 * Returns the absolute path of this file. 157 * 158 * @returns {String} The absolute path of this file. 159 */ 160 getAbsolutePath : function() { 161 if (this.isAbsolute()) { 162 return this.getPath(); 163 } else { 164 return jls.io.File.concat(jls.io.File.userDir, this.getPath()); 165 } 166 }, 167 /** 168 * Creates the directory named by this file. 169 * 170 * @returns {Boolean} true if the directory is created. 171 */ 172 mkdir : function() { 173 return _native.io.mkDir(this.path); 174 }, 175 mkdirs : function() { 176 throw new jls.lang.Exception('Not supported'); 177 return false; 178 }, 179 /** 180 * Renames this file. 181 * 182 * @param {String} newPath The new path for this file. 183 * @returns {Boolean} true if this file is renamed. 184 */ 185 renameTo : function(newPath) { 186 return _native.io.rename(this.path, newPath); 187 }, 188 setLastModified : function() { 189 throw new jls.lang.Exception('Not supported'); 190 return false; 191 } 192 }); 193 194 195 Object.extend(jls.io.File, /** @lends jls.io.File */ 196 { 197 isRoot : function(path) { 198 return (path != null) && ((path == '/') || ((path.length == 3) && path.endsWith(':\\'))); 199 }, 200 /** 201 * Concats to path parts and then returns the resulting path. 202 * 203 * @param {String} a The first path part to concat. 204 * @param {String} b The last path part to concat. 205 * @returns {String} The concated path. 206 */ 207 concat : function(a, b) { 208 return a + jls.io.File.separator + b; 209 }, 210 /** 211 * Normalizes a path. 212 * 213 * @param {String} path The path to normalize. 214 * @returns {String} The normalized path. 215 */ 216 normalize : function(path) { 217 var npath = path.replace(/[\\\/][\\\/]*/g, jls.io.File.separator); 218 if ((! jls.io.File.isRoot(path)) && (npath.length > 0) && (npath.endsWith(jls.io.File.separator))) { 219 npath = npath.substr(0, npath.length - 1 - jls.io.File.separator.length); 220 } 221 return npath; 222 }, 223 createTempFile : function() { 224 throw new jls.lang.Exception('Not supported'); 225 return false; 226 }, 227 /** 228 * Returns an array of file system roots. 229 * 230 * @returns {Array} An array of files denoting the available filesystem roots. 231 */ 232 listRoots : function() { 233 var list = []; 234 var roots = _native.io.listRoots(); 235 for (var i = 0; i < roots.length; i++) { 236 list.push(new jls.io.File(roots[i])); 237 } 238 return list; 239 }, 240 userDir : _native.core.properties['user.dir'], 241 pathSeparator : _native.core.properties['path.separator'], 242 separator : _native.core.properties['file.separator'] 243 }); 244 245