| 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.text.FieldPosition; |
| 36 | | | import java.text.Format; |
| 37 | | | import java.text.ParsePosition; |
| 38 | | | import java.util.Iterator; |
| 39 | | | import java.nio.CharBuffer; |
| 40 | | | import java.util.NoSuchElementException; |
| 41 | | | |
| 42 | | | |
| 43 | | | |
| 44 | | | |
| 45 | | | <code></code> |
| 46 | | | |
| 47 | | | |
| 48 | | | |
| 49 | | | |
| 50 | | | |
| 51 | | | |
| 52 | | | |
| 53 | | | |
| 54 | | | |
| 55 | | (1)(2) | public final class WhitespaceFormat |
| 56 | | | extends Format |
| 57 | | | { |
| 58 | | | static final char PRESERVED_CHAR = '\u0020'; |
| 59 | | | |
| 60 | | | private final Format mSubFormat; |
| 61 | | | |
| 62 | | | private static final class WhitespaceIterator |
| 63 | | | implements Iterator |
| 64 | | | { |
| 65 | | | private final CharBuffer mBuffer; |
| 66 | | | |
| 67 | | | |
| 68 | | | |
| 69 | | | |
| 70 | | | |
| 71 | | | @param |
| 72 | | | |
| 73 | | | private WhitespaceIterator (final CharBuffer buffer) |
| 74 | 100 | | { |
| 75 | 100 | | mBuffer = buffer.duplicate(); |
| 76 | 100 | | skip(); |
| 77 | 100 | | } |
| 78 | | | |
| 79 | | | {@inheritDoc} |
| 80 | | | public void remove () |
| 81 | | | { |
| 82 | 0 | | throw new UnsupportedOperationException(); |
| 83 | | | } |
| 84 | | | |
| 85 | | | {@inheritDoc} |
| 86 | | | public boolean hasNext () |
| 87 | | | { |
| 88 | 100 | | return mBuffer.remaining() > 0; |
| 89 | | | } |
| 90 | | | |
| 91 | | | |
| 92 | | | |
| 93 | | | <code></code> |
| 94 | | | |
| 95 | | | <code></code> |
| 96 | | | |
| 97 | | | @see |
| 98 | | | |
| 99 | | | public Object next () |
| 100 | | | { |
| 101 | 100 | | if (mBuffer.remaining() <= 0) |
| 102 | | | { |
| 103 | 0 | | throw new NoSuchElementException(); |
| 104 | | | } |
| 105 | 100 | | boolean wsFound = false; |
| 106 | 100 | | boolean postWsFound = false; |
| 107 | | | |
| 108 | 100 | | int startOfWs = 0; |
| 109 | 100 | | int endOfWs = 0; |
| 110 | | | |
| 111 | | (3) | |
| 112 | | | |
| 113 | 100 | | final CharBuffer rc = mBuffer.slice(); |
| 114 | 100 | | for (int i = 0; i < mBuffer.remaining() && ! (wsFound && postWsFound); |
| 115 | 100 | | ++i) |
| 116 | | | { |
| 117 | 100 | | final char c = mBuffer.charAt(i); |
| 118 | | | |
| 119 | 100 | | if (Character.isWhitespace(c) && ! (c == PRESERVED_CHAR)) |
| 120 | | | { |
| 121 | 100 | | if (! wsFound) |
| 122 | | | { |
| 123 | 100 | | startOfWs = i; |
| 124 | | | } |
| 125 | 100 | | endOfWs = i; |
| 126 | 100 | | wsFound = true; |
| 127 | 100 | | postWsFound = false; |
| 128 | | | } |
| 129 | | | else |
| 130 | | | { |
| 131 | 100 | | postWsFound = true; |
| 132 | | | } |
| 133 | | | } |
| 134 | 100 | | setPositions(rc, wsFound, postWsFound, startOfWs, endOfWs); |
| 135 | 100 | | return rc; |
| 136 | | | } |
| 137 | | | |
| 138 | | | |
| 139 | | | |
| 140 | | | |
| 141 | | | |
| 142 | | | @param <code></code> |
| 143 | | | @param |
| 144 | | | |
| 145 | | | @param |
| 146 | | | |
| 147 | | | @param |
| 148 | | | @param |
| 149 | | | |
| 150 | | | private void setPositions ( |
| 151 | | | final CharBuffer rc, |
| 152 | | | final boolean wsFound, |
| 153 | | | final boolean postWsFound, |
| 154 | | | final int startOfWs, |
| 155 | | | final int endOfWs) |
| 156 | | | { |
| 157 | 100 | | if (wsFound) |
| 158 | | | { |
| 159 | | (4) | |
| 160 | | | |
| 161 | | | |
| 162 | 100 | | if (rc.position() > 0) |
| 163 | | | { |
| 164 | 0 | | rc.limit(rc.position() + startOfWs); |
| 165 | | | } |
| 166 | | | else |
| 167 | | | { |
| 168 | 100 | | rc.limit(startOfWs); |
| 169 | | | } |
| 170 | 100 | | if (! postWsFound) |
| 171 | | | { |
| 172 | 100 | | mBuffer.position(mBuffer.limit()); |
| 173 | | | } |
| 174 | | | else |
| 175 | | | { |
| 176 | 100 | | mBuffer.position(mBuffer.position() + endOfWs + 1); |
| 177 | | | } |
| 178 | | | } |
| 179 | | | else |
| 180 | | | { |
| 181 | 100 | | mBuffer.position(mBuffer.limit()); |
| 182 | | | } |
| 183 | 100 | | } |
| 184 | | | |
| 185 | | | |
| 186 | | | |
| 187 | | | {@linkplain } |
| 188 | | | |
| 189 | | | private void skip () |
| 190 | | | { |
| 191 | 100 | | boolean wsFound = false; |
| 192 | 100 | | boolean postWsFound = false; |
| 193 | 100 | (5) | boolean first = true; |
| 194 | | | |
| 195 | 100 | | int endOfWs = 0; |
| 196 | | | |
| 197 | 100 | | for (int i = 0; i < mBuffer.remaining() |
| 198 | 100 | | && ((wsFound ^ postWsFound) || first); |
| 199 | 100 | | ++i) |
| 200 | | | { |
| 201 | 100 | | first = false; |
| 202 | 100 | | final char c = mBuffer.charAt(i); |
| 203 | | | |
| 204 | 100 | | if (Character.isWhitespace(c) && ! (c == PRESERVED_CHAR)) |
| 205 | | | { |
| 206 | 100 | | wsFound = true; |
| 207 | 100 | | endOfWs = i; |
| 208 | | | } |
| 209 | | | else |
| 210 | | | { |
| 211 | 100 | | postWsFound = wsFound; |
| 212 | | | } |
| 213 | | | } |
| 214 | 100 | | if (wsFound && ! postWsFound) |
| 215 | | | { |
| 216 | | | |
| 217 | 0 | | mBuffer.position(mBuffer.limit()); |
| 218 | | | } |
| 219 | 100 | | else if (wsFound) |
| 220 | | | { |
| 221 | | | |
| 222 | 100 | | mBuffer.position(mBuffer.position() + endOfWs); |
| 223 | | | } |
| 224 | 100 | | } |
| 225 | | | } |
| 226 | | | |
| 227 | | | |
| 228 | | | |
| 229 | | | |
| 230 | | | public WhitespaceFormat () |
| 231 | | | { |
| 232 | 100 | (6) | this(null); |
| 233 | 100 | | } |
| 234 | | | |
| 235 | | | |
| 236 | | | |
| 237 | | | |
| 238 | | | @param |
| 239 | | | |
| 240 | | | |
| 241 | | | public WhitespaceFormat (final Format subFormat) |
| 242 | 100 | | { |
| 243 | 100 | | mSubFormat = subFormat; |
| 244 | 100 | | } |
| 245 | | | |
| 246 | | | |
| 247 | | | |
| 248 | | | <code></code> |
| 249 | | | |
| 250 | | | <code></code> |
| 251 | | | |
| 252 | | | <code></code> |
| 253 | | | |
| 254 | | | @param |
| 255 | | | |
| 256 | | | @return |
| 257 | | | |
| 258 | | | public static String format (final String message) |
| 259 | | | { |
| 260 | 100 | | return format(CharBuffer.wrap(message)).toString(); |
| 261 | | | } |
| 262 | | | |
| 263 | | | |
| 264 | | | |
| 265 | | | |
| 266 | | | @see |
| 267 | | | |
| 268 | | | @param |
| 269 | | | |
| 270 | | | |
| 271 | | | @return |
| 272 | | | <code></code> |
| 273 | | | |
| 274 | | | public static CharBuffer format (final CharBuffer message) |
| 275 | | | { |
| 276 | 100 | | final WhitespaceIterator iter = new WhitespaceIterator(message); |
| 277 | | | |
| 278 | 100 | | CharBuffer rc = null; |
| 279 | 100 | | boolean isFirst = true; |
| 280 | 100 | | boolean flip = false; |
| 281 | | | |
| 282 | 100 | | while (iter.hasNext()) |
| 283 | | | { |
| 284 | 100 | | final CharBuffer cb = (CharBuffer) iter.next(); |
| 285 | | | |
| 286 | 100 | | if (! (isFirst || (rc == null))) |
| 287 | | | { |
| 288 | 100 | | rc.put(PRESERVED_CHAR); |
| 289 | 100 | | rc.put(cb); |
| 290 | | | } |
| 291 | 100 | | else if (isFirst) |
| 292 | | | { |
| 293 | 100 | | isFirst = false; |
| 294 | 100 | | if ((cb.limit() == message.limit()) |
| 295 | | | && (cb.position() == message.position())) |
| 296 | | | { |
| 297 | 100 | | rc = message.duplicate(); |
| 298 | | | } |
| 299 | | | else |
| 300 | | | { |
| 301 | 100 | | rc = CharBuffer.allocate(message.limit()); |
| 302 | 100 | | rc.put(cb); |
| 303 | 100 | | flip = true; |
| 304 | | | } |
| 305 | | | } |
| 306 | | | else |
| 307 | | | { |
| 308 | | | |
| 309 | 0 | (7) | throw new RuntimeException("More than one string parts and no " |
| 310 | | | + "target buffer is allocated"); |
| 311 | | | } |
| 312 | 100 | | } |
| 313 | 100 | | if (flip) |
| 314 | | | { |
| 315 | 100 | | rc.flip(); |
| 316 | | | } |
| 317 | 100 | | return rc; |
| 318 | | | } |
| 319 | | | |
| 320 | | | |
| 321 | | | |
| 322 | | | |
| 323 | | | {@link } |
| 324 | | | |
| 325 | | | @see |
| 326 | | | |
| 327 | | (8)(9)(10) | public Object parseObject (final String source, final ParsePosition pos) |
| 328 | | | { |
| 329 | | | Object rc; |
| 330 | 100 | | if (mSubFormat == null) |
| 331 | | | { |
| 332 | 100 | | int i = pos.getIndex(); |
| 333 | 100 | | final int len = source.length(); |
| 334 | 100 | | boolean endFound = false; |
| 335 | | | |
| 336 | 100 | | while (i < len && ! endFound) |
| 337 | | | { |
| 338 | 100 | | final char c = source.charAt(i); |
| 339 | 100 | | if (Character.isWhitespace(c) && c != PRESERVED_CHAR) |
| 340 | | | { |
| 341 | 100 | | endFound = true; |
| 342 | | | } |
| 343 | | | else |
| 344 | | | { |
| 345 | 100 | | ++i; |
| 346 | | | } |
| 347 | 100 | | } |
| 348 | 100 | | if (endFound) |
| 349 | | | { |
| 350 | 100 | | rc = source.substring(pos.getIndex(), i); |
| 351 | 100 | | pos.setIndex(i); |
| 352 | | | } |
| 353 | | | else |
| 354 | | | { |
| 355 | 100 | | rc = source.substring(pos.getIndex()); |
| 356 | 100 | | pos.setIndex(len); |
| 357 | | | } |
| 358 | 100 | | } |
| 359 | | | else |
| 360 | | | { |
| 361 | 100 | | rc = mSubFormat.parseObject(source, pos); |
| 362 | | | } |
| 363 | 100 | | return rc; |
| 364 | | | } |
| 365 | | | |
| 366 | | | |
| 367 | | | |
| 368 | | | |
| 369 | | | |
| 370 | | | |
| 371 | | | |
| 372 | | | @param |
| 373 | | | @param |
| 374 | | | |
| 375 | | | @param |
| 376 | | | |
| 377 | | | @return |
| 378 | | | |
| 379 | | | @see |
| 380 | | | |
| 381 | | | public StringBuffer format ( |
| 382 | | | final Object obj, |
| 383 | | | final StringBuffer toAppendTo, |
| 384 | | | final FieldPosition pos) |
| 385 | | | { |
| 386 | 100 | (11) | if (mSubFormat == null) |
| 387 | | | { |
| 388 | 0 | | if (! (obj instanceof String)) |
| 389 | | | { |
| 390 | 0 | | throw new IllegalArgumentException("Supplied object to be formatted" |
| 391 | | | + " must be a String but is " |
| 392 | | | + obj.getClass().getName() + ": " + obj); |
| 393 | | | } |
| 394 | 0 | | toAppendTo.append(format((String) obj)); |
| 395 | 0 | | if (pos != null) |
| 396 | | | { |
| 397 | 0 | | pos.setBeginIndex(0); |
| 398 | 0 | | pos.setEndIndex(0); |
| 399 | | | } |
| 400 | | | } |
| 401 | | | else |
| 402 | | | { |
| 403 | 100 | | StringBuffer sb = new StringBuffer(); |
| 404 | 100 | | sb = mSubFormat.format(obj, sb, pos); |
| 405 | 100 | | toAppendTo.append(WhitespaceFormat.format(sb.toString())); |
| 406 | | | } |
| 407 | 100 | | return toAppendTo; |
| 408 | | | } |
| 409 | | | } |