01: /*
02: * @(#)printAll.java 1.2 04/12/06
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.PnutsFunction;
12: import pnuts.lang.Context;
13: import pnuts.lang.Generator;
14: import pnuts.lang.Runtime;
15: import java.io.PrintWriter;
16: import java.util.Enumeration;
17:
18: public class printAll extends PnutsFunction {
19:
20: public printAll() {
21: super ("printAll");
22: }
23:
24: public boolean defined(int nargs) {
25: return nargs == 1;
26: }
27:
28: public Object exec(Object[] args, Context context) {
29: final PrintWriter output = context.getWriter();
30: if (output != null) {
31: Object target = args[0];
32: Enumeration e = Runtime.toEnumeration(target, context);
33: if (e != null) {
34: while (e.hasMoreElements()) {
35: output.println(e.nextElement());
36: }
37: } else if (target instanceof Generator) {
38: Generator g = (Generator) target;
39: Runtime.applyGenerator(g, new PnutsFunction() {
40: protected Object exec(Object[] args, Context ctx) {
41: output.println(args[0]);
42: return null;
43: }
44: }, context);
45: } else {
46: throw new IllegalArgumentException(String
47: .valueOf(target));
48: }
49: }
50: return null;
51: }
52:
53: public String toString() {
54: return "function printAll(arg)";
55: }
56: }
|