001 package org.trails.io;
002
003 import org.apache.commons.logging.Log;
004 import org.apache.commons.logging.LogFactory;
005 import org.apache.tapestry.services.DataSqueezer;
006 import org.apache.tapestry.util.io.SqueezeAdaptor;
007 import org.trails.exception.TrailsRuntimeException;
008 import org.trails.util.Utils;
009
010 /**
011 * Squeezes a {@link Class}
012 */
013 public class ClassAdaptor implements SqueezeAdaptor
014 {
015
016 private static final Log LOG = LogFactory.getLog(ClassAdaptor.class);
017
018 public static final String PREFIX = "D";
019
020 public Class getDataClass()
021 {
022 return Class.class;
023 }
024
025 public String getPrefix()
026 {
027 return PREFIX;
028 }
029
030 public String squeeze(DataSqueezer squeezer, Object object)
031 {
032 if (LOG.isDebugEnabled())
033 {
034 LOG.debug("squeezing: " + object.toString());
035 }
036
037
038 return PREFIX + ((Class) object).getName();
039 }
040
041 public Object unsqueeze(DataSqueezer squeezer, String string)
042 {
043 if (LOG.isDebugEnabled())
044 {
045 LOG.debug("unsqueezing: " + string);
046 }
047
048 final String className = string.substring(PREFIX.length());
049
050 try
051 {
052 return Utils.classForName(className);
053
054 } catch (TrailsRuntimeException cnfe)
055 {
056 throw new TrailsRuntimeException("decode-failure", cnfe);
057 }
058 }
059 }