01: /*
02: * @(#)encodeBean.java 1.2 04/12/06
03: *
04: * Copyright (c) 2002-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.beans;
10:
11: import pnuts.lang.*;
12: import java.beans.*;
13: import java.io.*;
14:
15: /*
16: * encodeBean(bean {, outputStream } )
17: */
18: public class encodeBean extends PnutsFunction {
19:
20: public encodeBean() {
21: super ("encodeBean");
22: }
23:
24: public boolean defined(int nargs) {
25: return nargs == 1 || nargs == 2;
26: }
27:
28: protected Object exec(Object[] args, Context context) {
29: int nargs = args.length;
30: if (nargs == 1) {
31: Object bean = args[0];
32: ByteArrayOutputStream bout = new ByteArrayOutputStream();
33: XMLEncoder enc = new XMLEncoder(bout);
34: enc.writeObject(bean);
35: enc.close();
36: return new ByteArrayInputStream(bout.toByteArray());
37: } else if (nargs == 2) {
38: Object bean = args[0];
39: OutputStream output = (OutputStream) args[1];
40: XMLEncoder enc = new XMLEncoder(output);
41: enc.writeObject(bean);
42: enc.flush();
43: } else {
44: undefined(args, context);
45: }
46: return null;
47: }
48:
49: public String toString() {
50: return "function encodeBean(bean {, outputStream })";
51: }
52: }
|