1 19 20 package org.objectweb.jac.aspects.persistence; 21 22 import org.apache.log4j.Logger; 23 import org.objectweb.jac.core.ACManager; 24 import org.objectweb.jac.core.rtti.ClassRepository; 25 import org.objectweb.jac.util.Base64; 26 import org.objectweb.jac.util.Log; 27 28 35 public class ValueConverter 36 { 37 static Logger logger = Logger.getLogger("persistence.converter"); 38 39 51 static public String objectToString(Storage currentStorage, Object obj) 52 { 53 if (obj == null) 54 return "null"; 55 Class lClass = obj.getClass(); 56 String className = lClass.getName(); 57 String value; 58 59 StringConverter converter = (StringConverter)ClassRepository.get() 60 .getClass(className).getAttribute(PersistenceAC.VALUE_CONVERTER); 61 62 if (obj instanceof OID) { 63 OID oid = (OID)obj; 64 if (oid.getStorage()==currentStorage) 65 value = ((OID)obj).localId(); 66 else 67 value = ((OID)obj).localId()+'@'+oid.getStorage().getId(); 68 } else { 69 if (lClass == java.lang.String .class) { 70 value = (String )obj; 71 } else if (converter != null) { 72 value = converter.objectToString(obj); 73 } else if (lClass.isArray() && 74 (lClass.getComponentType()==byte.class)) { 75 value = Base64.encodeToString((byte[])obj); 77 } else { 78 value = obj.toString(); 79 } 80 value = className+":"+value; 81 } 82 return value; 83 } 84 85 96 static public Object stringToObject(Storage currentStorage, String str) 97 { 98 logger.debug("stringToObject("+str+")"); 99 if (str.equals("null")) 100 return null; 101 char firstChar = str.charAt(0); 102 if (Character.isDigit(firstChar)) { 103 Storage storage; 104 int index = str.indexOf('@'); 105 if (index==-1) { 106 storage = currentStorage; 107 return new LongOID(storage,Long.parseLong(str)); 108 } else { 109 PersistenceAC ac = (PersistenceAC)ACManager.getACM().getAC("persistence"); 110 String storageName = str.substring(index+1); 111 storage = ac.getStorage(storageName.equals("null")?null:storageName); 112 return new LongOID(storage,Long.parseLong(str.substring(0,index))); 113 } 114 } else { 115 int separator = str.indexOf(":"); 116 String type = str.substring(0,separator); 117 str = str.substring(separator+1); 118 logger.debug("type = "+type+" ; value = "+str); 119 120 StringConverter converter = (StringConverter)ClassRepository.get() 121 .getClass(type).getAttribute(PersistenceAC.VALUE_CONVERTER); 122 123 if (type.equals("java.lang.String")) { 124 return str; 125 } else if (converter != null) { 126 return converter.stringToObject(str); 127 } else if (type.equals("boolean") || 128 type.equals("java.lang.Boolean")) { 129 if (str.equals("true")) 130 return Boolean.TRUE; 131 else if (str.equals("false")) 132 return Boolean.FALSE; 133 else { 134 logger.error("BAD BOOLEAN VALUE "+str); 135 return null; 136 } 137 } else if (type.equals("java.io.File")) { 138 return new java.io.File (str); 139 } else if (type.equals("org.objectweb.jac.util.File")) { 140 return new org.objectweb.jac.util.File(str); 141 } else if (type.equals("int") || type.equals("java.lang.Integer")) { 142 return new Integer (str); 143 } else if (type.equals("long") || type.equals("java.lang.Long")) { 144 return new Long (str); 145 } else if (type.equals("float") || type.equals("java.lang.Float")) { 146 return new Float (str); 147 } else if (type.equals("double") || type.equals("java.lang.Double")) { 148 return new Double (str); 149 } else if (type.equals("byte") || type.equals("java.lang.Byte")) { 150 return new Byte (str); 151 } else if (type.equals("[B")) { 152 return Base64.decode(str); 153 } 154 else { 155 logger.error("Unhandled type for value `"+str+"' : "+type); 156 throw new Error ("Unhandled type for value `"+str+"' : "+type); 157 } 158 } 159 } 160 161 } 162 | Popular Tags |