001: /*
002: * compile.java
003: *
004: * Copyright (c) 1997-2006 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.lib;
010:
011: import java.io.File;
012: import java.io.InputStream;
013: import java.io.Reader;
014: import java.util.zip.ZipOutputStream;
015: import java.net.URL;
016: import pnuts.lang.Context;
017: import pnuts.lang.Pnuts;
018: import pnuts.lang.PnutsException;
019: import pnuts.lang.PnutsFunction;
020: import pnuts.compiler.ClassFileHandler;
021: import pnuts.compiler.FileWriterHandler;
022: import pnuts.compiler.ZipWriterHandler;
023: import pnuts.compiler.Compiler;
024: import pnuts.ext.CachedScript;
025:
026: public class compile extends PnutsFunction {
027:
028: public compile() {
029: super ("compile");
030: }
031:
032: public boolean defined(int nargs) {
033: return nargs >= 1 && nargs <= 3;
034: }
035:
036: Object compileObject(Object arg, Context context) {
037: Compiler compiler = new Compiler();
038: if (arg instanceof PnutsFunction) {
039: return compiler.compile((PnutsFunction) arg, context);
040: }
041: Pnuts parsed = parse.parse(arg, context);
042: if (!(arg instanceof Pnuts)) {
043: parsed.setScriptSource(arg);
044: }
045: try {
046: return compiler.compile(parsed, context);
047: } catch (Exception e) {
048: return parsed;
049: }
050: }
051:
052: protected Object exec(Object args[], Context context) {
053: int nargs = args.length;
054: if (nargs == 1) {
055: Object arg = args[0];
056: return compileObject(arg, context);
057: } else if (nargs == 2) {
058: Object arg = args[0];
059: boolean reload = ((Boolean) args[1]).booleanValue();
060: if (!reload || (arg instanceof InputStream)
061: || (arg instanceof Reader)
062: || (arg instanceof String)) {
063: return compileObject(arg, context);
064: }
065: URL url;
066: try {
067: if (arg instanceof URL) {
068: url = (URL) arg;
069: } else if (arg instanceof File) {
070: url = ((File) arg).toURL();
071: } else {
072: throw new IllegalArgumentException(String
073: .valueOf(arg));
074: }
075: return new CachedScript(url, context
076: .getScriptEncoding(), context);
077: } catch (Exception e) {
078: throw new PnutsException(e, context);
079: }
080:
081: } else if (nargs == 3) {
082: Object src = args[0];
083: String name = (String) args[1];
084: Object dest = args[2];
085: Compiler compiler = new Compiler(name, false, true);
086: ClassFileHandler handler;
087: if (dest instanceof String) {
088: handler = new FileWriterHandler(new File((String) dest));
089: } else if (dest instanceof File) {
090: handler = new FileWriterHandler((File) dest);
091: } else if (dest instanceof ZipOutputStream) {
092: handler = new ZipWriterHandler((ZipOutputStream) dest);
093: } else {
094: throw new IllegalArgumentException(String.valueOf(dest));
095: }
096: Pnuts parsed = parse.parse(src, context);
097: if (!(src instanceof Pnuts)) {
098: parsed.setScriptSource(src);
099: }
100: compiler.compile(parsed, handler);
101: } else {
102: undefined(args, context);
103: }
104: return null;
105: }
106:
107: public String toString() {
108: return "function compile((InputStream|Reader|String|File|URL|Pnuts) input {, reloadUpdatedScript }) or ((Pnuts|String)source, String className, (File|ZipOutputStream)output)";
109: }
110: }
|