*
- * Examples:
+ * Examples:
*
*
- * find('.')
- * find('.', '.*\.class', rm); // remove all .class files
- * find('.', '.*\.java'); // print fullpath of each .java file
- * find('.', '.*\.java', cat); // print all .java files
+ * find('.')
+ * find('.', '.*\.class', rm); // remove all .class files
+ * find('.', '.*\.java'); // print fullpath of each .java file
+ * find('.', '.*\.java', cat); // print all .java files
*
*
*
@@ -637,23 +668,23 @@ function grep(pattern, files /*, one or more files */) {
* @param callback function to call for matching files
*/
function find(dir, pattern, callback) {
- dir = pathToFile(dir);
- if (!callback) callback = print;
- var files = dir.listFiles();
- for (var f in files) {
- var file = files[f];
- if (file.isDirectory()) {
- find(file, pattern, callback);
- } else {
- if (pattern) {
- if (file.getName().match(pattern)) {
- callback(file);
- }
- } else {
- callback(file);
- }
- }
- }
+ dir = pathToFile(dir);
+ if (!callback) callback = print;
+ var files = dir.listFiles();
+ for (var f in files) {
+ var file = files[f];
+ if (file.isDirectory()) {
+ find(file, pattern, callback);
+ } else {
+ if (pattern) {
+ if (file.getName().match(pattern)) {
+ callback(file);
+ }
+ } else {
+ callback(file);
+ }
+ }
+ }
}
// process utilities
@@ -664,40 +695,44 @@ function find(dir, pattern, callback) {
* @param cmd command to execute in child process
*/
function exec(cmd) {
- var process = java.lang.Runtime.getRuntime().exec(cmd);
- var inp = new DataInputStream(process.getInputStream());
- var line = null;
- while ((line = inp.readLine()) != null) {
- println(line);
- }
- process.waitFor();
- $exit = process.exitValue();
-}
-
-/**
- * Exit the shell program.
- *
- * @param exitCode integer code returned to OS shell.
- * optional, defaults to 0
- */
-function exit(code) {
- if (code) {
- java.lang.System.exit(code + 0);
- } else {
- java.lang.System.exit(0);
- }
-}
-
-/**
- * synonym for exit
- */
-function quit(code) {
- exit(code);
+ var process = java.lang.Runtime.getRuntime().exec(cmd);
+ var inp = new DataInputStream(process.getInputStream());
+ var line = null;
+ while ((line = inp.readLine()) != null) {
+ println(line);
+ }
+ process.waitFor();
+ $exit = process.exitValue();
+}
+
+if (typeof(exit) == 'undefined') {
+ /**
+ * Exit the shell program.
+ *
+ * @param exitCode integer code returned to OS shell.
+ * optional, defaults to 0
+ */
+ var exit = function (code) {
+ if (code) {
+ java.lang.System.exit(code + 0);
+ } else {
+ java.lang.System.exit(0);
+ }
+ }
+}
+
+if (typeof(quit) == 'undefined') {
+ /**
+ * synonym for exit
+ */
+ var quit = function (code) {
+ exit(code);
+ }
}
// XML utilities
-/**
+/**
* Converts input to DOM Document object
*
* @param inp file or reader. optional, without this param,
@@ -705,17 +740,17 @@ function quit(code) {
* @return returns a DOM Document object
*/
function XMLDocument(inp) {
- var factory = javax.xml.parsers.DocumentBuilderFactory.newInstance();
- var builder = factory.newDocumentBuilder();
- if (inp) {
- if (typeof(inp) == "string") {
- return builder.parse(pathToFile(inp));
- } else {
- return builder.parse(inp);
- }
- } else {
- return builder.newDocument();
- }
+ var factory = javax.xml.parsers.DocumentBuilderFactory.newInstance();
+ var builder = factory.newDocumentBuilder();
+ if (inp) {
+ if (typeof(inp) == "string") {
+ return builder.parse(pathToFile(inp));
+ } else {
+ return builder.parse(inp);
+ }
+ } else {
+ return builder.newDocument();
+ }
}
/**
@@ -725,14 +760,14 @@ function XMLDocument(inp) {
* @return XMLSource object
*/
function XMLSource(inp) {
- if (inp instanceof javax.xml.transform.Source) {
- return inp;
- } else if (inp instanceof Packages.org.w3c.dom.Document) {
- return new javax.xml.transform.dom.DOMSource(inp);
- } else {
- inp = new BufferedInputStream(inStream(inp));
- return new javax.xml.transform.stream.StreamSource(inp);
- }
+ if (inp instanceof javax.xml.transform.Source) {
+ return inp;
+ } else if (inp instanceof Packages.org.w3c.dom.Document) {
+ return new javax.xml.transform.dom.DOMSource(inp);
+ } else {
+ inp = new BufferedInputStream(inStream(inp));
+ return new javax.xml.transform.stream.StreamSource(inp);
+ }
}
/**
@@ -742,73 +777,73 @@ function XMLSource(inp) {
* @return XMLResult object
*/
function XMLResult(out) {
- if (out instanceof javax.xml.transform.Result) {
- return out;
- } else if (out instanceof Packages.org.w3c.dom.Document) {
- return new javax.xml.transform.dom.DOMResult(out);
- } else {
- out = new BufferedOutputStream(outStream(out));
- return new javax.xml.transform.stream.StreamResult(out);
- }
+ if (out instanceof javax.xml.transform.Result) {
+ return out;
+ } else if (out instanceof Packages.org.w3c.dom.Document) {
+ return new javax.xml.transform.dom.DOMResult(out);
+ } else {
+ out = new BufferedOutputStream(outStream(out));
+ return new javax.xml.transform.stream.StreamResult(out);
+ }
}
/**
- * Perform XSLT transform
+ * Perform XSLT transform
*
* @param inp Input XML to transform (URL, File or InputStream)
* @param style XSL Stylesheet to be used (URL, File or InputStream). optional.
* @param out Output XML (File or OutputStream
*/
function XSLTransform(inp, style, out) {
- switch (arguments.length) {
- case 2:
- inp = arguments[0];
- out = arguments[1];
- break;
- case 3:
- inp = arguments[0];
- style = arguments[1];
- out = arguments[2];
- break;
- default:
- println("XSL tranform requires 2 or 3 arguments");
- return;
- }
-
- var factory = javax.xml.transform.TransformerFactory.newInstance();
- var tranformer;
- if (style) {
- transformer = factory.newTransformer(XMLSource(style));
- } else {
- transformer = factory.newTransformer();
- }
- var source = XMLSource(inp);
- var result = XMLResult(out);
- transformer.transform(source, result);
- if (source.getInputStream) {
- streamClose(source.getInputStream());
- }
- if (result.getOutputStream) {
- streamClose(result.getOutputStream());
- }
+ switch (arguments.length) {
+ case 2:
+ inp = arguments[0];
+ out = arguments[1];
+ break;
+ case 3:
+ inp = arguments[0];
+ style = arguments[1];
+ out = arguments[2];
+ break;
+ default:
+ println("XSL tranform requires 2 or 3 arguments");
+ return;
+ }
+
+ var factory = javax.xml.transform.TransformerFactory.newInstance();
+ var transformer;
+ if (style) {
+ transformer = factory.newTransformer(XMLSource(style));
+ } else {
+ transformer = factory.newTransformer();
+ }
+ var source = XMLSource(inp);
+ var result = XMLResult(out);
+ transformer.transform(source, result);
+ if (source.getInputStream) {
+ streamClose(source.getInputStream());
+ }
+ if (result.getOutputStream) {
+ streamClose(result.getOutputStream());
+ }
}
// miscellaneous utilities
/**
- * Prints which command is selected from PATH
+ * Prints which command is selected from PATH
*
* @param cmd name of the command searched from PATH
*/
function which(cmd) {
- var st = new java.util.StringTokenizer(env.PATH, File.pathSeparator);
- while (st.hasMoreTokens()) {
- var file = new File(st.nextToken(), cmd);
- if (file.exists()) {
- println(file.getAbsolutePath());
- return;
- }
- }
+ var st = new java.util.StringTokenizer(env.PATH, File.pathSeparator);
+ while (st.hasMoreTokens()) {
+ var file = new File(st.nextToken(), cmd);
+ if (file.exists()) {
+ println(file.getAbsolutePath());
+ return;
+ }
+ }
}
/**
@@ -817,41 +852,43 @@ function which(cmd) {
* @param name domain name
*/
function ip(name) {
- var addrs = InetAddress.getAllByName(name);
- for (var i in addrs) {
- println(addrs[i]);
- }
+ var addrs = InetAddress.getAllByName(name);
+ for (var i in addrs) {
+ println(addrs[i]);
+ }
}
/**
* Prints current date in current locale
*/
function date() {
- println(new Date().toLocaleString());
+ println(new Date().toLocaleString());
}
/**
* Echoes the given string arguments
*/
function echo(x) {
- for (var i = 0; i < arguments.length; i++) {
- println(arguments[i]);
- }
+ for (var i = 0; i < arguments.length; i++) {
+ println(arguments[i]);
+ }
}
-/**
- * This is C-like printf
- *
- * @param format string to format the rest of the print items
- * @param args variadic argument list
- */
-function printf(format, args/*, more args*/) {
- var array = java.lang.reflect.Array.newInstance(java.lang.Object,
- arguments.length - 1);
- for (var i = 0; i < array.length; i++) {
- array[i] = arguments[i+1];
- }
- return java.lang.System.out.printf(format, array);
+if (typeof(printf) == 'undefined') {
+ /**
+ * This is C-like printf
+ *
+ * @param format string to format the rest of the print items
+ * @param args variadic argument list
+ */
+ var printf = function (format, args/*, more args*/) {
+ var array = java.lang.reflect.Array.newInstance(java.lang.Object,
+ arguments.length - 1);
+ for (var i = 0; i < array.length; i++) {
+ array[i] = arguments[i+1];
+ }
+ java.lang.System.out.printf(format, array);
+ }
}
/**
@@ -861,24 +898,48 @@ function printf(format, args/*, more args*/) {
* @param multiline to tell whether to read single line or multiple lines
*/
function read(prompt, multiline) {
- if (!prompt) {
- prompt = '>';
- }
- var inp = java.lang.System["in"];
- var reader = new BufferedReader(new InputStreamReader(inp));
- if (multiline) {
- var line = '';
- while (true) {
- java.lang.System.err.print(prompt);
- java.lang.System.err.flush();
- var tmp = reader.readLine();
- if (tmp == '' || tmp == null) break;
- line += tmp + '\n';
- }
- return line;
- } else {
- java.lang.System.err.print(prompt);
- java.lang.System.err.flush();
- return reader.readLine();
- }
+ if (!prompt) {
+ prompt = '>';
+ }
+ var inp = java.lang.System["in"];
+ var reader = new BufferedReader(new InputStreamReader(inp));
+ if (multiline) {
+ var line = '';
+ while (true) {
+ java.lang.System.err.print(prompt);
+ java.lang.System.err.flush();
+ var tmp = reader.readLine();
+ if (tmp == '' || tmp == null) break;
+ line += tmp + '\n';
+ }
+ return line;
+ } else {
+ java.lang.System.err.print(prompt);
+ java.lang.System.err.flush();
+ return reader.readLine();
+ }
+}
+
+if (typeof(println) == 'undefined') {
+ var print = function(str, newline) {
+ if (typeof(str) == 'undefined') {
+ str = 'undefined';
+ } else if (str == null) {
+ str = 'null';
+ }
+
+ if (!(out instanceof java.io.PrintWriter)) {
+ out = new java.io.PrintWriter(out);
+ }
+
+ out.print(String(str));
+ if (newline) {
+ out.print('\n');
+ }
+ out.flush();
+ }
+
+ var println = function(str) {
+ print(str, true);
+ };
}