View Javadoc

1   /*
2    * Copyright 2005,2009 Ivan SZKIBA
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.ini4j.spi;
17  
18  public class WinEscapeTool extends EscapeTool
19  {
20      private static final int ANSI_HEX_DIGITS = 2;
21      private static final int ANSI_OCTAL_DIGITS = 3;
22      private static final int OCTAL_RADIX = 8;
23      private static final WinEscapeTool INSTANCE = new WinEscapeTool();
24  
25      public static WinEscapeTool getInstance()
26      {
27          return INSTANCE;
28      }
29  
30      @Override void escapeBinary(StringBuilder buff, char c)
31      {
32          buff.append("\\x");
33          buff.append(HEX[(c >>> HEX_DIGIT_3_OFFSET) & HEX_DIGIT_MASK]);
34          buff.append(HEX[c & HEX_DIGIT_MASK]);
35      }
36  
37      @Override int unescapeBinary(StringBuilder buff, char escapeType, String line, int index)
38      {
39          int ret = index;
40  
41          if (escapeType == 'x')
42          {
43              try
44              {
45                  buff.append((char) Integer.parseInt(line.substring(index, index + ANSI_HEX_DIGITS), HEX_RADIX));
46                  ret = index + ANSI_HEX_DIGITS;
47              }
48              catch (Exception x)
49              {
50                  throw new IllegalArgumentException("Malformed \\xHH encoding.", x);
51              }
52          }
53          else if (escapeType == 'o')
54          {
55              try
56              {
57                  buff.append((char) Integer.parseInt(line.substring(index, index + ANSI_OCTAL_DIGITS), OCTAL_RADIX));
58                  ret = index + ANSI_OCTAL_DIGITS;
59              }
60              catch (Exception x)
61              {
62                  throw new IllegalArgumentException("Malformed \\oOO encoding.", x);
63              }
64          }
65  
66          return ret;
67      }
68  }