| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
import org.mozilla.javascript.*; |
| 8 |
import org.mozilla.javascript.annotations.JSConstructor; |
| 9 |
import org.mozilla.javascript.annotations.JSFunction; |
| 10 |
import org.mozilla.javascript.annotations.JSGetter; |
| 11 |
|
| 12 |
import java.io.*; |
| 13 |
import java.util.List; |
| 14 |
import java.util.ArrayList; |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public class File extends ScriptableObject { |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
private static final long serialVersionUID = 2549960399774237828L; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public File() { |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
@JSConstructor |
| 71 |
public static Scriptable jsConstructor(Context cx, Object[] args, |
| 72 |
Function ctorObj, |
| 73 |
boolean inNewExpr) |
| 74 |
{ |
| 75 |
File result = new File(); |
| 76 |
if (args.length == 0 || args[0] == Context.getUndefinedValue()) { |
| 77 |
result.name = ""; |
| 78 |
result.file = null; |
| 79 |
} else { |
| 80 |
result.name = Context.toString(args[0]); |
| 81 |
result.file = new java.io.File(result.name); |
| 82 |
} |
| 83 |
return result; |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
@Override |
| 90 |
public String getClassName() { |
| 91 |
return "File"; |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
@JSGetter |
| 100 |
public String getName() { |
| 101 |
return name; |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
@JSFunction |
| 116 |
public Object readLines() |
| 117 |
throws IOException |
| 118 |
{ |
| 119 |
List<String> list = new ArrayList<String>(); |
| 120 |
String s; |
| 121 |
while ((s = readLine()) != null) { |
| 122 |
list.add(s); |
| 123 |
} |
| 124 |
String[] lines = list.toArray(new String[list.size()]); |
| 125 |
Scriptable scope = ScriptableObject.getTopLevelScope(this); |
| 126 |
Context cx = Context.getCurrentContext(); |
| 127 |
return cx.newObject(scope, "Array", lines); |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
@JSFunction |
| 139 |
public String readLine() throws IOException { |
| 140 |
return getReader().readLine(); |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
@JSFunction |
| 151 |
public String readChar() throws IOException { |
| 152 |
int i = getReader().read(); |
| 153 |
if (i == -1) |
| 154 |
return null; |
| 155 |
char[] charArray = { (char) i }; |
| 156 |
return new String(charArray); |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
@JSFunction |
| 170 |
public static void write(Context cx, Scriptable thisObj, |
| 171 |
Object[] args, Function funObj) |
| 172 |
throws IOException |
| 173 |
{ |
| 174 |
write0(thisObj, args, false); |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
@JSFunction |
| 186 |
public static void writeLine(Context cx, Scriptable thisObj, |
| 187 |
Object[] args, Function funObj) |
| 188 |
throws IOException |
| 189 |
{ |
| 190 |
write0(thisObj, args, true); |
| 191 |
} |
| 192 |
|
| 193 |
@JSGetter |
| 194 |
public int getLineNumber() |
| 195 |
throws FileNotFoundException |
| 196 |
{ |
| 197 |
return getReader().getLineNumber(); |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
@JSFunction |
| 208 |
public void close() throws IOException { |
| 209 |
if (reader != null) { |
| 210 |
reader.close(); |
| 211 |
reader = null; |
| 212 |
} else if (writer != null) { |
| 213 |
writer.close(); |
| 214 |
writer = null; |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
@Override |
| 224 |
protected void finalize() { |
| 225 |
try { |
| 226 |
close(); |
| 227 |
} |
| 228 |
catch (IOException e) { |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
@JSFunction("getReader") |
| 236 |
public Object getJSReader() { |
| 237 |
if (reader == null) |
| 238 |
return null; |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
Scriptable parent = ScriptableObject.getTopLevelScope(this); |
| 243 |
return Context.javaToJS(reader, parent); |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
@JSFunction |
| 253 |
public Object getWriter() { |
| 254 |
if (writer == null) |
| 255 |
return null; |
| 256 |
Scriptable parent = ScriptableObject.getTopLevelScope(this); |
| 257 |
return Context.javaToJS(writer, parent); |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
private LineNumberReader getReader() throws FileNotFoundException { |
| 264 |
if (writer != null) { |
| 265 |
throw Context.reportRuntimeError("already writing file \"" |
| 266 |
+ name |
| 267 |
+ "\""); |
| 268 |
} |
| 269 |
if (reader == null) |
| 270 |
reader = new LineNumberReader(file == null |
| 271 |
? new InputStreamReader(System.in) |
| 272 |
: new FileReader(file)); |
| 273 |
return reader; |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
private static void write0(Scriptable thisObj, Object[] args, boolean eol) |
| 284 |
throws IOException |
| 285 |
{ |
| 286 |
File thisFile = checkInstance(thisObj); |
| 287 |
if (thisFile.reader != null) { |
| 288 |
throw Context.reportRuntimeError("already writing file \"" |
| 289 |
+ thisFile.name |
| 290 |
+ "\""); |
| 291 |
} |
| 292 |
if (thisFile.writer == null) |
| 293 |
thisFile.writer = new BufferedWriter( |
| 294 |
thisFile.file == null ? new OutputStreamWriter(System.out) |
| 295 |
: new FileWriter(thisFile.file)); |
| 296 |
for (int i=0; i < args.length; i++) { |
| 297 |
String s = Context.toString(args[i]); |
| 298 |
thisFile.writer.write(s, 0, s.length()); |
| 299 |
} |
| 300 |
if (eol) |
| 301 |
thisFile.writer.newLine(); |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
private static File checkInstance(Scriptable obj) { |
| 322 |
if (obj == null || !(obj instanceof File)) { |
| 323 |
throw Context.reportRuntimeError("called on incompatible object"); |
| 324 |
} |
| 325 |
return (File) obj; |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
private String name; |
| 332 |
private java.io.File file; |
| 333 |
private LineNumberReader reader; |
| 334 |
private BufferedWriter writer; |
| 335 |
} |
| 336 |
|