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. The parameters allows you to:
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>
127 * You can also add a fixed text if it starts with a simple-quote (').
131 * Example: "'Contact: |N@10|FN@20|NICK@+|PHOTO@x"
137 * the separator {@link String} to use between fields
139 * @return the {@link String} representation
141 public String
toString(String format
, String separator
) {
142 return toString(format
, separator
, null, -1, true, false);
146 * Return a {@link String} representation of this contact formated
147 * accordingly to the given format.
150 * The format is basically a list of field names separated by a pipe and
151 * optionally parametrised. The parameters allows you to:
153 * <li>@x: show only a present/not present info</li>
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>
160 * You can also add a fixed text if it starts with a simple-quote (').
164 * Example: "'Contact: |N@10|FN@20|NICK@+|PHOTO@x"
170 * the separator {@link String} to use between fields
172 * the {@link String} to use for left and right padding
174 * a fixed width or -1 for "as long as needed"
177 * allow Uniode 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
, boolean removeAccents
) {
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 * You can also add a fixed text if it starts with a simple-quote (').
212 * Example: "'Contact: |N@10|FN@20|NICK@+|PHOTO@x"
218 * the separator {@link String} to use between fields
220 * the {@link String} to use for left and right padding
222 * a fixed width or -1 for "as long as needed"
225 * allow Uniode or only ASCII characters
227 * @return the {@link String} representation
229 public String
[] toStringArray(String format
, String separator
,
230 String padding
, int width
, boolean unicode
) {
232 int numOfFields
= format
.split("\\|").length
;
233 if (separator
!= null)
234 width
-= (numOfFields
- 1) * separator
.length();
236 width
-= (numOfFields
) * (2 * padding
.length());
242 List
<String
> str
= new LinkedList
<String
>();
244 boolean first
= true;
245 for (String s
: toStringArray(format
, width
, unicode
)) {
251 str
.add(padding
+ s
+ padding
);
258 return str
.toArray(new String
[] {});
262 * Return a {@link String} representation of this contact formated
263 * accordingly to the given format, part by part.
266 * The format is basically a list of field names separated by a pipe and
267 * optionally parametrised. The parameters allows you to:
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>
276 * You can also add a fixed text if it starts with a simple-quote (').
280 * Example: "'Contact: |N@10|FN@20|NICK@+|PHOTO@x"
286 * a fixed width or -1 for "as long as needed"
288 * allow Uniode or only ASCII characters
290 * @return the {@link String} representation
292 public String
[] toStringArray(String format
, int width
, boolean unicode
) {
293 List
<String
> str
= new LinkedList
<String
>();
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;
303 for (int i
= 0; i
< formatFields
.length
; i
++) {
307 return str
.toArray(new String
[] {});
310 for (int i
= 0; i
< formatFields
.length
; i
++) {
311 String field
= formatFields
[i
];
314 boolean binary
= false;
315 boolean expand
= false;
317 if (field
.length() > 0 && field
.charAt(0) != '\''
318 && field
.contains("@")) {
319 String
[] opts
= field
.split("@");
322 for (int io
= 1; io
< opts
.length
; io
++) {
323 String opt
= opts
[io
];
324 if (opt
.equals("x")) {
326 } else if (opt
.equals("+")) {
328 numOfFieldsToExpand
++;
331 size
= Integer
.parseInt(opt
);
332 } catch (Exception e
) {
339 if (field
.length() > 0 && field
.charAt(0) == '\'') {
340 value
= field
.substring(1);
342 value
= getPreferredDataValue(field
);
348 value
= StringUtils
.sanitize(value
, unicode
);
352 value
= StringUtils
.padString(value
, size
);
355 expandedFields
[i
] = expand
;
356 fixedsizeFields
[i
] = (size
> -1);
359 if (value
!= null && !value
.equals(""))
366 totalSize
+= value
.length();
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
388 totalSize
= width
+ toDo
;
391 if (width
> -1 && numOfFieldsToExpand
> 0) {
392 int availablePadding
= width
- totalSize
;
394 if (availablePadding
> 0) {
395 int padPerItem
= availablePadding
/ numOfFieldsToExpand
;
396 int remainder
= availablePadding
% numOfFieldsToExpand
;
398 for (int i
= 0; i
< values
.length
; i
++) {
399 if (expandedFields
[i
]) {
401 values
[i
] = values
[i
]
402 + StringUtils
.padString("", remainder
);
405 if (padPerItem
> 0) {
406 values
[i
] = values
[i
]
407 + StringUtils
.padString("", padPerItem
);
417 for (int i
= 0; i
< values
.length
; i
++) {
418 currentSize
+= addToList(str
, values
[i
], currentSize
, width
);
421 return str
.toArray(new String
[] {});
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
431 * the contact with the newer information and optional BKeys
433 public void updateFrom(Contact vc
) {
436 List
<Data
> newDatas
= new LinkedList
<Data
>(vc
);
437 for (int i
= 0; i
< newDatas
.size(); i
++) {
438 Data data
= newDatas
.get(i
);
439 int bkey
= Parser
.getBKey(data
);
441 if (binaries
.containsKey(bkey
)) {
442 newDatas
.set(i
, binaries
.get(bkey
));
447 replaceListContent(newDatas
);
448 this.nextBKey
= vc
.nextBKey
;
452 public String
getId() {
453 return "" + getPreferredDataValue("UID");
457 public String
getState() {
462 * Return a {@link String} representation of this contact, in vCard 2.1,
465 * @return the {@link String} representation
468 public String
toString() {
469 return toString(Format
.VCard21
, -1);
473 * Mark all the binary fields with a BKey number.
476 * force the marking, and reset all the numbers.
478 protected void updateBKeys(boolean force
) {
480 binaries
= new HashMap
<Integer
, Data
>();
484 if (binaries
== null) {
485 binaries
= new HashMap
<Integer
, Data
>();
488 for (Data data
: this) {
489 if (data
.isBinary() && (data
.getB64Key() <= 0 || force
)) {
490 binaries
.put(nextBKey
, data
);
491 data
.resetB64Key(nextBKey
++);
497 * Load the data from the given {@link File} under the given {@link Format}.
500 * the {@link File} to load from
502 * the {@link Format} to load as
504 * @return the list of elements
505 * @throws IOException
506 * in case of IO error
508 static private List
<Data
> load(List
<Data
> content
) {
509 List
<Data
> datas
= new ArrayList
<Data
>();
514 if (content
!= null) {
515 for (Data data
: content
) {
516 if (data
.getName().equals("N")) {
518 } else if (data
.getName().equals("FN")) {
520 } else if (data
.getName().equals("UID")) {
524 if (!data
.getName().equals("VERSION")) {
531 if (!n
) // required since vCard 3.0, supported in 2.1
532 datas
.add(new Data(null, "N", "", null));
533 if (!fn
) // not required anymore but still supported in 4.0
534 datas
.add(new Data(null, "FN", "", null));
535 if (!uid
) // supported by vCard, required by this program
536 datas
.add(new Data(null, "UID", UUID
.randomUUID().toString(), null));
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.
551 static private int addToList(List
<String
> list
, String add
,
552 int currentSize
, int maxSize
) {
553 if (add
== null || add
.length() == 0) {
560 if (currentSize
< maxSize
) {
561 if (currentSize
+ add
.length() >= maxSize
) {
562 add
= add
.substring(0, maxSize
- currentSize
);