1 /*
2 * Copyright 2009 The Kuali Foundation
3 *
4 * Licensed under the Educational Community 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.osedu.org/licenses/ECL-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.kuali.student.contract.writer;
17
18 /**
19 *
20 * @author nwright
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 }