View Javadoc

1   package uk.ac.ebi.intenz.tools.sib.writer;
2   
3   /**
4    * Provides a default implementation which can be applied to all line types.
5    * <p/>
6    * The only exception is the <code><b>CC</b></code> line, which is an extension of this class.
7    *
8    * @author Michael Darsow
9    * @version $Revision: 1.2 $ $Date: 2008/01/28 11:43:23 $
10   */
11  public class DefaultLineFormatter implements LineFormatter {
12    /**
13     * Formats the text by adding the line headers and wrapping the content using {@link LineWrapper LineWrapper}
14     * implementations.
15     *
16     * @param text The text to be formatted.
17     * @param lineType The {@link LineType line type} .
18     * @return the formatted text.
19     * @throws EnzymeFlatFileWriteException if an error occured during the formatting process.
20     * @throws NullPointerException if any of the parameters is <code>null</code>.
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 += '.'; // Take care of the period.
32  
33      StringBuffer wrappedText = new StringBuffer();
34  
35      // Check if the given text fits into one line.
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);                                                    // Add the line type string.
48        if (restText.length() > 0 && restText.charAt(0) == ' ') restText.deleteCharAt(0); // Ignore leading space.
49        String line = restText.substring(0, position);                                    // Get the line.
50        restText.delete(0, position);                                                     // Remove the line from the text.
51        for (int iii = 0; iii < additionalIndent; iii++)                                  // Insert indent.
52          wrappedText.append(" ");
53        wrappedText.append(line);                                                         // Append line.
54        wrappedText.append("\n");
55      }
56  
57      // Append last line.
58      wrappedText.append(lineStart);
59      if (restText.length() > 0 && restText.charAt(0) == ' ') restText.deleteCharAt(0); // Ignore leading space.
60      for (int iii = 0; iii < additionalIndent; iii++)
61        wrappedText.append(" ");
62      wrappedText.append(restText.toString());
63  
64      return wrappedText.toString();
65    }
66  }