01: /*
02: * @(#)textGrab.java 1.3 05/01/14
03: *
04: * Copyright (c) 2001-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.text;
10:
11: import pnuts.lang.*;
12: import java.io.*;
13:
14: /*
15: * function textGrab(function)
16: */
17: public class textGrab extends PnutsFunction {
18:
19: public textGrab() {
20: super ("textGrab");
21: }
22:
23: public boolean defined(int narg) {
24: return (narg == 1);
25: }
26:
27: protected Object exec(Object args[], Context context) {
28: int narg = args.length;
29: if (narg != 1) {
30: undefined(args, context);
31: return null;
32: }
33: final PnutsFunction func = (PnutsFunction) args[0];
34: return new PnutsFunction() {
35: public boolean defined(int nargs) {
36: return true;
37: }
38:
39: protected Object exec(Object[] args, Context context) {
40: Context c = (Context) context.clone();
41: ByteArrayOutputStream bout = new ByteArrayOutputStream();
42: c.setOutputStream(bout);
43: func.call(args, c);
44: PrintWriter pw = c.getWriter();
45: if (pw != null) {
46: pw.flush();
47: }
48: return bout.toString();
49: }
50: };
51: }
52:
53: public String toString() {
54: return "function textGrab(func)";
55: }
56: }
|