001: /*
002: * Copyright 1999-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.jxpath;
017:
018: import java.beans.BeanInfo;
019: import java.beans.IntrospectionException;
020: import java.beans.Introspector;
021: import java.beans.PropertyDescriptor;
022: import java.util.Arrays;
023: import java.util.Comparator;
024:
025: /**
026: * An implementation of JXPathBeanInfo based on JavaBeans' BeanInfo. Properties
027: * advertised by JXPathBasicBeanInfo are the same as those advertised by
028: * BeanInfo for the corresponding class.
029: *
030: * See java.beans.BeanInfo, java.beans.Introspector
031: *
032: * @author Dmitri Plotnikov
033: * @version $Revision: 1.9 $ $Date: 2004/05/08 15:03:36 $
034: */
035: public class JXPathBasicBeanInfo implements JXPathBeanInfo {
036: private boolean atomic = false;
037: private Class clazz;
038: private PropertyDescriptor propertyDescriptors[];
039: private String[] propertyNames;
040: private Class dynamicPropertyHandlerClass;
041:
042: public JXPathBasicBeanInfo(Class clazz) {
043: this .clazz = clazz;
044: }
045:
046: public JXPathBasicBeanInfo(Class clazz, boolean atomic) {
047: this .clazz = clazz;
048: this .atomic = atomic;
049: }
050:
051: public JXPathBasicBeanInfo(Class clazz,
052: Class dynamicPropertyHandlerClass) {
053: this .clazz = clazz;
054: this .atomic = false;
055: this .dynamicPropertyHandlerClass = dynamicPropertyHandlerClass;
056: }
057:
058: /**
059: * Returns true if objects of this class are treated as atomic
060: * objects which have no properties of their own.
061: */
062: public boolean isAtomic() {
063: return atomic;
064: }
065:
066: /**
067: * Return true if the corresponding objects have dynamic properties.
068: */
069: public boolean isDynamic() {
070: return dynamicPropertyHandlerClass != null;
071: }
072:
073: public PropertyDescriptor[] getPropertyDescriptors() {
074: if (propertyDescriptors == null) {
075: try {
076: BeanInfo bi = null;
077: if (clazz.isInterface()) {
078: bi = Introspector.getBeanInfo(clazz);
079: } else {
080: bi = Introspector.getBeanInfo(clazz, Object.class);
081: }
082: PropertyDescriptor[] pds = bi.getPropertyDescriptors();
083: propertyDescriptors = new PropertyDescriptor[pds.length];
084: System.arraycopy(pds, 0, propertyDescriptors, 0,
085: pds.length);
086: Arrays.sort(propertyDescriptors, new Comparator() {
087: public int compare(Object left, Object right) {
088: return ((PropertyDescriptor) left).getName()
089: .compareTo(
090: ((PropertyDescriptor) right)
091: .getName());
092: }
093: });
094: } catch (IntrospectionException ex) {
095: ex.printStackTrace();
096: }
097: }
098: return propertyDescriptors;
099: }
100:
101: public PropertyDescriptor getPropertyDescriptor(String propertyName) {
102: if (propertyNames == null) {
103: PropertyDescriptor[] pds = getPropertyDescriptors();
104: propertyNames = new String[pds.length];
105: for (int i = 0; i < pds.length; i++) {
106: propertyNames[i] = pds[i].getName();
107: }
108: }
109:
110: for (int i = 0; i < propertyNames.length; i++) {
111: if (propertyNames[i] == propertyName) {
112: return propertyDescriptors[i];
113: }
114: }
115:
116: for (int i = 0; i < propertyNames.length; i++) {
117: if (propertyNames[i].equals(propertyName)) {
118: return propertyDescriptors[i];
119: }
120: }
121: return null;
122: }
123:
124: /**
125: * For a dynamic class, returns the corresponding DynamicPropertyHandler
126: * class.
127: */
128: public Class getDynamicPropertyHandlerClass() {
129: return dynamicPropertyHandlerClass;
130: }
131:
132: public String toString() {
133: StringBuffer buffer = new StringBuffer();
134: buffer.append("BeanInfo [class = ");
135: buffer.append(clazz.getName());
136: if (isDynamic()) {
137: buffer.append(", dynamic");
138: }
139: if (isAtomic()) {
140: buffer.append(", atomic");
141: }
142: buffer.append(", properties = ");
143: PropertyDescriptor[] jpds = getPropertyDescriptors();
144: for (int i = 0; i < jpds.length; i++) {
145: buffer.append("\n ");
146: buffer.append(jpds[i].getPropertyType());
147: buffer.append(": ");
148: buffer.append(jpds[i].getName());
149: }
150: buffer.append("]");
151: return buffer.toString();
152: }
153: }
|