[483] | 1 | //Helper functions to deal with file I/O. |
---|
| 2 | |
---|
| 3 | var fileUtil = {}; |
---|
| 4 | |
---|
| 5 | fileUtil.getLineSeparator = function(){ |
---|
| 6 | // summary: |
---|
| 7 | // Gives the line separator for the platform. |
---|
| 8 | // For web builds override this function. |
---|
| 9 | return java.lang.System.getProperty("line.separator"); //Java String |
---|
| 10 | } |
---|
| 11 | |
---|
| 12 | fileUtil.getFilteredFileList = function(/*String*/startDir, /*RegExp*/regExpFilters, /*boolean?*/makeUnixPaths, /*boolean?*/startDirIsJavaObject, /*boolean?*/dontRecurse){ |
---|
| 13 | // summary: |
---|
| 14 | // Recurses startDir and finds matches to the files that match regExpFilters.include |
---|
| 15 | // and do not match regExpFilters.exclude. Or just one regexp can be passed in for regExpFilters, |
---|
| 16 | // and it will be treated as the "include" case. |
---|
| 17 | // Ignores files/directories that start with a period (.). |
---|
| 18 | var files = []; |
---|
| 19 | |
---|
| 20 | var topDir = startDir; |
---|
| 21 | if(!startDirIsJavaObject){ |
---|
| 22 | topDir = new java.io.File(startDir); |
---|
| 23 | } |
---|
| 24 | |
---|
| 25 | var regExpInclude = regExpFilters.include || regExpFilters; |
---|
| 26 | var regExpExclude = regExpFilters.exclude || null; |
---|
| 27 | |
---|
| 28 | if(topDir.exists()){ |
---|
| 29 | var dirFileArray = topDir.listFiles(); |
---|
| 30 | for (var i = 0; i < dirFileArray.length; i++){ |
---|
| 31 | var file = dirFileArray[i]; |
---|
| 32 | if(file.isFile()){ |
---|
| 33 | var filePath = file.getPath(); |
---|
| 34 | if(makeUnixPaths){ |
---|
| 35 | //Make sure we have a JS string. |
---|
| 36 | filePath = new String(filePath); |
---|
| 37 | if(filePath.indexOf("/") == -1){ |
---|
| 38 | filePath = filePath.replace(/\\/g, "/"); |
---|
| 39 | } |
---|
| 40 | } |
---|
| 41 | |
---|
| 42 | var ok = true; |
---|
| 43 | if(regExpInclude){ |
---|
| 44 | ok = filePath.match(regExpInclude); |
---|
| 45 | } |
---|
| 46 | if(ok && regExpExclude){ |
---|
| 47 | ok = !filePath.match(regExpExclude); |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | if(ok && !file.getName().match(/^\./)){ |
---|
| 51 | files.push(filePath); |
---|
| 52 | } |
---|
| 53 | }else if(file.isDirectory() && !file.getName().match(/^\./) && !dontRecurse){ |
---|
| 54 | var dirFiles = this.getFilteredFileList(file, regExpFilters, makeUnixPaths, true); |
---|
| 55 | files.push.apply(files, dirFiles); |
---|
| 56 | } |
---|
| 57 | } |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | return files; //Array |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | |
---|
| 64 | fileUtil.copyDir = function(/*String*/srcDir, /*String*/destDir, /*RegExp*/regExpFilter, /*boolean?*/onlyCopyNew){ |
---|
| 65 | // summary: |
---|
| 66 | // copies files from srcDir to destDir using the regExpFilter to determine if the |
---|
| 67 | // file should be copied. Returns a list file name strings of the destinations that were copied. |
---|
| 68 | var fileNames = fileUtil.getFilteredFileList(srcDir, regExpFilter, true); |
---|
| 69 | var copiedFiles = []; |
---|
| 70 | |
---|
| 71 | for(var i = 0; i < fileNames.length; i++){ |
---|
| 72 | var srcFileName = fileNames[i]; |
---|
| 73 | var destFileName = srcFileName.replace(srcDir, destDir); |
---|
| 74 | |
---|
| 75 | if(fileUtil.copyFile(srcFileName, destFileName, onlyCopyNew)){ |
---|
| 76 | copiedFiles.push(destFileName); |
---|
| 77 | } |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | return copiedFiles.length ? copiedFiles : null; //Array or null |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | fileUtil.asyncFixEOLRe= new RegExp(fileUtil.getLineSeparator(), "g"); |
---|
| 84 | |
---|
| 85 | fileUtil.transformAsyncModule= function(filename, contents) { |
---|
| 86 | var match, |
---|
| 87 | bundleMatch, |
---|
| 88 | moduleId, |
---|
| 89 | requireArgs = [], |
---|
| 90 | lineSeparator = fileUtil.getLineSeparator(), |
---|
| 91 | dojo = { isBrowser:true }, |
---|
| 92 | getAsyncArgs = function(moduleId_, deps){ |
---|
| 93 | if(!deps){ |
---|
| 94 | //no moduleId given |
---|
| 95 | deps= moduleId_; |
---|
| 96 | } else { |
---|
| 97 | moduleId= moduleId_; |
---|
| 98 | } |
---|
| 99 | for (var i = 0; i < deps.length; i++) { |
---|
| 100 | if (deps[i]!="require") { |
---|
| 101 | requireArgs.push(deps[i].replace(/\//g, ".")); |
---|
| 102 | } |
---|
| 103 | } |
---|
| 104 | } |
---|
| 105 | ; |
---|
| 106 | |
---|
| 107 | // the v1.x content in the i18n bundles is bracketed by "//begin v1.x content" and "//end v1.x content" |
---|
| 108 | match = contents.match(/(\/\/begin\sv1\.x\scontent)([\s\S]+)(\/\/end\sv1\.x\scontent)/); |
---|
| 109 | if(match){ |
---|
| 110 | return match[2]; |
---|
| 111 | } |
---|
| 112 | // must not be an i18n bundle |
---|
| 113 | |
---|
| 114 | match = contents.match(/\/\/\s*AMD\-ID\s*"([^\n"]+)"/i); |
---|
| 115 | moduleId = (match && match[1]) || ""; |
---|
| 116 | if(moduleId || contents.substring(0, 8) == "define(\""){ |
---|
| 117 | if((match = contents.match(/^define\(([^\]]+)\]\s*\,[\s\n]*function.+$/m))){ |
---|
| 118 | eval("getAsyncArgs(" + match[1] + "])"); |
---|
| 119 | if(!moduleId){ |
---|
| 120 | logger.info("warning: the module " + filename + " looked like an AMD module, but didn't provide a module id"); |
---|
| 121 | return contents; |
---|
| 122 | } |
---|
| 123 | var prefix = "dojo.provide(\"" + moduleId.replace(/\//g, ".") + "\");" + lineSeparator; |
---|
| 124 | for(var req, reqs = requireArgs, i = 0; i<reqs.length; i++){ |
---|
| 125 | req = reqs[i]; |
---|
| 126 | if(req.substring(0, 5) == "text!"){ |
---|
| 127 | // do nothing |
---|
| 128 | }else if(req.substring(0, 5) == "i18n!"){ |
---|
| 129 | bundleMatch = req.match(/i18n\!(.+)\.nls\.(\w+)/); |
---|
| 130 | prefix += "dojo.requireLocalization(\"" + bundleMatch[1].replace(/\//g, ".") + "\", \"" + bundleMatch[2] + "\");" + lineSeparator; |
---|
| 131 | }else if(req != "dojo" && req != "dijit" && req != "dojox" && !/^dojo\.lib/.test(req)){ |
---|
| 132 | prefix += "dojo.require(\"" + req + "\");" + lineSeparator; |
---|
| 133 | } |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | // strip all module return values that end with the comment "// AMD-result" |
---|
| 137 | contents = contents.replace( /^\s*return\s+.+\/\/\s*AMD-return((\s.+)|(\s*))$/img , ""); |
---|
| 138 | var matchLength = match.index + match[0].length + 1; |
---|
| 139 | var contentsLength = contents.search(/\s*return\s+[_a-zA-Z\.0-9]+\s*;\s*(\/\/.+)?\s*\}\);\s*$/); |
---|
| 140 | if(contentsLength == -1){ |
---|
| 141 | //logger.info("warning: no return for: " + fileUtil.asyncProvideArg); |
---|
| 142 | contentsLength= contents.search(/\}\);\s*$/); |
---|
| 143 | } |
---|
| 144 | return prefix + lineSeparator + contents.substring(matchLength, contentsLength); |
---|
| 145 | } else { |
---|
| 146 | return contents; |
---|
| 147 | } |
---|
| 148 | } else { |
---|
| 149 | return contents; |
---|
| 150 | } |
---|
| 151 | }; |
---|
| 152 | |
---|
| 153 | fileUtil.copyFile = function(/*String*/srcFileName, /*String*/destFileName, /*boolean?*/onlyCopyNew){ |
---|
| 154 | // summary: |
---|
| 155 | // copies srcFileName to destFileName. If onlyCopyNew is set, it only copies the file if |
---|
| 156 | // srcFileName is newer than destFileName. Returns a boolean indicating if the copy occurred. |
---|
| 157 | var destFile = new java.io.File(destFileName); |
---|
| 158 | |
---|
| 159 | //logger.trace("Src filename: " + srcFileName); |
---|
| 160 | //logger.trace("Dest filename: " + destFileName); |
---|
| 161 | |
---|
| 162 | //If onlyCopyNew is true, then compare dates and only copy if the src is newer |
---|
| 163 | //than dest. |
---|
| 164 | if(onlyCopyNew){ |
---|
| 165 | var srcFile = new java.io.File(srcFileName); |
---|
| 166 | if(destFile.exists() && destFile.lastModified() >= srcFile.lastModified()){ |
---|
| 167 | return false; //Boolean |
---|
| 168 | } |
---|
| 169 | } |
---|
| 170 | |
---|
| 171 | //Make sure destination dir exists. |
---|
| 172 | var parentDir = destFile.getParentFile(); |
---|
| 173 | if(!parentDir.exists()){ |
---|
| 174 | if(!parentDir.mkdirs()){ |
---|
| 175 | throw "Could not create directory: " + parentDir.getAbsolutePath(); |
---|
| 176 | } |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | if (/.+\.js$/.test(srcFileName)) { |
---|
| 180 | fileUtil.saveUtf8File(destFileName, fileUtil.transformAsyncModule(srcFileName, fileUtil.readFile(srcFileName)).replace(fileUtil.asyncFixEOLRe, "\n")); |
---|
| 181 | } else { |
---|
| 182 | //Java's version of copy file. |
---|
| 183 | var srcChannel = new java.io.FileInputStream(srcFileName).getChannel(); |
---|
| 184 | var destChannel = new java.io.FileOutputStream(destFileName).getChannel(); |
---|
| 185 | destChannel.transferFrom(srcChannel, 0, srcChannel.size()); |
---|
| 186 | srcChannel.close(); |
---|
| 187 | destChannel.close(); |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | return true; //Boolean |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | fileUtil.readFile = function(/*String*/path, /*String?*/encoding){ |
---|
| 194 | // summary: |
---|
| 195 | // reads a file and returns a string |
---|
| 196 | encoding = encoding || "utf-8"; |
---|
| 197 | var file = new java.io.File(path); |
---|
| 198 | var lineSeparator = fileUtil.getLineSeparator(); |
---|
| 199 | var input = new java.io.BufferedReader(new java.io.InputStreamReader(new java.io.FileInputStream(file), encoding)); |
---|
| 200 | try{ |
---|
| 201 | var stringBuffer = new java.lang.StringBuffer(); |
---|
| 202 | var line = input.readLine(); |
---|
| 203 | |
---|
| 204 | // Byte Order Mark (BOM) - The Unicode Standard, version 3.0, page 324 |
---|
| 205 | // http://www.unicode.org/faq/utf_bom.html |
---|
| 206 | |
---|
| 207 | // Note that when we use utf-8, the BOM should appear as "EF BB BF", but it doesn't due to this bug in the JDK: |
---|
| 208 | // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4508058 |
---|
| 209 | if(line && line.length() && line.charAt(0) === 0xfeff){ |
---|
| 210 | // Eat the BOM, since we've already found the encoding on this file, |
---|
| 211 | // and we plan to concatenating this buffer with others; the BOM should |
---|
| 212 | // only appear at the top of a file. |
---|
| 213 | line = line.substring(1); |
---|
| 214 | } |
---|
| 215 | while(line !== null){ |
---|
| 216 | stringBuffer.append(line); |
---|
| 217 | stringBuffer.append(lineSeparator); |
---|
| 218 | line = input.readLine(); |
---|
| 219 | } |
---|
| 220 | //Make sure we return a JavaScript string and not a Java string. |
---|
| 221 | return new String(stringBuffer.toString()); //String |
---|
| 222 | }finally{ |
---|
| 223 | input.close(); |
---|
| 224 | } |
---|
| 225 | } |
---|
| 226 | |
---|
| 227 | fileUtil.saveUtf8File = function(/*String*/fileName, /*String*/fileContents){ |
---|
| 228 | // summary: |
---|
| 229 | // saves a file using UTF-8 encoding. |
---|
| 230 | fileUtil.saveFile(fileName, fileContents, "utf-8"); |
---|
| 231 | } |
---|
| 232 | |
---|
| 233 | fileUtil.saveFile = function(/*String*/fileName, /*String*/fileContents, /*String?*/encoding){ |
---|
| 234 | // summary: |
---|
| 235 | // saves a file. |
---|
| 236 | var outFile = new java.io.File(fileName); |
---|
| 237 | var outWriter; |
---|
| 238 | |
---|
| 239 | var parentDir = outFile.getParentFile(); |
---|
| 240 | if(!parentDir.exists()){ |
---|
| 241 | if(!parentDir.mkdirs()){ |
---|
| 242 | throw "Could not create directory: " + parentDir.getAbsolutePath(); |
---|
| 243 | } |
---|
| 244 | } |
---|
| 245 | |
---|
| 246 | if(encoding){ |
---|
| 247 | outWriter = new java.io.OutputStreamWriter(new java.io.FileOutputStream(outFile), encoding); |
---|
| 248 | }else{ |
---|
| 249 | outWriter = new java.io.OutputStreamWriter(new java.io.FileOutputStream(outFile)); |
---|
| 250 | } |
---|
| 251 | |
---|
| 252 | var os = new java.io.BufferedWriter(outWriter); |
---|
| 253 | try{ |
---|
| 254 | os.write(fileContents); |
---|
| 255 | }finally{ |
---|
| 256 | os.close(); |
---|
| 257 | } |
---|
| 258 | } |
---|
| 259 | |
---|
| 260 | fileUtil.deleteFile = function(/*String*/fileName){ |
---|
| 261 | // summary: |
---|
| 262 | // deletes a file or directory if it exists. |
---|
| 263 | var file = new java.io.File(fileName); |
---|
| 264 | if(file.exists()){ |
---|
| 265 | if(file.isDirectory()){ |
---|
| 266 | var files = file.listFiles(); |
---|
| 267 | for(var i = 0; i < files.length; i++){ |
---|
| 268 | this.deleteFile(files[i]); |
---|
| 269 | } |
---|
| 270 | } |
---|
| 271 | file["delete"](); |
---|
| 272 | } |
---|
| 273 | } |
---|