Source Code Cross Referenced for setBeanProperties.java in  » Scripting » Pnuts » org » pnuts » beans » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Scripting » Pnuts » org.pnuts.beans 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.