Commit | Line | Data |
---|---|---|
a3b510ab NR |
1 | package be.nikiroo.jvcard; |
2 | ||
26d2bd05 NR |
3 | import java.io.File; |
4 | import java.io.IOException; | |
5 | import java.util.ArrayList; | |
a3b510ab NR |
6 | import java.util.HashMap; |
7 | import java.util.LinkedList; | |
8 | import java.util.List; | |
9 | import java.util.Map; | |
e253bd50 | 10 | import java.util.UUID; |
a3b510ab NR |
11 | |
12 | import be.nikiroo.jvcard.parsers.Format; | |
13 | import be.nikiroo.jvcard.parsers.Parser; | |
7da41ecd | 14 | import be.nikiroo.jvcard.resources.StringUtils; |
a3b510ab NR |
15 | |
16 | /** | |
17 | * A contact is the information that represent a contact person or organisation. | |
18 | * | |
19 | * @author niki | |
20 | * | |
21 | */ | |
26d2bd05 | 22 | public class Contact extends BaseClass<Data> { |
a3b510ab NR |
23 | private int nextBKey = 1; |
24 | private Map<Integer, Data> binaries; | |
a3b510ab NR |
25 | |
26 | /** | |
27 | * Create a new Contact from the given information. Note that the BKeys data | |
28 | * will be reset. | |
29 | * | |
30 | * @param content | |
31 | * the information about the contact | |
32 | */ | |
33 | public Contact(List<Data> content) { | |
26d2bd05 | 34 | super(load(content)); |
a3b510ab NR |
35 | updateBKeys(true); |
36 | } | |
37 | ||
a3b510ab NR |
38 | /** |
39 | * Return the preferred Data field with the given name, or NULL if none. | |
40 | * | |
41 | * @param name | |
42 | * the name to look for | |
43 | * @return the Data field, or NULL | |
44 | */ | |
45 | public Data getPreferredData(String name) { | |
46 | Data first = null; | |
47 | for (Data data : getData(name)) { | |
48 | if (first == null) | |
49 | first = data; | |
3634193b NR |
50 | |
51 | if (data.isPreferred()) | |
52 | return data; | |
a3b510ab NR |
53 | } |
54 | ||
55 | return first; | |
56 | } | |
57 | ||
58 | /** | |
59 | * Return the value of the preferred data field with this name, or NULL if | |
60 | * none (you cannot differentiate a NULL value and no value). | |
61 | * | |
62 | * @param name | |
63 | * the name to look for | |
64 | * @return the value (which can be NULL), or NULL | |
65 | */ | |
66 | public String getPreferredDataValue(String name) { | |
67 | Data data = getPreferredData(name); | |
68 | if (data != null && data.getValue() != null) | |
69 | return data.getValue().trim(); | |
70 | return null; | |
71 | } | |
72 | ||
73 | /** | |
74 | * Get the Data fields that share the given name. | |
75 | * | |
76 | * @param name | |
77 | * the name to ook for | |
78 | * @return a list of Data fields with this name | |
79 | */ | |
80 | public List<Data> getData(String name) { | |
81 | List<Data> found = new LinkedList<Data>(); | |
82 | ||
26d2bd05 | 83 | for (Data data : this) { |
a3b510ab NR |
84 | if (data.getName().equals(name)) |
85 | found.add(data); | |
86 | } | |
87 | ||
88 | return found; | |
89 | } | |
90 | ||
91 | /** | |
92 | * Return a {@link String} representation of this contact. | |
93 | * | |
94 | * @param format | |
95 | * the {@link Format} to use | |
96 | * @param startingBKey | |
97 | * the starting BKey or -1 for no BKeys | |
98 | * @return the {@link String} representation | |
99 | */ | |
100 | public String toString(Format format, int startingBKey) { | |
101 | updateBKeys(false); | |
cf77cb35 NR |
102 | |
103 | StringBuilder builder = new StringBuilder(); | |
104 | for (String line : Parser.toStrings(this, format, startingBKey)) { | |
105 | builder.append(line); | |
106 | builder.append("\r\n"); | |
107 | } | |
108 | ||
109 | return builder.toString(); | |
a3b510ab NR |
110 | } |
111 | ||
0b0b2b0f NR |
112 | /** |
113 | * Return a {@link String} representation of this contact formated | |
114 | * accordingly to the given format. | |
115 | * | |
26d254a3 | 116 | * <p> |
0b0b2b0f NR |
117 | * The format is basically a list of field names separated by a pipe and |
118 | * optionally parametrised. The parameters allows you to: | |
119 | * <ul> | |
120 | * <li>@x: show only a present/not present info</li> | |
121 | * <li>@n: limit the size to a fixed value 'n'</li> | |
122 | * <li>@+: expand the size of this field as much as possible</li> | |
123 | * </ul> | |
26d254a3 | 124 | * </p> |
0b0b2b0f | 125 | * |
26d254a3 NR |
126 | * <p> |
127 | * You can also add a fixed text if it starts with a simple-quote ('). | |
128 | * </p> | |
129 | * | |
130 | * <p> | |
131 | * Example: "'Contact: |N@10|FN@20|NICK@+|PHOTO@x" | |
132 | * </p> | |
0b0b2b0f NR |
133 | * |
134 | * @param format | |
135 | * the format to use | |
26d254a3 NR |
136 | * @param separator |
137 | * the separator {@link String} to use between fields | |
0b0b2b0f NR |
138 | * |
139 | * @return the {@link String} representation | |
140 | */ | |
26d254a3 NR |
141 | public String toString(String format, String separator) { |
142 | return toString(format, separator, null, -1, true, false); | |
0b0b2b0f NR |
143 | } |
144 | ||
a3b510ab NR |
145 | /** |
146 | * Return a {@link String} representation of this contact formated | |
147 | * accordingly to the given format. | |
148 | * | |
26d254a3 | 149 | * <p> |
a3b510ab NR |
150 | * The format is basically a list of field names separated by a pipe and |
151 | * optionally parametrised. The parameters allows you to: | |
152 | * <ul> | |
26d254a3 | 153 | * <li>@x: show only a present/not present info</li> |
a3b510ab NR |
154 | * <li>@n: limit the size to a fixed value 'n'</li> |
155 | * <li>@+: expand the size of this field as much as possible</li> | |
156 | * </ul> | |
26d254a3 NR |
157 | * </p> |
158 | * | |
159 | * <p> | |
160 | * You can also add a fixed text if it starts with a simple-quote ('). | |
161 | * </p> | |
a3b510ab | 162 | * |
26d254a3 NR |
163 | * <p> |
164 | * Example: "'Contact: |N@10|FN@20|NICK@+|PHOTO@x" | |
165 | * </p> | |
a3b510ab NR |
166 | * |
167 | * @param format | |
168 | * the format to use | |
169 | * @param separator | |
170 | * the separator {@link String} to use between fields | |
0b0b2b0f NR |
171 | * @param padding |
172 | * the {@link String} to use for left and right padding | |
a3b510ab NR |
173 | * @param width |
174 | * a fixed width or -1 for "as long as needed" | |
175 | * | |
296a0b75 NR |
176 | * @param unicode |
177 | * allow Uniode or only ASCII characters | |
178 | * | |
a3b510ab NR |
179 | * @return the {@link String} representation |
180 | */ | |
0b0b2b0f | 181 | public String toString(String format, String separator, String padding, |
296a0b75 | 182 | int width, boolean unicode, boolean removeAccents) { |
9c8baf0c | 183 | StringBuilder builder = new StringBuilder(); |
a3b510ab | 184 | |
296a0b75 NR |
185 | for (String str : toStringArray(format, separator, padding, width, |
186 | unicode)) { | |
0b0b2b0f NR |
187 | builder.append(str); |
188 | } | |
a3b510ab | 189 | |
0b0b2b0f NR |
190 | return builder.toString(); |
191 | } | |
a3b510ab | 192 | |
0b0b2b0f NR |
193 | /** |
194 | * Return a {@link String} representation of this contact formated | |
195 | * accordingly to the given format, part by part. | |
196 | * | |
26d254a3 | 197 | * <p> |
0b0b2b0f NR |
198 | * The format is basically a list of field names separated by a pipe and |
199 | * optionally parametrised. The parameters allows you to: | |
200 | * <ul> | |
201 | * <li>@x: show only a present/not present info</li> | |
202 | * <li>@n: limit the size to a fixed value 'n'</li> | |
203 | * <li>@+: expand the size of this field as much as possible</li> | |
204 | * </ul> | |
26d254a3 | 205 | * </p> |
0b0b2b0f | 206 | * |
26d254a3 NR |
207 | * <p> |
208 | * You can also add a fixed text if it starts with a simple-quote ('). | |
209 | * </p> | |
210 | * | |
211 | * <p> | |
212 | * Example: "'Contact: |N@10|FN@20|NICK@+|PHOTO@x" | |
213 | * </p> | |
0b0b2b0f NR |
214 | * |
215 | * @param format | |
216 | * the format to use | |
217 | * @param separator | |
218 | * the separator {@link String} to use between fields | |
219 | * @param padding | |
220 | * the {@link String} to use for left and right padding | |
221 | * @param width | |
222 | * a fixed width or -1 for "as long as needed" | |
223 | * | |
296a0b75 NR |
224 | * @param unicode |
225 | * allow Uniode or only ASCII characters | |
226 | * | |
0b0b2b0f NR |
227 | * @return the {@link String} representation |
228 | */ | |
229 | public String[] toStringArray(String format, String separator, | |
296a0b75 | 230 | String padding, int width, boolean unicode) { |
0b0b2b0f NR |
231 | if (width > -1) { |
232 | int numOfFields = format.split("\\|").length; | |
233 | if (separator != null) | |
234 | width -= (numOfFields - 1) * separator.length(); | |
235 | if (padding != null) | |
236 | width -= (numOfFields) * (2 * padding.length()); | |
237 | ||
238 | if (width < 0) | |
239 | width = 0; | |
a3b510ab NR |
240 | } |
241 | ||
0b0b2b0f NR |
242 | List<String> str = new LinkedList<String>(); |
243 | ||
244 | boolean first = true; | |
296a0b75 | 245 | for (String s : toStringArray(format, width, unicode)) { |
0b0b2b0f NR |
246 | if (!first) { |
247 | str.add(separator); | |
248 | } | |
249 | ||
250 | if (padding != null) | |
251 | str.add(padding + s + padding); | |
252 | else | |
253 | str.add(s); | |
254 | ||
255 | first = false; | |
9c8baf0c NR |
256 | } |
257 | ||
0b0b2b0f | 258 | return str.toArray(new String[] {}); |
9c8baf0c NR |
259 | } |
260 | ||
261 | /** | |
262 | * Return a {@link String} representation of this contact formated | |
263 | * accordingly to the given format, part by part. | |
264 | * | |
26d254a3 | 265 | * <p> |
9c8baf0c | 266 | * The format is basically a list of field names separated by a pipe and |
0b0b2b0f NR |
267 | * optionally parametrised. The parameters allows you to: |
268 | * <ul> | |
269 | * <li>@x: show only a present/not present info</li> | |
270 | * <li>@n: limit the size to a fixed value 'n'</li> | |
271 | * <li>@+: expand the size of this field as much as possible</li> | |
272 | * </ul> | |
26d254a3 NR |
273 | * </p> |
274 | * | |
275 | * <p> | |
276 | * You can also add a fixed text if it starts with a simple-quote ('). | |
277 | * </p> | |
0b0b2b0f | 278 | * |
26d254a3 NR |
279 | * <p> |
280 | * Example: "'Contact: |N@10|FN@20|NICK@+|PHOTO@x" | |
281 | * </p> | |
9c8baf0c NR |
282 | * |
283 | * @param format | |
284 | * the format to use | |
285 | * @param width | |
286 | * a fixed width or -1 for "as long as needed" | |
296a0b75 NR |
287 | * @param unicode |
288 | * allow Uniode or only ASCII characters | |
289 | * | |
9c8baf0c NR |
290 | * @return the {@link String} representation |
291 | */ | |
296a0b75 | 292 | public String[] toStringArray(String format, int width, boolean unicode) { |
9c8baf0c NR |
293 | List<String> str = new LinkedList<String>(); |
294 | ||
295 | String[] formatFields = format.split("\\|"); | |
296 | String[] values = new String[formatFields.length]; | |
297 | Boolean[] expandedFields = new Boolean[formatFields.length]; | |
298 | Boolean[] fixedsizeFields = new Boolean[formatFields.length]; | |
299 | int numOfFieldsToExpand = 0; | |
300 | int totalSize = 0; | |
301 | ||
302 | if (width == 0) { | |
0b0b2b0f NR |
303 | for (int i = 0; i < formatFields.length; i++) { |
304 | str.add(""); | |
305 | } | |
bcb54330 | 306 | |
9c8baf0c NR |
307 | return str.toArray(new String[] {}); |
308 | } | |
309 | ||
a3b510ab NR |
310 | for (int i = 0; i < formatFields.length; i++) { |
311 | String field = formatFields[i]; | |
312 | ||
313 | int size = -1; | |
314 | boolean binary = false; | |
315 | boolean expand = false; | |
316 | ||
26d254a3 NR |
317 | if (field.length() > 0 && field.charAt(0) != '\'' |
318 | && field.contains("@")) { | |
a3b510ab NR |
319 | String[] opts = field.split("@"); |
320 | if (opts.length > 0) | |
321 | field = opts[0]; | |
322 | for (int io = 1; io < opts.length; io++) { | |
323 | String opt = opts[io]; | |
324 | if (opt.equals("x")) { | |
325 | binary = true; | |
326 | } else if (opt.equals("+")) { | |
327 | expand = true; | |
328 | numOfFieldsToExpand++; | |
329 | } else { | |
330 | try { | |
331 | size = Integer.parseInt(opt); | |
332 | } catch (Exception e) { | |
333 | } | |
334 | } | |
335 | } | |
336 | } | |
337 | ||
26d254a3 NR |
338 | String value = null; |
339 | if (field.length() > 0 && field.charAt(0) == '\'') { | |
340 | value = field.substring(1); | |
341 | } else { | |
342 | value = getPreferredDataValue(field); | |
343 | } | |
344 | ||
296a0b75 | 345 | if (value == null) { |
a3b510ab | 346 | value = ""; |
296a0b75 NR |
347 | } else { |
348 | value = StringUtils.sanitize(value, unicode); | |
349 | } | |
a3b510ab NR |
350 | |
351 | if (size > -1) { | |
296a0b75 | 352 | value = StringUtils.padString(value, size); |
a3b510ab NR |
353 | } |
354 | ||
355 | expandedFields[i] = expand; | |
356 | fixedsizeFields[i] = (size > -1); | |
357 | ||
358 | if (binary) { | |
359 | if (value != null && !value.equals("")) | |
360 | values[i] = "x"; | |
361 | else | |
362 | values[i] = " "; | |
363 | totalSize++; | |
364 | } else { | |
365 | values[i] = value; | |
366 | totalSize += value.length(); | |
367 | } | |
368 | } | |
9c8baf0c | 369 | |
a3b510ab NR |
370 | if (width > -1 && totalSize > width) { |
371 | int toDo = totalSize - width; | |
372 | for (int i = fixedsizeFields.length - 1; toDo > 0 && i >= 0; i--) { | |
373 | if (!fixedsizeFields[i]) { | |
374 | int valueLength = values[i].length(); | |
375 | if (valueLength > 0) { | |
376 | if (valueLength >= toDo) { | |
377 | values[i] = values[i].substring(0, valueLength | |
378 | - toDo); | |
379 | toDo = 0; | |
380 | } else { | |
381 | values[i] = ""; | |
382 | toDo -= valueLength; | |
383 | } | |
384 | } | |
385 | } | |
386 | } | |
387 | ||
388 | totalSize = width + toDo; | |
389 | } | |
9c8baf0c | 390 | |
a3b510ab NR |
391 | if (width > -1 && numOfFieldsToExpand > 0) { |
392 | int availablePadding = width - totalSize; | |
393 | ||
394 | if (availablePadding > 0) { | |
395 | int padPerItem = availablePadding / numOfFieldsToExpand; | |
396 | int remainder = availablePadding % numOfFieldsToExpand; | |
397 | ||
398 | for (int i = 0; i < values.length; i++) { | |
399 | if (expandedFields[i]) { | |
400 | if (remainder > 0) { | |
296a0b75 NR |
401 | values[i] = values[i] |
402 | + StringUtils.padString("", remainder); | |
a3b510ab NR |
403 | remainder = 0; |
404 | } | |
405 | if (padPerItem > 0) { | |
296a0b75 NR |
406 | values[i] = values[i] |
407 | + StringUtils.padString("", padPerItem); | |
a3b510ab NR |
408 | } |
409 | } | |
410 | } | |
411 | ||
412 | totalSize = width; | |
413 | } | |
414 | } | |
a3b510ab | 415 | |
9c8baf0c NR |
416 | int currentSize = 0; |
417 | for (int i = 0; i < values.length; i++) { | |
418 | currentSize += addToList(str, values[i], currentSize, width); | |
a3b510ab NR |
419 | } |
420 | ||
9c8baf0c | 421 | return str.toArray(new String[] {}); |
a3b510ab NR |
422 | } |
423 | ||
a3b510ab NR |
424 | /** |
425 | * Update the information from this contact with the information in the | |
426 | * given contact. Non present fields will be removed, new fields will be | |
427 | * added, BKey'ed fields will be completed with the binary information known | |
428 | * by this contact. | |
429 | * | |
430 | * @param vc | |
431 | * the contact with the newer information and optional BKeys | |
432 | */ | |
433 | public void updateFrom(Contact vc) { | |
434 | updateBKeys(false); | |
435 | ||
26d2bd05 | 436 | List<Data> newDatas = new LinkedList<Data>(vc); |
a3b510ab NR |
437 | for (int i = 0; i < newDatas.size(); i++) { |
438 | Data data = newDatas.get(i); | |
439 | int bkey = Parser.getBKey(data); | |
440 | if (bkey >= 0) { | |
441 | if (binaries.containsKey(bkey)) { | |
442 | newDatas.set(i, binaries.get(bkey)); | |
443 | } | |
444 | } | |
445 | } | |
446 | ||
26d2bd05 | 447 | replaceListContent(newDatas); |
a3b510ab | 448 | this.nextBKey = vc.nextBKey; |
78e4af97 NR |
449 | } |
450 | ||
e253bd50 NR |
451 | @Override |
452 | public String getId() { | |
453 | return "" + getPreferredDataValue("UID"); | |
454 | } | |
455 | ||
456 | @Override | |
457 | public String getState() { | |
e4444b0b | 458 | return getId(); |
e253bd50 NR |
459 | } |
460 | ||
78e4af97 NR |
461 | /** |
462 | * Return a {@link String} representation of this contact, in vCard 2.1, | |
463 | * without BKeys. | |
464 | * | |
465 | * @return the {@link String} representation | |
466 | */ | |
467 | @Override | |
468 | public String toString() { | |
469 | return toString(Format.VCard21, -1); | |
470 | } | |
471 | ||
a3b510ab NR |
472 | /** |
473 | * Mark all the binary fields with a BKey number. | |
474 | * | |
475 | * @param force | |
476 | * force the marking, and reset all the numbers. | |
477 | */ | |
478 | protected void updateBKeys(boolean force) { | |
479 | if (force) { | |
480 | binaries = new HashMap<Integer, Data>(); | |
481 | nextBKey = 1; | |
482 | } | |
483 | ||
484 | if (binaries == null) { | |
485 | binaries = new HashMap<Integer, Data>(); | |
486 | } | |
487 | ||
26d2bd05 | 488 | for (Data data : this) { |
a3b510ab NR |
489 | if (data.isBinary() && (data.getB64Key() <= 0 || force)) { |
490 | binaries.put(nextBKey, data); | |
491 | data.resetB64Key(nextBKey++); | |
492 | } | |
493 | } | |
494 | } | |
495 | ||
a3b510ab | 496 | /** |
26d2bd05 NR |
497 | * Load the data from the given {@link File} under the given {@link Format}. |
498 | * | |
499 | * @param file | |
500 | * the {@link File} to load from | |
501 | * @param format | |
502 | * the {@link Format} to load as | |
503 | * | |
504 | * @return the list of elements | |
505 | * @throws IOException | |
506 | * in case of IO error | |
a3b510ab | 507 | */ |
26d2bd05 NR |
508 | static private List<Data> load(List<Data> content) { |
509 | List<Data> datas = new ArrayList<Data>(); | |
a3b510ab | 510 | |
26d2bd05 NR |
511 | boolean fn = false; |
512 | boolean n = false; | |
e253bd50 | 513 | boolean uid = false; |
26d2bd05 NR |
514 | if (content != null) { |
515 | for (Data data : content) { | |
516 | if (data.getName().equals("N")) { | |
517 | n = true; | |
518 | } else if (data.getName().equals("FN")) { | |
519 | fn = true; | |
e253bd50 NR |
520 | } else if (data.getName().equals("UID")) { |
521 | uid = true; | |
26d2bd05 NR |
522 | } |
523 | ||
524 | if (!data.getName().equals("VERSION")) { | |
525 | datas.add(data); | |
526 | } | |
527 | } | |
78e4af97 | 528 | } |
78e4af97 | 529 | |
26d2bd05 | 530 | // required fields: |
e253bd50 | 531 | if (!n) // required since vCard 3.0, supported in 2.1 |
26d2bd05 | 532 | datas.add(new Data(null, "N", "", null)); |
e253bd50 | 533 | if (!fn) // not required anymore but still supported in 4.0 |
26d2bd05 | 534 | datas.add(new Data(null, "FN", "", null)); |
e253bd50 NR |
535 | if (!uid) // supported by vCard, required by this program |
536 | datas.add(new Data(null, "UID", UUID.randomUUID().toString(), null)); | |
26d2bd05 NR |
537 | |
538 | return datas; | |
a3b510ab | 539 | } |
296a0b75 | 540 | |
bcb54330 | 541 | /** |
78e4af97 NR |
542 | * Add a {@link String} to the given {@link List}, but make sure it does not |
543 | * exceed the maximum size, and truncate it if needed to fit. | |
bcb54330 | 544 | * |
78e4af97 NR |
545 | * @param list |
546 | * @param add | |
547 | * @param currentSize | |
548 | * @param maxSize | |
549 | * @return | |
bcb54330 | 550 | */ |
78e4af97 NR |
551 | static private int addToList(List<String> list, String add, |
552 | int currentSize, int maxSize) { | |
553 | if (add == null || add.length() == 0) { | |
554 | if (add != null) | |
555 | list.add(add); | |
556 | return 0; | |
557 | } | |
558 | ||
559 | if (maxSize > -1) { | |
560 | if (currentSize < maxSize) { | |
561 | if (currentSize + add.length() >= maxSize) { | |
562 | add = add.substring(0, maxSize - currentSize); | |
bcb54330 | 563 | } |
78e4af97 NR |
564 | } else { |
565 | add = ""; | |
bcb54330 NR |
566 | } |
567 | } | |
568 | ||
78e4af97 NR |
569 | list.add(add); |
570 | return add.length(); | |
bcb54330 | 571 | } |
a3b510ab | 572 | } |