1 package be
.nikiroo
.jvcard
;
4 import java
.io
.IOException
;
5 import java
.util
.ArrayList
;
6 import java
.util
.HashMap
;
7 import java
.util
.LinkedList
;
10 import java
.util
.UUID
;
12 import be
.nikiroo
.jvcard
.parsers
.Format
;
13 import be
.nikiroo
.jvcard
.parsers
.Parser
;
14 import be
.nikiroo
.jvcard
.resources
.StringUtils
;
17 * A contact is the information that represent a contact person or organisation.
22 public class Contact
extends BaseClass
<Data
> {
23 private int nextBKey
= 1;
24 private Map
<Integer
, Data
> binaries
;
27 * Create a new Contact from the given information. Note that the BKeys data
31 * the information about the contact
33 public Contact(List
<Data
> content
) {
39 * Return the preferred Data field with the given name, or NULL if none.
42 * the name to look for
43 * @return the Data field, or NULL
45 public Data
getPreferredData(String name
) {
47 for (Data data
: getData(name
)) {
51 if (data
.isPreferred())
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).
63 * the name to look for
64 * @return the value (which can be NULL), or NULL
66 public String
getPreferredDataValue(String name
) {
67 Data data
= getPreferredData(name
);
68 if (data
!= null && data
.getValue() != null)
69 return data
.getValue().trim();
74 * Get the Data fields that share the given name.
78 * @return a list of Data fields with this name
80 public List
<Data
> getData(String name
) {
81 List
<Data
> found
= new LinkedList
<Data
>();
83 for (Data data
: this) {
84 if (data
.getName().equals(name
))
92 * Return a {@link String} representation of this contact.
95 * the {@link Format} to use
97 * the starting BKey or -1 for no BKeys
98 * @return the {@link String} representation
100 public String
toString(Format format
, int startingBKey
) {
103 StringBuilder builder
= new StringBuilder();
104 for (String line
: Parser
.toStrings(this, format
, startingBKey
)) {
105 builder
.append(line
);
106 builder
.append("\r\n");
109 return builder
.toString();
113 * Return a {@link String} representation of this contact formated
114 * accordingly to the given format.
117 * The format is basically a list of field names separated by a pipe and
118 * optionally parametrised with the 'at' (@) symbol. The parameters allows
121 * <li>@x: show only a present/not present info</li>
122 * <li>@n: limit the size to a fixed value 'n'</li>
123 * <li>@+: expand the size of this field as much as possible</li>
128 * In case of lists or multiple-fields values, you can select a specific
129 * list or field with:
131 * <li>FIELD@(0): select the first value in a list</li>
132 * <li>FIELD@[1]: select the second field in a multiple-fields value</li>
137 * You can also add a fixed text if it starts with a simple-quote (').
141 * Example: "'Contact: |N@10|FN@20|NICK@+|PHOTO@x"
147 * the separator {@link String} to use between fields
149 * @return the {@link String} representation
151 public String
toString(String format
, String separator
) {
152 return toString(format
, separator
, null, -1, true, false);
156 * Return a {@link String} representation of this contact formated
157 * accordingly to the given format.
160 * The format is basically a list of field names separated by a pipe and
161 * optionally parametrised. The parameters allows you to:
163 * <li>@x: show only a present/not present info</li>
164 * <li>@n: limit the size to a fixed value 'n'</li>
165 * <li>@+: expand the size of this field as much as possible</li>
170 * In case of lists or multiple-fields values, you can select a specific
171 * list or field with:
173 * <li>FIELD@(0): select the first value in a list</li>
174 * <li>FIELD@[1]: select the second field in a multiple-fields value</li>
179 * You can also add a fixed text if it starts with a simple-quote (').
183 * Example: "'Contact: |N@10|FN@20|NICK@+|PHOTO@x"
189 * the separator {@link String} to use between fields
191 * the {@link String} to use for left and right padding
193 * a fixed width or -1 for "as long as needed"
196 * allow Uniode or only ASCII characters
198 * @return the {@link String} representation
200 public String
toString(String format
, String separator
, String padding
,
201 int width
, boolean unicode
, boolean removeAccents
) {
202 StringBuilder builder
= new StringBuilder();
204 for (String str
: toStringArray(format
, separator
, padding
, width
,
209 return builder
.toString();
213 * Return a {@link String} representation of this contact formated
214 * accordingly to the given format, part by part.
217 * The format is basically a list of field names separated by a pipe and
218 * optionally parametrised. The parameters allows you to:
220 * <li>@x: show only a present/not present info</li>
221 * <li>@n: limit the size to a fixed value 'n'</li>
222 * <li>@+: expand the size of this field as much as possible</li>
227 * In case of lists or multiple-fields values, you can select a specific
228 * list or field with:
230 * <li>FIELD@(0): select the first value in a list</li>
231 * <li>FIELD@[1]: select the second field in a multiple-fields value</li>
236 * You can also add a fixed text if it starts with a simple-quote (').
240 * Example: "'Contact: |N@10|FN@20|NICK@+|PHOTO@x"
246 * the separator {@link String} to use between fields
248 * the {@link String} to use for left and right padding
250 * a fixed width or -1 for "as long as needed"
253 * allow Uniode or only ASCII characters
255 * @return the {@link String} representation
257 public String
[] toStringArray(String format
, String separator
,
258 String padding
, int width
, boolean unicode
) {
260 int numOfFields
= format
.split("\\|").length
;
261 if (separator
!= null)
262 width
-= (numOfFields
- 1) * separator
.length();
264 width
-= (numOfFields
) * (2 * padding
.length());
270 List
<String
> str
= new LinkedList
<String
>();
272 boolean first
= true;
273 for (String s
: toStringArray(format
, width
, unicode
)) {
279 str
.add(padding
+ s
+ padding
);
286 return str
.toArray(new String
[] {});
290 * Return a {@link String} representation of this contact formated
291 * accordingly to the given format, part by part.
294 * The format is basically a list of field names separated by a pipe and
295 * optionally parametrised. The parameters allows you to:
297 * <li>@x: show only a present/not present info</li>
298 * <li>@n: limit the size to a fixed value 'n'</li>
299 * <li>@+: expand the size of this field as much as possible</li>
304 * In case of lists or multiple-fields values, you can select a specific
305 * list or field with:
307 * <li>FIELD@(0): select the first value in a list</li>
308 * <li>FIELD@[1]: select the second field in a multiple-fields value</li>
313 * You can also add a fixed text if it starts with a simple-quote (').
317 * Example: "'Contact: |N@10|FN@20|NICK@+|PHOTO@x"
323 * a fixed width or -1 for "as long as needed"
325 * allow Uniode or only ASCII characters
327 * @return the {@link String} representation
329 public String
[] toStringArray(String format
, int width
, boolean unicode
) {
330 List
<String
> str
= new LinkedList
<String
>();
332 String
[] formatFields
= format
.split("\\|");
333 String
[] values
= new String
[formatFields
.length
];
334 Boolean
[] expandedFields
= new Boolean
[formatFields
.length
];
335 Boolean
[] fixedsizeFields
= new Boolean
[formatFields
.length
];
336 int numOfFieldsToExpand
= 0;
340 for (int i
= 0; i
< formatFields
.length
; i
++) {
344 return str
.toArray(new String
[] {});
347 for (int i
= 0; i
< formatFields
.length
; i
++) {
348 String field
= formatFields
[i
];
351 boolean binary
= false;
352 boolean expand
= false;
356 if (field
.length() > 0 && field
.charAt(0) != '\''
357 && field
.contains("@")) {
358 String
[] opts
= field
.split("@");
361 for (int io
= 1; io
< opts
.length
; io
++) {
362 String opt
= opts
[io
];
363 if (opt
.equals("x")) {
365 } else if (opt
.equals("+")) {
367 numOfFieldsToExpand
++;
368 } else if (opt
.length() > 0 && opt
.charAt(0) == '(') {
370 opt
= opt
.substring(1, opt
.length() - 1);
371 valueNum
= Integer
.parseInt(opt
);
372 } catch (Exception e
) {
374 } else if (opt
.length() > 0 && opt
.charAt(0) == '[') {
376 opt
= opt
.substring(1, opt
.length() - 1);
377 fieldNum
= Integer
.parseInt(opt
);
378 } catch (Exception e
) {
382 size
= Integer
.parseInt(opt
);
383 } catch (NumberFormatException e
) {
390 if (field
.length() > 0 && field
.charAt(0) == '\'') {
391 value
= field
.substring(1);
392 } else if (valueNum
>= 0) {
393 List
<String
> vv
= getPreferredData(field
).getValues();
394 if (valueNum
< vv
.size()) {
395 value
= vv
.get(valueNum
);
397 } else if (fieldNum
>= 0) {
398 List
<String
> ff
= getPreferredData(field
).getFields();
399 if (fieldNum
< ff
.size()) {
400 value
= ff
.get(fieldNum
);
403 value
= getPreferredDataValue(field
);
409 value
= StringUtils
.sanitize(value
, unicode
);
413 value
= StringUtils
.padString(value
, size
);
416 expandedFields
[i
] = expand
;
417 fixedsizeFields
[i
] = (size
> -1);
420 if (value
!= null && !value
.equals(""))
427 totalSize
+= value
.length();
431 if (width
> -1 && totalSize
> width
) {
432 int toDo
= totalSize
- width
;
433 for (int i
= fixedsizeFields
.length
- 1; toDo
> 0 && i
>= 0; i
--) {
434 if (!fixedsizeFields
[i
]) {
435 int valueLength
= values
[i
].length();
436 if (valueLength
> 0) {
437 if (valueLength
>= toDo
) {
438 values
[i
] = values
[i
].substring(0, valueLength
449 totalSize
= width
+ toDo
;
452 if (width
> -1 && numOfFieldsToExpand
> 0) {
453 int availablePadding
= width
- totalSize
;
455 if (availablePadding
> 0) {
456 int padPerItem
= availablePadding
/ numOfFieldsToExpand
;
457 int remainder
= availablePadding
% numOfFieldsToExpand
;
459 for (int i
= 0; i
< values
.length
; i
++) {
460 if (expandedFields
[i
]) {
462 values
[i
] = values
[i
]
463 + StringUtils
.padString("", remainder
);
466 if (padPerItem
> 0) {
467 values
[i
] = values
[i
]
468 + StringUtils
.padString("", padPerItem
);
478 for (int i
= 0; i
< values
.length
; i
++) {
479 currentSize
+= addToList(str
, values
[i
], currentSize
, width
);
482 return str
.toArray(new String
[] {});
486 * Update the information from this contact with the information in the
487 * given contact. Non present fields will be removed, new fields will be
488 * added, BKey'ed fields will be completed with the binary information known
492 * the contact with the newer information and optional BKeys
494 public void updateFrom(Contact vc
) {
497 List
<Data
> newDatas
= new LinkedList
<Data
>(vc
);
498 for (int i
= 0; i
< newDatas
.size(); i
++) {
499 Data data
= newDatas
.get(i
);
500 int bkey
= Parser
.getBKey(data
);
502 if (binaries
.containsKey(bkey
)) {
503 newDatas
.set(i
, binaries
.get(bkey
));
508 replaceListContent(newDatas
);
509 this.nextBKey
= vc
.nextBKey
;
513 public String
getId() {
514 return "" + getPreferredDataValue("UID");
518 public String
getState() {
523 * Return a {@link String} representation of this contact, in vCard 2.1,
526 * @return the {@link String} representation
529 public String
toString() {
530 return toString(Format
.VCard21
, -1);
534 * Mark all the binary fields with a BKey number.
537 * force the marking, and reset all the numbers.
539 protected void updateBKeys(boolean force
) {
541 binaries
= new HashMap
<Integer
, Data
>();
545 if (binaries
== null) {
546 binaries
= new HashMap
<Integer
, Data
>();
549 for (Data data
: this) {
550 if (data
.isBinary() && (data
.getB64Key() <= 0 || force
)) {
551 binaries
.put(nextBKey
, data
);
552 data
.resetB64Key(nextBKey
++);
558 * Load the data from the given {@link File} under the given {@link Format}.
561 * the {@link File} to load from
563 * the {@link Format} to load as
565 * @return the list of elements
566 * @throws IOException
567 * in case of IO error
569 static private List
<Data
> load(List
<Data
> content
) {
570 List
<Data
> datas
= new ArrayList
<Data
>();
575 if (content
!= null) {
576 for (Data data
: content
) {
577 if (data
.getName().equals("N")) {
579 } else if (data
.getName().equals("FN")) {
581 } else if (data
.getName().equals("UID")) {
585 if (!data
.getName().equals("VERSION")) {
592 if (!n
) // required since vCard 3.0, supported in 2.1
593 datas
.add(new Data(null, "N", "", null));
594 if (!fn
) // not required anymore but still supported in 4.0
595 datas
.add(new Data(null, "FN", "", null));
596 if (!uid
) // supported by vCard, required by this program
597 datas
.add(new Data(null, "UID", UUID
.randomUUID().toString(), null));
603 * Add a {@link String} to the given {@link List}, but make sure it does not
604 * exceed the maximum size, and truncate it if needed to fit.
612 static private int addToList(List
<String
> list
, String add
,
613 int currentSize
, int maxSize
) {
614 if (add
== null || add
.length() == 0) {
621 if (currentSize
< maxSize
) {
622 if (currentSize
+ add
.length() >= maxSize
) {
623 add
= add
.substring(0, maxSize
- currentSize
);