From 9a8323b26bbd69345210626b7071c36725eb52ae Mon Sep 17 00:00:00 2001 From: mguessan Date: Tue, 13 Jul 2010 07:53:39 +0000 Subject: [PATCH] Carddav: decode multiline values git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@1181 3d1905a2-6b24-0410-a738-b14d5a86fcbd --- .../davmail/exchange/ExchangeSession.java | 44 ++++++++++++++++--- src/java/davmail/exchange/VCardReader.java | 5 --- 2 files changed, 39 insertions(+), 10 deletions(-) diff --git a/src/java/davmail/exchange/ExchangeSession.java b/src/java/davmail/exchange/ExchangeSession.java index ff85f26f..e1c56b57 100644 --- a/src/java/davmail/exchange/ExchangeSession.java +++ b/src/java/davmail/exchange/ExchangeSession.java @@ -648,7 +648,7 @@ public abstract class ExchangeSession { protected MultiCondition(Operator operator, Condition... conditions) { this.operator = operator; this.conditions = new ArrayList(); - for (Condition condition:conditions) { + for (Condition condition : conditions) { if (condition != null) { this.conditions.add(condition); } @@ -914,12 +914,12 @@ public abstract class ExchangeSession { // remove visible recipients from list Set visibleRecipients = new HashSet(); Address[] recipients = mimeMessage.getAllRecipients(); - for (Address address:recipients) { + for (Address address : recipients) { visibleRecipients.add(address.toString()); } for (String recipient : rcptToRecipients) { if (!visibleRecipients.contains(recipient)) { - mimeMessage.addRecipient(javax.mail.Message.RecipientType.BCC, new InternetAddress(recipient)); + mimeMessage.addRecipient(javax.mail.Message.RecipientType.BCC, new InternetAddress(recipient)); } } ByteArrayOutputStream baos = new ByteArrayOutputStream(); @@ -2001,7 +2001,7 @@ public abstract class ExchangeSession { result.writeLine(line); // append missing method if (method == null) { - result.writeLine("METHOD:PUBLISH"); + result.writeLine("METHOD:PUBLISH"); } continue; } @@ -2682,7 +2682,41 @@ public abstract class ExchangeSession { protected ItemResult createOrUpdateContact(String folderPath, String itemName, String itemBody, String etag, String noneMatch) throws IOException { // parse VCARD body to build contact property map - Map properties = new HashMap(); + Map properties = new HashMap() { + + @Override + public String put(String key, String value) { + String currentValue = get(key); + super.put(key, decodeValue(key, value)); + return currentValue; + } + + private String decodeValue(String key, String value) { + if (value == null || (value.indexOf('\\') < 0 && value.indexOf(',') < 0)) { + return value; + } else { + // decode value + StringBuilder decodedValue = new StringBuilder(); + for (int i = 0; i < value.length(); i++) { + char c = value.charAt(i); + if (c == '\\') { + //noinspection AssignmentToForLoopParameter + c = value.charAt(++i); + if (c == 'n') { + c = '\n'; + } else if (c == 'r') { + c = '\r'; + } + } else if (c == ',' && !"description".equals(key)) { + // convert multiple values to multiline values (e.g. street) + c = '\n'; + } + decodedValue.append(c); + } + return decodedValue.toString(); + } + } + }; properties.put("outlookmessageclass", "IPM.Contact"); VCardReader reader = new VCardReader(new StringReader(itemBody)); diff --git a/src/java/davmail/exchange/VCardReader.java b/src/java/davmail/exchange/VCardReader.java index 3cf3cf62..0a67f167 100644 --- a/src/java/davmail/exchange/VCardReader.java +++ b/src/java/davmail/exchange/VCardReader.java @@ -176,12 +176,7 @@ public class VCardReader extends ICSBufferedReader { if (currentChar == ';') { property.addValue(line.substring(startIndex, i)); startIndex = i + 1; - } else if (currentChar == '\\') { - state = State.BACKSLASH; } - // BACKSLASH state - } else { - state = State.VALUE; } } property.addValue(line.substring(startIndex));