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 public class StringQuoter {
23
24 public static String quote(String value) {
25 if (value == null) {
26 return null;
27 }
28 StringBuffer buf = new StringBuffer(value.length() + 3);
29 buf.append('"');
30 boolean lastCharWasEscape = false;
31 for (int i = 0; i < value.length(); i++) {
32 char c = value.charAt(i);
33 switch (c) {
34 case '"':
35 if (!lastCharWasEscape) {
36 buf.append('\\');
37 }
38 lastCharWasEscape = false;
39 break;
40 case '\\':
41 if (!lastCharWasEscape) {
42 buf.append('\\');
43 }
44 lastCharWasEscape = true;
45 break;
46 case '\n':
47 case '\r':
48 buf.append('"');
49 buf.append("\n");
50 buf.append("\t + ");
51 buf.append('"');
52 lastCharWasEscape = false;
53 continue;
54 default:
55 lastCharWasEscape = false;
56 }
57 buf.append(c);
58 }
59 buf.append('"');
60 return buf.toString();
61 }
62 }