| 1 |
|
| 2 |
|
| 3 |
var fileUtil = {}; |
| 4 |
|
| 5 |
fileUtil.getLineSeparator = function(){ |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
return java.lang.System.getProperty("line.separator"); |
| 10 |
}; |
| 11 |
|
| 12 |
fileUtil.getFilteredFileList = function(startDir, regExpFilters, makeUnixPaths, startDirIsJavaObject, dontRecurse){ |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 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 |
|
| 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; |
| 61 |
}; |
| 62 |
|
| 63 |
|
| 64 |
fileUtil.copyDir = function(srcDir, destDir, regExpFilter, onlyCopyNew){ |
| 65 |
|
| 66 |
|
| 67 |
|
| 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; |
| 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 |
|
| 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 |
|
| 108 |
match = contents.match(/(\/\/begin\sv1\.x\scontent)([\s\S]+)(\/\/end\sv1\.x\scontent)/); |
| 109 |
if(match){ |
| 110 |
return match[2]; |
| 111 |
} |
| 112 |
|
| 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 |
|
| 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" && req != "themes" && req != "dojo-themes" && !/^dojo\.lib/.test(req)){ |
| 132 |
prefix += "dojo.require(\"" + req + "\");" + lineSeparator; |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
|
| 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 |
|
| 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(srcFileName, destFileName, onlyCopyNew){ |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
var destFile = new java.io.File(destFileName); |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
if(onlyCopyNew){ |
| 165 |
var srcFile = new java.io.File(srcFileName); |
| 166 |
if(destFile.exists() && destFile.lastModified() >= srcFile.lastModified()){ |
| 167 |
return false; |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
|
| 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 |
|
| 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; |
| 191 |
}; |
| 192 |
|
| 193 |
fileUtil.readFile = function(path, encoding){ |
| 194 |
|
| 195 |
|
| 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 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
if(line && line.length() && line.charAt(0) === 0xfeff){ |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
line = line.substring(1); |
| 214 |
} |
| 215 |
while(line !== null){ |
| 216 |
stringBuffer.append(line); |
| 217 |
stringBuffer.append(lineSeparator); |
| 218 |
line = input.readLine(); |
| 219 |
} |
| 220 |
|
| 221 |
return new String(stringBuffer.toString()); |
| 222 |
}finally{ |
| 223 |
input.close(); |
| 224 |
} |
| 225 |
}; |
| 226 |
|
| 227 |
fileUtil.saveUtf8File = function(fileName, fileContents){ |
| 228 |
|
| 229 |
|
| 230 |
fileUtil.saveFile(fileName, fileContents, "utf-8"); |
| 231 |
}; |
| 232 |
|
| 233 |
fileUtil.saveFile = function(fileName, fileContents, encoding){ |
| 234 |
|
| 235 |
|
| 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(fileName){ |
| 261 |
|
| 262 |
|
| 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 |
}; |