Coverage Report - org.ini4j.spi.EscapeTool
 
Classes in this File Line Coverage Branch Coverage Complexity
EscapeTool
100%
72/72
100%
34/34
3.714
 
 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  3
 public class EscapeTool
 19  
 {
 20  
     private static final String ESCAPE_LETTERS = "\\tnfbr";
 21  
     private static final String ESCAPEABLE_CHARS = "\\\t\n\f\b\r";
 22  
     private static final char ESCAPE_CHAR = '\\';
 23  1
     static final char[] HEX = "0123456789abcdef".toCharArray();
 24  1
     private static final EscapeTool INSTANCE = ServiceFinder.findService(EscapeTool.class);
 25  
     private static final char ASCII_MIN = 0x20;
 26  
     private static final char ASCII_MAX = 0x7e;
 27  
     static final int HEX_DIGIT_MASK = 0x0f;
 28  
     static final int HEX_DIGIT_3_OFFSET = 4;
 29  
     static final int HEX_DIGIT_2_OFFSET = 8;
 30  
     static final int HEX_DIGIT_1_OFFSET = 12;
 31  
     static final int HEX_RADIX = 16;
 32  
     private static final int UNICODE_HEX_DIGITS = 4;
 33  
     static final char DOUBLE_QUOTE = '"';
 34  
 
 35  
     public static EscapeTool getInstance()
 36  
     {
 37  5612
         return INSTANCE;
 38  
     }
 39  
 
 40  
     public String escape(String line)
 41  
     {
 42  648
         int len = line.length();
 43  648
         StringBuilder buffer = new StringBuilder(len * 2);
 44  
 
 45  6282
         for (int i = 0; i < len; i++)
 46  
         {
 47  5634
             char c = line.charAt(i);
 48  5634
             int idx = ESCAPEABLE_CHARS.indexOf(c);
 49  
 
 50  5634
             if (idx >= 0)
 51  
             {
 52  29
                 buffer.append(ESCAPE_CHAR);
 53  29
                 buffer.append(ESCAPE_LETTERS.charAt(idx));
 54  
             }
 55  
             else
 56  
             {
 57  5605
                 if ((c < ASCII_MIN) || (c > ASCII_MAX))
 58  
                 {
 59  44
                     escapeBinary(buffer, c);
 60  
                 }
 61  
                 else
 62  
                 {
 63  5561
                     buffer.append(c);
 64  
                 }
 65  
             }
 66  
         }
 67  
 
 68  648
         return buffer.toString();
 69  
     }
 70  
 
 71  
     public String quote(String value)
 72  
     {
 73  147
         String ret = value;
 74  
 
 75  147
         if ((value != null) && (value.length() != 0))
 76  
         {
 77  145
             StringBuilder buff = new StringBuilder();
 78  
 
 79  145
             buff.append(DOUBLE_QUOTE);
 80  2723
             for (int i = 0; i < value.length(); i++)
 81  
             {
 82  2578
                 char c = value.charAt(i);
 83  
 
 84  2578
                 if ((c == ESCAPE_CHAR) || (c == DOUBLE_QUOTE))
 85  
                 {
 86  86
                     buff.append(ESCAPE_CHAR);
 87  
                 }
 88  
 
 89  2578
                 buff.append(c);
 90  
             }
 91  
 
 92  145
             buff.append(DOUBLE_QUOTE);
 93  145
             ret = buff.toString();
 94  
         }
 95  
 
 96  147
         return ret;
 97  
     }
 98  
 
 99  
     public String unescape(String line)
 100  
     {
 101  4994
         int n = line.length();
 102  4994
         StringBuilder buffer = new StringBuilder(n);
 103  4994
         int i = 0;
 104  
 
 105  55531
         while (i < n)
 106  
         {
 107  50540
             char c = line.charAt(i++);
 108  
 
 109  50540
             if (c == ESCAPE_CHAR)
 110  
             {
 111  283
                 c = line.charAt(i++);
 112  283
                 int next = unescapeBinary(buffer, c, line, i);
 113  
 
 114  280
                 if (next == i)
 115  
                 {
 116  230
                     int idx = ESCAPE_LETTERS.indexOf(c);
 117  
 
 118  230
                     if (idx >= 0)
 119  
                     {
 120  141
                         c = ESCAPEABLE_CHARS.charAt(idx);
 121  
                     }
 122  
 
 123  230
                     buffer.append(c);
 124  230
                 }
 125  
                 else
 126  
                 {
 127  50
                     i = next;
 128  
                 }
 129  280
             }
 130  
             else
 131  
             {
 132  50257
                 buffer.append(c);
 133  
             }
 134  50537
         }
 135  
 
 136  4991
         return buffer.toString();
 137  
     }
 138  
 
 139  
     public String unquote(String value)
 140  
     {
 141  737
         StringBuilder buff = new StringBuilder();
 142  737
         boolean escape = false;
 143  
 
 144  10945
         for (int i = 1; i < (value.length() - 1); i++)
 145  
         {
 146  10208
             char c = value.charAt(i);
 147  
 
 148  10208
             if (c == ESCAPE_CHAR)
 149  
             {
 150  492
                 if (!escape)
 151  
                 {
 152  246
                     escape = true;
 153  
 
 154  246
                     continue;
 155  
                 }
 156  
 
 157  246
                 escape = false;
 158  
             }
 159  
 
 160  9962
             buff.append(c);
 161  
         }
 162  
 
 163  737
         return buff.toString();
 164  
     }
 165  
 
 166  
     void escapeBinary(StringBuilder buff, char c)
 167  
     {
 168  26
         buff.append("\\u");
 169  26
         buff.append(HEX[(c >>> HEX_DIGIT_1_OFFSET) & HEX_DIGIT_MASK]);
 170  26
         buff.append(HEX[(c >>> HEX_DIGIT_2_OFFSET) & HEX_DIGIT_MASK]);
 171  26
         buff.append(HEX[(c >>> HEX_DIGIT_3_OFFSET) & HEX_DIGIT_MASK]);
 172  26
         buff.append(HEX[c & HEX_DIGIT_MASK]);
 173  26
     }
 174  
 
 175  
     int unescapeBinary(StringBuilder buff, char escapeType, String line, int index)
 176  
     {
 177  249
         int ret = index;
 178  
 
 179  249
         if (escapeType == 'u')
 180  
         {
 181  
             try
 182  
             {
 183  31
                 buff.append((char) Integer.parseInt(line.substring(index, index + UNICODE_HEX_DIGITS), HEX_RADIX));
 184  30
                 ret = index + UNICODE_HEX_DIGITS;
 185  
             }
 186  1
             catch (Exception x)
 187  
             {
 188  1
                 throw new IllegalArgumentException("Malformed \\uxxxx encoding.", x);
 189  30
             }
 190  
         }
 191  
 
 192  248
         return ret;
 193  
     }
 194  
 }