blob: 1bf92201fac67815a2187f178ce006ebb486d739 [file] [log] [blame]
Stuart McCulloch669423b2012-06-26 16:34:24 +00001package aQute.bnd.build.model.conversions;
2
3import aQute.bnd.build.model.*;
4
5public 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));
Stuart McCulloch2a0afd62012-09-06 18:28:06 +000026 result.append(BndEditModel.NEWLINE_LINE_SEPARATOR);
Stuart McCulloch669423b2012-06-26 16:34:24 +000027 position = newlineIndex + 1;
28 }
29
30 return result.toString();
31 }
32
33}