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