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: }
|