001: /*
002: * @(#)generateBeanClass.java 1.3 05/03/29
003: *
004: * Copyright (c) 2003-2005 Sun Microsystems, Inc. All Rights Reserved.
005: *
006: * See the file "LICENSE.txt" for information on usage and redistribution
007: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
008: */
009: package org.pnuts.beans;
010:
011: import pnuts.lang.*;
012: import org.pnuts.lib.BeanClassGenerator;
013: import org.pnuts.lib.PathHelper;
014: import java.io.*;
015: import java.util.*;
016: import java.util.zip.*;
017:
018: /*
019: * function generateBeanClass(typeMap, className {, superClass {, interfaces {, OutputStream|File|String }}})
020: */
021: public class generateBeanClass extends PnutsFunction {
022:
023: public generateBeanClass() {
024: super ("generateBeanClass");
025: }
026:
027: public boolean defined(int nargs) {
028: return nargs > 1 && nargs < 6;
029: }
030:
031: protected Object exec(Object[] args, Context context) {
032: Map typeMap;
033: String className;
034: String super ClassName = null;
035: String interfaces[] = null;
036: try {
037: int nargs = args.length;
038: if (nargs > 2) {
039: Object a2 = args[2];
040: if (a2 instanceof String) {
041: super ClassName = (String) a2;
042: } else if (a2 instanceof Class) {
043: super ClassName = ((Class) a2).getName();
044: } else if (a2 != null) {
045: throw new IllegalArgumentException(String
046: .valueOf(a2));
047: }
048: }
049: if (nargs > 3) {
050: Object[] a3 = (Object[]) args[3];
051: if (a3 instanceof String[]) {
052: interfaces = (String[]) a3;
053: } else if (a3 != null) {
054: interfaces = new String[a3.length];
055: for (int i = 0; i < a3.length; i++) {
056: Object a3i = a3[i];
057: if (a3i instanceof String) {
058: interfaces[i] = (String) a3i;
059: } else if (a3i instanceof Class) {
060: interfaces[i] = ((Class) a3i).getName();
061: } else {
062: throw new IllegalArgumentException(String
063: .valueOf(a3i));
064: }
065: }
066: }
067: }
068: if (nargs > 1) {
069: className = (String) args[1];
070: typeMap = (Map) args[0];
071: } else {
072: undefined(args, context);
073: return null;
074: }
075: if (nargs == 5) {
076: Object arg4 = args[4];
077: boolean needToClose = false;
078: OutputStream output = null;
079: if (arg4 instanceof OutputStream) {
080: output = (OutputStream) arg4;
081: } else if (arg4 instanceof File) {
082: output = new FileOutputStream((File) arg4);
083: needToClose = true;
084: } else if (arg4 instanceof String) {
085: File file = PathHelper.getFile((String) arg4,
086: context);
087: output = new FileOutputStream(file);
088: needToClose = true;
089: } else {
090: throw new IllegalArgumentException();
091: }
092: if (needToClose) {
093: try {
094: BeanClassGenerator.generate(typeMap, className,
095: super ClassName, interfaces, output);
096: } finally {
097: try {
098: output.close();
099: } catch (IOException e) {
100: // skip
101: }
102: }
103: } else {
104: BeanClassGenerator.generate(typeMap, className,
105: super ClassName, interfaces, output);
106: }
107: return null;
108: } else {
109: ClassLoader classLoader = context.getClassLoader();
110: if (classLoader == null) {
111: classLoader = Thread.currentThread()
112: .getContextClassLoader();
113: }
114: return BeanClassGenerator.generate(typeMap, className,
115: super ClassName, interfaces, classLoader);
116: }
117: } catch (IOException e) {
118: throw new PnutsException(e, context);
119: }
120: }
121:
122: public String toString() {
123: return "function generateBeanClass(typeMap, className {, superClass {, interfaces {, OutputStream|File|String }}})";
124: }
125: }
|