View Javadoc
1   /*
2    * DavMail POP/IMAP/SMTP/CalDav/LDAP Exchange Gateway
3    * Copyright (C) 2010  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   * VCard Writer
23   */
24  public class VCardWriter extends ICSBufferedWriter {
25      /**
26       * Begin VCard and version
27       */
28      public void startCard() {
29          startCard(null);
30      }
31  
32      /**
33       * Begin VCard and version
34       */
35      public void startCard(String version) {
36          writeLine("BEGIN:VCARD");
37          writeLine("VERSION:"+((version == null)?"4.0":version));
38      }
39  
40      /**
41       * Append compound value
42       *
43       * @param propertyName  property name
44       * @param propertyValue property values
45       */
46      public void appendProperty(String propertyName, String... propertyValue) {
47          boolean hasValue = false;
48          for (String value : propertyValue) {
49              if ((value != null) && (value.length() > 0)) {
50                  hasValue = true;
51                  break;
52              }
53          }
54          if (hasValue) {
55              boolean first = true;
56              StringBuilder lineBuffer = new StringBuilder();
57              lineBuffer.append(propertyName);
58              lineBuffer.append(':');
59              for (String value : propertyValue) {
60                  if (first) {
61                      first = false;
62                  } else {
63                      lineBuffer.append(';');
64                  }
65                  appendEncodedValue(lineBuffer, value);
66              }
67              writeLine(lineBuffer.toString());
68          }
69      }
70  
71      /**
72       * Encode and append value to buffer
73       *
74       * @param buffer current buffer
75       * @param value  property value
76       */
77      private void appendEncodedValue(StringBuilder buffer, String value) {
78          if (value != null) {
79              for (int i = 0; i < value.length(); i++) {
80                  char c = value.charAt(i);
81                  if (c == ',' || c == ';') {
82                      buffer.append('\\');
83                  }
84                  if (c == '\n') {
85                      buffer.append("\\n");
86                  } else if (c != '\r') {
87                      buffer.append(value.charAt(i));
88                  }
89              }
90          }
91      }
92  
93      /**
94       * End VCard
95       */
96      public void endCard() {
97          writeLine("END:VCARD");
98      }
99  }