001: /**
002: * Objective Database Abstraction Layer (ODAL)
003: * Copyright (c) 2004, The ODAL Development Group
004: * All rights reserved.
005: * For definition of the ODAL Development Group please refer to LICENCE.txt file
006: *
007: * Distributable under LGPL license.
008: * See terms of license at gnu.org.
009: */package com.completex.objective.components.sdl.writer.impl;
010:
011: import java.beans.BeanInfo;
012: import java.beans.IntrospectionException;
013: import java.beans.Introspector;
014: import java.beans.PropertyDescriptor;
015: import java.lang.reflect.Field;
016: import java.lang.reflect.Method;
017: import java.util.*;
018:
019: /**
020: * @author Andrew Suprun
021: */
022: public class AbstractSdlWriter {
023: private boolean preserveOrder; // By default will be sorted
024: private boolean preserveNulls; // By default null fields will not appear
025: private static final Object[] EMPTY_OBJECT_ARRAY = new Object[0];
026: private static final Map getterMaps = new HashMap();
027:
028: protected Map get(Object object) {
029: Map values = new LinkedHashMap();
030: Class aClass = object.getClass();
031: Map getters = getGetters(aClass);
032: Set set = getters.entrySet();
033: for (Iterator iterator = set.iterator(); iterator.hasNext();) {
034: Map.Entry entry = (Map.Entry) iterator.next();
035: String propertyName = (String) entry.getKey();
036: if (propertyName.startsWith("_")) {
037: propertyName = propertyName.substring(1);
038: }
039: if (!"class".equals(propertyName)) {
040: Method readMethod = (Method) entry.getValue();
041: try {
042: Object v = readMethod.invoke(object,
043: EMPTY_OBJECT_ARRAY);
044: if (v != null || preserveNulls) {
045: values.put(propertyName, v);
046: }
047: } catch (Throwable t) {
048: throw new RuntimeException(t);
049: }
050: }
051: }
052: return values;
053: }
054:
055: private synchronized Map getGetters(Class aClass) {
056: String className = aClass.getName();
057: Map getters = (Map) getterMaps.get(className);
058: if (getters != null) {
059: return getters;
060: }
061:
062: getters = new LinkedHashMap();
063: if (!preserveOrder) {
064: BeanInfo beanInfo = null;
065: try {
066: beanInfo = Introspector.getBeanInfo(aClass);
067: } catch (IntrospectionException e) {
068: throw new RuntimeException(e);
069: }
070: PropertyDescriptor[] propertyDescriptors = beanInfo
071: .getPropertyDescriptors();
072: for (int i = 0; i < propertyDescriptors.length; i++) {
073: PropertyDescriptor propertyDescriptor = propertyDescriptors[i];
074: Method readMethod = propertyDescriptor.getReadMethod();
075: if (readMethod != null) {
076: getters.put(propertyDescriptor.getName(),
077: readMethod);
078: }
079: }
080: } else {
081: Field[] fields = getAllFields(aClass);
082: String fname = null;
083: String mname = null;
084: for (int i = 0; i < fields.length; i++) {
085: try {
086: fname = fields[i].getName();
087: mname = "get"
088: + ((fname.startsWith("_")) ? "" : "_")
089: + fname;
090: getters.put(fname, aClass.getMethod(mname,
091: (java.lang.Class[]) null));
092: } catch (NoSuchMethodException e) {
093: }
094: }
095: }
096: getterMaps.put(className, getters);
097: return getters;
098: }
099:
100: /**
101: * Returns array of all fields belonging to Class cl (including inherited from parent classes)
102: */
103: private Field[] getAllFields(Class cl) {
104: Class super class = cl.getSuperclass();
105: Field[] fieldsup = new Field[0];
106: if (super class != null) {
107: fieldsup = getAllFields(super class);
108: }
109: Field[] fieldshere = cl.getDeclaredFields();
110: Field[] fields = new Field[fieldsup.length + fieldshere.length];
111: System.arraycopy(fieldsup, 0, fields, 0, fieldsup.length);
112: System.arraycopy(fieldshere, 0, fields, fieldsup.length,
113: fieldshere.length);
114: return fields;
115: }
116:
117: public boolean isPreserveOrder() {
118: return preserveOrder;
119: }
120:
121: public void setPreserveOrder(boolean preserveOrder) {
122: this .preserveOrder = preserveOrder;
123: }
124:
125: public boolean isPreserveNulls() {
126: return preserveNulls;
127: }
128:
129: public void setPreserveNulls(boolean preserveNulls) {
130: this.preserveNulls = preserveNulls;
131: }
132: }
|