4c6d5d443b9075d42823af98660df19c904c88d9
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
.tui
.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
)) {
50 for (int index
= 0; index
< data
.size(); index
++) {
51 TypeInfo type
= data
.get(index
);
52 if (type
.getName().equals("TYPE")
53 && type
.getValue().equals("pref")) {
63 * Return the value of the preferred data field with this name, or NULL if
64 * none (you cannot differentiate a NULL value and no value).
67 * the name to look for
68 * @return the value (which can be NULL), or NULL
70 public String
getPreferredDataValue(String name
) {
71 Data data
= getPreferredData(name
);
72 if (data
!= null && data
.getValue() != null)
73 return data
.getValue().trim();
78 * Get the Data fields that share the given name.
82 * @return a list of Data fields with this name
84 public List
<Data
> getData(String name
) {
85 List
<Data
> found
= new LinkedList
<Data
>();
87 for (Data data
: this) {
88 if (data
.getName().equals(name
))
96 * Return a {@link String} representation of this contact.
99 * the {@link Format} to use
100 * @param startingBKey
101 * the starting BKey or -1 for no BKeys
102 * @return the {@link String} representation
104 public String
toString(Format format
, int startingBKey
) {
106 return Parser
.toString(this, format
, startingBKey
);
110 * Return a {@link String} representation of this contact formated
111 * accordingly to the given format.
113 * The format is basically a list of field names separated by a pipe and
114 * optionally parametrised. The parameters allows you to:
116 * <li>@x: show only a present/not present info</li>
117 * <li>@n: limit the size to a fixed value 'n'</li>
118 * <li>@+: expand the size of this field as much as possible</li>
121 * Example: "N@10|FN@20|NICK@+|PHOTO@x"
126 * @return the {@link String} representation
128 public String
toString(String format
) {
129 return toString(format
, "|", null, -1, true, false);
133 * Return a {@link String} representation of this contact formated
134 * accordingly to the given format.
136 * The format is basically a list of field names separated by a pipe and
137 * optionally parametrised. The parameters allows you to:
139 * <li>@x: (the 'x' is the letter 'x') show only a present/not present info</li>
140 * <li>@n: limit the size to a fixed value 'n'</li>
141 * <li>@+: expand the size of this field as much as possible</li>
144 * Example: "N@10|FN@20|NICK@+|PHOTO@x"
149 * the separator {@link String} to use between fields
151 * the {@link String} to use for left and right padding
153 * a fixed width or -1 for "as long as needed"
156 * allow Uniode or only ASCII characters
158 * @return the {@link String} representation
160 public String
toString(String format
, String separator
, String padding
,
161 int width
, boolean unicode
, boolean removeAccents
) {
162 StringBuilder builder
= new StringBuilder();
164 for (String str
: toStringArray(format
, separator
, padding
, width
,
169 return builder
.toString();
173 * Return a {@link String} representation of this contact formated
174 * accordingly to the given format, part by part.
176 * The format is basically a list of field names separated by a pipe and
177 * optionally parametrised. The parameters allows you to:
179 * <li>@x: show only a present/not present info</li>
180 * <li>@n: limit the size to a fixed value 'n'</li>
181 * <li>@+: expand the size of this field as much as possible</li>
184 * Example: "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
[] toStringArray(String format
, String separator
,
201 String padding
, int width
, boolean unicode
) {
203 int numOfFields
= format
.split("\\|").length
;
204 if (separator
!= null)
205 width
-= (numOfFields
- 1) * separator
.length();
207 width
-= (numOfFields
) * (2 * padding
.length());
213 List
<String
> str
= new LinkedList
<String
>();
215 boolean first
= true;
216 for (String s
: toStringArray(format
, width
, unicode
)) {
222 str
.add(padding
+ s
+ padding
);
229 return str
.toArray(new String
[] {});
233 * Return a {@link String} representation of this contact formated
234 * accordingly to the given format, part by part.
236 * The format is basically a list of field names separated by a pipe and
237 * optionally parametrised. The parameters allows you to:
239 * <li>@x: show only a present/not present info</li>
240 * <li>@n: limit the size to a fixed value 'n'</li>
241 * <li>@+: expand the size of this field as much as possible</li>
244 * Example: "N@10|FN@20|NICK@+|PHOTO@x"
249 * a fixed width or -1 for "as long as needed"
251 * allow Uniode or only ASCII characters
253 * @return the {@link String} representation
255 public String
[] toStringArray(String format
, int width
, boolean unicode
) {
256 List
<String
> str
= new LinkedList
<String
>();
258 String
[] formatFields
= format
.split("\\|");
259 String
[] values
= new String
[formatFields
.length
];
260 Boolean
[] expandedFields
= new Boolean
[formatFields
.length
];
261 Boolean
[] fixedsizeFields
= new Boolean
[formatFields
.length
];
262 int numOfFieldsToExpand
= 0;
266 for (int i
= 0; i
< formatFields
.length
; i
++) {
270 return str
.toArray(new String
[] {});
273 for (int i
= 0; i
< formatFields
.length
; i
++) {
274 String field
= formatFields
[i
];
277 boolean binary
= false;
278 boolean expand
= false;
280 if (field
.contains("@")) {
281 String
[] opts
= field
.split("@");
284 for (int io
= 1; io
< opts
.length
; io
++) {
285 String opt
= opts
[io
];
286 if (opt
.equals("x")) {
288 } else if (opt
.equals("+")) {
290 numOfFieldsToExpand
++;
293 size
= Integer
.parseInt(opt
);
294 } catch (Exception e
) {
300 String value
= getPreferredDataValue(field
);
304 value
= StringUtils
.sanitize(value
, unicode
);
308 value
= StringUtils
.padString(value
, size
);
311 expandedFields
[i
] = expand
;
312 fixedsizeFields
[i
] = (size
> -1);
315 if (value
!= null && !value
.equals(""))
322 totalSize
+= value
.length();
326 if (width
> -1 && totalSize
> width
) {
327 int toDo
= totalSize
- width
;
328 for (int i
= fixedsizeFields
.length
- 1; toDo
> 0 && i
>= 0; i
--) {
329 if (!fixedsizeFields
[i
]) {
330 int valueLength
= values
[i
].length();
331 if (valueLength
> 0) {
332 if (valueLength
>= toDo
) {
333 values
[i
] = values
[i
].substring(0, valueLength
344 totalSize
= width
+ toDo
;
347 if (width
> -1 && numOfFieldsToExpand
> 0) {
348 int availablePadding
= width
- totalSize
;
350 if (availablePadding
> 0) {
351 int padPerItem
= availablePadding
/ numOfFieldsToExpand
;
352 int remainder
= availablePadding
% numOfFieldsToExpand
;
354 for (int i
= 0; i
< values
.length
; i
++) {
355 if (expandedFields
[i
]) {
357 values
[i
] = values
[i
]
358 + StringUtils
.padString("", remainder
);
361 if (padPerItem
> 0) {
362 values
[i
] = values
[i
]
363 + StringUtils
.padString("", padPerItem
);
373 for (int i
= 0; i
< values
.length
; i
++) {
374 currentSize
+= addToList(str
, values
[i
], currentSize
, width
);
377 return str
.toArray(new String
[] {});
381 * Update the information from this contact with the information in the
382 * given contact. Non present fields will be removed, new fields will be
383 * added, BKey'ed fields will be completed with the binary information known
387 * the contact with the newer information and optional BKeys
389 public void updateFrom(Contact vc
) {
392 List
<Data
> newDatas
= new LinkedList
<Data
>(vc
);
393 for (int i
= 0; i
< newDatas
.size(); i
++) {
394 Data data
= newDatas
.get(i
);
395 int bkey
= Parser
.getBKey(data
);
397 if (binaries
.containsKey(bkey
)) {
398 newDatas
.set(i
, binaries
.get(bkey
));
403 replaceListContent(newDatas
);
404 this.nextBKey
= vc
.nextBKey
;
408 public String
getId() {
409 return "" + getPreferredDataValue("UID");
413 public String
getState() {
414 return "" + getPreferredDataValue("UID");
418 * Return a {@link String} representation of this contact, in vCard 2.1,
421 * @return the {@link String} representation
424 public String
toString() {
425 return toString(Format
.VCard21
, -1);
429 * Mark all the binary fields with a BKey number.
432 * force the marking, and reset all the numbers.
434 protected void updateBKeys(boolean force
) {
436 binaries
= new HashMap
<Integer
, Data
>();
440 if (binaries
== null) {
441 binaries
= new HashMap
<Integer
, Data
>();
444 for (Data data
: this) {
445 if (data
.isBinary() && (data
.getB64Key() <= 0 || force
)) {
446 binaries
.put(nextBKey
, data
);
447 data
.resetB64Key(nextBKey
++);
453 * Load the data from the given {@link File} under the given {@link Format}.
456 * the {@link File} to load from
458 * the {@link Format} to load as
460 * @return the list of elements
461 * @throws IOException
462 * in case of IO error
464 static private List
<Data
> load(List
<Data
> content
) {
465 List
<Data
> datas
= new ArrayList
<Data
>();
470 if (content
!= null) {
471 for (Data data
: content
) {
472 if (data
.getName().equals("N")) {
474 } else if (data
.getName().equals("FN")) {
476 } else if (data
.getName().equals("UID")) {
480 if (!data
.getName().equals("VERSION")) {
487 if (!n
) // required since vCard 3.0, supported in 2.1
488 datas
.add(new Data(null, "N", "", null));
489 if (!fn
) // not required anymore but still supported in 4.0
490 datas
.add(new Data(null, "FN", "", null));
491 if (!uid
) // supported by vCard, required by this program
492 datas
.add(new Data(null, "UID", UUID
.randomUUID().toString(), null));
498 * Add a {@link String} to the given {@link List}, but make sure it does not
499 * exceed the maximum size, and truncate it if needed to fit.
507 static private int addToList(List
<String
> list
, String add
,
508 int currentSize
, int maxSize
) {
509 if (add
== null || add
.length() == 0) {
516 if (currentSize
< maxSize
) {
517 if (currentSize
+ add
.length() >= maxSize
) {
518 add
= add
.substring(0, maxSize
- currentSize
);