01: /*
02: * getFile.java
03: *
04: * Copyright (c) 1997-2006 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.servlet;
10:
11: import pnuts.servlet.*;
12: import pnuts.lang.*;
13: import java.io.*;
14:
15: /*
16: * getFile( { { parent, } name } )
17: */
18: public class getFile extends PnutsFunction {
19:
20: private final static String SERVLET_FILE = "pnuts.servlet.file"
21: .intern();
22:
23: public getFile() {
24: super ("getFile");
25: }
26:
27: public boolean defined(int narg) {
28: return (narg == 0 || narg == 1 || narg == 2);
29: }
30:
31: protected Object exec(Object[] args, Context context) {
32: int nargs = args.length;
33: if (nargs == 0) {
34: return context.get(SERVLET_FILE);
35: } else if (nargs == 1) {
36: File f = (File) context.get(SERVLET_FILE);
37: if (f != null) {
38: f = f.getParentFile();
39: }
40: Object arg = args[0];
41: if (arg instanceof String) {
42: String name = (String) args[0];
43: if (f != null) {
44: return new File(f, name);
45: } else {
46: return new File(name);
47: }
48: } else if (arg instanceof File) {
49: return arg;
50: } else {
51: throw new IllegalArgumentException();
52: }
53: } else if (nargs == 2) {
54: Object arg = args[0];
55: File parent;
56: if (arg instanceof String) {
57: File f = (File) context.get(SERVLET_FILE);
58: if (f != null) {
59: f = f.getParentFile();
60: }
61: parent = new File(f, (String) arg);
62: } else if (arg instanceof File) {
63: parent = (File) arg;
64: } else {
65: throw new IllegalArgumentException();
66: }
67: Object name = args[1];
68: if (name instanceof String) {
69: return new File(parent, (String) name);
70: } else {
71: throw new IllegalArgumentException();
72: }
73: } else {
74: undefined(args, context);
75: return null;
76: }
77: }
78:
79: public String toString() {
80: return "function getFile( { { parent, } name } )";
81: }
82: }
|