Hi!
I've been looking for SqueezeAdaptor for Map for some time. I wanted to
override the default bahavior of DataSqueezer which uses
SerializableAdaptor to sqeeze maps (which results in an unnaceptable
long Base64-encoded parameter in url).
As I haven't found one, so I made an attempt and wrote mine.
Maybe some of you would be interested. Propositions of improvements
would be highly appreciated ;-)
regards,
Bernard
/**
* ISqueezaAdaptor for Map. Uses DataSquezer to un/squeeze key and
value taken
* from the map;
*
* @author bernard
*/
public class MapAdaptor
implements ISqueezeAdaptor
{
private static final String PREFIX = "M";
private static final String SEPARATOR = "&";
private static final String CONNECTOR = "=";
public void register(DataSqueezer squeezer)
{
squeezer.register(PREFIX, Map.class, this);
}
public String squeeze(DataSqueezer squeezer, Object data)
throws IOException
{
Map map = (Map)data;
Iterator it = map.keySet().iterator();
StringBuffer result = new StringBuffer(PREFIX);
while (it.hasNext()) {
Object key = it.next();
Map.Entry entry = new MapEntry(key, map.get(key)); // (key,
value)
squeezeEntry(squeezer, result, entry);
if (it.hasNext()) {
result.append(SEPARATOR);
}
}
return result.toString();
}
private void squeezeEntry(DataSqueezer squeezer, StringBuffer sb,
Map.Entry entry)
throws IOException
{
URLCodec codec = new URLCodec();
String key, value;
try {
//encode squeezed values to remove any
connectors/separators that may occur
key = codec.encode(squeezer.squeeze(entry.getKey()));
value = codec.encode(squeezer.squeeze(entry.getValue()));
sb.append(key).append(CONNECTOR).append(value);
}
catch (EncoderException exc) {
IOException ioExc = new IOException(exc.getLocalizedMessage());
ioExc.setStackTrace(exc.getStackTrace());
throw ioExc;
}
}
public Object unsqueeze(DataSqueezer squeezer, String string)
throws IOException
{
string = string.substring(1);
StringTokenizer stk = new StringTokenizer(string, SEPARATOR);
HashMap result = new HashMap();
while (stk.hasMoreTokens()) {
Map.Entry entry = unsqueezeEntry(squeezer, stk.nextToken());
result.put(entry.getKey(), entry.getValue());
}
return result;
}
private Map.Entry unsqueezeEntry(DataSqueezer squeezer, String
keyValuePair)
throws IOException
{
URLCodec codec = new URLCodec();
String[] strings = keyValuePair.split(CONNECTOR);
//decode and then unsqueeze
try {
Object key = squeezer.unsqueeze(codec.decode(strings[0]));
Object value = squeezer.unsqueeze(codec.decode(strings[1]));
return new MapEntry(key, value);
}
catch (DecoderException exc) {
IOException ioExc = new IOException(exc.getLocalizedMessage());
ioExc.setStackTrace(exc.getStackTrace());
throw ioExc;
}
}
private class MapEntry
implements Map.Entry
{
private Object key;
private Object value;
public MapEntry(Object key, Object value)
{
this.key = key;
this.value = value;
}
public Object getKey()
{
return key;
}
public Object getValue()
{
return value;
}
public Object setValue(Object value)
{
Object old = this.value;
this.value = value;
return old;
}
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]