LexicalUtil xref

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&uuml;c&uuml;
    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 followIndex = -1;  // index of char following the latest backslash
    41      while (i != -1) {
    42        outBuf.append(inBuf.subSequence(followIndex + 1, i));
    43        followIndex = i + 1;
    44        char c = inBuf.charAt(followIndex);
    45        switch (c) {
    46          case 'u':
    47            char unicodeChar = readUnicode(inBuf, followIndex + 1);
    48            outBuf.append(unicodeChar);
    49            followIndex += 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          case ':': // see http://tinyurl.com/bprdgnk , i.e. the javadocs for Properties.store()
    64          case '#':
    65          case '!':
    66          case '=':
    67            outBuf.append(c);
    68            break;
    69          default:
    70            outBuf.append('\\');
    71            outBuf.append(c);
    72        }
    73        // search for the next \
    74        i = inBuf.indexOf("\\", followIndex + 1);
    75      }
    76      outBuf.append(inBuf.subSequence(followIndex + 1, inBuf.length()));
    77      return outBuf;
    78    }
    79
    80    // see http://java.sun.com/docs/books/jls/second_edition/html/lexical.doc.html#95504
    81
    82    private static char readUnicode(StringBuilder inBuf, int i) {
    83      int r = 0;
    84      for (int j = i; j < i + 4; j++) {
    85        char atJ = inBuf.charAt(j);
    86
    87        if (atJ >= '0' && atJ <= '9') {
    88          r = (r << 4) + atJ - '0';
    89          continue;
    90        }
    91        if (atJ >= 'A' && atJ <= 'F') {
    92          // '7' = 'A' - 10
    93          r = (r << 4) + atJ - '7';
    94          continue;
    95        }
    96        if (atJ >= 'a' && atJ <= 'f') {
    97          // 'W' = 'a' - 10
    98          r = (r << 4) + atJ - 'W';
    99          continue;
    100       }
    101     }
    102     return (char) r;
    103   }
    104 }