Stuart McCulloch | 669423b | 2012-06-26 16:34:24 +0000 | [diff] [blame] | 1 | package aQute.bnd.build.model.conversions; |
| 2 | |
| 3 | import aQute.bnd.build.model.*; |
| 4 | |
| 5 | public class NewlineEscapedStringFormatter implements Converter<String,String> { |
| 6 | |
| 7 | public String convert(String input) throws IllegalArgumentException { |
| 8 | if (input == null) |
| 9 | return null; |
| 10 | |
| 11 | // Shortcut the result for the majority of cases where there is no |
| 12 | // newline |
| 13 | if (input.indexOf('\n') == -1) |
| 14 | return input; |
| 15 | |
| 16 | // Build a new string with newlines escaped |
| 17 | StringBuilder result = new StringBuilder(); |
| 18 | int position = 0; |
| 19 | while (position < input.length()) { |
| 20 | int newlineIndex = input.indexOf('\n', position); |
| 21 | if (newlineIndex == -1) { |
| 22 | result.append(input.substring(position)); |
| 23 | break; |
| 24 | } |
| 25 | result.append(input.substring(position, newlineIndex)); |
| 26 | result.append(BndEditModel.LINE_SEPARATOR); |
| 27 | position = newlineIndex + 1; |
| 28 | } |
| 29 | |
| 30 | return result.toString(); |
| 31 | } |
| 32 | |
| 33 | } |