/tags/jsdoc_toolkit-2.0.1/jsdoc-toolkit/app/run.js

http://jsdoc-toolkit.googlecode.com/ · JavaScript · 346 lines · 203 code · 45 blank · 98 comment · 54 complexity · 766b764b1ca2000b5cfd09f35ec7ddb0 MD5 · raw file

  1. /**
  2. * @fileOverview
  3. * A bootstrap script that creates some basic required objects
  4. * for loading other scripts.
  5. * @author Michael Mathews, micmath@gmail.com
  6. * @version $Id: run.js 554 2008-04-01 00:12:25Z micmath $
  7. */
  8. /**
  9. * @namespace Keep track of any messages from the running script.
  10. */
  11. LOG = {
  12. warn: function(msg, e) {
  13. if (e) msg = e.fileName+", line "+e.lineNumber+": "+msg;
  14. msg = ">> WARNING: "+msg;
  15. LOG.warnings.push(msg);
  16. if (LOG.out) LOG.out.write(msg+"\n");
  17. else print(msg);
  18. },
  19. inform: function(msg) {
  20. msg = " > "+msg;
  21. if (LOG.out) LOG.out.write(msg+"\n");
  22. else if (typeof LOG.verbose != "undefined" && LOG.verbose) print(msg);
  23. }
  24. };
  25. LOG.warnings = [];
  26. LOG.verbose = false
  27. LOG.out = undefined;
  28. /**
  29. * @class Manipulate a filepath.
  30. */
  31. function FilePath(absPath, separator) {
  32. this.slash = separator || "/";
  33. this.root = this.slash;
  34. this.path = [];
  35. this.file = "";
  36. var parts = absPath.split(/[\\\/]/);
  37. if (parts) {
  38. if (parts.length) this.root = parts.shift() + this.slash;
  39. if (parts.length) this.file = parts.pop()
  40. if (parts.length) this.path = parts;
  41. }
  42. this.path = this.resolvePath();
  43. }
  44. /** Collapse any dot-dot or dot items in a filepath. */
  45. FilePath.prototype.resolvePath = function() {
  46. var resolvedPath = [];
  47. for (var i = 0; i < this.path.length; i++) {
  48. if (this.path[i] == "..") resolvedPath.pop();
  49. else if (this.path[i] != ".") resolvedPath.push(this.path[i]);
  50. }
  51. return resolvedPath;
  52. }
  53. /** Trim off the filename. */
  54. FilePath.prototype.toDir = function() {
  55. if (this.file) this.file = "";
  56. return this;
  57. }
  58. /** Go up a directory. */
  59. FilePath.prototype.upDir = function() {
  60. this.toDir();
  61. if (this.path.length) this.path.pop();
  62. return this;
  63. }
  64. FilePath.prototype.toString = function() {
  65. return this.root
  66. + this.path.join(this.slash)
  67. + ((this.path.length > 0)? this.slash : "")
  68. + this.file;
  69. }
  70. /**
  71. * Turn a path into just the name of the file.
  72. */
  73. FilePath.fileName = function(path) {
  74. var nameStart = Math.max(path.lastIndexOf("/")+1, path.lastIndexOf("\\")+1, 0);
  75. return path.substring(nameStart);
  76. }
  77. /**
  78. * Get the extension of a filename
  79. */
  80. FilePath.fileExtension = function(filename) {
  81. return filename.split(".").pop().toLowerCase();
  82. };
  83. /**
  84. * Turn a path into just the directory part.
  85. */
  86. FilePath.dir = function(path) {
  87. var nameStart = Math.max(path.lastIndexOf("/")+1, path.lastIndexOf("\\")+1, 0);
  88. return path.substring(0, nameStart-1);
  89. }
  90. importClass(java.lang.System);
  91. /**
  92. * @namespace A collection of information about your system.
  93. */
  94. SYS = {
  95. /**
  96. * Information about your operating system: arch, name, version.
  97. * @type string
  98. */
  99. os: [
  100. new String(System.getProperty("os.arch")),
  101. new String(System.getProperty("os.name")),
  102. new String(System.getProperty("os.version"))
  103. ].join(", "),
  104. /**
  105. * Which way does your slash lean.
  106. * @type string
  107. */
  108. slash: System.getProperty("file.separator")||"/",
  109. /**
  110. * The path to the working directory where you ran java.
  111. * @type string
  112. */
  113. userDir: new String(System.getProperty("user.dir")),
  114. /**
  115. * Where is Java's home folder.
  116. * @type string
  117. */
  118. javaHome: new String(System.getProperty("java.home")),
  119. /**
  120. * The absolute path to the directory containing this script.
  121. * @type string
  122. */
  123. pwd: undefined
  124. };
  125. // jsrun appends an argument, with the path to here.
  126. if (arguments[arguments.length-1].match(/^-j=(.+)/)) {
  127. if (RegExp.$1.charAt(0) == SYS.slash || RegExp.$1.charAt(1) == ":") { // absolute path to here
  128. SYS.pwd = new FilePath(RegExp.$1).toDir().toString();
  129. }
  130. else { // relative path to here
  131. SYS.pwd = new FilePath(SYS.userDir + SYS.slash + RegExp.$1).toDir().toString();
  132. }
  133. arguments.pop();
  134. }
  135. else {
  136. print("The run.js script requires you use jsrun.jar.");
  137. quit();
  138. }
  139. // shortcut
  140. var File = Packages.java.io.File;
  141. /**
  142. * @namespace A collection of functions that deal with reading a writing to disk.
  143. */
  144. IO = {
  145. /**
  146. * Create a new file in the given directory, with the given name and contents.
  147. */
  148. saveFile: function(/**string*/ outDir, /**string*/ fileName, /**string*/ content) {
  149. var out = new Packages.java.io.PrintWriter(
  150. new Packages.java.io.OutputStreamWriter(
  151. new Packages.java.io.FileOutputStream(outDir+SYS.slash+fileName),
  152. IO.encoding
  153. )
  154. );
  155. out.write(content);
  156. out.flush();
  157. out.close();
  158. },
  159. /**
  160. * @type string
  161. */
  162. readFile: function(/**string*/ path) {
  163. if (!IO.exists(path)) {
  164. throw "File doesn't exist there: "+path;
  165. }
  166. return readFile(path, IO.encoding);
  167. },
  168. /**
  169. * @param inFile
  170. * @param outDir
  171. * @param [fileName=The original filename]
  172. */
  173. copyFile: function(/**string*/ inFile, /**string*/ outDir, /**string*/ fileName) {
  174. if (fileName == null) fileName = FilePath.fileName(inFile);
  175. var inFile = new File(inFile);
  176. var outFile = new File(outDir+SYS.slash+fileName);
  177. var bis = new Packages.java.io.BufferedInputStream(new Packages.java.io.FileInputStream(inFile), 4096);
  178. var bos = new Packages.java.io.BufferedOutputStream(new Packages.java.io.FileOutputStream(outFile), 4096);
  179. var theChar;
  180. while ((theChar = bis.read()) != -1) {
  181. bos.write(theChar);
  182. }
  183. bos.close();
  184. bis.close();
  185. },
  186. /**
  187. * Creates a series of nested directories.
  188. */
  189. mkPath: function(/**Array*/ path) {
  190. if (path.constructor != Array) path = path.split(/[\\\/]/);
  191. var make = "";
  192. for (var i = 0, l = path.length; i < l; i++) {
  193. make += path[i] + SYS.slash;
  194. if (! IO.exists(make)) {
  195. IO.makeDir(make);
  196. }
  197. }
  198. },
  199. /**
  200. * Creates a directory at the given path.
  201. */
  202. makeDir: function(/**string*/ path) {
  203. (new File(path)).mkdir();
  204. },
  205. /**
  206. * @type string[]
  207. * @param dir The starting directory to look in.
  208. * @param [recurse=1] How many levels deep to scan.
  209. * @returns An array of all the paths to files in the given dir.
  210. */
  211. ls: function(/**string*/ dir, /**number*/ recurse, _allFiles, _path) {
  212. if (_path === undefined) { // initially
  213. var _allFiles = [];
  214. var _path = [dir];
  215. }
  216. if (_path.length == 0) return _allFiles;
  217. if (recurse === undefined) recurse = 1;
  218. dir = new File(dir);
  219. if (!dir.directory) return [String(dir)];
  220. var files = dir.list();
  221. for (var f = 0; f < files.length; f++) {
  222. var file = String(files[f]);
  223. if (file.match(/^\.[^\.\/\\]/)) continue; // skip dot files
  224. if ((new File(_path.join(SYS.slash)+SYS.slash+file)).list()) { // it's a directory
  225. _path.push(file);
  226. if (_path.length-1 < recurse) IO.ls(_path.join(SYS.slash), recurse, _allFiles, _path);
  227. _path.pop();
  228. }
  229. else {
  230. _allFiles.push((_path.join(SYS.slash)+SYS.slash+file).replace(SYS.slash+SYS.slash, SYS.slash));
  231. }
  232. }
  233. return _allFiles;
  234. },
  235. /**
  236. * @type boolean
  237. */
  238. exists: function(/**string*/ path) {
  239. file = new File(path);
  240. if (file.isDirectory()){
  241. return true;
  242. }
  243. if (!file.exists()){
  244. return false;
  245. }
  246. if (!file.canRead()){
  247. return false;
  248. }
  249. return true;
  250. },
  251. /**
  252. *
  253. */
  254. open: function(/**string*/ path, /**string*/ append) {
  255. var append = true;
  256. var outFile = new File(path);
  257. var out = new Packages.java.io.PrintWriter(
  258. new Packages.java.io.OutputStreamWriter(
  259. new Packages.java.io.FileOutputStream(outFile, append),
  260. IO.encoding
  261. )
  262. );
  263. return out;
  264. },
  265. /**
  266. * Sets {@link IO.encoding}.
  267. * Encoding is used when reading and writing text to files,
  268. * and in the meta tags of HTML output.
  269. */
  270. setEncoding: function(/**string*/ encoding) {
  271. if (/ISO-8859-([0-9]+)/i.test(encoding)) {
  272. IO.encoding = "ISO8859_"+RegExp.$1;
  273. }
  274. else {
  275. IO.encoding = encoding;
  276. }
  277. },
  278. /**
  279. * @default "utf-8"
  280. * @private
  281. */
  282. encoding: "utf-8",
  283. /**
  284. * Load the given script.
  285. */
  286. include: function(relativePath) {
  287. load(SYS.pwd+relativePath);
  288. },
  289. /**
  290. * Loads all scripts from the given directory path.
  291. */
  292. includeDir: function(path) {
  293. if (!path) return;
  294. for (var lib = IO.ls(SYS.pwd+path), i = 0; i < lib.length; i++)
  295. load(lib[i]);
  296. }
  297. }
  298. // now run the application
  299. IO.include("frame.js");
  300. IO.include("main.js");
  301. main();