Source Code Cross Referenced for FieldHash.java in  » Web-Framework » RSF » uk » org » ponder » reflect » 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 » Web Framework » RSF » uk.org.ponder.reflect 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


01:        /*
02:         * Created on Oct 25, 2004
03:         */
04:        package uk.org.ponder.reflect;
05:
06:        import java.lang.reflect.Field;
07:        import java.util.Iterator;
08:        import java.util.Map;
09:        import java.util.TreeMap;
10:
11:        import uk.org.ponder.stringutil.StringList;
12:        import uk.org.ponder.util.UniversalRuntimeException;
13:
14:        /**
15:         * Automates the process of reflecting a value type full of Strings to and from
16:         * a Map of Strings. Useful for HTTP requests and the like. Old-fashioned code,
17:         * shortly to be destroyed.
18:         * 
19:         * @author Antranig Basman (antranig@caret.cam.ac.uk)
20:         *  
21:         */
22:        public class FieldHash {
23:            public TreeMap fieldmap = new TreeMap();
24:            private Class target;
25:
26:            public FieldHash(Class target) {
27:                this .target = target;
28:            }
29:
30:            public void addField(String fieldname) {
31:                Field f = null;
32:                try {
33:                    f = target.getField(fieldname);
34:                } catch (Throwable t) {
35:                    throw UniversalRuntimeException.accumulate(t,
36:                            "Unable to reflect field " + fieldname
37:                                    + " for class target");
38:                }
39:                if (f.getType() != String.class) {
40:                    throw new UniversalRuntimeException(
41:                            "Only String-typed fields are supported by FieldHash");
42:                }
43:                fieldmap.put(fieldname, f);
44:            }
45:
46:            /** Select those fields from the supplied map which match keys stored in
47:             * this fieldhash, and set them in the supplied target object. Can deal with 
48:             * either String[]-valued parameters or String-valued ones.
49:             */
50:            public void fromMap(Map from, Object targetobj) {
51:                for (Iterator keys = from.keySet().iterator(); keys.hasNext();) {
52:                    String fieldname = (String) keys.next();
53:                    Field field = (Field) fieldmap.get(fieldname);
54:                    if (field != null) {
55:                        Object valueo = from.get(fieldname);
56:                        String value = valueo instanceof  String ? (String) valueo
57:                                : ((String[]) valueo)[0];
58:                        try {
59:                            field.set(targetobj, value);
60:                        } catch (Throwable t) {
61:                            throw UniversalRuntimeException.accumulate(t,
62:                                    "Unable to set value of field " + fieldname
63:                                            + " in object " + target);
64:                        }
65:                    }
66:                }
67:
68:            }
69:
70:            /** Returns a 2-element array of StringLists, the first holding key names,
71:             * the second holding values.
72:             */
73:            public StringList[] fromObj(Object targetobj) {
74:                StringList[] togo = new StringList[2];
75:                togo[0] = new StringList();
76:                togo[1] = new StringList();
77:                try {
78:                    for (Iterator keys = fieldmap.keySet().iterator(); keys
79:                            .hasNext();) {
80:                        String fieldname = (String) keys.next();
81:                        Field field = (Field) fieldmap.get(fieldname);
82:                        String value = (String) field.get(targetobj);
83:                        if (value != null) {
84:                            togo[0].add(fieldname);
85:                            togo[1].add(value);
86:                        }
87:                    }
88:                } catch (Throwable t) {
89:                    throw UniversalRuntimeException.accumulate(t,
90:                            "Unable to get value of field");
91:                }
92:                return togo;
93:            }
94:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.