01: /*
02: * Copyright (C) Chaperon. All rights reserved.
03: * -------------------------------------------------------------------------
04: * This software is published under the terms of the Apache Software License
05: * version 1.1, a copy of which has been included with this distribution in
06: * the LICENSE file.
07: */
08:
09: package net.sourceforge.chaperon.helpers;
10:
11: /**
12: * The class maskes special characters
13: *
14: * @author Stephan Michels
15: * @version %version%
16: */
17: public class Decoder {
18:
19: /**
20: * Mask special characters
21: *
22: * @param string String
23: *
24: * @return Mask string
25: */
26: public static String decode(String string) {
27: StringBuffer buffer = new StringBuffer();
28:
29: for (int i = 0; i < string.length(); i++) {
30: if (string.charAt(i) == '\n')
31: buffer.append("\\n");
32: else if (string.charAt(i) == '\t')
33: buffer.append("\\t");
34: else if (string.charAt(i) == '\r')
35: buffer.append("\\r");
36: else if (string.charAt(i) == '\"')
37: buffer.append("\"");
38: else if (string.charAt(i) == '\\')
39: buffer.append("\\");
40: else if ((string.charAt(i) >= '!')
41: && (string.charAt(i) <= '~'))
42: buffer.append(string.charAt(i));
43: else {
44: String hexstring = Integer.toHexString((int) string
45: .charAt(i));
46: String zeros = "0000";
47:
48: buffer.append("\\u"
49: + zeros.substring(4 - hexstring.length())
50: + hexstring);
51: }
52: }
53: return buffer.toString();
54: }
55: }
|