01: /*
02: * @(#)includeFile.java 1.3 05/06/07
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.File;
18: import java.io.InputStream;
19: import java.io.FileNotFoundException;
20:
21: /**
22: * Implements include() function and includeFile() function.
23: */
24: public class includeFile extends PnutsFunction {
25:
26: /**
27: * Constructor
28: */
29: public includeFile() {
30: super ("includeFile");
31: }
32:
33: public boolean defined(int narg) {
34: return (narg == 1);
35: }
36:
37: protected Object exec(Object args[], Context context) {
38: try {
39: if (args.length != 1) {
40: undefined(args, context);
41: return null;
42: }
43: Object arg = args[0];
44: if (arg instanceof String) {
45: File file = PathHelper.getFile((String) arg, context);
46: return Pnuts.loadFile(file.getPath(), context);
47: } else if (arg instanceof File) {
48: return Pnuts.loadFile(((File) arg).getPath(), context);
49: }
50: throw new IllegalArgumentException(String.valueOf(arg));
51:
52: } catch (FileNotFoundException e) {
53: throw new PnutsException(e, context);
54: }
55: }
56:
57: public String toString() {
58: return "function includeFile(String fileName)";
59: }
60: }
|