Coverage details for org.chwf.filter.GenericBeanFilter

LineHitsSource
1 /*
2 Chrysalis Web Framework [http://chrysalis.sourceforge.net]
3 Copyright (c) 2002, 2003, 2004, Paul Strack
4  
5 All rights reserved.
6  
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9  
10 1. Redistributions of source code must retain the above copyright notice, this
11 list of conditions and the following disclaimer.
12  
13 2. Redistributions in binary form must reproduce the above copyright notice,
14 this list of conditions and the following disclaimer in the documentation
15 and/or other materials provided with the distribution.
16  
17 3. Neither the name of the copyright holder nor the names of its contributors
18 may be used to endorse or promote products derived from this software without
19 specific prior written permission.
20  
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
22 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
25 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
28 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32  
33 package org.chwf.filter;
34  
35 import java.beans.BeanInfo;
36 import java.beans.IntrospectionException;
37 import java.beans.Introspector;
38 import java.beans.PropertyDescriptor;
39 import java.util.Iterator;
40 import java.util.Map;
41  
42 import org.chwf.config.Config;
43 import org.chwf.config.PolymorphicConfig;
44 import org.chwf.converter.Converter;
45 import org.chwf.plugin.OptionLister;
46 import org.chwf.util.MiscUtils;
47  
48 /**
49  * Generic default filter created through introspection. Custom bean
50  * filters can subclass this class, instead of <code>BeanFilter</code>,
51  * to set most of the filter's functionality through reflection. The
52  * custom filter can then customize the filters for individual properties
53  * and methods by calling the appropriate <code>putFilter</code> method:
54  *
55  * <pre>
56  * package example;
57  * import java.text.*;
58  * import org.chwf.filter.*;
59  *
60  * public class AccountFilter extends GenericBeanFilter {
61  * public AccountFilter() {
62  * super(Account.class);
63  *
64  * // startDate property filter as an anonymous inner class:
65  * PropertyFilter sdFilter =
66  * new PropertyFilter("startDate", java.util.Date.class) {
67  *
68  * public String get(Object object) {
69  * DateFormat df = SimpleDateFormat("mm/dd/yyyy");
70  * java.util.Date date = ((Account) object).getStartDate();
71  * if (date == null) {
72  * return null;
73  * } else {
74  * return df.format(date);
75  * }
76  * }
77  *
78  * public void set(Object object, String value) {
79  * DateFormat df = SimpleDateFormat("mm/dd/yyyy");
80  * try {
81  * ((Account) object).setStartDate(df.parse(value));
82  * } catch (java.text.ParseException ex) {
83  * throw new IllegalArgumentException(ex.getMessage());
84  * }
85  * }
86  * };
87  *
88  * putPropertyFilter("startDate", sdFilter);
89  *
90  * initializePropertyAttributes(); // Using configuration
91  * }
92  * }</pre>
93  *
94  * @author <a href="mailto:pfstrack@users.sourceforge.net">Paul Strack</a>
95  */
961public class GenericBeanFilter extends BeanFilter {
97  
98   /**
99    * This constructor initializes the property and method filters for the
100    * bean. It does <i>not</i> initialize property attributes.<p>
101    *
102    * @param beanClass The class being filtered.
103    * @throws InitializationException If initialization fails for any reason.
104    */
105   public GenericBeanFilter(Class beanClass) throws InitializationException {
10626    super(beanClass);
107     try {
10826      BeanInfo info = Introspector.getBeanInfo(beanClass);
10926      initializeProperties(info);
11026      Introspector.flushFromCaches(beanClass); // To avoid cluttering memory
11126    } catch (Exception ex) {
1120      throw new InitializationException(
113         InitializationException.MESSAGE_INIT_FAILED,
114         new Object[] { ex });
115     }
11626  }
117  
118   /**
119    * This method initializes property attributes, using the JavaBean config
120    * file described in the package documentation.<p>
121    *
122    * @throws InitializationException If introspection fails.
123    */
124   protected void initializePropertyAttributes()
125     throws InitializationException {
126     // Set defaults:
12726    PropertyFilter[] filters = getPropertyFilters();
12826    initDefaultAttributeValues(filters);
12926    initFromConfig(filters);
13026  }
131  
132   /**
133    * Initialize default attribute values for label, datatype and readonly.
134    *
135    * @param filters The Property Filters
136    */
137   private void initDefaultAttributeValues(PropertyFilter[] filters) {
138129    for (int i = 0; i < filters.length; i++) {
139103      PropertyFilter filter = filters[i];
140103      String label = deriveLabel(filter);
141103      filter.setAttribute(ATTRIBUTE_LABEL, label);
142103      String type = MiscUtils.getWrapper(filter.getType()).getName();
143103      filter.setAttribute(ATTRIBUTE_DATATYPE, type);
144103      if (!filter.isWriteable()) {
14560        filter.setAttribute(ATTRIBUTE_READONLY, new Boolean(true));
146       }
147     }
14826  }
149  
150   /**
151    * Initialize property attributes using config.
152    *
153    * @param filters The Property Filters
154    * @throws InitializationException If something goes wrong.
155    */
156   private void initFromConfig(PropertyFilter[] filters)
157     throws InitializationException {
158     try {
15926      Class beanClass = getBeanClass();
16026      Config config = PolymorphicConfig.getConfig(beanClass);
161129      for (int j = 0; j < filters.length; j++) {
162103        PropertyFilter filter = filters[j];
163103        String filterName = filter.getName();
164103        Map data = config.getMap(filterName);
165103        Iterator attributes = data.keySet().iterator();
166103        Class type = filter.getType();
167280        while (attributes.hasNext()) {
16874          String attribute = (String) attributes.next();
16974          if (!(attribute.indexOf('.') > 0)) {
17027            String value = (String) data.get(attribute);
17127            if (attribute.equals(ATTRIBUTE_MAXLENGTH)
172               || attribute.equals(ATTRIBUTE_MINLENGTH)
173               || attribute.equals(ATTRIBUTE_SCALE)) {
1747              Converter converter = Converter.getConverter(Integer.TYPE);
1757              filter.setAttribute(attribute, converter.parse(value));
17620            } else if (
177               attribute.equals(ATTRIBUTE_MIN)
178                 || attribute.equals(ATTRIBUTE_MAX)) {
17915              Converter converter = Converter.getConverter(type);
18015              filter.setAttribute(attribute, converter.parse(value));
1815            } else if (attribute.equals(ATTRIBUTE_REQUIRED)) {
1821              Converter converter = Converter.getConverter(Boolean.TYPE);
1831              filter.setAttribute(attribute, converter.parse(value));
184             } else {
1854              filter.setAttribute(attribute, value);
186             }
187           }
188         }
189  
190103        Map options =
191           OptionLister.getInstance().loadOptionList(beanClass, filterName);
192103        if (options != null) {
1936          filter.setAttribute(BeanFilter.ATTRIBUTE_OPTIONS, options);
194         }
195       }
19626    } catch (Exception ex) {
1970      Object[] arguments = { ex };
1980      throw new InitializationException(
199         InitializationException.MESSAGE_PROPERTY_INIT_FAILED,
200         new Object[] { ex });
201     }
20226  }
203  
204   /**
205    * Initialize property filters.
206    *
207    * @param info The BeanInfo for the class.
208    * @throws IntrospectionException For BeanInfo exceptions.
209    */
210   private void initializeProperties(BeanInfo info)
211     throws IntrospectionException {
21226    PropertyDescriptor[] properties = info.getPropertyDescriptors();
213129    for (int i = 0; i < properties.length; i++) {
214       try {
215103        putPropertyFilter(
216           properties[i].getName(),
217           new GenericPropertyFilter(getBeanClass(), properties[i]));
218103      } catch (InitializationException ex) {
219         // Suppress properties whose filter cannot be constructed.
2200        i = i * 1; // Dummy op so that checkstyle does not complain
221       }
222     }
22326  }
224  
225   /**
226    * Method that derives the propery label from the property name,
227    * by "un-camel-casing" the name.
228    *
229    * @param filter The property filter.
230    * @return The derived label.
231    */
232   private String deriveLabel(PropertyFilter filter) {
233103    char[] name = filter.getName().toCharArray();
234103    StringBuffer label = new StringBuffer(name.length);
235103    label.append(Character.toUpperCase(name[0]));
236103    boolean wasUppercase = Character.isUpperCase(name[0]);
237750    for (int i = 1; i < name.length; i++) {
238647      char c = name[i];
239647      if (Character.isUpperCase(c)) {
24049        if (!wasUppercase) {
24145          label.append(' ');
242         }
24349        label.append(c);
24449        wasUppercase = true;
245       } else {
246598        label.append(c);
247598        wasUppercase = false;
248       }
249     }
250103    return label.toString();
251   }
252  
253   /** Constant for specifying string parameters in reflection. */
2541  private static final Class[] STRING_PARAM = { String.class };
255 }

this report was generated by version 1.0.5 of jcoverage.
visit www.jcoverage.com for updates.

copyright © 2003, jcoverage ltd. all rights reserved.
Java is a trademark of Sun Microsystems, Inc. in the United States and other countries.