Source Code Cross Referenced for BeanToPropertyValueTransformer.java in  » Library » Apache-commons-beanutils-1.8.0-BETA-src » org » apache » commons » beanutils » 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 » Library » Apache commons beanutils 1.8.0 BETA src » org.apache.commons.beanutils 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Licensed to the Apache Software Foundation (ASF) under one or more
003:         * contributor license agreements.  See the NOTICE file distributed with
004:         * this work for additional information regarding copyright ownership.
005:         * The ASF licenses this file to You under the Apache License, Version 2.0
006:         * (the "License"); you may not use this file except in compliance with
007:         * the License.  You may obtain a copy of the License at
008:         *
009:         *      http://www.apache.org/licenses/LICENSE-2.0
010:         *
011:         * Unless required by applicable law or agreed to in writing, software
012:         * distributed under the License is distributed on an "AS IS" BASIS,
013:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         * See the License for the specific language governing permissions and
015:         * limitations under the License.
016:         */
017:
018:        package org.apache.commons.beanutils;
019:
020:        import org.apache.commons.collections.Transformer;
021:        import org.apache.commons.logging.Log;
022:        import org.apache.commons.logging.LogFactory;
023:
024:        import java.lang.reflect.InvocationTargetException;
025:
026:        /**
027:         * <p><code>Transformer</code> that outputs a property value.</p>
028:         *
029:         * <p>An implementation of <code>org.apache.commons.collections.Transformer</code> that transforms
030:         * the object provided by returning the value of a specified property of the object.  The
031:         * constructor for <code>BeanToPropertyValueTransformer</code> requires the name of the property
032:         * that will be used in the transformation.  The property can be a simple, nested, indexed, or
033:         * mapped property as defined by <code>org.apache.commons.beanutils.PropertyUtils</code>. If any
034:         * object in the property path specified by <code>propertyName</code> is <code>null</code> then the
035:         * outcome is based on the value of the <code>ignoreNull</code> attribute.
036:         * </p>
037:         *
038:         * <p>
039:         * A typical usage might look like:
040:         * <code><pre>
041:         * // create the transformer
042:         * BeanToPropertyValueTransformer transformer = new BeanToPropertyValueTransformer( "person.address.city" );
043:         *
044:         * // transform the Collection
045:         * Collection peoplesCities = CollectionUtils.collect( peopleCollection, transformer );
046:         * </pre></code>
047:         * </p>
048:         *
049:         * <p>
050:         * This would take a <code>Collection</code> of person objects and return a <code>Collection</code>
051:         * of objects which represents the cities in which each person lived. Assuming...
052:         * <ul>
053:         *    <li>
054:         *       The top level object in the <code>peeopleCollection</code> is an object which represents a
055:         *       person.
056:         *    </li>
057:         *    <li>
058:         *       The person object has a <code>getAddress()</code> method which returns an object which
059:         *       represents a person's address.
060:         *    </li>
061:         *    <li>
062:         *       The address object has a <code>getCity()</code> method which returns an object which
063:         *       represents the city in which a person lives.
064:         *    </li>
065:         * </ul>
066:         *
067:         * @author Norm Deane
068:         * @see org.apache.commons.beanutils.PropertyUtils
069:         * @see org.apache.commons.collections.Transformer
070:         */
071:        public class BeanToPropertyValueTransformer implements  Transformer {
072:
073:            /** For logging. */
074:            private final Log log = LogFactory.getLog(this .getClass());
075:
076:            /** The name of the property that will be used in the transformation of the object. */
077:            private String propertyName;
078:
079:            /**
080:             * <p>Should null objects on the property path throw an <code>IllegalArgumentException</code>?</p>
081:             * <p>
082:             * Determines whether <code>null</code> objects in the property path will genenerate an
083:             * <code>IllegalArgumentException</code> or not. If set to <code>true</code> then if any objects
084:             * in the property path evaluate to <code>null</code> then the
085:             * <code>IllegalArgumentException</code> throw by <code>PropertyUtils</code> will be logged but
086:             * not rethrown and <code>null</code> will be returned.  If set to <code>false</code> then if any
087:             * objects in the property path evaluate to <code>null</code> then the
088:             * <code>IllegalArgumentException</code> throw by <code>PropertyUtils</code> will be logged and
089:             * rethrown.
090:             * </p>
091:             */
092:            private boolean ignoreNull;
093:
094:            /**
095:             * Constructs a Transformer which does not ignore nulls.
096:             * Constructor which takes the name of the property that will be used in the transformation and
097:             * assumes <code>ignoreNull</code> to be <code>false</code>.
098:             *
099:             * @param propertyName The name of the property that will be used in the transformation.
100:             * @throws IllegalArgumentException If the <code>propertyName</code> is <code>null</code> or
101:             * empty.
102:             */
103:            public BeanToPropertyValueTransformer(String propertyName) {
104:                this (propertyName, false);
105:            }
106:
107:            /**
108:             * Constructs a Transformer and sets ignoreNull.
109:             * Constructor which takes the name of the property that will be used in the transformation and
110:             * a boolean which determines whether <code>null</code> objects in the property path will
111:             * genenerate an <code>IllegalArgumentException</code> or not.
112:             *
113:             * @param propertyName The name of the property that will be used in the transformation.
114:             * @param ignoreNull Determines whether <code>null</code> objects in the property path will
115:             * genenerate an <code>IllegalArgumentException</code> or not.
116:             * @throws IllegalArgumentException If the <code>propertyName</code> is <code>null</code> or
117:             * empty.
118:             */
119:            public BeanToPropertyValueTransformer(String propertyName,
120:                    boolean ignoreNull) {
121:                super ();
122:
123:                if ((propertyName != null) && (propertyName.length() > 0)) {
124:                    this .propertyName = propertyName;
125:                    this .ignoreNull = ignoreNull;
126:                } else {
127:                    throw new IllegalArgumentException(
128:                            "propertyName cannot be null or empty");
129:                }
130:            }
131:
132:            /**
133:             * Returns the value of the property named in the transformer's constructor for
134:             * the object provided. If any object in the property path leading up to the target property is
135:             * <code>null</code> then the outcome will be based on the value of the <code>ignoreNull</code>
136:             * attribute. By default, <code>ignoreNull</code> is <code>false</code> and would result in an
137:             * <code>IllegalArgumentException</code> if an object in the property path leading up to the
138:             * target property is <code>null</code>.
139:             *
140:             * @param object The object to be transformed.
141:             * @return The value of the property named in the transformer's constructor for the object
142:             * provided.
143:             * @throws IllegalArgumentException If an IllegalAccessException, InvocationTargetException, or
144:             * NoSuchMethodException is thrown when trying to access the property specified on the object
145:             * provided. Or if an object in the property path provided is <code>null</code> and
146:             * <code>ignoreNull</code> is set to <code>false</code>.
147:             */
148:            public Object transform(Object object) {
149:
150:                Object propertyValue = null;
151:
152:                try {
153:                    propertyValue = PropertyUtils.getProperty(object,
154:                            propertyName);
155:                } catch (IllegalArgumentException e) {
156:                    final String errorMsg = "Problem during transformation. Null value encountered in property path...";
157:
158:                    if (ignoreNull) {
159:                        log.warn("WARNING: " + errorMsg, e);
160:                    } else {
161:                        log.error("ERROR: " + errorMsg, e);
162:                        throw e;
163:                    }
164:                } catch (IllegalAccessException e) {
165:                    final String errorMsg = "Unable to access the property provided.";
166:                    log.error(errorMsg, e);
167:                    throw new IllegalArgumentException(errorMsg);
168:                } catch (InvocationTargetException e) {
169:                    final String errorMsg = "Exception occurred in property's getter";
170:                    log.error(errorMsg, e);
171:                    throw new IllegalArgumentException(errorMsg);
172:                } catch (NoSuchMethodException e) {
173:                    final String errorMsg = "No property found for name ["
174:                            + propertyName + "]";
175:                    log.error(errorMsg, e);
176:                    throw new IllegalArgumentException(errorMsg);
177:                }
178:
179:                return propertyValue;
180:            }
181:
182:            /**
183:             * Returns the name of the property that will be used in the transformation of the bean.
184:             *
185:             * @return The name of the property that will be used in the transformation of the bean.
186:             */
187:            public String getPropertyName() {
188:                return propertyName;
189:            }
190:
191:            /**
192:             * Returns the flag which determines whether <code>null</code> objects in the property path will
193:             * genenerate an <code>IllegalArgumentException</code> or not. If set to <code>true</code> then
194:             * if any objects in the property path evaluate to <code>null</code> then the
195:             * <code>IllegalArgumentException</code> throw by <code>PropertyUtils</code> will be logged but
196:             * not rethrown and <code>null</code> will be returned.  If set to <code>false</code> then if any
197:             * objects in the property path evaluate to <code>null</code> then the
198:             * <code>IllegalArgumentException</code> throw by <code>PropertyUtils</code> will be logged and
199:             * rethrown.
200:             *
201:             * @return The flag which determines whether <code>null</code> objects in the property path will
202:             * genenerate an <code>IllegalArgumentException</code> or not.
203:             */
204:            public boolean isIgnoreNull() {
205:                return ignoreNull;
206:            }
207:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.