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 formated
93 * accordingly to the given format.
96 * The format is basically a list of field names separated by a pipe and
97 * optionally parametrised with the 'at' (@) symbol. The parameters allows
100 * <li>@x: show only a present/not present info</li>
101 * <li>@n: limit the size to a fixed value 'n'</li>
102 * <li>@+: expand the size of this field as much as possible</li>
107 * In case of lists or multiple-fields values, you can select a specific
108 * list or field with:
110 * <li>FIELD@(0): select the first value in a list</li>
111 * <li>FIELD@[1]: select the second field in a multiple-fields value</li>
116 * You can also add a fixed text if it starts with a simple-quote (').
120 * Example: "'Contact: |N@10|FN@20|NICK@+|PHOTO@x"
126 * the separator {@link String} to use between fields
128 * @return the {@link String} representation
130 public String
toString(String format
, String separator
) {
131 return toString(format
, separator
, null, -1, true, false);
135 * Return a {@link String} representation of this contact formated
136 * accordingly to the given format.
139 * The format is basically a list of field names separated by a pipe and
140 * optionally parametrised. The parameters allows you to:
142 * <li>@x: show only a present/not present info</li>
143 * <li>@n: limit the size to a fixed value 'n'</li>
144 * <li>@+: expand the size of this field as much as possible</li>
149 * In case of lists or multiple-fields values, you can select a specific
150 * list or field with:
152 * <li>FIELD@(0): select the first value in a list</li>
153 * <li>FIELD@[1]: select the second field in a multiple-fields value</li>
158 * You can also add a fixed text if it starts with a simple-quote (').
162 * Example: "'Contact: |N@10|FN@20|NICK@+|PHOTO@x"
168 * the separator {@link String} to use between fields
170 * the {@link String} to use for left and right padding
172 * a fixed width or -1 for "as long as needed"
175 * allow Uniode or only ASCII characters
177 * @return the {@link String} representation
179 public String
toString(String format
, String separator
, String padding
,
180 int width
, boolean unicode
, boolean removeAccents
) {
181 StringBuilder builder
= new StringBuilder();
183 for (String str
: toStringArray(format
, separator
, padding
, width
,
188 return builder
.toString();
192 * Return a {@link String} representation of this contact formated
193 * accordingly to the given format, part by part.
196 * The format is basically a list of field names separated by a pipe and
197 * optionally parametrised. The parameters allows you to:
199 * <li>@x: show only a present/not present info</li>
200 * <li>@n: limit the size to a fixed value 'n'</li>
201 * <li>@+: expand the size of this field as much as possible</li>
206 * In case of lists or multiple-fields values, you can select a specific
207 * list or field with:
209 * <li>FIELD@(0): select the first value in a list</li>
210 * <li>FIELD@[1]: select the second field in a multiple-fields value</li>
215 * You can also add a fixed text if it starts with a simple-quote (').
219 * Example: "'Contact: |N@10|FN@20|NICK@+|PHOTO@x"
225 * the separator {@link String} to use between fields
227 * the {@link String} to use for left and right padding
229 * a fixed width or -1 for "as long as needed"
232 * allow Uniode or only ASCII characters
234 * @return the {@link String} representation
236 public String
[] toStringArray(String format
, String separator
,
237 String padding
, int width
, boolean unicode
) {
239 int numOfFields
= format
.split("\\|").length
;
240 if (separator
!= null)
241 width
-= (numOfFields
- 1) * separator
.length();
243 width
-= (numOfFields
) * (2 * padding
.length());
249 List
<String
> str
= new LinkedList
<String
>();
251 boolean first
= true;
252 for (String s
: toStringArray(format
, width
, unicode
)) {
258 str
.add(padding
+ s
+ padding
);
265 return str
.toArray(new String
[] {});
269 * Return a {@link String} representation of this contact formated
270 * accordingly to the given format, part by part.
273 * The format is basically a list of field names separated by a pipe and
274 * optionally parametrised. The parameters allows you to:
276 * <li>@x: show only a present/not present info</li>
277 * <li>@n: limit the size to a fixed value 'n'</li>
278 * <li>@+: expand the size of this field as much as possible</li>
283 * In case of lists or multiple-fields values, you can select a specific
284 * list or field with:
286 * <li>FIELD@(0): select the first value in a list</li>
287 * <li>FIELD@[1]: select the second field in a multiple-fields value</li>
292 * You can also add a fixed text if it starts with a simple-quote (').
296 * Example: "'Contact: |N@10|FN@20|NICK@+|PHOTO@x"
302 * a fixed width or -1 for "as long as needed"
304 * allow Uniode or only ASCII characters
306 * @return the {@link String} representation
308 public String
[] toStringArray(String format
, int width
, boolean unicode
) {
309 List
<String
> str
= new LinkedList
<String
>();
311 String
[] formatFields
= format
.split("\\|");
312 String
[] values
= new String
[formatFields
.length
];
313 Boolean
[] expandedFields
= new Boolean
[formatFields
.length
];
314 Boolean
[] fixedsizeFields
= new Boolean
[formatFields
.length
];
315 int numOfFieldsToExpand
= 0;
319 for (int i
= 0; i
< formatFields
.length
; i
++) {
323 return str
.toArray(new String
[] {});
326 for (int i
= 0; i
< formatFields
.length
; i
++) {
327 String field
= formatFields
[i
];
330 boolean binary
= false;
331 boolean expand
= false;
335 if (field
.length() > 0 && field
.charAt(0) != '\''
336 && field
.contains("@")) {
337 String
[] opts
= field
.split("@");
340 for (int io
= 1; io
< opts
.length
; io
++) {
341 String opt
= opts
[io
];
342 if (opt
.equals("x")) {
344 } else if (opt
.equals("+")) {
346 numOfFieldsToExpand
++;
347 } else if (opt
.length() > 0 && opt
.charAt(0) == '(') {
349 opt
= opt
.substring(1, opt
.length() - 1);
350 valueNum
= Integer
.parseInt(opt
);
351 } catch (Exception e
) {
353 } else if (opt
.length() > 0 && opt
.charAt(0) == '[') {
355 opt
= opt
.substring(1, opt
.length() - 1);
356 fieldNum
= Integer
.parseInt(opt
);
357 } catch (Exception e
) {
361 size
= Integer
.parseInt(opt
);
362 } catch (NumberFormatException e
) {
369 if (field
.length() > 0 && field
.charAt(0) == '\'') {
370 value
= field
.substring(1);
371 } else if (valueNum
>= 0) {
372 List
<String
> vv
= getPreferredData(field
).getValues();
373 if (valueNum
< vv
.size()) {
374 value
= vv
.get(valueNum
);
376 } else if (fieldNum
>= 0) {
377 List
<String
> ff
= getPreferredData(field
).getFields();
378 if (fieldNum
< ff
.size()) {
379 value
= ff
.get(fieldNum
);
382 // we don't need the *data* in binary mode...
384 value
= getData(field
).size() > 0 ?
"x" : null;
386 value
= getPreferredDataValue(field
);
392 value
= StringUtils
.sanitize(value
, unicode
);
396 value
= StringUtils
.padString(value
, size
);
399 expandedFields
[i
] = expand
;
400 fixedsizeFields
[i
] = (size
> -1);
403 if (value
!= null && !value
.equals(""))
410 totalSize
+= value
.length();
414 if (width
> -1 && totalSize
> width
) {
415 int toDo
= totalSize
- width
;
416 for (int i
= fixedsizeFields
.length
- 1; toDo
> 0 && i
>= 0; i
--) {
417 if (!fixedsizeFields
[i
]) {
418 int valueLength
= values
[i
].length();
419 if (valueLength
> 0) {
420 if (valueLength
>= toDo
) {
421 values
[i
] = values
[i
].substring(0, valueLength
432 totalSize
= width
+ toDo
;
435 if (width
> -1 && numOfFieldsToExpand
> 0) {
436 int availablePadding
= width
- totalSize
;
438 if (availablePadding
> 0) {
439 int padPerItem
= availablePadding
/ numOfFieldsToExpand
;
440 int remainder
= availablePadding
% numOfFieldsToExpand
;
442 for (int i
= 0; i
< values
.length
; i
++) {
443 if (expandedFields
[i
]) {
445 values
[i
] = values
[i
]
446 + StringUtils
.padString("", remainder
);
449 if (padPerItem
> 0) {
450 values
[i
] = values
[i
]
451 + StringUtils
.padString("", padPerItem
);
461 for (int i
= 0; i
< values
.length
; i
++) {
462 currentSize
+= addToList(str
, values
[i
], currentSize
, width
);
465 return str
.toArray(new String
[] {});
469 * Update the information from this contact with the information in the
470 * given contact. Non present fields will be removed, new fields will be
471 * added, BKey'ed fields will be completed with the binary information known
475 * the contact with the newer information and optional BKeys
477 public void updateFrom(Contact vc
) {
480 List
<Data
> newDatas
= new LinkedList
<Data
>(vc
);
481 for (int i
= 0; i
< newDatas
.size(); i
++) {
482 Data data
= newDatas
.get(i
);
483 int bkey
= Parser
.getBKey(data
);
485 if (binaries
.containsKey(bkey
)) {
486 newDatas
.set(i
, binaries
.get(bkey
));
491 replaceListContent(newDatas
);
492 this.nextBKey
= vc
.nextBKey
;
496 public String
getId() {
497 return "" + getPreferredDataValue("UID");
501 public String
getState() {
506 * Return a {@link String} representation of this contact, in vCard 2.1,
509 * @return the {@link String} representation
512 public String
toString() {
513 return "[Contact: " + getPreferredDataValue("FN") + "]";
517 * Mark all the binary fields with a BKey number.
520 * force the marking, and reset all the numbers.
522 protected void updateBKeys(boolean force
) {
524 binaries
= new HashMap
<Integer
, Data
>();
528 if (binaries
== null) {
529 binaries
= new HashMap
<Integer
, Data
>();
532 for (Data data
: this) {
533 if (data
.isBinary() && (data
.getB64Key() <= 0 || force
)) {
534 binaries
.put(nextBKey
, data
);
535 data
.resetB64Key(nextBKey
++);
541 * Load the data from the given {@link File} under the given {@link Format}.
544 * the {@link File} to load from
546 * the {@link Format} to load as
548 * @return the list of elements
549 * @throws IOException
550 * in case of IO error
552 static private List
<Data
> load(List
<Data
> content
) {
553 List
<Data
> datas
= new ArrayList
<Data
>();
558 if (content
!= null) {
559 for (Data data
: content
) {
560 if (data
.getName().equals("N")) {
562 } else if (data
.getName().equals("FN")) {
564 } else if (data
.getName().equals("UID")) {
568 if (!data
.getName().equals("VERSION")) {
575 if (!n
) // required since vCard 3.0, supported in 2.1
576 datas
.add(new Data(null, "N", "", null));
577 if (!fn
) // not required anymore but still supported in 4.0
578 datas
.add(new Data(null, "FN", "", null));
579 if (!uid
) // supported by vCard, required by this program
580 datas
.add(new Data(null, "UID", UUID
.randomUUID().toString(), null));
586 * Add a {@link String} to the given {@link List}, but make sure it does not
587 * exceed the maximum size, and truncate it if needed to fit.
595 static private int addToList(List
<String
> list
, String add
,
596 int currentSize
, int maxSize
) {
597 if (add
== null || add
.length() == 0) {
604 if (currentSize
< maxSize
) {
605 if (currentSize
+ add
.length() >= maxSize
) {
606 add
= add
.substring(0, maxSize
- currentSize
);