1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
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 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
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 |
| |
43 |
| |
44 |
| |
45 |
| |
46 |
| |
47 |
| |
48 |
| private ISqueezeAdaptor[] _adaptorByPrefix = new ISqueezeAdaptor[ARRAY_SIZE]; |
49 |
| |
50 |
| |
51 |
| |
52 |
| |
53 |
| |
54 |
| private StrategyRegistry _adaptors = new StrategyRegistryImpl(); |
55 |
| |
56 |
| |
57 |
| |
58 |
| |
59 |
| |
60 |
| private ClassResolver _resolver; |
61 |
| |
62 |
| |
63 |
| |
64 |
| |
65 |
| |
66 |
36
| public DataSqueezerImpl(ClassResolver resolver)
|
67 |
| { |
68 |
36
| this(resolver, null);
|
69 |
| } |
70 |
| |
71 |
| |
72 |
| |
73 |
| |
74 |
| |
75 |
| |
76 |
| |
77 |
| |
78 |
| |
79 |
| |
80 |
37
| public DataSqueezerImpl(ClassResolver resolver, ISqueezeAdaptor[] adaptors)
|
81 |
| { |
82 |
37
| _resolver = resolver;
|
83 |
| |
84 |
37
| registerDefaultAdaptors();
|
85 |
| |
86 |
37
| if (adaptors != null)
|
87 |
1
| for (int i = 0; i < adaptors.length; i++)
|
88 |
1
| adaptors[i].register(this);
|
89 |
| } |
90 |
| |
91 |
37
| private void registerDefaultAdaptors()
|
92 |
| { |
93 |
37
| new CharacterAdaptor().register(this);
|
94 |
37
| new StringAdaptor().register(this);
|
95 |
37
| new IntegerAdaptor().register(this);
|
96 |
37
| new DoubleAdaptor().register(this);
|
97 |
37
| new ByteAdaptor().register(this);
|
98 |
37
| new FloatAdaptor().register(this);
|
99 |
37
| new LongAdaptor().register(this);
|
100 |
37
| new ShortAdaptor().register(this);
|
101 |
37
| new BooleanAdaptor().register(this);
|
102 |
37
| new SerializableAdaptor().register(this);
|
103 |
37
| new ComponentAddressAdaptor().register(this);
|
104 |
| } |
105 |
| |
106 |
| |
107 |
| |
108 |
| |
109 |
| |
110 |
| |
111 |
| |
112 |
| |
113 |
| |
114 |
| |
115 |
| |
116 |
| |
117 |
413
| public synchronized void register(String prefix, Class dataClass, ISqueezeAdaptor adaptor)
|
118 |
| { |
119 |
413
| int prefixLength = prefix.length();
|
120 |
413
| int offset;
|
121 |
| |
122 |
413
| if (prefixLength < 1)
|
123 |
1
| throw new IllegalArgumentException(Tapestry.getMessage("DataSqueezer.short-prefix"));
|
124 |
| |
125 |
412
| if (dataClass == null)
|
126 |
1
| throw new IllegalArgumentException(Tapestry.getMessage("DataSqueezer.null-class"));
|
127 |
| |
128 |
411
| if (adaptor == null)
|
129 |
1
| throw new IllegalArgumentException(Tapestry.getMessage("DataSqueezer.null-adaptor"));
|
130 |
| |
131 |
410
| for (int i = 0; i < prefixLength; i++)
|
132 |
| { |
133 |
817
| char ch = prefix.charAt(i);
|
134 |
| |
135 |
817
| if (ch < '!' | ch > 'z')
|
136 |
1
| throw new IllegalArgumentException(Tapestry
|
137 |
| .getMessage("DataSqueezer.prefix-out-of-range")); |
138 |
| |
139 |
816
| offset = ch - FIRST_ADAPTOR_OFFSET;
|
140 |
| |
141 |
816
| if (_adaptorByPrefix[offset] != null)
|
142 |
1
| throw new IllegalArgumentException(Tapestry.format(
|
143 |
| "DataSqueezer.adaptor-prefix-taken", |
144 |
| prefix.substring(i, i))); |
145 |
| |
146 |
815
| _adaptorByPrefix[offset] = adaptor;
|
147 |
| |
148 |
| } |
149 |
| |
150 |
408
| _adaptors.register(dataClass, adaptor);
|
151 |
| } |
152 |
| |
153 |
| |
154 |
| |
155 |
| |
156 |
| |
157 |
| |
158 |
84
| public String squeeze(Object data) throws IOException
|
159 |
| { |
160 |
84
| ISqueezeAdaptor adaptor;
|
161 |
| |
162 |
84
| if (data == null)
|
163 |
2
| return NULL_PREFIX;
|
164 |
| |
165 |
82
| adaptor = (ISqueezeAdaptor) _adaptors.getStrategy(data.getClass());
|
166 |
| |
167 |
82
| return adaptor.squeeze(this, data);
|
168 |
| } |
169 |
| |
170 |
| |
171 |
| |
172 |
| |
173 |
| |
174 |
| |
175 |
14
| public String[] squeeze(Object[] data) throws IOException
|
176 |
| { |
177 |
14
| if (data == null)
|
178 |
1
| return null;
|
179 |
| |
180 |
13
| int length = data.length;
|
181 |
13
| String[] result;
|
182 |
| |
183 |
13
| result = new String[length];
|
184 |
| |
185 |
13
| for (int i = 0; i < length; i++)
|
186 |
34
| result[i] = squeeze(data[i]);
|
187 |
| |
188 |
13
| return result;
|
189 |
| } |
190 |
| |
191 |
| |
192 |
| |
193 |
| |
194 |
| |
195 |
| |
196 |
| |
197 |
82
| public Object unsqueeze(String string) throws IOException
|
198 |
| { |
199 |
82
| ISqueezeAdaptor adaptor = null;
|
200 |
| |
201 |
82
| if (string.equals(NULL_PREFIX))
|
202 |
2
| return null;
|
203 |
| |
204 |
80
| int offset = string.charAt(0) - FIRST_ADAPTOR_OFFSET;
|
205 |
| |
206 |
80
| if (offset >= 0 && offset < _adaptorByPrefix.length)
|
207 |
80
| adaptor = _adaptorByPrefix[offset];
|
208 |
| |
209 |
| |
210 |
| |
211 |
| |
212 |
| |
213 |
80
| if (adaptor == null)
|
214 |
2
| return string;
|
215 |
| |
216 |
| |
217 |
| |
218 |
| |
219 |
78
| return adaptor.unsqueeze(this, string);
|
220 |
| } |
221 |
| |
222 |
| |
223 |
| |
224 |
| |
225 |
| |
226 |
| |
227 |
| |
228 |
21
| public Object[] unsqueeze(String[] strings) throws IOException
|
229 |
| { |
230 |
21
| if (strings == null)
|
231 |
1
| return null;
|
232 |
| |
233 |
20
| int length = strings.length;
|
234 |
20
| Object[] result;
|
235 |
| |
236 |
20
| result = new Object[length];
|
237 |
| |
238 |
20
| for (int i = 0; i < length; i++)
|
239 |
42
| result[i] = unsqueeze(strings[i]);
|
240 |
| |
241 |
20
| return result;
|
242 |
| } |
243 |
| |
244 |
| |
245 |
| |
246 |
| |
247 |
| |
248 |
| |
249 |
0
| public boolean isPrefixRegistered(char prefix)
|
250 |
| { |
251 |
0
| int offset = prefix - FIRST_ADAPTOR_OFFSET;
|
252 |
| |
253 |
| |
254 |
| |
255 |
0
| if (prefix == NULL_PREFIX_CH)
|
256 |
0
| return true;
|
257 |
| |
258 |
0
| if (offset < 0 || offset >= _adaptorByPrefix.length)
|
259 |
0
| return false;
|
260 |
| |
261 |
0
| return _adaptorByPrefix[offset] != null;
|
262 |
| } |
263 |
| |
264 |
0
| public String toString()
|
265 |
| { |
266 |
0
| StringBuffer buffer;
|
267 |
| |
268 |
0
| buffer = new StringBuffer();
|
269 |
0
| buffer.append("DataSqueezer[adaptors=<");
|
270 |
0
| buffer.append(_adaptors.toString());
|
271 |
0
| buffer.append(">]");
|
272 |
| |
273 |
0
| return buffer.toString();
|
274 |
| } |
275 |
| |
276 |
| |
277 |
| |
278 |
| |
279 |
| |
280 |
| |
281 |
| |
282 |
1
| public ClassResolver getResolver()
|
283 |
| { |
284 |
1
| return _resolver;
|
285 |
| } |
286 |
| |
287 |
| } |