| 1 | /* |
|---|
| 2 | * $Id$ |
|---|
| 3 | * |
|---|
| 4 | * Copyright 2006, The jCoderZ.org Project. All rights reserved. |
|---|
| 5 | * |
|---|
| 6 | * Redistribution and use in source and binary forms, with or without |
|---|
| 7 | * modification, are permitted provided that the following conditions are |
|---|
| 8 | * met: |
|---|
| 9 | * |
|---|
| 10 | * * Redistributions of source code must retain the above copyright |
|---|
| 11 | * notice, this list of conditions and the following disclaimer. |
|---|
| 12 | * * Redistributions in binary form must reproduce the above |
|---|
| 13 | * copyright notice, this list of conditions and the following |
|---|
| 14 | * disclaimer in the documentation and/or other materials |
|---|
| 15 | * provided with the distribution. |
|---|
| 16 | * * Neither the name of the jCoderZ.org Project nor the names of |
|---|
| 17 | * its contributors may be used to endorse or promote products |
|---|
| 18 | * derived from this software without specific prior written |
|---|
| 19 | * permission. |
|---|
| 20 | * |
|---|
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND |
|---|
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|---|
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|---|
| 24 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS |
|---|
| 25 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|---|
| 26 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|---|
| 27 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
|---|
| 28 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
|---|
| 29 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
|---|
| 30 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
|---|
| 31 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 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.StringTokenizer; |
|---|
| 39 | |
|---|
| 40 | /** |
|---|
| 41 | * This Format class is used for formatting a String as it is, i.e. do no |
|---|
| 42 | * formatting of the String. For enabling this as a parser for the format, |
|---|
| 43 | * delimiters can be set when constructing an instance of this. But if this |
|---|
| 44 | * functionality is actually desired, one has to be sure the delimiter is not |
|---|
| 45 | * part of the string to format otherwise the result might not be as expected. |
|---|
| 46 | * |
|---|
| 47 | */ |
|---|
| 48 | public final class AsItIsFormat |
|---|
| 49 | extends Format |
|---|
| 50 | { |
|---|
| 51 | private static final long serialVersionUID = 3905519414995792952L; |
|---|
| 52 | |
|---|
| 53 | private final String mDelimiters; |
|---|
| 54 | |
|---|
| 55 | /** |
|---|
| 56 | * Constructor, saves the delimiters for parsing. When this is used for |
|---|
| 57 | * parsing a String, the delimiters work as for a StringTokenizer. |
|---|
| 58 | * |
|---|
| 59 | * @param delimiters String containing delimiting chars, used when parsing. |
|---|
| 60 | */ |
|---|
| 61 | public AsItIsFormat (final String delimiters) |
|---|
| 62 | { |
|---|
| 63 | if (delimiters == null || delimiters.length() == 0) |
|---|
| 64 | { |
|---|
| 65 | mDelimiters = null; |
|---|
| 66 | } |
|---|
| 67 | else |
|---|
| 68 | { |
|---|
| 69 | mDelimiters = delimiters; |
|---|
| 70 | } |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | /** {@inheritDoc} */ |
|---|
| 74 | public Object parseObject (String source, ParsePosition pos) |
|---|
| 75 | { |
|---|
| 76 | String rc = null; |
|---|
| 77 | |
|---|
| 78 | final String string = source.substring(pos.getIndex()); |
|---|
| 79 | if (mDelimiters == null) |
|---|
| 80 | { |
|---|
| 81 | rc = string; |
|---|
| 82 | pos.setIndex(source.length()); |
|---|
| 83 | } |
|---|
| 84 | else |
|---|
| 85 | { |
|---|
| 86 | final StringTokenizer tokenizer = new StringTokenizer( |
|---|
| 87 | string, mDelimiters); |
|---|
| 88 | if (tokenizer.hasMoreTokens()) |
|---|
| 89 | { |
|---|
| 90 | rc = tokenizer.nextToken(); |
|---|
| 91 | pos.setIndex(pos.getIndex() + rc.length()); |
|---|
| 92 | } |
|---|
| 93 | else |
|---|
| 94 | { |
|---|
| 95 | pos.setErrorIndex(pos.getIndex()); |
|---|
| 96 | } |
|---|
| 97 | } |
|---|
| 98 | return rc; |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | /** {@inheritDoc} */ |
|---|
| 102 | public StringBuffer format ( |
|---|
| 103 | Object obj, |
|---|
| 104 | StringBuffer toAppendTo, |
|---|
| 105 | FieldPosition pos) |
|---|
| 106 | { |
|---|
| 107 | if (! (obj instanceof String)) |
|---|
| 108 | { |
|---|
| 109 | throw new IllegalArgumentException("Supplied object to be formatted " |
|---|
| 110 | + "must be a String but is " + obj.getClass().getName() + ": " |
|---|
| 111 | + obj); |
|---|
| 112 | } |
|---|
| 113 | pos.setBeginIndex(0); |
|---|
| 114 | pos.setEndIndex(0); |
|---|
| 115 | toAppendTo.append(obj); |
|---|
| 116 | return toAppendTo; |
|---|
| 117 | } |
|---|
| 118 | } |
|---|