1 15 package org.apache.tapestry.util.io; 16 17 import java.io.IOException ; 18 19 import org.apache.hivemind.ClassResolver; 20 import org.apache.hivemind.lib.util.StrategyRegistry; 21 import org.apache.hivemind.lib.util.StrategyRegistryImpl; 22 import org.apache.tapestry.Tapestry; 23 import org.apache.tapestry.services.DataSqueezer; 24 25 31 32 public class DataSqueezerImpl implements DataSqueezer 33 { 34 private static final String NULL_PREFIX = "X"; 35 36 private static final char NULL_PREFIX_CH = 'X'; 37 38 private static final int ARRAY_SIZE = 90; 39 40 private static final int FIRST_ADAPTOR_OFFSET = 33; 41 42 47 48 private ISqueezeAdaptor[] _adaptorByPrefix = new ISqueezeAdaptor[ARRAY_SIZE]; 49 50 53 54 private StrategyRegistry _adaptors = new StrategyRegistryImpl(); 55 56 59 60 private ClassResolver _resolver; 61 62 65 66 public DataSqueezerImpl(ClassResolver resolver) 67 { 68 this(resolver, null); 69 } 70 71 79 80 public DataSqueezerImpl(ClassResolver resolver, ISqueezeAdaptor[] adaptors) 81 { 82 _resolver = resolver; 83 84 registerDefaultAdaptors(); 85 86 if (adaptors != null) 87 for (int i = 0; i < adaptors.length; i++) 88 adaptors[i].register(this); 89 } 90 91 private void registerDefaultAdaptors() 92 { 93 new CharacterAdaptor().register(this); 94 new StringAdaptor().register(this); 95 new IntegerAdaptor().register(this); 96 new DoubleAdaptor().register(this); 97 new ByteAdaptor().register(this); 98 new FloatAdaptor().register(this); 99 new LongAdaptor().register(this); 100 new ShortAdaptor().register(this); 101 new BooleanAdaptor().register(this); 102 new SerializableAdaptor().register(this); 103 new ComponentAddressAdaptor().register(this); 104 } 105 106 116 117 public synchronized void register(String prefix, Class dataClass, ISqueezeAdaptor adaptor) 118 { 119 int prefixLength = prefix.length(); 120 int offset; 121 122 if (prefixLength < 1) 123 throw new IllegalArgumentException (Tapestry.getMessage("DataSqueezer.short-prefix")); 124 125 if (dataClass == null) 126 throw new IllegalArgumentException (Tapestry.getMessage("DataSqueezer.null-class")); 127 128 if (adaptor == null) 129 throw new IllegalArgumentException (Tapestry.getMessage("DataSqueezer.null-adaptor")); 130 131 for (int i = 0; i < prefixLength; i++) 132 { 133 char ch = prefix.charAt(i); 134 135 if (ch < '!' | ch > 'z') 136 throw new IllegalArgumentException (Tapestry 137 .getMessage("DataSqueezer.prefix-out-of-range")); 138 139 offset = ch - FIRST_ADAPTOR_OFFSET; 140 141 if (_adaptorByPrefix[offset] != null) 142 throw new IllegalArgumentException (Tapestry.format( 143 "DataSqueezer.adaptor-prefix-taken", 144 prefix.substring(i, i))); 145 146 _adaptorByPrefix[offset] = adaptor; 147 148 } 149 150 _adaptors.register(dataClass, adaptor); 151 } 152 153 157 158 public String squeeze(Object data) throws IOException 159 { 160 ISqueezeAdaptor adaptor; 161 162 if (data == null) 163 return NULL_PREFIX; 164 165 adaptor = (ISqueezeAdaptor) _adaptors.getStrategy(data.getClass()); 166 167 return adaptor.squeeze(this, data); 168 } 169 170 174 175 public String [] squeeze(Object [] data) throws IOException 176 { 177 if (data == null) 178 return null; 179 180 int length = data.length; 181 String [] result; 182 183 result = new String [length]; 184 185 for (int i = 0; i < length; i++) 186 result[i] = squeeze(data[i]); 187 188 return result; 189 } 190 191 196 197 public Object unsqueeze(String string) throws IOException 198 { 199 ISqueezeAdaptor adaptor = null; 200 201 if (string.equals(NULL_PREFIX)) 202 return null; 203 204 int offset = string.charAt(0) - FIRST_ADAPTOR_OFFSET; 205 206 if (offset >= 0 && offset < _adaptorByPrefix.length) 207 adaptor = _adaptorByPrefix[offset]; 208 209 213 if (adaptor == null) 214 return string; 215 216 219 return adaptor.unsqueeze(this, string); 220 } 221 222 227 228 public Object [] unsqueeze(String [] strings) throws IOException 229 { 230 if (strings == null) 231 return null; 232 233 int length = strings.length; 234 Object [] result; 235 236 result = new Object [length]; 237 238 for (int i = 0; i < length; i++) 239 result[i] = unsqueeze(strings[i]); 240 241 return result; 242 } 243 244 248 249 public boolean isPrefixRegistered(char prefix) 250 { 251 int offset = prefix - FIRST_ADAPTOR_OFFSET; 252 253 255 if (prefix == NULL_PREFIX_CH) 256 return true; 257 258 if (offset < 0 || offset >= _adaptorByPrefix.length) 259 return false; 260 261 return _adaptorByPrefix[offset] != null; 262 } 263 264 public String toString() 265 { 266 StringBuffer buffer; 267 268 buffer = new StringBuffer (); 269 buffer.append("DataSqueezer[adaptors=<"); 270 buffer.append(_adaptors.toString()); 271 buffer.append(">]"); 272 273 return buffer.toString(); 274 } 275 276 281 282 public ClassResolver getResolver() 283 { 284 return _resolver; 285 } 286 287 } | Popular Tags |