2009-07-21 04:39:18 -04:00
|
|
|
/*
|
|
|
|
* DavMail POP/IMAP/SMTP/CalDav/LDAP Exchange Gateway
|
|
|
|
* Copyright (C) 2009 Mickael Guessant
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
2009-01-04 09:21:50 -05:00
|
|
|
package davmail.exchange;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ICS String writer.
|
|
|
|
* split lines longer than 75 characters
|
|
|
|
*/
|
|
|
|
public class ICSBufferedWriter {
|
2009-02-24 06:53:02 -05:00
|
|
|
final StringBuilder buffer = new StringBuilder();
|
2009-01-04 09:21:50 -05:00
|
|
|
|
2009-11-02 17:56:08 -05:00
|
|
|
/**
|
|
|
|
* Write content to buffer, do not split lines.
|
|
|
|
*
|
|
|
|
* @param content ics content
|
|
|
|
*/
|
|
|
|
public void write(String content) {
|
|
|
|
if (content != null) {
|
|
|
|
buffer.append(content);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-13 17:04:46 -04:00
|
|
|
/**
|
|
|
|
* Write line to buffer, split lines at 75 characters.
|
2009-09-21 17:34:13 -04:00
|
|
|
*
|
2009-08-13 17:04:46 -04:00
|
|
|
* @param line ics event line
|
|
|
|
*/
|
2009-01-04 09:21:50 -05:00
|
|
|
public void writeLine(String line) {
|
2010-09-24 04:44:13 -04:00
|
|
|
writeLine(line, false);
|
2010-07-24 11:53:06 -04:00
|
|
|
}
|
|
|
|
|
2010-09-24 04:44:13 -04:00
|
|
|
/**
|
|
|
|
* Write line with or without continuation prefix.
|
|
|
|
*
|
|
|
|
* @param line line content
|
|
|
|
* @param prefix continuation flag
|
|
|
|
*/
|
2010-07-24 11:53:06 -04:00
|
|
|
public void writeLine(String line, boolean prefix) {
|
|
|
|
int maxLength = 77;
|
|
|
|
if (prefix) {
|
|
|
|
maxLength--;
|
2009-01-04 09:21:50 -05:00
|
|
|
buffer.append(' ');
|
2010-07-24 11:53:06 -04:00
|
|
|
}
|
|
|
|
if (line.length() > maxLength) {
|
|
|
|
buffer.append(line.substring(0, maxLength));
|
|
|
|
newLine();
|
|
|
|
writeLine(line.substring(maxLength), true);
|
2009-01-04 09:21:50 -05:00
|
|
|
} else {
|
|
|
|
buffer.append(line);
|
2010-05-13 08:36:45 -04:00
|
|
|
newLine();
|
2009-01-04 09:21:50 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-27 05:37:59 -04:00
|
|
|
/**
|
|
|
|
* Append CRLF.
|
|
|
|
*/
|
2010-06-16 09:30:36 -04:00
|
|
|
public void newLine() {
|
2009-01-04 09:21:50 -05:00
|
|
|
buffer.append((char) 13).append((char) 10);
|
|
|
|
}
|
|
|
|
|
2009-08-13 17:04:46 -04:00
|
|
|
/**
|
|
|
|
* Get buffer as String
|
2009-09-21 17:34:13 -04:00
|
|
|
*
|
2009-08-13 17:04:46 -04:00
|
|
|
* @return ICS content as String
|
|
|
|
*/
|
2009-01-04 09:21:50 -05:00
|
|
|
@Override
|
|
|
|
public String toString() {
|
2009-09-21 17:34:13 -04:00
|
|
|
return buffer.toString();
|
2009-01-04 09:21:50 -05:00
|
|
|
}
|
|
|
|
|
2011-06-20 06:27:07 -04:00
|
|
|
/**
|
|
|
|
* Append single value property
|
|
|
|
*
|
|
|
|
* @param propertyName property name
|
|
|
|
* @param propertyValue property value
|
|
|
|
*/
|
|
|
|
public void appendProperty(String propertyName, String propertyValue) {
|
|
|
|
if ((propertyValue != null) && (propertyValue.length() > 0)) {
|
|
|
|
StringBuilder lineBuffer = new StringBuilder();
|
|
|
|
lineBuffer.append(propertyName);
|
|
|
|
lineBuffer.append(':');
|
|
|
|
appendMultilineEncodedValue(lineBuffer, propertyValue);
|
|
|
|
writeLine(lineBuffer.toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Append and encode \n to \\n in value.
|
|
|
|
*
|
|
|
|
* @param buffer line buffer
|
|
|
|
* @param value value
|
|
|
|
*/
|
|
|
|
protected void appendMultilineEncodedValue(StringBuilder buffer, String value) {
|
|
|
|
for (int i = 0; i < value.length(); i++) {
|
|
|
|
char c = value.charAt(i);
|
|
|
|
if (c == '\n') {
|
|
|
|
buffer.append("\\n");
|
2012-02-22 17:47:37 -05:00
|
|
|
// skip carriage return
|
|
|
|
} else if (c != '\r') {
|
2011-06-20 06:27:07 -04:00
|
|
|
buffer.append(value.charAt(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-04 09:21:50 -05:00
|
|
|
}
|