Project Report: fawkez

Packagesummary org.jcoderz.commons.logging

org.jcoderz.commons.logging.StringEscapeFormat

LineHitsNoteSource
1  /*
2   * $Id: StringEscapeFormat.java 1011 2008-06-16 17:57:36Z amandel $
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  
39  import org.jcoderz.commons.util.Assert;
40  
41  
42  /**
43   * This formatter is used to escape chars in the source string with a
44   * configured escape char.The default escape char is '\'.
45   *
46   */
47 (1)public class StringEscapeFormat
48        extends Format
49  {
50     static final char ESCAPE_CHAR = '\\';
51  
52     private static final long serialVersionUID = 3257567291389851442L;
53  
54     private final char mEscapeChar;
55     private final String mCharsToEscape;
56  
57     /**
58      * Creates an instance of this, which will use the default escape char
59      * {@linkplain #ESCAPE_CHAR}.
60      *
61      * @param charsToEscape the characters which will be escaped by
62      * {@linkplain #ESCAPE_CHAR}.
63      */
64     public StringEscapeFormat (final String charsToEscape)
65     {
66100       this(charsToEscape, ESCAPE_CHAR);
67100    }
68  
69     /**
70      * Creates an instance of this and sets the supplied escape char.
71      *
72      * @param charsToEscape The characters which will be escaped by
73      * <code>escapeChar</code>.
74      * @param escapeChar The char by which to escape <code>charToEscape</code>.
75      * This must not be included in <code>charsToEscape</code> and must not be 0.
76      *
77      * @throws IllegalArgumentException if <code>escapeChar</code> is included
78      * within <code>charsToEscape</code> or is <code>0</code>.
79      */
80     public StringEscapeFormat (
81           final String charsToEscape,
82           final char escapeChar)
83     {
84100       super();
85100       Assert.notNull("charsToEscape", charsToEscape);
86100       mEscapeChar = escapeChar;
87100       mCharsToEscape = charsToEscape;
88100       if (escapeChar == 0)
89        {
900(2)         throw new IllegalArgumentException(
91                 "The escape character must not be 0");
92        }
93100       if ((charsToEscape != null) && (charsToEscape.indexOf(escapeChar) >= 0))
94        {
950(3)         throw new IllegalArgumentException(
96              "The escape character must not be one of the characters to escape");
97        }
98100    }
99  
100     /**
101      * @see java.text.Format#parseObject(java.lang.String, java.text.ParsePosition)
102      *
103      * Parses the source string. All characters configured as characters to
104      * escape are considered as delimiters (thus parsing is stopped at one of
105      * those) if they are not escaped with the configured character.
106      * A new string is created from this substring with all escape chars before
107      * an char to escape removed. Escape chars not being followed by a character
108      * to escape are not removed for the result string.
109      *
110      * @param source The string to parse.
111      * @param pos the position within <code>source</code>.
112      *
113      * @return the String being parsed from <code>source</code> with escape
114      *       chars removed.
115      */
116     public Object parseObject (String source, ParsePosition pos)
117     {
118100       final int sourceLen = source.length();
119100       final StringBuffer rc = new StringBuffer();
120100       final int offs = pos.getIndex();
121  
122100       int fromIndex = offs;
123100       boolean escaped = false;
124100       boolean delimiterFound = false;
125        int index;
126100       for (index = offs; index < sourceLen && ! delimiterFound; ++index)
127        {
128100          final char c = source.charAt(index);
129  
130100          if (mCharsToEscape.indexOf(c) >= 0)
131           {
132100             if (escaped)
133              {
134100                if (fromIndex != index - 1)
135                 {
136                    // skip over escape char
137100                   rc.append(source.substring(fromIndex, index - 1));
138                 }
139100                fromIndex = index;
140              }
141              else
142              {
143100                delimiterFound = true;
144100                index--;
145              }
146100             escaped = false;
147           }
148           else
149           {
150100             escaped = (c == mEscapeChar);
151           }
152        }
153100       if (fromIndex < index)
154        {
155100          rc.append(source.substring(fromIndex, index));
156        }
157100       if (index > offs)
158        {
159100          pos.setIndex(index);
160        }
161100       return rc.toString();
162     }
163  
164     /**
165      * Formats the supplied object by calling the object's toString() method
166      * and escaping the configured chars of this string with the configured
167      * escape char.
168      * @see Format#format(java.lang.Object, java.lang.StringBuffer, java.text.FieldPosition)
169      *
170      * @param obj the object to format.
171      * @param toAppendTo the StringBuffer where to append to the formatted
172      * string.
173      * @param pos the field position.
174      *
175      * @return StringBuffer where the formatted Object has been appended to.
176      */
177     public StringBuffer format (Object obj, StringBuffer toAppendTo,
178           FieldPosition pos)
179     {
180100       final String objString = String.valueOf(obj);
181100       int fromIndex = 0;
182100       final int len = objString.length();
183100       for (int i = 0; i < len; ++i)
184        {
185100          final char currentChar = objString.charAt(i);
186100          if (mCharsToEscape.indexOf(currentChar) >= 0)
187           {
188100             if (fromIndex < i)
189              {
190100                toAppendTo.append(objString.substring(fromIndex, i));
191100                fromIndex = i;
192              }
193100             toAppendTo.append(mEscapeChar);
194           }
195        }
196100       if (fromIndex == 0)
197        {
198100          toAppendTo.append(objString);
199        }
200        else
201        {
202100          toAppendTo.append(objString.substring(fromIndex));
203        }
204100       return toAppendTo;
205     }
206  }

Findings in this File

c (1) 47 : 0 Type Javadoc comment is missing an @author tag.
i (2) 90 : 0 method new org.jcoderz.commons.logging.StringEscapeFormat(String, char) throws exception with static message string
i (3) 95 : 0 method new org.jcoderz.commons.logging.StringEscapeFormat(String, char) throws exception with static message string