01: /*
02: * @(#)classGenerator.java 1.2 04/12/06
03: *
04: * Copyright (c) 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.lib.ScriptPackage;
12: import pnuts.lang.PnutsFunction;
13: import pnuts.lang.Context;
14: import pnuts.compiler.*;
15: import org.pnuts.lang.ClassFileLoader;
16: import java.io.File;
17: import java.util.zip.ZipOutputStream;
18:
19: public class classGenerator extends PnutsFunction {
20:
21: public classGenerator() {
22: super ("classGenerator");
23: }
24:
25: public boolean defined(int nargs) {
26: return nargs == 1 || nargs == 0;
27: }
28:
29: protected Object exec(Object[] args, Context context) {
30: int nargs = args.length;
31: Object arg;
32: ClassFileHandler handler = null;
33: ClassLoader loader = null;
34:
35: if (nargs == 0) {
36: arg = Thread.currentThread().getContextClassLoader();
37: } else if (nargs == 1) {
38: arg = args[0];
39: } else {
40: undefined(args, context);
41: return null;
42: }
43: if (arg instanceof ClassLoader) {
44: ClassLoader classLoader = (ClassLoader) arg;
45: if (classLoader instanceof ClassFileLoader) {
46: loader = classLoader;
47: } else {
48: loader = new ClassFileLoader((ClassLoader) arg);
49: }
50: handler = (ClassFileHandler) loader;
51: } else if (arg instanceof String) {
52: handler = new FileWriterHandler(PathHelper.getFile(
53: (String) arg, context));
54: } else if (arg instanceof File) {
55: handler = new FileWriterHandler((File) arg);
56: } else if (arg instanceof ZipOutputStream) {
57: handler = new ZipWriterHandler((ZipOutputStream) arg);
58: } else {
59: throw new IllegalArgumentException(String.valueOf(arg));
60: }
61: ScriptPackage pkg = new ScriptPackage();
62: pkg.set("subclass".intern(), new subclass(handler));
63: pkg.set("interface".intern(), new _interface(handler));
64: pkg.set("beanclass".intern(), new beanclass(handler));
65: pkg.set("getClassLoader".intern(), new getClassLoader(loader));
66: return pkg;
67: }
68:
69: static class getClassLoader extends PnutsFunction {
70: ClassLoader loader;
71:
72: getClassLoader(ClassLoader loader) {
73: super ("getClassLoader");
74: this .loader = loader;
75: }
76:
77: public boolean defined(int nargs) {
78: return nargs == 0;
79: }
80:
81: protected Object exec(Object[] args, Context context) {
82: return loader;
83: }
84: }
85:
86: public String toString() {
87: return "function classGenerator( { (ClassLoader | ZipOutputStream | File | String) } )";
88: }
89: }
|