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.util.Utils;
008    import org.trails.descriptor.DescriptorService;
009    import org.trails.descriptor.IClassDescriptor;
010    import org.trails.engine.encoders.abbreviator.EntityNameAbbreviator;
011    
012    
013    /**
014     * Squeezes a {@link IClassDescriptor}
015     */
016    public class ClassDescriptorSqueezerStrategy implements SqueezeAdaptor
017    {
018    
019            private static final Log LOG = LogFactory.getLog(ClassDescriptorSqueezerStrategy.class);
020    
021            public static final String PREFIX = "Y";
022            private DescriptorService descriptorService;
023            private EntityNameAbbreviator entityNameAbbreviator;
024            private boolean shouldAbbreviate = false;
025    
026            public Class getDataClass()
027            {
028                    return IClassDescriptor.class;
029            }
030    
031            public String getPrefix()
032            {
033                    return PREFIX;
034            }
035    
036            public void setDescriptorService(DescriptorService descriptorService)
037            {
038                    this.descriptorService = descriptorService;
039            }
040    
041            public String squeeze(DataSqueezer squeezer, Object object)
042            {
043                    IClassDescriptor classDescriptor = (IClassDescriptor) object;
044                    final String squeezed = abbreviate(classDescriptor.getType());
045    
046                    if (LOG.isDebugEnabled())
047                    {
048                            LOG.debug("squeezing descriptor for: " + squeezed);
049                    }
050    
051                    return PREFIX + squeezed;
052            }
053    
054            public Object unsqueeze(DataSqueezer squeezer, String string)
055            {
056                    if (LOG.isDebugEnabled())
057                    {
058                            LOG.debug("unsqueezing: " + string);
059                    }
060    
061                    final String squeezed = string.substring(PREFIX.length());
062    
063                    return descriptorService.getClassDescriptor(unabbreviate(squeezed));
064            }
065    
066            public void setEntityNameAbbreviator(EntityNameAbbreviator entityNameAbbreviator)
067            {
068                    this.entityNameAbbreviator = entityNameAbbreviator;
069                    shouldAbbreviate = entityNameAbbreviator != null;
070            }
071    
072            private String abbreviate(Class clazz)
073            {
074                    return shouldAbbreviate ? entityNameAbbreviator.abbreviate(clazz) : clazz.getName();
075            }
076    
077            private Class unabbreviate(String abbreviation)
078            {
079                    return shouldAbbreviate ? entityNameAbbreviator.unabbreviate(abbreviation) : Utils.classForName(abbreviation);
080            }
081    }