01: /*
02: * @(#)getFile.java 1.2 04/12/06
03: *
04: * Copyright (c) 1997-2004 Sun Microsystems, Inc. All Rights Reserved.
05: *
06: * See the file "LICENSE.txt" for information on usage and redistribution
07: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
08: */
09: package org.pnuts.lib;
10:
11: import java.io.InputStream;
12: import java.io.DataInputStream;
13: import java.io.File;
14: import java.io.IOException;
15: import pnuts.lang.Context;
16: import pnuts.lang.PnutsFunction;
17: import pnuts.lang.PnutsException;
18:
19: public class getFile extends PnutsFunction {
20:
21: public getFile() {
22: super ("getFile");
23: }
24:
25: public boolean defined(int narg) {
26: return (narg == 1 || narg == 2);
27: }
28:
29: protected Object exec(Object[] args, Context context) {
30: int nargs = args.length;
31: if (nargs == 1) {
32: File file = null;
33: Object arg = args[0];
34: if (arg instanceof File) {
35: file = (File) arg;
36: } else if (arg instanceof String) {
37: file = PathHelper.getFile((String) arg, context);
38: } else {
39: throw new IllegalArgumentException(String.valueOf(arg));
40: }
41: if (file.getPath() != "") {
42: try {
43: return new File(file.getCanonicalPath());
44: } catch (IOException e) {
45: throw new PnutsException(e, context);
46: }
47: } else {
48: return new File("");
49: }
50: } else if (nargs == 2) {
51: Object arg0 = args[0];
52: if (arg0 instanceof String) {
53: return new File((String) args[0], (String) args[1]);
54: } else if (arg0 instanceof File) {
55: return new File((File) args[0], (String) args[1]);
56: } else {
57: throw new IllegalArgumentException(String.valueOf(arg0));
58: }
59: } else {
60: undefined(args, context);
61: return null;
62: }
63: }
64:
65: public String toString() {
66: return "getFile(String|File {, String })";
67: }
68: }
|