| 1 | | | |
| 2 | | | |
| 3 | | | |
| 4 | | | |
| 5 | | | |
| 6 | | | |
| 7 | | | |
| 8 | | | |
| 9 | | | |
| 10 | | | |
| 11 | | | |
| 12 | | | |
| 13 | | | |
| 14 | | | |
| 15 | | | |
| 16 | | | |
| 17 | | | |
| 18 | | | |
| 19 | | | |
| 20 | | | |
| 21 | | | |
| 22 | | | |
| 23 | | | |
| 24 | | | |
| 25 | | | |
| 26 | | | |
| 27 | | | |
| 28 | | | |
| 29 | | | |
| 30 | | | |
| 31 | | | |
| 32 | | | |
| 33 | | | package org.jcoderz.commons.logging; |
| 34 | | | |
| 35 | | | import java.nio.CharBuffer; |
| 36 | | | import java.text.FieldPosition; |
| 37 | | | import java.text.Format; |
| 38 | | | import java.text.ParsePosition; |
| 39 | | | import java.util.ArrayList; |
| 40 | | | import java.util.Collection; |
| 41 | | | import java.util.Iterator; |
| 42 | | | import java.util.List; |
| 43 | | | |
| 44 | | | |
| 45 | | | |
| 46 | | | |
| 47 | | | |
| 48 | | | |
| 49 | | | |
| 50 | | (1) | public class CollectionFormat |
| 51 | | | extends Format |
| 52 | | | { |
| 53 | | | private static final long serialVersionUID = 3258126947170400564L; |
| 54 | | | |
| 55 | | | |
| 56 | | | private static final String SEPARATOR = ","; |
| 57 | | | |
| 58 | | | private static final String LIST_BEGIN = "["; |
| 59 | | | private static final String LIST_END = "]"; |
| 60 | | | |
| 61 | | | private final String mListBegin; |
| 62 | | | private final String mListEnd; |
| 63 | | | private final Format mListElementFormat; |
| 64 | | | private final String mSeparator; |
| 65 | | | |
| 66 | | | private final int mLengthListBegin; |
| 67 | | | private final int mLengthListEnd; |
| 68 | | | private final int mLengthSeparator; |
| 69 | | | |
| 70 | | | |
| 71 | | | |
| 72 | | | |
| 73 | | | |
| 74 | | | |
| 75 | | | |
| 76 | | | @param |
| 77 | | | |
| 78 | | | |
| 79 | | | public CollectionFormat (final Format elementFormat) |
| 80 | | | { |
| 81 | 100 | | this(elementFormat, LIST_BEGIN, LIST_END, SEPARATOR); |
| 82 | 100 | | } |
| 83 | | | |
| 84 | | | |
| 85 | | | |
| 86 | | | |
| 87 | | | |
| 88 | | | |
| 89 | | | |
| 90 | | | @param |
| 91 | | | |
| 92 | | | @param |
| 93 | | | |
| 94 | | | @param |
| 95 | | | |
| 96 | | | @param |
| 97 | | | |
| 98 | | | |
| 99 | | | |
| 100 | | | public CollectionFormat ( |
| 101 | | | final Format elementFormat, |
| 102 | | | final String begin, |
| 103 | | | final String end, |
| 104 | | | final String separator) |
| 105 | | | { |
| 106 | 100 | | super(); |
| 107 | 100 | | mListBegin = (begin != null) ? begin : ""; |
| 108 | 100 | | mListEnd = (end != null) ? end : ""; |
| 109 | 100 | | mListElementFormat = elementFormat; |
| 110 | 75 | | mSeparator = (separator != null) ? separator : ""; |
| 111 | 100 | | mLengthListBegin = mListBegin.length(); |
| 112 | 100 | | mLengthListEnd = mListEnd.length(); |
| 113 | 100 | | mLengthSeparator = mSeparator.length(); |
| 114 | 100 | | } |
| 115 | | | |
| 116 | | | |
| 117 | | | @see |
| 118 | | | |
| 119 | | | |
| 120 | | | |
| 121 | | | |
| 122 | | | |
| 123 | | | |
| 124 | | | |
| 125 | | | |
| 126 | | | |
| 127 | | | |
| 128 | | | @param |
| 129 | | | @param <code></code> |
| 130 | | | |
| 131 | | | @return |
| 132 | | | |
| 133 | | | |
| 134 | | | public Object parseObject (String source, ParsePosition pos) |
| 135 | | | { |
| 136 | 100 | | List rc = null; |
| 137 | 100 | (2) | final int len = source.length(); |
| 138 | 100 | (3) | boolean errorOccurred = false; |
| 139 | | | |
| 140 | 100 | | final CharBuffer sourceBuffer |
| 141 | | | = CharBuffer.wrap(source, pos.getIndex(), source.length()); |
| 142 | | | |
| 143 | 100 | (4) | final int currentPos = pos.getIndex(); |
| 144 | | | |
| 145 | | | |
| 146 | | | |
| 147 | 100 | | if (beginOfList(sourceBuffer, pos)) |
| 148 | | | { |
| 149 | 100 | | rc = new ArrayList(); |
| 150 | | | |
| 151 | 100 | | if (! (mLengthListEnd != 0 && endOfList(sourceBuffer, pos))) |
| 152 | | | { |
| 153 | 100 | | errorOccurred = parseListElements( |
| 154 | | | sourceBuffer, source, pos, len, rc); |
| 155 | | | |
| 156 | 100 | | if (! (errorOccurred || endOfList(sourceBuffer, pos))) |
| 157 | | | { |
| 158 | 0 | | pos.setErrorIndex(pos.getIndex()); |
| 159 | 0 | | errorOccurred = true; |
| 160 | | | } |
| 161 | | | } |
| 162 | 100 | | if (errorOccurred) |
| 163 | | | { |
| 164 | 0 | | rc.clear(); |
| 165 | 0 | | rc = null; |
| 166 | 0 | | pos.setIndex(currentPos); |
| 167 | | | } |
| 168 | | | } |
| 169 | | | else |
| 170 | | | { |
| 171 | 0 | | pos.setErrorIndex(pos.getIndex()); |
| 172 | | | } |
| 173 | 100 | | return rc; |
| 174 | | | } |
| 175 | | | |
| 176 | | | private boolean parseListElements ( |
| 177 | | | final CharBuffer sourceBuffer, |
| 178 | | | final String source, |
| 179 | | | final ParsePosition pos, |
| 180 | | | final int len, |
| 181 | | | final List elements) |
| 182 | | | { |
| 183 | 100 | | boolean rc = false; |
| 184 | | | |
| 185 | | | |
| 186 | | | do |
| 187 | | | { |
| 188 | 100 | | final Object obj = mListElementFormat.parseObject(source, pos); |
| 189 | 100 | | if (obj == null) |
| 190 | | | { |
| 191 | 0 | | rc = true; |
| 192 | | | } |
| 193 | | | else |
| 194 | | | { |
| 195 | 100 | | elements.add(obj); |
| 196 | | | } |
| 197 | | | } |
| 198 | | | while (separator(sourceBuffer, pos) |
| 199 | | | && (pos.getIndex() < len) |
| 200 | 100 | | && ! rc); |
| 201 | | | |
| 202 | 100 | | return rc; |
| 203 | | | } |
| 204 | | | |
| 205 | | | |
| 206 | | | |
| 207 | | | |
| 208 | | | @see |
| 209 | | | |
| 210 | | | @param |
| 211 | | | @param |
| 212 | | | |
| 213 | | | @param |
| 214 | | | |
| 215 | | | @return |
| 216 | | | |
| 217 | | | @throws <code></code> |
| 218 | | | |
| 219 | | | public StringBuffer format ( |
| 220 | | | Object obj, |
| 221 | | | StringBuffer toAppendTo, |
| 222 | | | FieldPosition pos) |
| 223 | | | { |
| 224 | 100 | | if (! (obj instanceof Collection)) |
| 225 | | | { |
| 226 | 0 | | throw new IllegalArgumentException("The object to format must be " |
| 227 | | | + " a java.util.Collection, but is: " + obj); |
| 228 | | | } |
| 229 | 100 | | pos.setBeginIndex(0); |
| 230 | 100 | | pos.setEndIndex(0); |
| 231 | | | |
| 232 | 100 | | final Collection elements = (Collection) obj; |
| 233 | 100 | | final StringBuffer tempBuffer = new StringBuffer(mListBegin); |
| 234 | 100 | | boolean first = true; |
| 235 | 100 | | for (final Iterator iter = elements.iterator(); iter.hasNext(); ) |
| 236 | | | { |
| 237 | 100 | | if (first) |
| 238 | | | { |
| 239 | 100 | | first = false; |
| 240 | | | } |
| 241 | | | else |
| 242 | | | { |
| 243 | 100 | | tempBuffer.append(mSeparator); |
| 244 | | | } |
| 245 | 100 | | mListElementFormat.format(iter.next(), tempBuffer, pos); |
| 246 | | | } |
| 247 | 100 | | tempBuffer.append(mListEnd); |
| 248 | 100 | | toAppendTo.append(tempBuffer); |
| 249 | | | |
| 250 | 100 | | return toAppendTo; |
| 251 | | | } |
| 252 | | | |
| 253 | | | |
| 254 | | | |
| 255 | | | |
| 256 | | | |
| 257 | | | |
| 258 | | | @param |
| 259 | | | @param |
| 260 | | | |
| 261 | | | @return |
| 262 | | | |
| 263 | | | |
| 264 | | | private boolean beginOfList ( |
| 265 | | | final CharBuffer buffer, |
| 266 | | | final ParsePosition pos) |
| 267 | | | { |
| 268 | 100 | | boolean rc = false; |
| 269 | 100 | | buffer.position(pos.getIndex()); |
| 270 | 100 | | if (mLengthListBegin == 0) |
| 271 | | | { |
| 272 | 100 | | rc = true; |
| 273 | | | } |
| 274 | | | else |
| 275 | | | { |
| 276 | 100 | | if (buffer.remaining() >= mLengthListBegin) |
| 277 | | | { |
| 278 | 100 | | boolean allOk = true; |
| 279 | 100 | | for (int i = 0; i < mLengthListBegin && allOk; ++i) |
| 280 | | | { |
| 281 | 100 | | if (buffer.charAt(i) != mListBegin.charAt(i)) |
| 282 | | | { |
| 283 | 0 | | allOk = false; |
| 284 | | | } |
| 285 | | | } |
| 286 | 100 | | rc = allOk; |
| 287 | 100 | | if (rc) |
| 288 | | | { |
| 289 | 100 | | pos.setIndex(pos.getIndex() + mLengthListBegin); |
| 290 | 100 | | buffer.position(pos.getIndex()); |
| 291 | | | } |
| 292 | | | } |
| 293 | | | } |
| 294 | 100 | | return rc; |
| 295 | | | } |
| 296 | | | |
| 297 | | | |
| 298 | | | |
| 299 | | | |
| 300 | | | |
| 301 | | | |
| 302 | | | @param |
| 303 | | | @param |
| 304 | | | |
| 305 | | | @return |
| 306 | | | |
| 307 | | | |
| 308 | | | private boolean endOfList ( |
| 309 | | | final CharBuffer buffer, |
| 310 | | | final ParsePosition pos) |
| 311 | | | { |
| 312 | 100 | | boolean rc = false; |
| 313 | 100 | | buffer.position(pos.getIndex()); |
| 314 | 100 | | if (mLengthListEnd == 0) |
| 315 | | | { |
| 316 | 100 | | rc = true; |
| 317 | | | } |
| 318 | | | else |
| 319 | | | { |
| 320 | 100 | | if (buffer.remaining() >= mLengthListEnd) |
| 321 | | | { |
| 322 | 100 | | boolean allOk = true; |
| 323 | 100 | | for (int i = 0; i < mLengthListEnd && allOk; ++i) |
| 324 | | | { |
| 325 | 100 | | if (buffer.charAt(i) != mListEnd.charAt(i)) |
| 326 | | | { |
| 327 | 100 | | allOk = false; |
| 328 | | | } |
| 329 | | | } |
| 330 | 100 | | rc = allOk; |
| 331 | 100 | | if (rc) |
| 332 | | | { |
| 333 | 100 | | pos.setIndex(pos.getIndex() + mLengthListEnd); |
| 334 | 100 | | buffer.position(pos.getIndex()); |
| 335 | | | } |
| 336 | | | } |
| 337 | | | } |
| 338 | 100 | | return rc; |
| 339 | | | } |
| 340 | | | |
| 341 | | | |
| 342 | | | |
| 343 | | | |
| 344 | | | |
| 345 | | | |
| 346 | | | @param |
| 347 | | | @param |
| 348 | | | |
| 349 | | | @return |
| 350 | | | |
| 351 | | | private boolean separator (final CharBuffer buffer, final ParsePosition pos) |
| 352 | | | { |
| 353 | 100 | | boolean rc = false; |
| 354 | 100 | (5) | boolean allOk = true; |
| 355 | | | |
| 356 | 100 | | buffer.position(pos.getIndex()); |
| 357 | | | |
| 358 | 100 | | if (buffer.remaining() >= mLengthSeparator) |
| 359 | | | { |
| 360 | 100 | | for (int i = 0; i < mLengthSeparator && allOk; ++i) |
| 361 | | | { |
| 362 | 100 | | if (buffer.charAt(i) != mSeparator.charAt(i)) |
| 363 | | | { |
| 364 | 100 | | allOk = false; |
| 365 | | | } |
| 366 | | | } |
| 367 | 100 | | rc = allOk; |
| 368 | 100 | | if (rc) |
| 369 | | | { |
| 370 | 100 | | pos.setIndex(pos.getIndex() + mLengthSeparator); |
| 371 | 100 | | buffer.position(pos.getIndex()); |
| 372 | | | } |
| 373 | | | } |
| 374 | 100 | | return rc; |
| 375 | | | } |
| 376 | | | } |