1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package davmail.exchange;
20
21
22
23
24 public class VCardWriter extends ICSBufferedWriter {
25
26
27
28 public void startCard() {
29 startCard(null);
30 }
31
32
33
34
35 public void startCard(String version) {
36 writeLine("BEGIN:VCARD");
37 writeLine("VERSION:"+((version == null)?"4.0":version));
38 }
39
40
41
42
43
44
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
73
74
75
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
95
96 public void endCard() {
97 writeLine("END:VCARD");
98 }
99 }