0001: /*
0002: * Created on 07.03.2005
0003: *
0004: */
0005: package net.sf.crispy.util;
0006:
0007: import java.beans.IntrospectionException;
0008: import java.lang.reflect.Array;
0009: import java.lang.reflect.Constructor;
0010: import java.lang.reflect.Method;
0011: import java.math.BigDecimal;
0012: import java.math.BigInteger;
0013: import java.security.AccessController;
0014: import java.sql.Time;
0015: import java.sql.Timestamp;
0016: import java.text.DateFormat;
0017: import java.text.ParseException;
0018: import java.text.SimpleDateFormat;
0019: import java.util.ArrayList;
0020: import java.util.Collection;
0021: import java.util.Comparator;
0022: import java.util.Date;
0023: import java.util.HashSet;
0024: import java.util.Hashtable;
0025: import java.util.Iterator;
0026: import java.util.Locale;
0027: import java.util.Map;
0028: import java.util.Properties;
0029: import java.util.Set;
0030: import java.util.TreeMap;
0031: import java.util.Vector;
0032: import java.util.Map.Entry;
0033:
0034: import net.sf.crispy.InvocationException;
0035: import net.sf.crispy.impl.ServiceManager;
0036:
0037: import org.apache.commons.logging.Log;
0038: import org.apache.commons.logging.LogFactory;
0039:
0040: /**
0041: * Convert complex object in a simple form and back to the object.
0042: *
0043: * @author Linke
0044: *
0045: */
0046: public class Converter {
0047:
0048: public final static String HASH_CODE_KEY = "~hashCode~";
0049: public final static int GET_METHOD = 1;
0050: public final static int SET_METHOD = 2;
0051:
0052: protected static final Log log = LogFactory.getLog(Converter.class);
0053: protected static final Map isSimpleDataType = new Hashtable();
0054: protected static final ClassPropertiesCache classPropertiesCacheGetter = new ClassPropertiesCache();
0055: protected static final ClassPropertiesCache classPropertiesCacheSetter = new ClassPropertiesCache();
0056:
0057: private String nullValue = null;
0058: private boolean withSimpleKeyMapper = false;
0059:
0060: public Converter() {
0061: }
0062:
0063: public Converter(String pvNullValue) {
0064: setNullValue(pvNullValue);
0065: }
0066:
0067: public Converter(int pvStartHashCode) {
0068: uniqueIdMap = new Hashtable();
0069: startHashCode = pvStartHashCode;
0070: }
0071:
0072: public void setWithSimpleKeyMapper(boolean pvWithSimpleKeyMapper) {
0073: withSimpleKeyMapper = pvWithSimpleKeyMapper;
0074: }
0075:
0076: public boolean getWithSimpleKeyMapper() {
0077: return withSimpleKeyMapper;
0078: }
0079:
0080: public static boolean isValideEdge(Object pvObject) {
0081: if (pvObject == null) {
0082: return false;
0083: }
0084: if (isSimpleDataType.size() == 0) {
0085: isSimpleDataType.put(String.class, String.class);
0086: isSimpleDataType.put(Boolean.class, Boolean.class);
0087: isSimpleDataType.put(boolean.class, boolean.class);
0088: isSimpleDataType.put(Byte.class, Byte.class);
0089: isSimpleDataType.put(byte.class, byte.class);
0090: isSimpleDataType.put(Short.class, Short.class);
0091: isSimpleDataType.put(short.class, short.class);
0092: isSimpleDataType.put(Integer.class, Integer.class);
0093: isSimpleDataType.put(int.class, int.class);
0094: isSimpleDataType.put(Long.class, Long.class);
0095: isSimpleDataType.put(long.class, long.class);
0096: isSimpleDataType.put(Float.class, Float.class);
0097: isSimpleDataType.put(float.class, float.class);
0098: isSimpleDataType.put(Double.class, Double.class);
0099: isSimpleDataType.put(double.class, double.class);
0100: isSimpleDataType.put(Character.class, Character.class);
0101: isSimpleDataType.put(char.class, char.class);
0102: isSimpleDataType.put(BigDecimal.class, BigDecimal.class);
0103: isSimpleDataType
0104: .put(StringBuffer.class, StringBuffer.class);
0105: isSimpleDataType.put(BigInteger.class, BigInteger.class);
0106: isSimpleDataType.put(Class.class, Class.class);
0107: isSimpleDataType.put(java.sql.Date.class,
0108: java.sql.Date.class);
0109: isSimpleDataType.put(java.util.Date.class,
0110: java.util.Date.class);
0111: isSimpleDataType.put(Time.class, Time.class);
0112: isSimpleDataType.put(Timestamp.class, Timestamp.class);
0113: }
0114: boolean b = isSimpleDataType.containsKey(pvObject.getClass());
0115: // return ! b;
0116: if (b == true) {
0117: return false;
0118: } else {
0119: return true;
0120: }
0121: }
0122:
0123: private Map uniqueIdMap = null;
0124: private Map uniqueIdMap2 = null;
0125: private int hashCode = 0;
0126: private int startHashCode = -1000000000;
0127:
0128: // private int startHashCode = 1;
0129:
0130: public String getNullValue() {
0131: return nullValue;
0132: }
0133:
0134: public void setNullValue(String pvNullValue) {
0135: nullValue = pvNullValue;
0136: }
0137:
0138: public String findNextFreeId(int pvHashCode) {
0139: String lvHashCodeStr = Integer.toString(pvHashCode);
0140: if (uniqueIdMap.containsKey(lvHashCodeStr)) {
0141: int next = pvHashCode + 1;
0142: lvHashCodeStr = findNextFreeId(next);
0143: }
0144: return lvHashCodeStr;
0145: }
0146:
0147: private String getUniqueId(Object pvObject) {
0148: if (pvObject == null) {
0149: return null;
0150: }
0151:
0152: String lvHashCode = (String) uniqueIdMap2.get(pvObject);
0153: if (lvHashCode != null) {
0154: return lvHashCode;
0155: }
0156:
0157: lvHashCode = Integer.toString(pvObject.hashCode());
0158: Object o = uniqueIdMap.get(lvHashCode);
0159: if (o == null) {
0160: uniqueIdMap.put(lvHashCode, pvObject);
0161: uniqueIdMap2.put(pvObject, lvHashCode);
0162: } else { //if (!(o.equals(pvObject))) {
0163: lvHashCode = findNextFreeId(hashCode);
0164: uniqueIdMap.put(lvHashCode, pvObject);
0165: uniqueIdMap2.put(pvObject, lvHashCode);
0166: }
0167: return lvHashCode;
0168: }
0169:
0170: /**
0171: * Convert complex object in a Hashtable and relations in Vector.
0172: * Cycles are finded and removed.
0173: *
0174: * @param pvObject Complex or simple object.
0175: * @return Simple object structure.
0176: * @throws Exception
0177: */
0178: public Object makeSimple(Object pvObject) throws Exception {
0179: uniqueIdMap = new Hashtable();
0180: uniqueIdMap2 = new Hashtable();
0181: hashCode = startHashCode;
0182: Object lvReturn = null;
0183: try {
0184: lvReturn = makeSimpleInternal(pvObject, null, new Graph());
0185: } finally {
0186: uniqueIdMap = null;
0187: uniqueIdMap2 = null;
0188: hashCode = startHashCode;
0189: }
0190: return lvReturn;
0191: }
0192:
0193: private Object makeSimpleInternal(Object pvObject,
0194: String pvPropName, Graph pvGraph) throws Exception {
0195: Method lvMethod = null;
0196:
0197: if (pvObject == null) {
0198: return nullValue;
0199: }
0200:
0201: Class clazz = pvObject.getClass();
0202:
0203: // Zugelassene Datentypen
0204: if (clazz.equals(String.class)) {
0205: return pvObject;
0206: } else if (clazz.equals(Date.class)) {
0207: return pvObject;
0208: } else if (clazz.equals(Integer.class)) {
0209: return pvObject;
0210: } else if (clazz.equals(Double.class)) {
0211: return pvObject;
0212: } else if (clazz.equals(Boolean.class)) {
0213: return pvObject;
0214: }
0215:
0216: else if ((clazz.isArray())
0217: && (clazz.getComponentType().equals(byte.class))) {
0218: return pvObject;
0219: }
0220:
0221: // Konvertierung von primitiven Typen in zugelassene primitive Typen
0222: else if (clazz.equals(Long.class)) {
0223: return Integer.valueOf(pvObject.toString());
0224: } else if (clazz.equals(Byte.class)) {
0225: return Integer.valueOf(pvObject.toString());
0226: } else if (clazz.equals(Float.class)) {
0227: return new Double(pvObject.toString());
0228: } else if (clazz.equals(Character.class)) {
0229: return pvObject.toString();
0230: } else if (clazz.equals(Short.class)) {
0231: return Integer.valueOf(pvObject.toString());
0232: } else if (clazz.equals(BigDecimal.class)) {
0233: return new Double(pvObject.toString());
0234: } else if (clazz.equals(StringBuffer.class)) {
0235: return pvObject.toString();
0236: } else if (clazz.equals(BigInteger.class)) {
0237: return Integer.valueOf(pvObject.toString());
0238: } else if (clazz.equals(Class.class)) {
0239: return ((Class) pvObject).getName();
0240: } else if (clazz.equals(java.sql.Date.class)) {
0241: return new Date(((java.sql.Date) pvObject).getTime());
0242: } else if (clazz.equals(Time.class)) {
0243: return (Date) pvObject;
0244: } else if (clazz.equals(Timestamp.class)) {
0245: return (Date) pvObject;
0246: }
0247:
0248: // Behaelter
0249: else if (pvObject instanceof Collection) {
0250: final Collection coll = (Collection) pvObject;
0251: Iterator it = coll.iterator();
0252: Vector v = new Vector(coll.size());
0253: while (it.hasNext()) {
0254: Object lvValue = it.next();
0255: String lvHashCode = getUniqueId(lvValue);
0256: if (pvGraph.getVertexCount(lvValue) == true) {
0257: v.add(HASH_CODE_KEY + lvHashCode);
0258: } else {
0259: Object o = makeSimpleInternal(lvValue, pvPropName,
0260: pvGraph);
0261: if (o != null) {
0262: v.add(o);
0263: }
0264: }
0265: }
0266: return v;
0267: }
0268:
0269: else if (pvObject instanceof Map) {
0270: Map map = (Map) pvObject;
0271: Hashtable lvHashtable = new Hashtable(map.size());
0272: // um Reihenfolge zu merken, ist wichtig,
0273: // wenn Objekte aus der Map geholt werden
0274: int pos = 0;
0275: SimpleKeyMapper lvKeyMapper = new SimpleKeyMapper();
0276: Iterator it = map.entrySet().iterator();
0277: while (it.hasNext()) {
0278: Map.Entry lvMapEntry = (Entry) it.next();
0279: Object lvKey = lvMapEntry.getKey();
0280: pos = ++pos;
0281: if (isValideEdge(lvKey)) {
0282: throw new InvocationException(
0283: "Complex Map-Key datatypes are not supported: "
0284: + lvKey.getClass().getName());
0285: }
0286: Object lvValue = lvMapEntry.getValue();
0287: String lvHashCode = getUniqueId(lvValue);
0288: // class before transformatio
0289: Class lvKeyClass = lvKey.getClass();
0290: // transformation from the key to simple object (e.g. from Long to Integer)
0291: lvKey = makeSimpleInternal(lvKey, pvPropName, pvGraph);
0292: // den Schluessel in eine Map verpacken, damit die Reihenfolge NICHT verloren geht
0293: Object key = lvKeyMapper.toSimpleNewKey(lvKey,
0294: lvKeyClass, pos, withSimpleKeyMapper);
0295: if (pvGraph.getVertexCount(lvValue) == true) {
0296: lvHashtable.put(key, HASH_CODE_KEY + lvHashCode);
0297: } else {
0298: Object o = makeSimpleInternal(lvValue, pvPropName,
0299: pvGraph);
0300: if (o != null) {
0301: lvHashtable.put(key, o);
0302: }
0303: }
0304: }
0305: return lvHashtable;
0306: }
0307:
0308: else if (clazz.isArray()) {
0309: int length = Array.getLength(pvObject);
0310: Vector v = new Vector(length);
0311: for (int i = 0; i < length; i++) {
0312: Object lvValue = Array.get(pvObject, i);
0313: String lvHashCode = getUniqueId(lvValue);
0314: if (pvGraph.getVertexCount(lvValue) == true) {
0315: v.add(HASH_CODE_KEY + lvHashCode);
0316: } else {
0317: Object o = makeSimpleInternal(lvValue, pvPropName,
0318: pvGraph);
0319: if (o != null) {
0320: v.add(o);
0321: }
0322: }
0323: }
0324: return v;
0325: }
0326:
0327: // Konvertierung von Objekten in eine Hashtable
0328: else {
0329: Hashtable map = new Hashtable();
0330: String lvHashCode = getUniqueId(pvObject);
0331: map.put(HASH_CODE_KEY, lvHashCode);
0332: pvGraph.addVertex(pvObject);
0333: Map lvGetterMap = getAllGetterMethodWithCache(clazz);
0334: Iterator it = lvGetterMap.entrySet().iterator();
0335: while (it.hasNext()) {
0336: Map.Entry lvMapEntry = (Entry) it.next();
0337: String propName = (String) lvMapEntry.getKey();
0338: if (propName.equals("class") == false) {
0339: Object lvValue = null;
0340: try {
0341: lvMethod = (Method) lvMapEntry.getValue();
0342: lvValue = lvMethod.invoke(pvObject, null);
0343: } catch (Exception e) {
0344: if (log.isDebugEnabled()) {
0345: log.debug(
0346: "Problem by invoke method (in makeSimple): "
0347: + lvMethod, e);
0348: }
0349: if (ServiceManager.DEBUG_MODE_ON) {
0350: e.printStackTrace();
0351: }
0352: }
0353:
0354: // test of cycles
0355: if (lvValue != null) {
0356: Edge lvEdge = new Edge(pvObject, lvValue);
0357: if (pvGraph.containsKey(lvEdge) == false) {
0358: if (isValideEdge(lvValue) == true) {
0359: pvGraph.put(lvEdge, "");
0360: }
0361: Edge lvKnownEdgeToThisNode = pvGraph
0362: .isFromContains(lvValue);
0363: if (lvKnownEdgeToThisNode != null) {
0364: lvEdge
0365: .setMethodName(lvMethod
0366: .getName());
0367: map.put(propName, HASH_CODE_KEY
0368: + getUniqueId(lvValue));
0369: } else {
0370: Object o = makeSimpleInternal(lvValue,
0371: propName, pvGraph);
0372: map.put(propName, o);
0373: }
0374: }
0375:
0376: else {
0377: map.put(propName, HASH_CODE_KEY
0378: + getUniqueId(lvValue));
0379: }
0380: }
0381:
0382: } else {
0383: // fuer das unmarshalling
0384: map.put("class", clazz.getName());
0385: }
0386: }
0387: return map;
0388: }
0389: }
0390:
0391: public Object makeComplex(Object pvObject) throws Exception {
0392: return makeComplex(pvObject, null);
0393: }
0394:
0395: public Object makeComplex(Object pvObject, Class clazz)
0396: throws Exception {
0397: return makeComplex(pvObject, clazz, null);
0398: }
0399:
0400: public Object makeComplex(Object pvObject, Class clazz,
0401: Class clazzInArray) throws Exception {
0402: return makeComplexInternal(pvObject, clazz, clazzInArray,
0403: new Hashtable());
0404: }
0405:
0406: private Object makeComplexInternal(final Object pvObject,
0407: final Class pvClazz, final Class clazzInArray,
0408: final Map pvHashCodeMap) throws Exception {
0409: Method lvMethod = null;
0410: Class clazz = pvClazz;
0411:
0412: if (pvObject == null || pvObject.equals(nullValue)) {
0413: return null;
0414: }
0415:
0416: if (pvClazz == null) {
0417: clazz = pvObject.getClass();
0418: }
0419:
0420: // clazz ist vom Typ Object, pvObject aber NICHT,
0421: // deshalb den typ von pvObject uebernehmen
0422: if ((clazz.equals(Object.class))
0423: && (!(pvObject.getClass().equals(Object.class)))) {
0424: if (log.isDebugEnabled()) {
0425: log.debug("Class: " + clazz.getName()
0426: + " is different from Object: "
0427: + pvObject.getClass().getName());
0428: }
0429: clazz = pvObject.getClass();
0430: }
0431:
0432: // Zugelassene Datentypen
0433: if (clazz.equals(String.class)) {
0434: return pvObject;
0435: } else if (clazz.equals(Integer.class)) {
0436: return pvObject;
0437: } else if (clazz.equals(Double.class)) {
0438: return pvObject;
0439: } else if (clazz.equals(Boolean.class)) {
0440: return pvObject;
0441: } else if (clazz.equals(Date.class)) {
0442: return pvObject;
0443: } else if ((clazz.isArray())
0444: && (clazz.getComponentType().equals(byte.class))) {
0445: return pvObject;
0446: }
0447:
0448: // Primitive Datentypen
0449: else if (clazz.equals(boolean.class)) {
0450: return pvObject;
0451: } else if (clazz.equals(short.class)) {
0452: return new Short(pvObject.toString());
0453: } else if (clazz.equals(byte.class)) {
0454: return new Byte(pvObject.toString());
0455: } else if (clazz.equals(int.class)) {
0456: return pvObject;
0457: } else if (clazz.equals(long.class)) {
0458: return new Long(pvObject.toString());
0459: } else if (clazz.equals(double.class)) {
0460: return new Double(pvObject.toString());
0461: } else if (clazz.equals(float.class)) {
0462: return new Float(pvObject.toString());
0463: } else if (clazz.equals(char.class)) {
0464: return new Character(pvObject.toString().charAt(0));
0465: }
0466:
0467: // Konvertierung von primitiven Typen in zugelassene primitive Typen
0468: else if (clazz.equals(Byte.class)) {
0469: return new Byte(pvObject.toString());
0470: } else if (clazz.equals(Float.class)) {
0471: return new Float(pvObject.toString());
0472: } else if (clazz.equals(Character.class)) {
0473: return new Character(pvObject.toString().charAt(0));
0474: } else if (clazz.equals(Long.class)) {
0475: return new Long(pvObject.toString());
0476: } else if (clazz.equals(Short.class)) {
0477: return new Short(pvObject.toString());
0478: } else if (clazz.equals(BigDecimal.class)) {
0479: return new BigDecimal(((Double) pvObject).doubleValue());
0480: } else if (clazz.equals(StringBuffer.class)) {
0481: return new StringBuffer(pvObject.toString());
0482: } else if (clazz.equals(BigInteger.class)) {
0483: return new BigInteger(pvObject.toString());
0484: } else if (clazz.equals(Class.class)) {
0485: return Class.forName(pvObject.toString());
0486: } else if (clazz.equals(java.sql.Date.class)) {
0487: return new java.sql.Date(((Date) pvObject).getTime());
0488: } else if (clazz.equals(Time.class)) {
0489: if (pvObject instanceof Time) {
0490: return (Time) pvObject;
0491: } else if (pvObject instanceof Timestamp) {
0492: Timestamp ts = (Timestamp) pvObject;
0493: return new Time(ts.getTime());
0494: } else if (pvObject instanceof Date) {
0495: return new Time(((Date) pvObject).getTime());
0496: } else if (pvObject instanceof Long) {
0497: Long l = (Long) pvObject;
0498: return new Time(l.longValue());
0499: } else {
0500: throw new InvocationException(
0501: "Exception by Converter.makeComplex. The datatype: "
0502: + pvObject.getClass().getName()
0503: + " is not compatible to java.sql.Time");
0504: }
0505:
0506: } else if (clazz.equals(Timestamp.class)) {
0507: if (pvObject instanceof Timestamp) {
0508: return (Timestamp) pvObject;
0509: } else if (pvObject instanceof Time) {
0510: Time t = (Time) pvObject;
0511: return new Timestamp(t.getTime());
0512: } else if (pvObject instanceof Date) {
0513: return new Timestamp(((Date) pvObject).getTime());
0514: } else if (pvObject instanceof Long) {
0515: Long l = (Long) pvObject;
0516: return new Timestamp(l.longValue());
0517: } else {
0518: throw new InvocationException(
0519: "Exception by Converter.makeComplex. The datatype: "
0520: + pvObject.getClass().getName()
0521: + " is not compatible to java.sql.Timestamp");
0522: }
0523:
0524: }
0525:
0526: else if (clazz.isArray()) {
0527: // wenn pvObject kein Array ist
0528: if ((!pvObject.getClass().isArray())
0529: && (pvObject instanceof Collection)) {
0530: Collection coll = (Collection) pvObject;
0531: Class lvType = (clazzInArray == null ? getArrayType(pvObject)
0532: : clazzInArray);
0533: Object[] lvTempArray = (Object[]) Array.newInstance(
0534: lvType, coll.size());
0535: Iterator it = coll.iterator();
0536: int i = 0;
0537: while (it.hasNext()) {
0538: Object objInList = it.next();
0539: String lvHashCode = objInList.toString();
0540: if (lvHashCode.startsWith(HASH_CODE_KEY) == true) {
0541: lvHashCode = lvHashCode.substring(HASH_CODE_KEY
0542: .length());
0543: Object value = pvHashCodeMap.get(lvHashCode);
0544: if (value == null) {
0545: throw new InvocationException(
0546: "Internal Converter-Exception. "
0547: + "For the HashCode: "
0548: + lvHashCode
0549: + " can not find a object in the Array: "
0550: + coll);
0551: } else {
0552: lvTempArray[i] = value;
0553: }
0554: } else {
0555: if (clazzInArray == null) {
0556: lvTempArray[i] = objInList;
0557: } else {
0558: lvTempArray[i] = makeComplexInternal(
0559: objInList, clazzInArray, null,
0560: pvHashCodeMap);
0561: }
0562: }
0563: i++;
0564: }
0565: return lvTempArray;
0566: }
0567:
0568: // wenn pvObject kein Array ist
0569: if ((!pvObject.getClass().isArray())
0570: && (pvObject instanceof Map)) {
0571: return makeComplexInternal(pvObject, null, null,
0572: pvHashCodeMap);
0573: } else {
0574: int length = Array.getLength(pvObject);
0575: Class lvType = (clazzInArray == null ? getArrayType(pvObject)
0576: : clazzInArray);
0577: Object[] o = (Object[]) Array.newInstance(lvType,
0578: length);
0579: for (int i = 0; i < length; i++) {
0580: Object _o = Array.get(pvObject, i);
0581: o[i] = makeComplexInternal(_o, lvType, null,
0582: pvHashCodeMap);
0583: }
0584: return o;
0585: }
0586: }
0587:
0588: // Behaelter
0589: else if (pvObject instanceof Collection) {
0590: Collection coll = (Collection) pvObject;
0591: Iterator it = coll.iterator();
0592: Collection lvNewColl = new Vector(coll.size());
0593: while (it.hasNext()) {
0594: Object o = it.next();
0595: // null value are ignored
0596: if (o != null) {
0597: String lvHashCode = o.toString();
0598: if (lvHashCode.startsWith(HASH_CODE_KEY) == true) {
0599: lvHashCode = lvHashCode.substring(HASH_CODE_KEY
0600: .length());
0601: Object value = pvHashCodeMap.get(lvHashCode);
0602: if (value == null) {
0603: throw new InvocationException(
0604: "Internal Converter-Exception. "
0605: + "For the HashCode: "
0606: + lvHashCode
0607: + " can not find a object in the Collection: "
0608: + coll);
0609: } else {
0610: lvNewColl.add(value);
0611: }
0612: } else {
0613: Object value = makeComplexInternal(o, o
0614: .getClass(), null, pvHashCodeMap);
0615: lvNewColl.add(value);
0616: }
0617: }
0618: }
0619: return lvNewColl;
0620: }
0621:
0622: else if (pvObject instanceof Map) {
0623: Map map = (Map) pvObject;
0624: String aClazzStr = (String) map.get("class");
0625: if (aClazzStr == null) {
0626:
0627: SimpleKeyMapper lvSimpleKeyMapper = new SimpleKeyMapper();
0628: map = lvSimpleKeyMapper.orderMapByKey(map,
0629: withSimpleKeyMapper);
0630:
0631: Iterator it = map.entrySet().iterator();
0632: Hashtable lvHashtable = new Hashtable(map.size());
0633: while (it.hasNext()) {
0634: Map.Entry lvMapEntry = (Entry) it.next();
0635: Object lvKey = lvMapEntry.getKey();
0636: Object key = lvSimpleKeyMapper.toComplexNewKey(
0637: lvKey, withSimpleKeyMapper);
0638: Object lvValue = lvMapEntry.getValue();
0639: String lvHashCode = extractHashCode(lvValue);
0640: if (lvHashCode != null) {
0641: lvValue = pvHashCodeMap.get(lvHashCode);
0642: if (lvValue == null) {
0643: throw new InvocationException(
0644: "Internal Converter-Exception. "
0645: + "For the HashCode: "
0646: + lvHashCode
0647: + " can not find a object in a map: "
0648: + map);
0649: } else {
0650: lvHashtable.put(key, lvValue);
0651: }
0652: } else {
0653: lvValue = makeComplexInternal(lvValue, lvValue
0654: .getClass(), null, pvHashCodeMap);
0655: lvHashtable.put(key, lvValue);
0656: }
0657: }
0658: return lvHashtable;
0659:
0660: } else {
0661: Class aClazz = null;
0662: try {
0663: aClazz = Class.forName(aClazzStr);
0664: } catch (Exception e) {
0665: throw new InvocationException("Can't load class: "
0666: + aClazzStr);
0667: }
0668: Object ret = null;
0669: try {
0670: ret = aClazz.newInstance();
0671: } catch (Exception e) {
0672: throw new InvocationException(
0673: "Can't create object from class: "
0674: + aClazzStr
0675: + " (No constructor without parameter!)");
0676: }
0677:
0678: String lvMapHashCode = (String) map.get(HASH_CODE_KEY);
0679: if (lvMapHashCode != null) {
0680: pvHashCodeMap.put(lvMapHashCode, ret);
0681: }
0682:
0683: try {
0684: Map lvSetterMap = getAllSetterMethodWithCache(aClazz);
0685: Iterator it = lvSetterMap.entrySet().iterator();
0686: while (it.hasNext()) {
0687: Map.Entry lvMapEntry = (Entry) it.next();
0688: String propName = (String) lvMapEntry.getKey();
0689: Object value = map.get(propName);
0690: if (value != null) {
0691: String lvHashCode = extractHashCode(value);
0692: lvMethod = (Method) lvMapEntry.getValue();
0693: Class lvParamType = lvMethod
0694: .getParameterTypes()[0];
0695:
0696: if (lvHashCode != null) {
0697: value = pvHashCodeMap.get(lvHashCode);
0698: } else {
0699: value = makeComplexInternal(value,
0700: lvParamType, lvParamType
0701: .getComponentType(),
0702: pvHashCodeMap);
0703: }
0704: try {
0705: value = makeAssignable(lvParamType,
0706: value);
0707: lvMethod.invoke(ret,
0708: new Object[] { value });
0709: } catch (Exception e) {
0710: if (log.isDebugEnabled()) {
0711: log.debug(
0712: "Problem by invoke method (in makeComplex): "
0713: + lvMethod, e);
0714: }
0715: if (ServiceManager.DEBUG_MODE_ON) {
0716: e.printStackTrace();
0717: }
0718: }
0719: } // value != null
0720: }
0721: } catch (Exception e) {
0722: if (log.isWarnEnabled()) {
0723: log.warn(
0724: "Error in Converter.makeComplex by Method: "
0725: + lvMethod, e);
0726: }
0727: if (ServiceManager.DEBUG_MODE_ON) {
0728: e.printStackTrace();
0729: }
0730: }
0731:
0732: return ret;
0733: }
0734: } else {
0735: if (log.isInfoEnabled()) {
0736: log.info("Invalide Type in makeComplex for Class: "
0737: + clazz + " with value: " + pvObject
0738: + " (Class from Object: "
0739: + pvObject.getClass().getName() + ")");
0740: }
0741: return null;
0742: }
0743: }
0744:
0745: public static Object makeAssignable(Class pvSourceClass,
0746: Object pvValue) {
0747: if (pvSourceClass.equals(Object.class)) {
0748: return pvValue;
0749: } else if (pvSourceClass.isAssignableFrom(Set.class)
0750: && pvValue instanceof Collection) {
0751: return new HashSet((Collection) pvValue);
0752: } else if (pvSourceClass.isAssignableFrom(Properties.class)
0753: && pvValue instanceof Map) {
0754: Map lvMap = (Map) pvValue;
0755: Properties lvProperties = new Properties();
0756: Iterator it = lvMap.entrySet().iterator();
0757: while (it.hasNext()) {
0758: Map.Entry lvMapEntry = (Entry) it.next();
0759: lvProperties.put(lvMapEntry.getKey(), lvMapEntry
0760: .getValue());
0761: }
0762: return lvProperties;
0763: }
0764: return pvValue;
0765: }
0766:
0767: /**
0768: * Analysed the objects in an Array/Collection. If all Object from the same class type,
0769: * then is the return value this class. If are several class types in the Array,
0770: * then ist the return class from type Object.
0771: * @param pvListObj
0772: * @return Class, that are in the Array or Collection
0773: */
0774: public static Class getArrayType(Object pvListObj) {
0775: Class lvType = Object.class;
0776: if (pvListObj == null) {
0777: return lvType;
0778: }
0779: if (pvListObj.getClass().isArray()) {
0780: Object o[] = (Object[]) pvListObj;
0781: if (o.length > 0) {
0782: Class lvClass = o[0].getClass();
0783:
0784: // !!!!! Specialfall ?????
0785: if (Map.class.isAssignableFrom(lvClass)) {
0786: return Object.class;
0787: }
0788:
0789: for (int i = 0; i < o.length; i++) {
0790: if (!lvClass.equals(o[i].getClass())) {
0791: return lvType;
0792: }
0793: }
0794: return lvClass;
0795: } else {
0796: return o.getClass().getComponentType();
0797: }
0798: } else if (pvListObj instanceof Collection) {
0799: Collection coll = (Collection) pvListObj;
0800: if (coll.size() > 0) {
0801: Class lvClass = coll.iterator().next().getClass();
0802: Iterator it = coll.iterator();
0803: while (it.hasNext()) {
0804: if (!lvClass.equals(it.next().getClass())) {
0805: return lvType;
0806: }
0807: }
0808: return lvClass;
0809: } else {
0810: return lvType;
0811: }
0812: }
0813: return lvType;
0814: }
0815:
0816: public static Object convertClassString2Object(String pvClassName)
0817: throws Exception {
0818:
0819: if (pvClassName == null) {
0820: return null;
0821: }
0822:
0823: else if (pvClassName.equals(int.class.getName())) {
0824: return Integer.valueOf("0");
0825: } else if (pvClassName.equals(short.class.getName())) {
0826: return Short.valueOf("0");
0827: } else if (pvClassName.equals(byte.class.getName())) {
0828: return Byte.valueOf("0");
0829: } else if (pvClassName.equals(long.class.getName())) {
0830: return Long.valueOf("0");
0831: }
0832:
0833: else if (pvClassName.equals(double.class.getName())) {
0834: return Double.valueOf("0");
0835: } else if (pvClassName.equals(float.class.getName())) {
0836: return Float.valueOf("0");
0837: }
0838:
0839: else if (pvClassName.equals(char.class.getName())) {
0840: return new Character(' ');
0841: } else if (pvClassName.equals(Character.class.getName())) {
0842: return new Character(' ');
0843: }
0844:
0845: else if (pvClassName.equals(boolean.class.getName())) {
0846: return Boolean.TRUE;
0847: } else if (pvClassName.equals(Boolean.class.getName())) {
0848: return Boolean.TRUE;
0849: }
0850:
0851: else if ((pvClassName.startsWith("[L"))
0852: && (pvClassName.endsWith(";"))) {
0853: try {
0854: String lvClassName = pvClassName.substring(2,
0855: (pvClassName.length() - 1));
0856: lvClassName = lvClassName.replaceAll("/", "\\.");
0857: Class clazz = Class.forName(lvClassName);
0858: Object lvArray = Array.newInstance(clazz, 0);
0859: return lvArray;
0860: } catch (Exception e) {
0861: if (log.isDebugEnabled()) {
0862: log.debug("convertClassString2Object: "
0863: + pvClassName, e);
0864: }
0865: if (ServiceManager.DEBUG_MODE_ON) {
0866: e.printStackTrace();
0867: }
0868: return null;
0869: }
0870: }
0871:
0872: else {
0873: Object lvReturn = createObjectWithParamConstructor(null,
0874: null, pvClassName);
0875: if (lvReturn == null) {
0876: lvReturn = createObjectWithParamConstructor(
0877: new Long(0), long.class, pvClassName);
0878: }
0879: if (lvReturn == null) {
0880: lvReturn = createObjectWithParamConstructor("0",
0881: String.class, pvClassName);
0882: }
0883: return lvReturn;
0884: }
0885:
0886: }
0887:
0888: public static Object createObjectWithParamConstructor(
0889: Object pvParam, Class pvParamType, String pvClassName) {
0890: try {
0891: Class clazz = Class.forName(pvClassName);
0892: if (pvParam == null) {
0893: return clazz.newInstance();
0894: } else {
0895: Constructor lvConstructor = clazz
0896: .getConstructor(new Class[] { pvParamType });
0897: return lvConstructor
0898: .newInstance(new Object[] { pvParam });
0899: }
0900: } catch (Exception e) {
0901: if (log.isDebugEnabled()) {
0902: log.debug("convertString2ClassString: " + pvClassName,
0903: e);
0904: }
0905: if (ServiceManager.DEBUG_MODE_ON) {
0906: e.printStackTrace();
0907: }
0908: }
0909: return null;
0910: }
0911:
0912: /**
0913: * For example String to Integer or String to Date, ...
0914: *
0915: * @param pvStringValue Value in String class.
0916: * @param pvClassName Class name to convert the String value.
0917: * @return The converted String value.
0918: */
0919: public static Object convertString2Value(String pvStringValue,
0920: String pvClassName) throws Exception {
0921: Class clazz = null;
0922:
0923: if (pvClassName.equals(int.class.getName())) {
0924: clazz = int.class;
0925: } else if (pvClassName.equals(short.class.getName())) {
0926: clazz = short.class;
0927: } else if (pvClassName.equals(byte.class.getName())) {
0928: clazz = byte.class;
0929: } else if (pvClassName.equals(long.class.getName())) {
0930: clazz = long.class;
0931: }
0932:
0933: else if (pvClassName.equals(double.class.getName())) {
0934: clazz = double.class;
0935: } else if (pvClassName.equals(float.class.getName())) {
0936: clazz = float.class;
0937: }
0938:
0939: else if (pvClassName.equals(char.class.getName())) {
0940: clazz = char.class;
0941: }
0942:
0943: else if (pvClassName.equals(boolean.class.getName())) {
0944: clazz = boolean.class;
0945: }
0946:
0947: else {
0948: try {
0949: clazz = Class.forName(pvClassName);
0950: } catch (Exception e) {
0951: if (ServiceManager.DEBUG_MODE_ON) {
0952: e.printStackTrace();
0953: }
0954: }
0955:
0956: }
0957:
0958: return convertString2Value(pvStringValue, clazz);
0959: }
0960:
0961: public static Object convertString2Value(Object pvValue,
0962: Class pvClass) {
0963: if (pvValue instanceof String) {
0964: return convertString2Value((String) pvValue, pvClass);
0965: } else {
0966: return pvValue;
0967: }
0968: }
0969:
0970: /**
0971: * For example String to Integer or String to Date, ...
0972: *
0973: * @param pvStringValue Value in String class.
0974: * @param pvClass Type to convert the String value.
0975: * @return The converted String value.
0976: */
0977: public static Object convertString2Value(String pvStringValue,
0978: Class pvClass) {
0979: if ((pvClass == null) || (pvStringValue == null)) {
0980: return null;
0981: }
0982:
0983: if ((pvClass.equals(Integer.class))
0984: || (pvClass.equals(int.class))) {
0985: return Integer.valueOf(pvStringValue);
0986: } else if ((pvClass.equals(Short.class))
0987: || (pvClass.equals(short.class))) {
0988: return Short.valueOf(pvStringValue);
0989: } else if ((pvClass.equals(Byte.class))
0990: || (pvClass.equals(byte.class))) {
0991: return Byte.valueOf(pvStringValue);
0992: } else if ((pvClass.equals(Long.class))
0993: || (pvClass.equals(long.class))) {
0994: return Long.valueOf(pvStringValue);
0995: }
0996:
0997: else if ((pvClass.equals(Double.class))
0998: || (pvClass.equals(double.class))) {
0999: return Double.valueOf(pvStringValue);
1000: } else if ((pvClass.equals(Float.class))
1001: || (pvClass.equals(float.class))) {
1002: return Float.valueOf(pvStringValue);
1003: } else if (pvClass.equals(BigDecimal.class)) {
1004: return new BigDecimal(pvStringValue);
1005: }
1006:
1007: else if ((pvClass.equals(Boolean.class))
1008: || (pvClass.equals(boolean.class))) {
1009: return Boolean.valueOf(pvStringValue);
1010: }
1011:
1012: else if ((pvClass.equals(Character.class))
1013: || (pvClass.equals(char.class))) {
1014: return new Character(pvStringValue.charAt(0));
1015: } else if (pvClass.equals(String.class)) {
1016: return pvStringValue;
1017: }
1018:
1019: else if ((pvClass.equals(Date.class))
1020: || (pvClass.equals(java.sql.Date.class))) {
1021: return Converter.string2Date(pvStringValue);
1022: }
1023:
1024: else {
1025: // ??? throw new IllegalArgumentException("Illegal value: " + pvStringValue + " or type: " + pvClass); ???
1026: log.warn("Illegal value in Converter.convertString2Value: "
1027: + pvStringValue + " for class: " + pvClass);
1028: try {
1029: return pvClass.newInstance();
1030: } catch (Exception e) {
1031: if (log.isDebugEnabled()) {
1032: log.debug(
1033: "Error by convertString2Value by new Instance for class: "
1034: + pvClass, e);
1035: if (ServiceManager.DEBUG_MODE_ON) {
1036: e.printStackTrace();
1037: }
1038: }
1039: return null;
1040: }
1041: }
1042: }
1043:
1044: /**
1045: * Convert String-Parameter (contains in the Vector) in Method-ParameterTypes.
1046: * For example: Vector ["5", "8"] convert to new Integer [] { new Integer(5), new Integer (8) }
1047: *
1048: * @param pvMethod Method with ParameterTypes.
1049: * @param pvParams Vector with Strings.
1050: *
1051: * @return Array with ParameterTypes and ParameterValues.
1052: */
1053: public static Object[] convertStringParams2MethodParams(
1054: Method pvMethod, Vector pvParams) {
1055: if ((pvMethod == null) || (pvParams == null)) {
1056: throw new IllegalArgumentException("The method parameter: "
1057: + pvMethod + " or params array: " + pvParams
1058: + " must not null!");
1059: }
1060:
1061: Class lvParamTypes[] = pvMethod.getParameterTypes();
1062: if (pvParams.size() != lvParamTypes.length) {
1063: throw new IllegalArgumentException("The parameter size: "
1064: + pvParams.size()
1065: + " is not equal to method parameter: "
1066: + lvParamTypes.length);
1067: }
1068: Object lvNewParams[] = new Object[pvParams.size()];
1069: for (int i = 0; i < lvNewParams.length; i++) {
1070: lvNewParams[i] = Converter.convertString2Value(pvParams
1071: .get(i), lvParamTypes[i]);
1072: }
1073: return lvNewParams;
1074: }
1075:
1076: /**
1077: * Convert a Date-String to a Date. The Converter <b>neglected the Millisecond</b>.
1078: * Example: Thu Aug 11 19:30:57 CEST 2005
1079: *
1080: * @param pvDateString The Date-String (unequal null).
1081: * @return Valid <code>java.util.Date</code>.
1082: */
1083: public static Date string2Date(String pvDateString) {
1084: if (pvDateString == null) {
1085: throw new IllegalArgumentException(
1086: "The Date-String was null by string2Date.");
1087: }
1088:
1089: Date date = null;
1090:
1091: // 1. Versuch, Datum wie es bei der toString-Methode zurueck kommt, umzuwandeln
1092: DateFormat df = new SimpleDateFormat(
1093: "EEE MMM dd HH:mm:ss 'CEST' yyyy", Locale.ENGLISH);
1094: try {
1095: date = df.parse(pvDateString);
1096: return date;
1097: } catch (ParseException e) {
1098: log.debug("Error by string2Date: " + pvDateString, e);
1099: }
1100:
1101: // 2. Versuch, Datum wie es bei der toString-Methode zurueck kommt, umzuwandeln
1102: df = new SimpleDateFormat("EEE MMM dd HH:mm:ss 'CET' yyyy",
1103: Locale.ENGLISH);
1104: try {
1105: date = df.parse(pvDateString);
1106: return date;
1107: } catch (ParseException e) {
1108: log.debug("Error by string2Date: " + pvDateString, e);
1109: }
1110:
1111: // 3. Verusch, Medium Datum umzuwandeln
1112: df = DateFormat.getDateInstance(DateFormat.MEDIUM);
1113: try {
1114: date = df.parse(pvDateString);
1115: return date;
1116: } catch (ParseException e) {
1117: log.debug("Error by string2Date: " + pvDateString, e);
1118: }
1119:
1120: throw new IllegalStateException("The String: \"" + pvDateString
1121: + "\" is not valid date.");
1122: }
1123:
1124: /**
1125: * Extract from package + class name, the class name.
1126: * Example (<code>pvToLowerCase = false)</code>): Input: <code>net.sf.crispy.ServiceManager</code> -> Output: <code>ServiceManger</code>.
1127: * Example (<code>pvToLowerCase = true)</code>): Input: <code>net.sf.crispy.ServiceManager</code> -> Output: <code>servicemanger</code>.
1128: *
1129: * @param pvClassWitchPackage
1130: * @param pvToLowerCase
1131: * @return The class without package.
1132: */
1133: public static String getClassWithoutPackage(
1134: String pvClassWitchPackage, boolean pvToLowerCase) {
1135: if (pvClassWitchPackage == null) {
1136: return null;
1137: }
1138: int lvIndex = pvClassWitchPackage.lastIndexOf('.') + 1;
1139: String lvClassName = pvClassWitchPackage.substring(lvIndex);
1140: if (pvToLowerCase == true) {
1141: lvClassName = lvClassName.toLowerCase();
1142: }
1143: return lvClassName;
1144: }
1145:
1146: /**
1147: * If pvObject.toString - method start with <code>Converter.HASH_CODE_KEY</code>, then extract
1148: * the hash code.
1149: * Example: make from <code>~hashCode~24306761</code> to <code>24306761</code>.
1150: *
1151: * @param pvObject
1152: * @return The hash code.
1153: */
1154: public static String extractHashCode(Object pvObject) {
1155: if (pvObject == null) {
1156: return null;
1157: }
1158: String lvHashCode = pvObject.toString();
1159: if (lvHashCode.startsWith(HASH_CODE_KEY) == true) {
1160: lvHashCode = lvHashCode.substring(HASH_CODE_KEY.length());
1161: return lvHashCode;
1162: } else {
1163: return null;
1164: }
1165:
1166: }
1167:
1168: /**
1169: * Remove all getter-method where no setter-method exist.
1170: */
1171: public static Map getAllNotEqualsGetterAndSetterAndRemoveThisProperties(
1172: Map pvGetterMap, Map pvSetterMap) {
1173: Iterator it = new ArrayList(pvGetterMap.keySet()).iterator();
1174: while (it.hasNext()) {
1175: Object lvGetterProp = it.next();
1176: if (!(pvSetterMap.containsKey(lvGetterProp))
1177: && !(lvGetterProp.equals("class"))) {
1178: if (log.isDebugEnabled()) {
1179: log.debug("For Getter: " + lvGetterProp
1180: + " --> no Setter!");
1181: }
1182: pvGetterMap.remove(lvGetterProp);
1183: }
1184: }
1185: return pvGetterMap;
1186: }
1187:
1188: /**
1189: * Find all getter-method from a Class and remove all getter-method where no setter-method exist.
1190: * @param pvClass Class to anaylse.
1191: * @return Map from getter-method (key=property name, value=method).
1192: * @throws IntrospectionException
1193: */
1194: public static Map getAllGetterMethodWithCache(Class pvClass)
1195: throws IntrospectionException {
1196: Map lvGetterMap = classPropertiesCacheGetter
1197: .getClassPropertiesMapByClass(pvClass);
1198: if (lvGetterMap == null) {
1199: try {
1200: lvGetterMap = Converter.getAllGetterMethod(pvClass);
1201: Map lvSetterMap = getAllSetterMethodWithCache(pvClass);
1202: lvGetterMap = getAllNotEqualsGetterAndSetterAndRemoveThisProperties(
1203: lvGetterMap, lvSetterMap);
1204: classPropertiesCacheGetter.addClassPropertiesMap(
1205: pvClass, lvGetterMap);
1206: } catch (IntrospectionException e) {
1207: if (ServiceManager.DEBUG_MODE_ON) {
1208: e.printStackTrace();
1209: }
1210: }
1211: }
1212: return lvGetterMap;
1213: }
1214:
1215: /**
1216: * Find all setter-method from a Class.
1217: * @param pvClass Class to analyse.
1218: * @return Map all setter-Method (key=property name, value=method).
1219: * @throws IntrospectionException
1220: */
1221: public static Map getAllSetterMethodWithCache(Class pvClass)
1222: throws IntrospectionException {
1223: Map lvMap = classPropertiesCacheSetter
1224: .getClassPropertiesMapByClass(pvClass);
1225: if (lvMap == null) {
1226: try {
1227: lvMap = Converter.getAllSetterMethod(pvClass);
1228: classPropertiesCacheSetter.addClassPropertiesMap(
1229: pvClass, lvMap);
1230: } catch (IntrospectionException e) {
1231: if (ServiceManager.DEBUG_MODE_ON) {
1232: e.printStackTrace();
1233: }
1234: }
1235: }
1236: return lvMap;
1237: }
1238:
1239: /**
1240: * Find all getter-method from a Class.
1241: * @param pvClass Class to analyse.
1242: * @return Map all getter-Method (key=property name, value=method).
1243: * @throws IntrospectionException
1244: */
1245: public static Map getAllGetterMethod(Class pvClass)
1246: throws IntrospectionException {
1247: return getAllGetterAndSetterMethod(pvClass, GET_METHOD);
1248: }
1249:
1250: /**
1251: * Find all setter-method from a Class.
1252: * @param pvClass Class to analyse.
1253: * @return Map all setter-Method (key=property name, value=method).
1254: * @throws IntrospectionException
1255: */
1256: public static Map getAllSetterMethod(Class pvClass)
1257: throws IntrospectionException {
1258: return getAllGetterAndSetterMethod(pvClass, SET_METHOD);
1259: }
1260:
1261: /**
1262: * Get all set/get methods from a Class. With methods from all super classes.
1263: * @param pvClass Analyse Class.
1264: * @return All finded methods.
1265: */
1266: public static Method[] getAllMethodsByClass(Class pvClass) {
1267: Collection lvColl = getAllMethodsByClassIntern(pvClass,
1268: new Hashtable());
1269: return (Method[]) getAllMethodsByClassIntern(pvClass,
1270: new Hashtable()).toArray(new Method[lvColl.size()]);
1271: }
1272:
1273: /**
1274: * Recursive search alle method from the Class in the Class Hierarchy to Object.class.
1275: * @param pvClass Search class.
1276: * @param pvMethodsMap Method map (key=property name, value=method).
1277: * @return All finded methods.
1278: */
1279: private static Collection getAllMethodsByClassIntern(Class pvClass,
1280: Map pvMethodsMap) {
1281: putAllMethodsIntern(pvClass.getMethods(), pvMethodsMap);
1282: putAllMethodsIntern(pvClass.getDeclaredMethods(), pvMethodsMap);
1283:
1284: if (!(pvClass.getSuperclass().equals(Object.class))) {
1285: getAllMethodsByClassIntern(pvClass.getSuperclass(),
1286: pvMethodsMap);
1287: }
1288:
1289: return pvMethodsMap.values();
1290: }
1291:
1292: private static void putAllMethodsIntern(Method pvAllMethods[],
1293: Map pvMethodsMap) {
1294: for (int i = 0; i < pvAllMethods.length; i++) {
1295: String lvMethodName = pvAllMethods[i].getName();
1296: if (lvMethodName.startsWith("set")
1297: || lvMethodName.startsWith("get")) {
1298: pvMethodsMap.put(pvAllMethods[i], pvAllMethods[i]);
1299: }
1300: }
1301: }
1302:
1303: /**
1304: *
1305: * @param pvClass Find all get or set method from a Class.
1306: * @param pvMethodType get or set
1307: * @return Method map (key=property name, value=method).
1308: * @throws IntrospectionException
1309: */
1310: public static Map getAllGetterAndSetterMethod(Class pvClass,
1311: int pvMethodType) throws IntrospectionException {
1312: Method lvAllAmethods[] = getAllMethodsByClass(pvClass);
1313: Map lvGetterOrSetter = new TreeMap();
1314: for (int i = 0; i < lvAllAmethods.length; i++) {
1315: Method lvMethod = null;
1316: String lvPropName = lvAllAmethods[i].getName();
1317: switch (pvMethodType) {
1318: case GET_METHOD:
1319: if (lvPropName.startsWith("get")) {
1320: lvMethod = lvAllAmethods[i];
1321: }
1322: break;
1323: case SET_METHOD:
1324: if (lvPropName.startsWith("set")) {
1325: lvMethod = lvAllAmethods[i];
1326: }
1327: break;
1328: default:
1329: break;
1330: }
1331: if (lvMethod != null) {
1332:
1333: AccessController
1334: .doPrivileged(new MethodAccessiblePrivilegedAction(
1335: lvMethod));
1336:
1337: lvPropName = lvPropName.substring(3);
1338: // PropName muss aus set oder get UND einen Namen bestehen
1339: if (lvPropName.length() > 0) {
1340: lvPropName = lvPropName.substring(0, 1)
1341: .toLowerCase()
1342: + lvPropName.substring(1);
1343: if (lvPropName.equals("class")) {
1344: lvGetterOrSetter.put(lvPropName, pvClass
1345: .getName());
1346: } else {
1347: lvGetterOrSetter.put(lvPropName, lvMethod);
1348: }
1349: } else {
1350: throw new InvocationException(
1351: "Invalid Property-Name: '"
1352: + lvAllAmethods[i].getName()
1353: + "' (Valid Property-Name is: set[name] themed get[name], eg. setYear and getYear).");
1354: }
1355: } // if method != null
1356: } // for
1357: return lvGetterOrSetter;
1358: }
1359:
1360: public static Object createDefaultValueForPrimitiveClass(
1361: Class pvClass) {
1362: Object lvReturn = null;
1363: if (pvClass.equals(boolean.class)) {
1364: lvReturn = Boolean.FALSE;
1365: } else if (pvClass.equals(short.class)) {
1366: lvReturn = Short.valueOf("0");
1367: } else if (pvClass.equals(byte.class)) {
1368: lvReturn = Byte.valueOf("0");
1369: } else if (pvClass.equals(int.class)) {
1370: lvReturn = Integer.valueOf("0");
1371: } else if (pvClass.equals(long.class)) {
1372: lvReturn = Long.valueOf("0");
1373: } else if (pvClass.equals(double.class)) {
1374: lvReturn = Double.valueOf("0");
1375: } else if (pvClass.equals(float.class)) {
1376: lvReturn = Float.valueOf("0");
1377: } else if (pvClass.equals(char.class)) {
1378: lvReturn = new Character(' ');
1379: }
1380:
1381: return lvReturn;
1382: }
1383:
1384: private class SimpleKeyMapper implements Comparator {
1385:
1386: public static final String DELIMITER = "~_-_~";
1387:
1388: private String pos = null;
1389: private Object key = null;
1390:
1391: public SimpleKeyMapper() {
1392: }
1393:
1394: public SimpleKeyMapper(String pvPos, Object pvKey) {
1395: pos = pvPos;
1396: key = pvKey;
1397: }
1398:
1399: public int getPos() {
1400: Integer integer = Integer.valueOf(pos);
1401: return integer.intValue();
1402: }
1403:
1404: public Object getKey() {
1405: return key;
1406: }
1407:
1408: public Object toSimpleNewKey(final Object pvOldKey,
1409: final Class pvOldKeyClass, int pvPos,
1410: boolean pvWithSimpleKeyMapper) {
1411: if (pvWithSimpleKeyMapper) {
1412: String lvNewKey = null;
1413: int l = pvOldKey.toString().split(DELIMITER).length;
1414: if (l == 3 || l == 2) {
1415: lvNewKey = pvOldKey.toString();
1416: } else {
1417: if (pvOldKeyClass.equals(String.class)) {
1418: lvNewKey = pvPos + DELIMITER + pvOldKey;
1419: } else {
1420: lvNewKey = pvPos + DELIMITER + pvOldKey
1421: + DELIMITER + pvOldKeyClass.getName();
1422: }
1423: }
1424: return lvNewKey;
1425: } else {
1426: return pvOldKey;
1427: }
1428: }
1429:
1430: public Object toComplexNewKey(Object pvOriginalKey,
1431: boolean pvWithSimpleKeyMapper) {
1432: if (pvWithSimpleKeyMapper == false) {
1433: return pvOriginalKey;
1434: } else {
1435: Object lvKey = ((SimpleKeyMapper) pvOriginalKey)
1436: .getKey();
1437: return lvKey;
1438: }
1439: }
1440:
1441: public Map orderMapByKey(final Map pvMap,
1442: boolean pvWithSimpleKeyMapper) throws Exception {
1443: if (pvWithSimpleKeyMapper == false) {
1444: return pvMap;
1445: } else {
1446: Iterator it = pvMap.keySet().iterator();
1447: TreeMap lvTreeMap = new TreeMap(new SimpleKeyMapper());
1448: while (it.hasNext()) {
1449: Object o = it.next();
1450: if (!(o instanceof String)) {
1451: throw new IllegalArgumentException(
1452: "Expected String and object is: " + o
1453: + " -> class: "
1454: + o.getClass().getName());
1455: }
1456: String lvKeyStr = (String) o;
1457: String lvKeyArray[] = lvKeyStr.split(DELIMITER);
1458: if (lvKeyArray.length < 2) {
1459: throw new IllegalArgumentException(
1460: "If the property Converter.withSimpleKeyMappe is true, than must the key contains"
1461: + " the delimiter: "
1462: + DELIMITER
1463: + " - "
1464: + lvKeyStr
1465: + " ("
1466: + lvKeyArray.length
1467: + ")");
1468: }
1469:
1470: Object lvValue = pvMap.get(lvKeyStr);
1471:
1472: String lvPos = lvKeyArray[0];
1473: String lvKeyValue = lvKeyArray[1];
1474: Object lvKey = null;
1475: if (lvKeyArray.length == 3) {
1476: String lvKeyClass = lvKeyArray[2];
1477: lvKey = Converter.convertString2Value(
1478: lvKeyValue, lvKeyClass);
1479: } else {
1480: lvKey = lvKeyValue;
1481: }
1482:
1483: SimpleKeyMapper skm = new SimpleKeyMapper(lvPos,
1484: lvKey);
1485: lvTreeMap.put(skm, lvValue);
1486: }
1487: return lvTreeMap;
1488: }
1489: }
1490:
1491: public int compare(Object pvO1, Object pvO2) {
1492: SimpleKeyMapper skm1 = (SimpleKeyMapper) pvO1;
1493: SimpleKeyMapper skm2 = (SimpleKeyMapper) pvO2;
1494: if (skm1.getPos() > skm2.getPos()) {
1495: return 1;
1496: } else {
1497: return -1;
1498: }
1499: }
1500: }
1501: }
|