KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > aspects > persistence > ValueConverter


1 /*
2   Copyright (C) 2002 Laurent Martelli <laurent@aopsys.com>,
3   Julien van Malderen <julien@aopsys.com>
4
5   This program is free software; you can redistribute it and/or modify
6   it under the terms of the GNU Lesser General Public License as
7   published by the Free Software Foundation; either version 2 of the
8   License, or (at your option) any later version.
9
10   This program is distributed in the hope that it will be useful, but
11   WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13   Lesser General Public License for more details.
14
15   You should have received a copy of the GNU Lesser General Public
16   License along with this program; if not, write to the Free Software
17   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18   USA */

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 /**
29  * General converter from and to String for all Objects.
30  *
31  * <p>It converts Objects into Strings to store them for persistence,
32  * and converts stored Strings into their original form for new
33  * use. OIDs are converted to <localId>@<storage_id></p>
34  */

35 public class ValueConverter
36 {
37     static Logger logger = Logger.getLogger("persistence.converter");
38
39     /**
40      * Returns a string representation of a value so that it can be
41      * stored.<p>
42      *
43      * @param currentStorage the current storage. OID values from this
44      * storage will be converted to a string which does not contain
45      * the storage id.
46      * @param obj a persistent or primitive object
47      * @return a ready to store string representation
48      *
49      * @see #stringToObject(Storage,String)
50      */

51     static public String JavaDoc objectToString(Storage currentStorage, Object JavaDoc obj)
52     {
53         if (obj == null)
54             return "null";
55         Class JavaDoc lClass = obj.getClass();
56         String JavaDoc className = lClass.getName();
57         String JavaDoc 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 JavaDoc.class) {
70                 value = (String JavaDoc)obj;
71             } else if (converter != null) {
72                 value = converter.objectToString(obj);
73             } else if (lClass.isArray() &&
74                        (lClass.getComponentType()==byte.class)) {
75                 // array of bytes
76
value = Base64.encodeToString((byte[])obj);
77             } else {
78                 value = obj.toString();
79             }
80             value = className+":"+value;
81         }
82         return value;
83     }
84
85     /**
86      * Returns an object from a string, depending on the needed type.<p>
87      *
88      * @param currentStorage the current storage. OID values with no
89      * storage id will be attributed to this storage.
90      * @param str the type and value in a string format (type:value)
91      * @return an object value deduced from the string representation
92      * and from the needed type
93      *
94      * @see #objectToString(Storage,Object)
95      */

96     static public Object JavaDoc stringToObject(Storage currentStorage, String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc(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 JavaDoc(str);
143             } else if (type.equals("long") || type.equals("java.lang.Long")) {
144                 return new Long JavaDoc(str);
145             } else if (type.equals("float") || type.equals("java.lang.Float")) {
146                 return new Float JavaDoc(str);
147             } else if (type.equals("double") || type.equals("java.lang.Double")) {
148                 return new Double JavaDoc(str);
149             } else if (type.equals("byte") || type.equals("java.lang.Byte")) {
150                 return new Byte JavaDoc(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 JavaDoc("Unhandled type for value `"+str+"' : "+type);
157             }
158         }
159     }
160
161 }
162
Popular Tags