1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.student.contract.writer; |
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | 0 | public class StringQuoter |
23 | |
{ |
24 | |
|
25 | |
public static String quote (String value) |
26 | |
{ |
27 | 0 | if (value == null) |
28 | |
{ |
29 | 0 | return null; |
30 | |
} |
31 | 0 | StringBuffer buf = new StringBuffer (value.length () + 3); |
32 | 0 | buf.append ('"'); |
33 | 0 | boolean lastCharWasEscape = false; |
34 | 0 | for (int i = 0; i < value.length (); i ++) |
35 | |
{ |
36 | 0 | char c = value.charAt (i); |
37 | 0 | switch (c) |
38 | |
{ |
39 | |
case '"': |
40 | 0 | if ( ! lastCharWasEscape) |
41 | |
{ |
42 | 0 | buf.append ('\\'); |
43 | |
} |
44 | 0 | lastCharWasEscape = false; |
45 | 0 | break; |
46 | |
case '\\': |
47 | 0 | if ( ! lastCharWasEscape) |
48 | |
{ |
49 | 0 | buf.append ('\\'); |
50 | |
} |
51 | 0 | lastCharWasEscape = true; |
52 | 0 | break; |
53 | |
case '\n': |
54 | |
case '\r': |
55 | 0 | buf.append ('"'); |
56 | 0 | buf.append ("\n"); |
57 | 0 | buf.append ("\t + "); |
58 | 0 | buf.append ('"'); |
59 | 0 | lastCharWasEscape = false; |
60 | 0 | continue; |
61 | |
default: |
62 | 0 | lastCharWasEscape = false; |
63 | |
} |
64 | 0 | buf.append (c); |
65 | |
} |
66 | 0 | buf.append ('"'); |
67 | 0 | return buf.toString (); |
68 | |
} |
69 | |
|
70 | |
} |