001: /*
002: * @(#)setBeanProperties.java 1.2 04/12/06
003: *
004: * Copyright (c) 2002-2004 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.PnutsFunction;
012: import pnuts.lang.Context;
013: import pnuts.lang.PnutsImpl;
014: import pnuts.lang.NamedValue;
015: import pnuts.lang.Pnuts;
016: import pnuts.lang.PnutsException;
017: import pnuts.lang.Runtime;
018: import pnuts.lang.Package;
019: import java.lang.reflect.*;
020: import java.beans.*;
021: import java.util.*;
022: import pnuts.beans.BeanUtil;
023:
024: /**
025: * Set properties at a time using list
026: * setBeanProperties(bean, [["prop1", value], ...])
027: * setBeanProperties(bean, $(`prop1=value; ...`))
028: * setBeanProperties(bean, "prop1=...;...")
029: */
030: public class setBeanProperties extends PnutsFunction {
031:
032: private static PnutsImpl pureImpl = new PnutsImpl();
033:
034: public setBeanProperties() {
035: super ("setBeanProperties");
036: }
037:
038: public boolean defined(int narg) {
039: return (narg == 2);
040: }
041:
042: static Map getMap(Object arg, Context context) {
043: Map map = new HashMap();
044:
045: if (arg instanceof Object[]) {
046: Object[] array = (Object[]) arg;
047: for (int i = 0; i < array.length; i++) {
048: Object[] pair = (Object[]) array[i];
049: String name = (String) pair[0];
050: Object value = pair[1];
051: map.put(name, value);
052: }
053: } else if (arg instanceof String) {
054: Context ctx = new Context(context);
055: Package pkg = new Package(null, null);
056: ctx.setImplementation(pureImpl);
057: ctx.setCurrentPackage(pkg);
058: Pnuts.eval((String) arg, ctx);
059:
060: for (Enumeration _enum = pkg.bindings(); _enum
061: .hasMoreElements();) {
062: NamedValue val = (NamedValue) _enum.nextElement();
063: String name = val.getName();
064: Object value = val.get();
065: map.put(name, value);
066: }
067: } else if (arg instanceof Map) {
068: map = (Map) arg;
069: } else if (arg instanceof Package) {
070: Package pkg = (Package) arg;
071: for (Enumeration e = pkg.keys(); e.hasMoreElements();) {
072: String name = (String) e.nextElement();
073: Object value = pkg.get(name, context);
074: map.put(name, value);
075: }
076: } else {
077: throw new IllegalArgumentException();
078: }
079: return map;
080: }
081:
082: protected Object exec(Object[] args, Context context) {
083: int nargs = args.length;
084:
085: if (args.length != 2) {
086: undefined(args, context);
087: return null;
088: }
089: Object bean = args[0];
090: Object arg = args[1];
091: Map map = getMap(arg, context);
092:
093: try {
094: BeanUtil.setProperties(bean, map);
095: } catch (IntrospectionException e) {
096: throw new PnutsException(e, context);
097: } catch (IllegalAccessException e2) {
098: throw new PnutsException(e2, context);
099: } catch (InvocationTargetException e3) {
100: throw new PnutsException(e3, context);
101: }
102: return bean;
103: }
104:
105: public String toString() {
106: return "function setBeanProperties(bean, [[\"prop1\", value], ...]) or (bean, \"prop1=...;...\") or (bean, java.util.Map) or (bean, pnuts.lang.Package)";
107: }
108: }
|