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
.utils
.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, the first one if
40 * none is preferred, or NULL if none at all.
43 * the name to look for
45 * @return the {@link Data} field, or NULL
47 public Data
getPreferredData(String name
) {
49 int ipref
= Integer
.MAX_VALUE
;
50 for (Data data
: getData(name
)) {
54 if (data
.getPreferred() < ipref
)
62 * Return the value of the preferred data field with this name, or NULL if
63 * none (you cannot differentiate a NULL value and no value).
66 * the name to look for
67 * @return the value (which can be NULL), or NULL
69 public String
getPreferredDataValue(String name
) {
70 Data data
= getPreferredData(name
);
71 if (data
!= null && data
.getValue() != null)
72 return data
.getValue().trim();
77 * Get the Data fields that share the given name.
81 * @return a list of Data fields with this name
83 public List
<Data
> getData(String name
) {
84 List
<Data
> found
= new LinkedList
<Data
>();
86 for (Data data
: this) {
87 if (data
.getName().equals(name
))
95 * Return a {@link String} representation of this contact formated
96 * accordingly to the given format.
99 * The format is basically a list of field names separated by a pipe and
100 * optionally parametrised with the 'at' (@) symbol. The parameters allows
103 * <li>@x: show only a present/not present info</li>
104 * <li>@n: limit the size to a fixed value 'n'</li>
105 * <li>@+: expand the size of this field as much as possible</li>
110 * In case of lists or multiple-fields values, you can select a specific
111 * list or field with:
113 * <li>FIELD@(0): select the first value in a list</li>
114 * <li>FIELD@[1]: select the second field in a multiple-fields value</li>
119 * You can also add a fixed text if it starts with a simple-quote (').
123 * Example: "'Contact: |N@10|FN@20|NICK@+|PHOTO@x"
129 * the separator {@link String} to use between fields
131 * @return the {@link String} representation
133 public String
toString(String format
, String separator
) {
134 return toString(format
, separator
, null, -1, true);
138 * Return a {@link String} representation of this contact formated
139 * accordingly to the given format.
142 * The format is basically a list of field names separated by a pipe and
143 * optionally parametrised. The parameters allows you to:
145 * <li>@x: show only a present/not present info</li>
146 * <li>@n: limit the size to a fixed value 'n'</li>
147 * <li>@+: expand the size of this field as much as possible</li>
152 * In case of lists or multiple-fields values, you can select a specific
153 * list or field with:
155 * <li>FIELD@(0): select the first value in a list</li>
156 * <li>FIELD@[1]: select the second field in a multiple-fields value</li>
161 * You can also add a fixed text if it starts with a simple-quote (').
165 * Example: "'Contact: |N@10|FN@20|NICK@+|PHOTO@x"
171 * the separator {@link String} to use between fields
173 * the {@link String} to use for left and right padding
175 * a fixed width or -1 for "as long as needed"
177 * allow Unicode or only ASCII characters
179 * @return the {@link String} representation
181 public String
toString(String format
, String separator
, String padding
,
182 int width
, boolean unicode
) {
183 StringBuilder builder
= new StringBuilder();
185 for (String str
: toStringArray(format
, separator
, padding
, width
,
190 return builder
.toString();
194 * Return a {@link String} representation of this contact formated
195 * accordingly to the given format, part by part.
198 * The format is basically a list of field names separated by a pipe and
199 * optionally parametrised. The parameters allows you to:
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>
208 * In case of lists or multiple-fields values, you can select a specific
209 * list or field with:
211 * <li>FIELD@(0): select the first value in a list</li>
212 * <li>FIELD@[1]: select the second field in a multiple-fields value</li>
217 * You can also add a fixed text if it starts with a simple-quote (').
221 * Example: "'Contact: |N@10|FN@20|NICK@+|PHOTO@x"
227 * the separator {@link String} to use between fields
229 * the {@link String} to use for left and right padding
231 * a fixed width or -1 for "as long as needed"
234 * allow Uniode or only ASCII characters
236 * @return the {@link String} representation
238 public String
[] toStringArray(String format
, String separator
,
239 String padding
, int width
, boolean unicode
) {
241 int numOfFields
= format
.split("\\|").length
;
242 if (separator
!= null)
243 width
-= (numOfFields
- 1) * separator
.length();
245 width
-= (numOfFields
) * (2 * padding
.length());
251 List
<String
> str
= new LinkedList
<String
>();
253 boolean first
= true;
254 for (String s
: toStringArray(format
, width
, unicode
)) {
260 str
.add(padding
+ s
+ padding
);
267 return str
.toArray(new String
[] {});
271 * Return a {@link String} representation of this contact formated
272 * accordingly to the given format, part by part.
275 * The format is basically a list of field names separated by a pipe and
276 * optionally parametrised. The parameters allows you to:
278 * <li>@x: show only a present/not present info</li>
279 * <li>@n: limit the size to a fixed value 'n'</li>
280 * <li>@+: expand the size of this field as much as possible</li>
285 * In case of lists or multiple-fields values, you can select a specific
286 * list or field with:
288 * <li>FIELD@(0): select the first value in a list</li>
289 * <li>FIELD@[1]: select the second field in a multiple-fields value</li>
294 * You can also add a fixed text if it starts with a simple-quote (').
298 * Example: "'Contact: |N@10|FN@20|NICK@+|PHOTO@x"
304 * a fixed width or -1 for "as long as needed"
306 * allow Uniode or only ASCII characters
308 * @return the {@link String} representation
310 public String
[] toStringArray(String format
, int width
, boolean unicode
) {
311 List
<String
> str
= new LinkedList
<String
>();
313 String
[] formatFields
= format
.split("\\|");
314 String
[] values
= new String
[formatFields
.length
];
315 Boolean
[] expandedFields
= new Boolean
[formatFields
.length
];
316 Boolean
[] fixedsizeFields
= new Boolean
[formatFields
.length
];
317 int numOfFieldsToExpand
= 0;
321 for (int i
= 0; i
< formatFields
.length
; i
++) {
325 return str
.toArray(new String
[] {});
328 for (int i
= 0; i
< formatFields
.length
; i
++) {
329 String field
= formatFields
[i
];
332 boolean binary
= false;
333 boolean expand
= false;
337 if (field
.length() > 0 && field
.charAt(0) != '\''
338 && field
.contains("@")) {
339 String
[] opts
= field
.split("@");
342 for (int io
= 1; io
< opts
.length
; io
++) {
343 String opt
= opts
[io
];
344 if (opt
.equals("x")) {
346 } else if (opt
.equals("+")) {
348 numOfFieldsToExpand
++;
349 } else if (opt
.length() > 0 && opt
.charAt(0) == '(') {
351 opt
= opt
.substring(1, opt
.length() - 1);
352 valueNum
= Integer
.parseInt(opt
);
353 } catch (Exception e
) {
355 } else if (opt
.length() > 0 && opt
.charAt(0) == '[') {
357 opt
= opt
.substring(1, opt
.length() - 1);
358 fieldNum
= Integer
.parseInt(opt
);
359 } catch (Exception e
) {
363 size
= Integer
.parseInt(opt
);
364 } catch (NumberFormatException e
) {
371 if (field
.length() > 0 && field
.charAt(0) == '\'') {
372 value
= field
.substring(1);
373 } else if (valueNum
>= 0) {
374 List
<String
> vv
= getPreferredData(field
).getValues();
375 if (valueNum
< vv
.size()) {
376 value
= vv
.get(valueNum
);
378 } else if (fieldNum
>= 0) {
379 List
<String
> ff
= getPreferredData(field
).getFields();
380 if (fieldNum
< ff
.size()) {
381 value
= ff
.get(fieldNum
);
384 // we don't need the *data* in binary mode...
386 value
= getData(field
).size() > 0 ?
"x" : null;
388 value
= getPreferredDataValue(field
);
394 value
= StringUtils
.sanitize(value
, unicode
);
398 value
= StringUtils
.padString(value
, size
);
401 expandedFields
[i
] = expand
;
402 fixedsizeFields
[i
] = (size
> -1);
405 if (value
!= null && !value
.equals(""))
412 totalSize
+= value
.length();
416 if (width
> -1 && totalSize
> width
) {
417 int toDo
= totalSize
- width
;
418 for (int i
= fixedsizeFields
.length
- 1; toDo
> 0 && i
>= 0; i
--) {
419 if (!fixedsizeFields
[i
]) {
420 int valueLength
= values
[i
].length();
421 if (valueLength
> 0) {
422 if (valueLength
>= toDo
) {
423 values
[i
] = values
[i
].substring(0, valueLength
434 totalSize
= width
+ toDo
;
437 if (width
> -1 && numOfFieldsToExpand
> 0) {
438 int availablePadding
= width
- totalSize
;
440 if (availablePadding
> 0) {
441 int padPerItem
= availablePadding
/ numOfFieldsToExpand
;
442 int remainder
= availablePadding
% numOfFieldsToExpand
;
444 for (int i
= 0; i
< values
.length
; i
++) {
445 if (expandedFields
[i
]) {
447 values
[i
] = values
[i
]
448 + StringUtils
.padString("", remainder
);
451 if (padPerItem
> 0) {
452 values
[i
] = values
[i
]
453 + StringUtils
.padString("", padPerItem
);
463 for (int i
= 0; i
< values
.length
; i
++) {
464 currentSize
+= addToList(str
, values
[i
], currentSize
, width
);
467 return str
.toArray(new String
[] {});
471 * Update the information from this contact with the information in the
472 * given contact. Non present fields will be removed, new fields will be
473 * added, BKey'ed fields will be completed with the binary information known
477 * the contact with the newer information and optional BKeys
479 public void updateFrom(Contact vc
) {
482 List
<Data
> newDatas
= new LinkedList
<Data
>(vc
);
483 for (int i
= 0; i
< newDatas
.size(); i
++) {
484 Data data
= newDatas
.get(i
);
485 int bkey
= Parser
.getBKey(data
);
487 if (binaries
.containsKey(bkey
)) {
488 newDatas
.set(i
, binaries
.get(bkey
));
493 replaceListContent(newDatas
);
494 this.nextBKey
= vc
.nextBKey
;
498 public String
getId() {
499 return "" + getPreferredDataValue("UID");
503 public String
getState() {
508 * Return a {@link String} representation of this contact, in vCard 2.1,
511 * @return the {@link String} representation
514 public String
toString() {
515 return "[Contact: " + getPreferredDataValue("FN") + "]";
519 * Mark all the binary fields with a BKey number.
522 * force the marking, and reset all the numbers.
524 protected void updateBKeys(boolean force
) {
526 binaries
= new HashMap
<Integer
, Data
>();
530 if (binaries
== null) {
531 binaries
= new HashMap
<Integer
, Data
>();
534 for (Data data
: this) {
535 if (data
.isBinary() && (data
.getB64Key() <= 0 || force
)) {
536 binaries
.put(nextBKey
, data
);
537 data
.resetB64Key(nextBKey
++);
543 * Load the data from the given {@link File} under the given {@link Format}.
546 * the {@link File} to load from
548 * the {@link Format} to load as
550 * @return the list of elements
551 * @throws IOException
552 * in case of IO error
554 static private List
<Data
> load(List
<Data
> content
) {
555 List
<Data
> datas
= new ArrayList
<Data
>();
560 if (content
!= null) {
561 for (Data data
: content
) {
562 if (data
.getName().equals("N")) {
564 } else if (data
.getName().equals("FN")) {
566 } else if (data
.getName().equals("UID")) {
570 if (!data
.getName().equals("VERSION")) {
577 if (!n
) // required since vCard 3.0, supported in 2.1
578 datas
.add(new Data(null, "N", "", null));
579 if (!fn
) // not required anymore but still supported in 4.0
580 datas
.add(new Data(null, "FN", "", null));
581 if (!uid
) // supported by vCard, required by this program
582 datas
.add(new Data(null, "UID", UUID
.randomUUID().toString(), null));
588 * Add a {@link String} to the given {@link List}, but make sure it does not
589 * exceed the maximum size, and truncate it if needed to fit.
597 static private int addToList(List
<String
> list
, String add
,
598 int currentSize
, int maxSize
) {
599 if (add
== null || add
.length() == 0) {
606 if (currentSize
< maxSize
) {
607 if (currentSize
+ add
.length() >= maxSize
) {
608 add
= add
.substring(0, maxSize
- currentSize
);