author | wetmore |
Thu, 21 Sep 2017 12:13:25 -0700 | |
changeset 47238 | 2557ac47e731 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
16482
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
2 |
* Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
/** |
|
27 |
* jrunscript JavaScript built-in functions and objects. |
|
28 |
*/ |
|
29 |
||
30 |
/** |
|
16482
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
31 |
* Creates an object that delegates all method calls on |
2 | 32 |
* it to the 'invoke' method on the given delegate object.<br> |
33 |
* |
|
34 |
* Example: |
|
35 |
* <pre> |
|
36 |
* <code> |
|
37 |
* var x = { invoke: function(name, args) { //code...} |
|
38 |
* var y = new JSInvoker(x); |
|
39 |
* y.func(3, 3); // calls x.invoke('func', args); where args is array of arguments |
|
40 |
* </code> |
|
41 |
* </pre> |
|
42 |
* @param obj object to be wrapped by JSInvoker |
|
43 |
* @constructor |
|
44 |
*/ |
|
45 |
function JSInvoker(obj) { |
|
16482
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
46 |
return new JSAdapter({ |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
47 |
__get__ : function(name) { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
48 |
return function() { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
49 |
return obj.invoke(name, arguments); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
50 |
} |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
51 |
} |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
52 |
}); |
2 | 53 |
} |
54 |
||
55 |
/** |
|
56 |
* This variable represents OS environment. Environment |
|
57 |
* variables can be accessed as fields of this object. For |
|
58 |
* example, env.PATH will return PATH value configured. |
|
59 |
*/ |
|
60 |
var env = new JSAdapter({ |
|
16482
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
61 |
__get__ : function (name) { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
62 |
return java.lang.System.getenv(name); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
63 |
}, |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
64 |
__has__ : function (name) { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
65 |
return java.lang.System.getenv().containsKey(name); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
66 |
}, |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
67 |
__getIds__ : function() { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
68 |
return java.lang.System.getenv().keySet().toArray(); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
69 |
}, |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
70 |
__delete__ : function(name) { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
71 |
println("can't delete env item"); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
72 |
}, |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
73 |
__put__ : function (name, value) { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
74 |
println("can't change env item"); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
75 |
}, |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
76 |
toString: function() { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
77 |
return java.lang.System.getenv().toString(); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
78 |
} |
2 | 79 |
}); |
80 |
||
81 |
/** |
|
82 |
* Creates a convenient script object to deal with java.util.Map instances. |
|
83 |
* The result script object's field names are keys of the Map. For example, |
|
84 |
* scriptObj.keyName can be used to access value associated with given key.<br> |
|
85 |
* Example: |
|
86 |
* <pre> |
|
87 |
* <code> |
|
88 |
* var x = java.lang.SystemProperties(); |
|
89 |
* var y = jmap(x); |
|
90 |
* println(y['java.class.path']); // prints java.class.path System property |
|
91 |
* delete y['java.class.path']; // remove java.class.path System property |
|
92 |
* </code> |
|
93 |
* </pre> |
|
16482
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
94 |
* |
2 | 95 |
* @param map java.util.Map instance that will be wrapped |
96 |
* @constructor |
|
97 |
*/ |
|
16482
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
98 |
function jmap(map) { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
99 |
return new JSAdapter({ |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
100 |
__get__ : function(name) { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
101 |
if (map.containsKey(name)) { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
102 |
return map.get(name); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
103 |
} else { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
104 |
return undefined; |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
105 |
} |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
106 |
}, |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
107 |
__has__ : function(name) { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
108 |
return map.containsKey(name); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
109 |
}, |
2 | 110 |
|
16482
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
111 |
__delete__ : function (name) { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
112 |
return map.remove(name); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
113 |
}, |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
114 |
__put__ : function(name, value) { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
115 |
map.put(name, value); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
116 |
}, |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
117 |
__getIds__ : function() { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
118 |
return map.keySet().toArray(); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
119 |
}, |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
120 |
toString: function() { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
121 |
return map.toString(); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
122 |
} |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
123 |
}); |
2 | 124 |
} |
125 |
||
126 |
/** |
|
127 |
* Creates a convenient script object to deal with java.util.List instances. |
|
128 |
* The result script object behaves like an array. For example, |
|
129 |
* scriptObj[index] syntax can be used to access values in the List instance. |
|
130 |
* 'length' field gives size of the List. <br> |
|
131 |
* |
|
132 |
* Example: |
|
133 |
* <pre> |
|
134 |
* <code> |
|
135 |
* var x = new java.util.ArrayList(4); |
|
136 |
* x.add('Java'); |
|
137 |
* x.add('JavaScript'); |
|
138 |
* x.add('SQL'); |
|
139 |
* x.add('XML'); |
|
140 |
* |
|
141 |
* var y = jlist(x); |
|
142 |
* println(y[2]); // prints third element of list |
|
143 |
* println(y.length); // prints size of the list |
|
144 |
* |
|
145 |
* @param map java.util.List instance that will be wrapped |
|
146 |
* @constructor |
|
147 |
*/ |
|
148 |
function jlist(list) { |
|
16482
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
149 |
function isValid(index) { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
150 |
return typeof(index) == 'number' && |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
151 |
index > -1 && index < list.size(); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
152 |
} |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
153 |
return new JSAdapter({ |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
154 |
__get__ : function(name) { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
155 |
if (isValid(name)) { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
156 |
return list.get(name); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
157 |
} else if (name == 'length') { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
158 |
return list.size(); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
159 |
} else { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
160 |
return undefined; |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
161 |
} |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
162 |
}, |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
163 |
__has__ : function (name) { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
164 |
return isValid(name) || name == 'length'; |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
165 |
}, |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
166 |
__delete__ : function(name) { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
167 |
if (isValid(name)) { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
168 |
list.remove(name); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
169 |
} |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
170 |
}, |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
171 |
__put__ : function(name, value) { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
172 |
if (isValid(name)) { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
173 |
list.set(name, value); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
174 |
} |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
175 |
}, |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
176 |
__getIds__: function() { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
177 |
var res = new Array(list.size()); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
178 |
for (var i = 0; i < res.length; i++) { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
179 |
res[i] = i; |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
180 |
} |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
181 |
return res; |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
182 |
}, |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
183 |
toString: function() { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
184 |
return list.toString(); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
185 |
} |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
186 |
}); |
2 | 187 |
} |
188 |
||
189 |
/** |
|
16482
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
190 |
* This is java.lang.System properties wrapped by JSAdapter. |
2 | 191 |
* For eg. to access java.class.path property, you can use |
192 |
* the syntax sysProps["java.class.path"] |
|
193 |
*/ |
|
16482
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
194 |
var sysProps = new JSAdapter({ |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
195 |
__get__ : function (name) { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
196 |
return java.lang.System.getProperty(name); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
197 |
}, |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
198 |
__has__ : function (name) { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
199 |
return java.lang.System.getProperty(name) != null; |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
200 |
}, |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
201 |
__getIds__ : function() { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
202 |
return java.lang.System.getProperties().keySet().toArray(); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
203 |
}, |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
204 |
__delete__ : function(name) { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
205 |
java.lang.System.clearProperty(name); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
206 |
return true; |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
207 |
}, |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
208 |
__put__ : function (name, value) { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
209 |
java.lang.System.setProperty(name, value); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
210 |
}, |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
211 |
toString: function() { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
212 |
return "<system properties>"; |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
213 |
} |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
214 |
}); |
2 | 215 |
|
216 |
// stdout, stderr & stdin |
|
217 |
var out = java.lang.System.out; |
|
218 |
var err = java.lang.System.err; |
|
219 |
// can't use 'in' because it is a JavaScript keyword :-( |
|
220 |
var inp = java.lang.System["in"]; |
|
221 |
||
16482
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
222 |
var BufferedInputStream = java.io.BufferedInputStream; |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
223 |
var BufferedOutputStream = java.io.BufferedOutputStream; |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
224 |
var BufferedReader = java.io.BufferedReader; |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
225 |
var DataInputStream = java.io.DataInputStream; |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
226 |
var File = java.io.File; |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
227 |
var FileInputStream = java.io.FileInputStream; |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
228 |
var FileOutputStream = java.io.FileOutputStream; |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
229 |
var InputStream = java.io.InputStream; |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
230 |
var InputStreamReader = java.io.InputStreamReader; |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
231 |
var OutputStream = java.io.OutputStream; |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
232 |
var Reader = java.io.Reader; |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
233 |
var URL = java.net.URL; |
2 | 234 |
|
235 |
/** |
|
236 |
* Generic any object to input stream mapper |
|
16482
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
237 |
* @param str input file name, URL or InputStream |
2 | 238 |
* @return InputStream object |
239 |
* @private |
|
240 |
*/ |
|
241 |
function inStream(str) { |
|
16482
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
242 |
if (typeof(str) == "string") { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
243 |
// '-' means standard input |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
244 |
if (str == '-') { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
245 |
return java.lang.System["in"]; |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
246 |
} |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
247 |
// try file first |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
248 |
var file = null; |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
249 |
try { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
250 |
file = pathToFile(str); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
251 |
} catch (e) { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
252 |
} |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
253 |
if (file && file.exists()) { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
254 |
return new FileInputStream(file); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
255 |
} else { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
256 |
try { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
257 |
// treat the string as URL |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
258 |
return new URL(str).openStream(); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
259 |
} catch (e) { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
260 |
throw 'file or URL ' + str + ' not found'; |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
261 |
} |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
262 |
} |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
263 |
} else { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
264 |
if (str instanceof InputStream) { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
265 |
return str; |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
266 |
} else if (str instanceof URL) { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
267 |
return str.openStream(); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
268 |
} else if (str instanceof File) { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
269 |
return new FileInputStream(str); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
270 |
} |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
271 |
} |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
272 |
// everything failed, just give input stream |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
273 |
return java.lang.System["in"]; |
2 | 274 |
} |
275 |
||
276 |
/** |
|
277 |
* Generic any object to output stream mapper |
|
16482
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
278 |
* |
2 | 279 |
* @param out output file name or stream |
280 |
* @return OutputStream object |
|
281 |
* @private |
|
282 |
*/ |
|
283 |
function outStream(out) { |
|
16482
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
284 |
if (typeof(out) == "string") { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
285 |
if (out == '>') { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
286 |
return java.lang.System.out; |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
287 |
} else { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
288 |
// treat it as file |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
289 |
return new FileOutputStream(pathToFile(out)); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
290 |
} |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
291 |
} else { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
292 |
if (out instanceof OutputStream) { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
293 |
return out; |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
294 |
} else if (out instanceof File) { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
295 |
return new FileOutputStream(out); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
296 |
} |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
297 |
} |
2 | 298 |
|
16482
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
299 |
// everything failed, just return System.out |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
300 |
return java.lang.System.out; |
2 | 301 |
} |
302 |
||
303 |
/** |
|
304 |
* stream close takes care not to close stdin, out & err. |
|
305 |
* @private |
|
306 |
*/ |
|
307 |
function streamClose(stream) { |
|
16482
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
308 |
if (stream) { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
309 |
if (stream != java.lang.System["in"] && |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
310 |
stream != java.lang.System.out && |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
311 |
stream != java.lang.System.err) { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
312 |
try { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
313 |
stream.close(); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
314 |
} catch (e) { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
315 |
println(e); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
316 |
} |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
317 |
} |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
318 |
} |
2 | 319 |
} |
320 |
||
321 |
/** |
|
322 |
* Loads and evaluates JavaScript code from a stream or file or URL<br> |
|
323 |
* |
|
324 |
* Examples: |
|
325 |
* <pre> |
|
326 |
* <code> |
|
327 |
* load('test.js'); // load script file 'test.js' |
|
328 |
* load('http://java.sun.com/foo.js'); // load from a URL |
|
329 |
* </code> |
|
330 |
* </pre> |
|
331 |
* |
|
332 |
* @param str input from which script is loaded and evaluated |
|
333 |
*/ |
|
16482
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
334 |
if (typeof(load) == 'undefined') { |
19064
5a45b684b8b1
8021773: print function as defined by jrunscript's init.js script is incompatible with nashorn's definition
sundar
parents:
16482
diff
changeset
|
335 |
this.load = function(str) { |
16482
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
336 |
var stream = inStream(str); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
337 |
var bstream = new BufferedInputStream(stream); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
338 |
var reader = new BufferedReader(new InputStreamReader(bstream)); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
339 |
var oldFilename = engine.get(engine.FILENAME); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
340 |
engine.put(engine.FILENAME, str); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
341 |
try { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
342 |
engine.eval(reader); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
343 |
} finally { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
344 |
engine.put(engine.FILENAME, oldFilename); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
345 |
streamClose(stream); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
346 |
} |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
347 |
} |
2 | 348 |
} |
349 |
||
350 |
// file system utilities |
|
351 |
||
352 |
/** |
|
353 |
* Creates a Java byte[] of given length |
|
354 |
* @param len size of the array to create |
|
355 |
* @private |
|
356 |
*/ |
|
357 |
function javaByteArray(len) { |
|
16482
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
358 |
return java.lang.reflect.Array.newInstance(java.lang.Byte.TYPE, len); |
2 | 359 |
} |
360 |
||
361 |
var curDir = new File('.'); |
|
362 |
||
363 |
/** |
|
364 |
* Print present working directory |
|
365 |
*/ |
|
366 |
function pwd() { |
|
16482
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
367 |
println(curDir.getAbsolutePath()); |
2 | 368 |
} |
369 |
||
370 |
/** |
|
371 |
* Changes present working directory to given directory |
|
372 |
* @param target directory to change to. optional, defaults to user's HOME |
|
373 |
*/ |
|
374 |
function cd(target) { |
|
16482
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
375 |
if (target == undefined) { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
376 |
target = sysProps["user.home"]; |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
377 |
} |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
378 |
if (!(target instanceof File)) { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
379 |
target = pathToFile(target); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
380 |
} |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
381 |
if (target.exists() && target.isDirectory()) { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
382 |
curDir = target; |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
383 |
} else { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
384 |
println(target + " is not a directory"); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
385 |
} |
2 | 386 |
} |
387 |
||
388 |
/** |
|
389 |
* Converts path to java.io.File taking care of shell present working dir |
|
390 |
* |
|
391 |
* @param pathname file path to be converted |
|
392 |
* @private |
|
393 |
*/ |
|
394 |
function pathToFile(pathname) { |
|
16482
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
395 |
var tmp = pathname; |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
396 |
if (!(tmp instanceof File)) { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
397 |
tmp = new File(tmp); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
398 |
} |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
399 |
if (!tmp.isAbsolute()) { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
400 |
return new File(curDir, pathname); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
401 |
} else { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
402 |
return tmp; |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
403 |
} |
2 | 404 |
} |
405 |
||
406 |
/** |
|
407 |
* Copies a file or URL or stream to another file or stream |
|
408 |
* |
|
409 |
* @param from input file or URL or stream |
|
410 |
* @param to output stream or file |
|
411 |
*/ |
|
412 |
function cp(from, to) { |
|
16482
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
413 |
if (from == to) { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
414 |
println("file " + from + " cannot be copied onto itself!"); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
415 |
return; |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
416 |
} |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
417 |
var inp = inStream(from); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
418 |
var out = outStream(to); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
419 |
var binp = new BufferedInputStream(inp); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
420 |
var bout = new BufferedOutputStream(out); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
421 |
var buff = javaByteArray(1024); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
422 |
var len; |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
423 |
while ((len = binp.read(buff)) > 0 ) |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
424 |
bout.write(buff, 0, len); |
2 | 425 |
|
16482
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
426 |
bout.flush(); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
427 |
streamClose(inp); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
428 |
streamClose(out); |
2 | 429 |
} |
430 |
||
431 |
/** |
|
432 |
* Shows the content of a file or URL or any InputStream<br> |
|
433 |
* Examples: |
|
434 |
* <pre> |
|
435 |
* <code> |
|
436 |
* cat('test.txt'); // show test.txt file contents |
|
16482
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
437 |
* cat('http://java.net'); // show the contents from the URL http://java.net |
2 | 438 |
* </code> |
439 |
* </pre> |
|
440 |
* @param obj input to show |
|
441 |
* @param pattern optional. show only the lines matching the pattern |
|
442 |
*/ |
|
443 |
function cat(obj, pattern) { |
|
16482
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
444 |
if (obj instanceof File && obj.isDirectory()) { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
445 |
ls(obj); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
446 |
return; |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
447 |
} |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
448 |
|
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
449 |
var inp = null; |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
450 |
if (!(obj instanceof Reader)) { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
451 |
inp = inStream(obj); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
452 |
obj = new BufferedReader(new InputStreamReader(inp)); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
453 |
} |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
454 |
var line; |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
455 |
if (pattern) { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
456 |
var count = 1; |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
457 |
while ((line=obj.readLine()) != null) { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
458 |
if (line.match(pattern)) { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
459 |
println(count + "\t: " + line); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
460 |
} |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
461 |
count++; |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
462 |
} |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
463 |
} else { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
464 |
while ((line=obj.readLine()) != null) { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
465 |
println(line); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
466 |
} |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
467 |
} |
2 | 468 |
} |
469 |
||
470 |
/** |
|
471 |
* Returns directory part of a filename |
|
472 |
* |
|
473 |
* @param pathname input path name |
|
474 |
* @return directory part of the given file name |
|
475 |
*/ |
|
476 |
function dirname(pathname) { |
|
16482
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
477 |
var dirName = "."; |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
478 |
// Normalize '/' to local file separator before work. |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
479 |
var i = pathname.replace('/', File.separatorChar ).lastIndexOf( |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
480 |
File.separator ); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
481 |
if ( i != -1 ) |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
482 |
dirName = pathname.substring(0, i); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
483 |
return dirName; |
2 | 484 |
} |
485 |
||
486 |
/** |
|
487 |
* Creates a new dir of given name |
|
488 |
* |
|
489 |
* @param dir name of the new directory |
|
490 |
*/ |
|
491 |
function mkdir(dir) { |
|
16482
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
492 |
dir = pathToFile(dir); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
493 |
println(dir.mkdir()? "created" : "can not create dir"); |
2 | 494 |
} |
495 |
||
496 |
/** |
|
16482
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
497 |
* Creates the directory named by given pathname, including |
2 | 498 |
* any necessary but nonexistent parent directories. |
499 |
* |
|
500 |
* @param dir input path name |
|
501 |
*/ |
|
502 |
function mkdirs(dir) { |
|
16482
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
503 |
dir = pathToFile(dir); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
504 |
println(dir.mkdirs()? "created" : "can not create dirs"); |
2 | 505 |
} |
16482
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
506 |
|
2 | 507 |
/** |
16482
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
508 |
* Removes a given file |
2 | 509 |
* |
16482
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
510 |
* @param pathname name of the file |
2 | 511 |
*/ |
512 |
function rm(pathname) { |
|
16482
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
513 |
var file = pathToFile(pathname); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
514 |
if (!file.exists()) { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
515 |
println("file not found: " + pathname); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
516 |
return false; |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
517 |
} |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
518 |
// note that delete is a keyword in JavaScript! |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
519 |
println(file["delete"]()? "deleted" : "can not delete"); |
2 | 520 |
} |
521 |
||
522 |
/** |
|
523 |
* Removes a given directory |
|
524 |
* |
|
525 |
* @param pathname name of the directory |
|
526 |
*/ |
|
527 |
function rmdir(pathname) { |
|
16482
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
528 |
rm(pathname); |
2 | 529 |
} |
530 |
||
531 |
/** |
|
532 |
* Synonym for 'rm' |
|
533 |
*/ |
|
534 |
function del(pathname) { |
|
16482
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
535 |
rm(pathname); |
2 | 536 |
} |
537 |
||
538 |
/** |
|
539 |
* Moves a file to another |
|
540 |
* |
|
541 |
* @param from original name of the file |
|
542 |
* @param to new name for the file |
|
543 |
*/ |
|
544 |
function mv(from, to) { |
|
16482
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
545 |
println(pathToFile(from).renameTo(pathToFile(to))? |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
546 |
"moved" : "can not move"); |
2 | 547 |
} |
548 |
||
549 |
/** |
|
550 |
* Synonym for 'mv'. |
|
551 |
*/ |
|
552 |
function ren(from, to) { |
|
16482
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
553 |
mv(from, to); |
2 | 554 |
} |
555 |
||
16482
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
556 |
var months = [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
557 |
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ]; |
2 | 558 |
|
559 |
/** |
|
560 |
* Helper function called by ls |
|
561 |
* @private |
|
16482
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
562 |
*/ |
2 | 563 |
function printFile(f) { |
16482
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
564 |
var sb = new java.lang.StringBuffer(); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
565 |
sb.append(f.isDirectory()? "d" : "-"); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
566 |
sb.append(f.canRead() ? "r": "-" ); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
567 |
sb.append(f.canWrite() ? "w": "-" ); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
568 |
sb.append(" "); |
2 | 569 |
|
16482
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
570 |
var d = new java.util.Date(f.lastModified()); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
571 |
var c = new java.util.GregorianCalendar(); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
572 |
c.setTime(d); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
573 |
var day = c.get(java.util.Calendar.DAY_OF_MONTH); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
574 |
sb.append(months[c.get(java.util.Calendar.MONTH)] |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
575 |
+ " " + day ); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
576 |
if (day < 10) { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
577 |
sb.append(" "); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
578 |
} |
2 | 579 |
|
16482
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
580 |
// to get fixed length 'length' field |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
581 |
var fieldlen = 8; |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
582 |
var len = new java.lang.StringBuffer(); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
583 |
for(var j=0; j<fieldlen; j++) |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
584 |
len.append(" "); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
585 |
len.insert(0, java.lang.Long.toString(f.length())); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
586 |
len.setLength(fieldlen); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
587 |
// move the spaces to the front |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
588 |
var si = len.toString().indexOf(" "); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
589 |
if ( si != -1 ) { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
590 |
var pad = len.toString().substring(si); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
591 |
len.setLength(si); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
592 |
len.insert(0, pad); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
593 |
} |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
594 |
sb.append(len.toString()); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
595 |
sb.append(" "); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
596 |
sb.append(f.getName()); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
597 |
if (f.isDirectory()) { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
598 |
sb.append('/'); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
599 |
} |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
600 |
println(sb.toString()); |
2 | 601 |
} |
602 |
||
603 |
/** |
|
604 |
* Lists the files in a directory |
|
605 |
* |
|
606 |
* @param dir directory from which to list the files. optional, default to pwd |
|
607 |
* @param filter pattern to filter the files listed. optional, default is '.'. |
|
608 |
*/ |
|
609 |
function ls(dir, filter) { |
|
16482
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
610 |
if (dir) { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
611 |
dir = pathToFile(dir); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
612 |
} else { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
613 |
dir = curDir; |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
614 |
} |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
615 |
if (dir.isDirectory()) { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
616 |
var files = dir.listFiles(); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
617 |
for (var i in files) { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
618 |
var f = files[i]; |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
619 |
if (filter) { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
620 |
if(!f.getName().match(filter)) { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
621 |
continue; |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
622 |
} |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
623 |
} |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
624 |
printFile(f); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
625 |
} |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
626 |
} else { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
627 |
printFile(dir); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
628 |
} |
2 | 629 |
} |
630 |
||
631 |
/** |
|
632 |
* Synonym for 'ls'. |
|
633 |
*/ |
|
634 |
function dir(d, filter) { |
|
16482
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
635 |
ls(d, filter); |
2 | 636 |
} |
637 |
||
638 |
/** |
|
639 |
* Unix-like grep, but accepts JavaScript regex patterns |
|
640 |
* |
|
641 |
* @param pattern to search in files |
|
642 |
* @param files one or more files |
|
643 |
*/ |
|
644 |
function grep(pattern, files /*, one or more files */) { |
|
16482
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
645 |
if (arguments.length < 2) return; |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
646 |
for (var i = 1; i < arguments.length; i++) { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
647 |
println(arguments[i] + ":"); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
648 |
cat(arguments[i], pattern); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
649 |
} |
2 | 650 |
} |
651 |
||
652 |
/** |
|
653 |
* Find in files. Calls arbitrary callback function |
|
654 |
* for each matching file.<br> |
|
655 |
* |
|
16482
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
656 |
* Examples: |
2 | 657 |
* <pre> |
658 |
* <code> |
|
16482
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
659 |
* find('.') |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
660 |
* find('.', '.*\.class', rm); // remove all .class files |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
661 |
* find('.', '.*\.java'); // print fullpath of each .java file |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
662 |
* find('.', '.*\.java', cat); // print all .java files |
2 | 663 |
* </code> |
664 |
* </pre> |
|
665 |
* |
|
666 |
* @param dir directory to search files |
|
667 |
* @param pattern to search in the files |
|
668 |
* @param callback function to call for matching files |
|
669 |
*/ |
|
670 |
function find(dir, pattern, callback) { |
|
16482
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
671 |
dir = pathToFile(dir); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
672 |
if (!callback) callback = print; |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
673 |
var files = dir.listFiles(); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
674 |
for (var f in files) { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
675 |
var file = files[f]; |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
676 |
if (file.isDirectory()) { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
677 |
find(file, pattern, callback); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
678 |
} else { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
679 |
if (pattern) { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
680 |
if (file.getName().match(pattern)) { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
681 |
callback(file); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
682 |
} |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
683 |
} else { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
684 |
callback(file); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
685 |
} |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
686 |
} |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
687 |
} |
2 | 688 |
} |
689 |
||
690 |
// process utilities |
|
691 |
||
692 |
/** |
|
693 |
* Exec's a child process, waits for completion & returns exit code |
|
694 |
* |
|
695 |
* @param cmd command to execute in child process |
|
696 |
*/ |
|
697 |
function exec(cmd) { |
|
16482
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
698 |
var process = java.lang.Runtime.getRuntime().exec(cmd); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
699 |
var inp = new DataInputStream(process.getInputStream()); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
700 |
var line = null; |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
701 |
while ((line = inp.readLine()) != null) { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
702 |
println(line); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
703 |
} |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
704 |
process.waitFor(); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
705 |
$exit = process.exitValue(); |
2 | 706 |
} |
707 |
||
16482
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
708 |
if (typeof(exit) == 'undefined') { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
709 |
/** |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
710 |
* Exit the shell program. |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
711 |
* |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
712 |
* @param exitCode integer code returned to OS shell. |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
713 |
* optional, defaults to 0 |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
714 |
*/ |
19064
5a45b684b8b1
8021773: print function as defined by jrunscript's init.js script is incompatible with nashorn's definition
sundar
parents:
16482
diff
changeset
|
715 |
this.exit = function (code) { |
16482
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
716 |
if (code) { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
717 |
java.lang.System.exit(code + 0); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
718 |
} else { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
719 |
java.lang.System.exit(0); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
720 |
} |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
721 |
} |
2 | 722 |
} |
723 |
||
16482
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
724 |
if (typeof(quit) == 'undefined') { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
725 |
/** |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
726 |
* synonym for exit |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
727 |
*/ |
19064
5a45b684b8b1
8021773: print function as defined by jrunscript's init.js script is incompatible with nashorn's definition
sundar
parents:
16482
diff
changeset
|
728 |
this.quit = function (code) { |
16482
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
729 |
exit(code); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
730 |
} |
2 | 731 |
} |
732 |
||
733 |
// XML utilities |
|
734 |
||
16482
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
735 |
/** |
2 | 736 |
* Converts input to DOM Document object |
737 |
* |
|
738 |
* @param inp file or reader. optional, without this param, |
|
739 |
* this function returns a new DOM Document. |
|
740 |
* @return returns a DOM Document object |
|
741 |
*/ |
|
742 |
function XMLDocument(inp) { |
|
16482
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
743 |
var factory = javax.xml.parsers.DocumentBuilderFactory.newInstance(); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
744 |
var builder = factory.newDocumentBuilder(); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
745 |
if (inp) { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
746 |
if (typeof(inp) == "string") { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
747 |
return builder.parse(pathToFile(inp)); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
748 |
} else { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
749 |
return builder.parse(inp); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
750 |
} |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
751 |
} else { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
752 |
return builder.newDocument(); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
753 |
} |
2 | 754 |
} |
755 |
||
756 |
/** |
|
757 |
* Converts arbitrary stream, file, URL to XMLSource |
|
758 |
* |
|
759 |
* @param inp input stream or file or URL |
|
760 |
* @return XMLSource object |
|
761 |
*/ |
|
762 |
function XMLSource(inp) { |
|
16482
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
763 |
if (inp instanceof javax.xml.transform.Source) { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
764 |
return inp; |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
765 |
} else if (inp instanceof Packages.org.w3c.dom.Document) { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
766 |
return new javax.xml.transform.dom.DOMSource(inp); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
767 |
} else { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
768 |
inp = new BufferedInputStream(inStream(inp)); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
769 |
return new javax.xml.transform.stream.StreamSource(inp); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
770 |
} |
2 | 771 |
} |
772 |
||
773 |
/** |
|
774 |
* Converts arbitrary stream, file to XMLResult |
|
775 |
* |
|
776 |
* @param inp output stream or file |
|
777 |
* @return XMLResult object |
|
778 |
*/ |
|
779 |
function XMLResult(out) { |
|
16482
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
780 |
if (out instanceof javax.xml.transform.Result) { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
781 |
return out; |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
782 |
} else if (out instanceof Packages.org.w3c.dom.Document) { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
783 |
return new javax.xml.transform.dom.DOMResult(out); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
784 |
} else { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
785 |
out = new BufferedOutputStream(outStream(out)); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
786 |
return new javax.xml.transform.stream.StreamResult(out); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
787 |
} |
2 | 788 |
} |
789 |
||
790 |
/** |
|
16482
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
791 |
* Perform XSLT transform |
2 | 792 |
* |
793 |
* @param inp Input XML to transform (URL, File or InputStream) |
|
794 |
* @param style XSL Stylesheet to be used (URL, File or InputStream). optional. |
|
795 |
* @param out Output XML (File or OutputStream |
|
796 |
*/ |
|
797 |
function XSLTransform(inp, style, out) { |
|
16482
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
798 |
switch (arguments.length) { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
799 |
case 2: |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
800 |
inp = arguments[0]; |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
801 |
out = arguments[1]; |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
802 |
break; |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
803 |
case 3: |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
804 |
inp = arguments[0]; |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
805 |
style = arguments[1]; |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
806 |
out = arguments[2]; |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
807 |
break; |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
808 |
default: |
21591 | 809 |
println("XSL transform requires 2 or 3 arguments"); |
16482
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
810 |
return; |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
811 |
} |
2 | 812 |
|
16482
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
813 |
var factory = javax.xml.transform.TransformerFactory.newInstance(); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
814 |
var transformer; |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
815 |
if (style) { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
816 |
transformer = factory.newTransformer(XMLSource(style)); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
817 |
} else { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
818 |
transformer = factory.newTransformer(); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
819 |
} |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
820 |
var source = XMLSource(inp); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
821 |
var result = XMLResult(out); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
822 |
transformer.transform(source, result); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
823 |
if (source.getInputStream) { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
824 |
streamClose(source.getInputStream()); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
825 |
} |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
826 |
if (result.getOutputStream) { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
827 |
streamClose(result.getOutputStream()); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
828 |
} |
2 | 829 |
} |
830 |
||
831 |
// miscellaneous utilities |
|
832 |
||
833 |
/** |
|
16482
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
834 |
* Prints which command is selected from PATH |
2 | 835 |
* |
836 |
* @param cmd name of the command searched from PATH |
|
837 |
*/ |
|
838 |
function which(cmd) { |
|
16482
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
839 |
var st = new java.util.StringTokenizer(env.PATH, File.pathSeparator); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
840 |
while (st.hasMoreTokens()) { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
841 |
var file = new File(st.nextToken(), cmd); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
842 |
if (file.exists()) { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
843 |
println(file.getAbsolutePath()); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
844 |
return; |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
845 |
} |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
846 |
} |
2 | 847 |
} |
848 |
||
849 |
/** |
|
850 |
* Prints IP addresses of given domain name |
|
851 |
* |
|
852 |
* @param name domain name |
|
853 |
*/ |
|
854 |
function ip(name) { |
|
16482
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
855 |
var addrs = InetAddress.getAllByName(name); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
856 |
for (var i in addrs) { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
857 |
println(addrs[i]); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
858 |
} |
2 | 859 |
} |
860 |
||
861 |
/** |
|
862 |
* Prints current date in current locale |
|
863 |
*/ |
|
864 |
function date() { |
|
16482
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
865 |
println(new Date().toLocaleString()); |
2 | 866 |
} |
867 |
||
868 |
/** |
|
869 |
* Echoes the given string arguments |
|
870 |
*/ |
|
871 |
function echo(x) { |
|
16482
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
872 |
for (var i = 0; i < arguments.length; i++) { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
873 |
println(arguments[i]); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
874 |
} |
2 | 875 |
} |
876 |
||
16482
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
877 |
if (typeof(printf) == 'undefined') { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
878 |
/** |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
879 |
* This is C-like printf |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
880 |
* |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
881 |
* @param format string to format the rest of the print items |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
882 |
* @param args variadic argument list |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
883 |
*/ |
19064
5a45b684b8b1
8021773: print function as defined by jrunscript's init.js script is incompatible with nashorn's definition
sundar
parents:
16482
diff
changeset
|
884 |
this.printf = function (format, args/*, more args*/) { |
16482
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
885 |
var array = java.lang.reflect.Array.newInstance(java.lang.Object, |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
886 |
arguments.length - 1); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
887 |
for (var i = 0; i < array.length; i++) { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
888 |
array[i] = arguments[i+1]; |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
889 |
} |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
890 |
java.lang.System.out.printf(format, array); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
891 |
} |
2 | 892 |
} |
893 |
||
894 |
/** |
|
895 |
* Reads one or more lines from stdin after printing prompt |
|
896 |
* |
|
897 |
* @param prompt optional, default is '>' |
|
898 |
* @param multiline to tell whether to read single line or multiple lines |
|
899 |
*/ |
|
900 |
function read(prompt, multiline) { |
|
16482
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
901 |
if (!prompt) { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
902 |
prompt = '>'; |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
903 |
} |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
904 |
var inp = java.lang.System["in"]; |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
905 |
var reader = new BufferedReader(new InputStreamReader(inp)); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
906 |
if (multiline) { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
907 |
var line = ''; |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
908 |
while (true) { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
909 |
java.lang.System.err.print(prompt); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
910 |
java.lang.System.err.flush(); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
911 |
var tmp = reader.readLine(); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
912 |
if (tmp == '' || tmp == null) break; |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
913 |
line += tmp + '\n'; |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
914 |
} |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
915 |
return line; |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
916 |
} else { |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
917 |
java.lang.System.err.print(prompt); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
918 |
java.lang.System.err.flush(); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
919 |
return reader.readLine(); |
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
920 |
} |
2 | 921 |
} |
16482
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
922 |
|
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
923 |
if (typeof(println) == 'undefined') { |
19064
5a45b684b8b1
8021773: print function as defined by jrunscript's init.js script is incompatible with nashorn's definition
sundar
parents:
16482
diff
changeset
|
924 |
// just synonym to print |
5a45b684b8b1
8021773: print function as defined by jrunscript's init.js script is incompatible with nashorn's definition
sundar
parents:
16482
diff
changeset
|
925 |
this.println = print; |
5a45b684b8b1
8021773: print function as defined by jrunscript's init.js script is incompatible with nashorn's definition
sundar
parents:
16482
diff
changeset
|
926 |
} |
16482
cbc9c93df1fd
8010136: Make jrunscript's init.js to work on nashorn
sundar
parents:
9035
diff
changeset
|
927 |