01: /*
02: * @(#)include.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 pnuts.lang.Context;
12: import pnuts.lang.Pnuts;
13: import pnuts.lang.PnutsFunction;
14: import pnuts.lang.PnutsException;
15: import java.net.URL;
16: import java.io.Reader;
17: import java.io.InputStream;
18: import java.io.FileNotFoundException;
19:
20: /**
21: * include() function
22: */
23: public class include extends PnutsFunction {
24:
25: /**
26: * Constructor
27: */
28: public include() {
29: super ("include");
30: }
31:
32: public boolean defined(int narg) {
33: return (narg == 1);
34: }
35:
36: protected Object exec(Object args[], Context context) {
37: try {
38: if (args.length != 1) {
39: undefined(args, context);
40: return null;
41: }
42: Object arg = args[0];
43: if (arg instanceof String) {
44: return Pnuts.load((String) arg, context);
45: } else if (arg instanceof URL) {
46: return Pnuts.load((URL) arg, context);
47: } else if (arg instanceof InputStream) {
48: return Pnuts.load((InputStream) arg, context);
49: } else if (arg instanceof Reader) {
50: return Pnuts.load((Reader) arg, context);
51: } else {
52: throw new IllegalArgumentException(arg.toString());
53: }
54: } catch (FileNotFoundException e) {
55: throw new PnutsException(e, context);
56: }
57: }
58:
59: public String toString() {
60: return "function include((String|URL|InputStream|Reader) script)";
61: }
62: }
|