View Javadoc

1   /*
2    * Copyright (c) 2009 QOS.ch All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS  IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  package ch.qos.cal10n.util;
23  
24  /**
25   * @author Ceki Gücü
26   */
27  public class LexicalUtil {
28  
29  
30    public static StringBuilder convertSpecialCharacters(StringBuilder inBuf) {
31  
32      int i = inBuf.indexOf("\\");
33  
34      if (i == -1) {
35        return inBuf;
36      }
37  
38      StringBuilder outBuf = new StringBuilder(inBuf.length());
39  
40      int last = -1;
41      while (i != -1) {
42        outBuf.append(inBuf.subSequence(last + 1, i));
43        last = i + 1;
44        char next = inBuf.charAt(last);
45        switch (next) {
46          case 'u':
47            char unicodeChar = readUnicode(inBuf, last + 1);
48            outBuf.append(unicodeChar);
49            last += 4;
50            break;
51          case 'n':
52            outBuf.append('\n');
53            break;
54          case 'r':
55            outBuf.append('\r');
56            break;
57          case 't':
58            outBuf.append('\t');
59            break;
60          case 'f':
61            outBuf.append('\f');
62            break;
63          default:
64            outBuf.append('\\');
65            outBuf.append(next);
66  
67        }
68        i = inBuf.indexOf("\\", last);
69      }
70      outBuf.append(inBuf.subSequence(last + 1, inBuf.length()));
71      return outBuf;
72    }
73  
74    // see http://java.sun.com/docs/books/jls/second_edition/html/lexical.doc.html#95504
75  
76    private static char readUnicode(StringBuilder inBuf, int i) {
77      int r = 0;
78      for (int j = i; j < i + 4; j++) {
79        char atJ = inBuf.charAt(j);
80  
81        if (atJ >= '0' && atJ <= '9') {
82          r = (r << 4) + atJ - '0';
83          continue;
84        }
85        if (atJ >= 'A' && atJ <= 'F') {
86          // '7' = 'A' - 10 
87          r = (r << 4) + atJ - '7';
88          continue;
89        }
90        if (atJ >= 'a' && atJ <= 'f') {
91          // 'W' = 'a' - 10
92          r = (r << 4) + atJ - 'W';
93          continue;
94        }
95      }
96      return (char) r;
97    }
98  }