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          writeLine(line, false);
46      }
47  
48      /**
49       * Write line with or without continuation prefix.
50       *
51       * @param line   line content
52       * @param prefix continuation flag
53       */
54      public void writeLine(String line, boolean prefix) {
55          int maxLength = 77;
56          if (prefix) {
57              maxLength--;
58              buffer.append(' ');
59          }
60          if (line.length() > maxLength) {
61              buffer.append(line, 0, maxLength);
62              newLine();
63              writeLine(line.substring(maxLength), true);
64          } else {
65              buffer.append(line);
66              newLine();
67          }
68      }
69  
70      /**
71       * Append CRLF.
72       */
73      public void newLine() {
74          buffer.append((char) 13).append((char) 10);
75      }
76  
77      /**
78       * Get buffer as String
79       *
80       * @return ICS content as String
81       */
82      @Override
83      public String toString() {
84          return buffer.toString();
85      }
86  
87      /**
88       * Append single value property
89       *
90       * @param propertyName  property name
91       * @param propertyValue property value
92       */
93      public void appendProperty(String propertyName, String propertyValue) {
94          if ((propertyValue != null) && (propertyValue.length() > 0)) {
95              StringBuilder lineBuffer = new StringBuilder();
96              lineBuffer.append(propertyName);
97              lineBuffer.append(':');
98              appendMultilineEncodedValue(lineBuffer, propertyValue);
99              writeLine(lineBuffer.toString());
100         }
101 
102     }
103 
104     /**
105      * Append and encode \n to \\n in value.
106      *
107      * @param buffer line buffer
108      * @param value  value
109      */
110     protected void appendMultilineEncodedValue(StringBuilder buffer, String value) {
111         for (int i = 0; i < value.length(); i++) {
112             char c = value.charAt(i);
113             if (c == '\n') {
114                 buffer.append("\\n");
115             // skip carriage return
116             } else if (c != '\r') {
117                 buffer.append(value.charAt(i));
118             }
119         }
120     }
121 
122 }