1 package uk.ac.ebi.intenz.tools.sib.writer;
2
3
4
5
6
7
8
9
10
11 public class DefaultLineFormatter implements LineFormatter {
12
13
14
15
16
17
18
19
20
21
22 public String formatLines(String text, LineType lineType) throws EnzymeFlatFileWriteException {
23 if (text == null || lineType == null) throw new NullPointerException();
24
25 String lineStart = lineType.toString() + " ";
26 int netLineWidth = LINEWIDTH - lineStart.length();
27
28 if(!text.equals("") &&
29 (lineType == LineType.DE || lineType == LineType.AN || lineType == LineType.CA ||
30 lineType == LineType.CF || lineType == LineType.DI) &&
31 !text.endsWith(".")) text += '.';
32
33 StringBuffer wrappedText = new StringBuffer();
34
35
36 if (text.length() <= netLineWidth) {
37 wrappedText.append(lineStart);
38 wrappedText.append(text);
39 return wrappedText.toString();
40 }
41
42 StringBuffer restText = new StringBuffer(text);
43 LineWrapper lineWrapPositioner = LineWrapperFactory.create(text, lineType);
44 int additionalIndent = 0;
45 while (restText.toString().trim().length() > netLineWidth) {
46 int position = lineWrapPositioner.findPosition(restText.toString().trim(), netLineWidth);
47 wrappedText.append(lineStart);
48 if (restText.length() > 0 && restText.charAt(0) == ' ') restText.deleteCharAt(0);
49 String line = restText.substring(0, position);
50 restText.delete(0, position);
51 for (int iii = 0; iii < additionalIndent; iii++)
52 wrappedText.append(" ");
53 wrappedText.append(line);
54 wrappedText.append("\n");
55 }
56
57
58 wrappedText.append(lineStart);
59 if (restText.length() > 0 && restText.charAt(0) == ' ') restText.deleteCharAt(0);
60 for (int iii = 0; iii < additionalIndent; iii++)
61 wrappedText.append(" ");
62 wrappedText.append(restText.toString());
63
64 return wrappedText.toString();
65 }
66 }