| 1 |
|
package org.trails.io; |
| 2 |
|
|
| 3 |
|
import org.apache.commons.logging.Log; |
| 4 |
|
import org.apache.commons.logging.LogFactory; |
| 5 |
|
import org.apache.tapestry.services.DataSqueezer; |
| 6 |
|
import org.apache.tapestry.util.io.SqueezeAdaptor; |
| 7 |
|
import org.trails.util.Utils; |
| 8 |
|
import org.trails.descriptor.DescriptorService; |
| 9 |
|
import org.trails.descriptor.IClassDescriptor; |
| 10 |
|
import org.trails.engine.encoders.abbreviator.EntityNameAbbreviator; |
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
36 |
public class ClassDescriptorSqueezerStrategy implements SqueezeAdaptor |
| 17 |
|
{ |
| 18 |
|
|
| 19 |
4 |
private static final Log LOG = LogFactory.getLog(ClassDescriptorSqueezerStrategy.class); |
| 20 |
|
|
| 21 |
|
public static final String PREFIX = "Y"; |
| 22 |
|
private DescriptorService descriptorService; |
| 23 |
|
private EntityNameAbbreviator entityNameAbbreviator; |
| 24 |
16 |
private boolean shouldAbbreviate = false; |
| 25 |
|
|
| 26 |
|
public Class getDataClass() |
| 27 |
|
{ |
| 28 |
0 |
return IClassDescriptor.class; |
| 29 |
|
} |
| 30 |
|
|
| 31 |
|
public String getPrefix() |
| 32 |
|
{ |
| 33 |
0 |
return PREFIX; |
| 34 |
|
} |
| 35 |
|
|
| 36 |
|
public void setDescriptorService(DescriptorService descriptorService) |
| 37 |
|
{ |
| 38 |
8 |
this.descriptorService = descriptorService; |
| 39 |
8 |
} |
| 40 |
|
|
| 41 |
|
public String squeeze(DataSqueezer squeezer, Object object) |
| 42 |
|
{ |
| 43 |
4 |
IClassDescriptor classDescriptor = (IClassDescriptor) object; |
| 44 |
4 |
final String squeezed = abbreviate(classDescriptor.getType()); |
| 45 |
|
|
| 46 |
4 |
if (LOG.isDebugEnabled()) |
| 47 |
|
{ |
| 48 |
0 |
LOG.debug("squeezing descriptor for: " + squeezed); |
| 49 |
|
} |
| 50 |
|
|
| 51 |
4 |
return PREFIX + squeezed; |
| 52 |
|
} |
| 53 |
|
|
| 54 |
|
public Object unsqueeze(DataSqueezer squeezer, String string) |
| 55 |
|
{ |
| 56 |
4 |
if (LOG.isDebugEnabled()) |
| 57 |
|
{ |
| 58 |
0 |
LOG.debug("unsqueezing: " + string); |
| 59 |
|
} |
| 60 |
|
|
| 61 |
4 |
final String squeezed = string.substring(PREFIX.length()); |
| 62 |
|
|
| 63 |
4 |
return descriptorService.getClassDescriptor(unabbreviate(squeezed)); |
| 64 |
|
} |
| 65 |
|
|
| 66 |
|
public void setEntityNameAbbreviator(EntityNameAbbreviator entityNameAbbreviator) |
| 67 |
|
{ |
| 68 |
0 |
this.entityNameAbbreviator = entityNameAbbreviator; |
| 69 |
0 |
shouldAbbreviate = entityNameAbbreviator != null; |
| 70 |
0 |
} |
| 71 |
|
|
| 72 |
|
private String abbreviate(Class clazz) |
| 73 |
|
{ |
| 74 |
4 |
return shouldAbbreviate ? entityNameAbbreviator.abbreviate(clazz) : clazz.getName(); |
| 75 |
|
} |
| 76 |
|
|
| 77 |
|
private Class unabbreviate(String abbreviation) |
| 78 |
|
{ |
| 79 |
4 |
return shouldAbbreviate ? entityNameAbbreviator.unabbreviate(abbreviation) : Utils.classForName(abbreviation); |
| 80 |
|
} |
| 81 |
|
} |