1 /*
2 * Copyright (C) 2002 Carsten Krebs (Team-Konzept GmbH & Co.KG)
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 package com.teamkonzept.dom4jb.beans;
19
20 import java.beans.IntrospectionException;
21 import java.beans.PropertyDescriptor;
22 import java.lang.reflect.InvocationTargetException;
23 import java.lang.reflect.Method;
24 import java.util.List;
25
26 import com.teamkonzept.dom4jb.dom.Document;
27 import com.teamkonzept.dom4jb.schema.ContentIterator;
28 import com.teamkonzept.dom4jb.schema.Groupable;
29
30 public abstract class Property implements Groupable {
31
32 private static final Object[] NO_PARAMS = new Object[0];
33
34 public static final PropertyDescriptor IDENTITY = null;
35
36 protected PropertyDescriptor property;
37
38 protected Property() {
39 /* NOP */
40 }
41
42 public Property(final PropertyDescriptor property) {
43 this.property = property;
44 }
45
46 public static PropertyDescriptor getProperty(final String name,
47 final Class beanClass)
48 throws IntrospectionException {
49
50 if (name == null || name.length() <= 0) {
51 throw new IntrospectionException("bad property name");
52 }
53
54 final String base =
55 (new StringBuffer())
56 .append(Character.toUpperCase(name.charAt(0)))
57 .append(name.substring(1))
58 .toString();
59
60 final Method readMethod =
61 findMethod(
62 beanClass,
63 new String[] { "is", "get" },
64 base,
65 new Class[0]);
66 if (readMethod == null) {
67 throw new IntrospectionException(
68 "no such method is"
69 + base
70 + "/"
71 + "get"
72 + base
73 + " in class "
74 + beanClass.getName());
75 }
76
77 final Method writeMethod =
78 findMethod(
79 beanClass,
80 new String[] { "set" },
81 base,
82 new Class[] { readMethod.getReturnType()});
83
84 return new PropertyDescriptor(name, readMethod, writeMethod);
85 }
86
87 protected static Method findMethod(
88 final Class beanClass, final String[] prefix, final String base,
89 final Class[] params) {
90
91 for (int i = 0; i < prefix.length; i++) {
92 try {
93 final String name =
94 (new StringBuffer(prefix[i])).append(base).toString();
95
96 return beanClass.getMethod(name, params);
97 } catch (NoSuchMethodException e) {
98 /* NOP */
99 }
100 }
101 return null;
102 }
103
104 public final ContentIterator getContentIterator(final Document doc,
105 final Object obj) {
106 return null;
107 }
108
109 protected final Object getObject(final Object bean)
110 throws IllegalAccessException, InvocationTargetException {
111 return property == IDENTITY
112 ? bean
113 : property.getReadMethod().invoke(bean, NO_PARAMS);
114 }
115
116 public void setPropertyDescriptor(final PropertyDescriptor descriptor) {
117 this.property = descriptor;
118 }
119
120 public void addTo(final List list) {
121 list.add(this);
122 }
123
124 /***
125 * @see java.lang.Object#toString()
126 */
127 public String toString() {
128 if (property == IDENTITY) {
129 return "Property.Identity";
130 }
131 final StringBuffer buf = new StringBuffer();
132 buf.append(property.getReadMethod().getDeclaringClass().getName());
133 buf.append('.');
134 buf.append(property.getReadMethod().getName());
135 buf.append("()");
136 return buf.toString();
137 }
138 }
This page was automatically generated by Maven