View Javadoc
1   /*
2    * DavMail POP/IMAP/SMTP/CalDav/LDAP Exchange Gateway
3    * Copyright (C) 2009  Mickael Guessant
4    *
5    * This program is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU General Public License
7    * as published by the Free Software Foundation; either version 2
8    * of the License, or (at your option) any later version.
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with this program; if not, write to the Free Software
17   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18   */
19  package davmail.exchange;
20  
21  /**
22   * ICS String writer.
23   * split lines longer than 75 characters
24   */
25  public class ICSBufferedWriter {
26      final StringBuilder buffer = new StringBuilder();
27  
28      /**
29       * Write content to buffer, do not split lines.
30       *
31       * @param content ics content
32       */
33      public void write(String content) {
34          if (content != null) {
35              buffer.append(content);
36          }
37      }
38  
39      /**
40       * Write line to buffer, split lines at 75 characters.
41       *
42       * @param line ics event line
43       */
44      public void writeLine(String line) {
45          int maxLength = 77;
46          if (line.length() <= maxLength) {
47              buffer.append(line);
48              newLine();
49              return;
50          }
51  
52          // first chunk, no space prefix
53          buffer.append(line, 0, maxLength);
54          newLine();
55          int offset = maxLength;
56  
57          // continuation lines, prefixed with space
58          maxLength--;
59          while (offset < line.length()) {
60              buffer.append(' ');
61              int end = Math.min(offset + maxLength, line.length());
62              buffer.append(line, offset, end);
63              newLine();
64              offset = end;
65          }
66      }
67  
68      /**
69       * Append CRLF.
70       */
71      public void newLine() {
72          buffer.append((char) 13).append((char) 10);
73      }
74  
75      /**
76       * Get buffer as String
77       *
78       * @return ICS content as String
79       */
80      @Override
81      public String toString() {
82          return buffer.toString();
83      }
84  
85      /**
86       * Append single value property
87       *
88       * @param propertyName  property name
89       * @param propertyValue property value
90       */
91      public void appendProperty(String propertyName, String propertyValue) {
92          if ((propertyValue != null) && (!propertyValue.isEmpty())) {
93              StringBuilder lineBuffer = new StringBuilder();
94              lineBuffer.append(propertyName);
95              lineBuffer.append(':');
96              appendMultilineEncodedValue(lineBuffer, propertyValue);
97              writeLine(lineBuffer.toString());
98          }
99  
100     }
101 
102     /**
103      * Append and encode \n to \\n in value.
104      *
105      * @param buffer line buffer
106      * @param value  value
107      */
108     protected void appendMultilineEncodedValue(StringBuilder buffer, String value) {
109         for (int i = 0; i < value.length(); i++) {
110             char c = value.charAt(i);
111             if (c == '\n') {
112                 buffer.append("\\n");
113             // skip carriage return
114             } else if (c != '\r') {
115                 buffer.append(value.charAt(i));
116             }
117         }
118     }
119 
120 }