1 package be
.nikiroo
.jvcard
;
3 import java
.util
.HashMap
;
4 import java
.util
.LinkedList
;
8 import be
.nikiroo
.jvcard
.parsers
.Format
;
9 import be
.nikiroo
.jvcard
.parsers
.Parser
;
12 * A contact is the information that represent a contact person or organisation.
17 public class Contact
{
18 private List
<Data
> datas
;
19 private int nextBKey
= 1;
20 private Map
<Integer
, Data
> binaries
;
21 private boolean dirty
;
25 * Create a new Contact from the given information. Note that the BKeys data
29 * the information about the contact
31 public Contact(List
<Data
> content
) {
32 this.datas
= new LinkedList
<Data
>();
36 for (Data data
: content
) {
37 if (data
.getName().equals("N")) {
39 } else if (data
.getName().equals("FN")) {
43 if (!data
.getName().equals("VERSION")) {
50 datas
.add(new Data(null, "N", "", null));
53 datas
.add(new Data(null, "FN", "", null));
60 * Return the informations (note: this is the actual list, be careful).
62 * @return the list of data anout this contact
64 public List
<Data
> getContent() {
69 * Return the preferred Data field with the given name, or NULL if none.
72 * the name to look for
73 * @return the Data field, or NULL
75 public Data
getPreferredData(String name
) {
77 for (Data data
: getData(name
)) {
80 for (TypeInfo type
: data
.getTypes()) {
81 if (type
.getName().equals("TYPE")
82 && type
.getValue().equals("pref")) {
92 * Return the value of the preferred data field with this name, or NULL if
93 * none (you cannot differentiate a NULL value and no value).
96 * the name to look for
97 * @return the value (which can be NULL), or NULL
99 public String
getPreferredDataValue(String name
) {
100 Data data
= getPreferredData(name
);
101 if (data
!= null && data
.getValue() != null)
102 return data
.getValue().trim();
107 * Get the Data fields that share the given name.
110 * the name to ook for
111 * @return a list of Data fields with this name
113 public List
<Data
> getData(String name
) {
114 List
<Data
> found
= new LinkedList
<Data
>();
116 for (Data data
: datas
) {
117 if (data
.getName().equals(name
))
125 * Return a {@link String} representation of this contact.
128 * the {@link Format} to use
129 * @param startingBKey
130 * the starting BKey or -1 for no BKeys
131 * @return the {@link String} representation
133 public String
toString(Format format
, int startingBKey
) {
135 return Parser
.toString(this, format
, startingBKey
);
139 * Return a {@link String} representation of this contact formated
140 * 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>
150 * Example: "N@10|FN@20|NICK@+|PHOTO@x"
155 * the separator {@link String} to use between fields
157 * a fixed width or -1 for "as long as needed"
159 * @return the {@link String} representation
161 public String
toString(String format
, String separator
, int width
) {
162 StringBuilder builder
= new StringBuilder();
164 String
[] formatFields
= format
.split("\\|");
165 if (width
> -1 && separator
!= null && separator
.length() > 0
166 && formatFields
.length
> 1) {
167 int swidth
= (formatFields
.length
- 1) * separator
.length();
168 if (swidth
>= width
) {
169 int num
= width
/ separator
.length();
170 int remainder
= width
% separator
.length();
175 while (builder
.length() < width
) {
176 if (builder
.length() + separator
.length() <= width
)
177 builder
.append(separator
);
179 builder
.append(separator
180 .substring(0, (builder
.length() + separator
185 return builder
.toString();
191 for (String str
: toStringArray(format
, width
)) {
195 return builder
.toString();
199 * Return a {@link String} representation of this contact formated
200 * accordingly to the given format, part by part.
202 * The format is basically a list of field names separated by a pipe and
203 * optionally parametrised. See {@link Contact#toString} for more
204 * information about the format.
209 * a fixed width or -1 for "as long as needed"
211 * @return the {@link String} representation
213 public String
[] toStringArray(String format
, int width
) {
214 List
<String
> str
= new LinkedList
<String
>();
216 String
[] formatFields
= format
.split("\\|");
217 String
[] values
= new String
[formatFields
.length
];
218 Boolean
[] expandedFields
= new Boolean
[formatFields
.length
];
219 Boolean
[] fixedsizeFields
= new Boolean
[formatFields
.length
];
220 int numOfFieldsToExpand
= 0;
224 return str
.toArray(new String
[] {});
227 for (int i
= 0; i
< formatFields
.length
; i
++) {
228 String field
= formatFields
[i
];
231 boolean binary
= false;
232 boolean expand
= false;
234 if (field
.contains("@")) {
235 String
[] opts
= field
.split("@");
238 for (int io
= 1; io
< opts
.length
; io
++) {
239 String opt
= opts
[io
];
240 if (opt
.equals("x")) {
242 } else if (opt
.equals("+")) {
244 numOfFieldsToExpand
++;
247 size
= Integer
.parseInt(opt
);
248 } catch (Exception e
) {
254 String value
= getPreferredDataValue(field
);
259 value
= fixedString(value
, size
);
262 expandedFields
[i
] = expand
;
263 fixedsizeFields
[i
] = (size
> -1);
266 if (value
!= null && !value
.equals(""))
273 totalSize
+= value
.length();
277 if (width
> -1 && totalSize
> width
) {
278 int toDo
= totalSize
- width
;
279 for (int i
= fixedsizeFields
.length
- 1; toDo
> 0 && i
>= 0; i
--) {
280 if (!fixedsizeFields
[i
]) {
281 int valueLength
= values
[i
].length();
282 if (valueLength
> 0) {
283 if (valueLength
>= toDo
) {
284 values
[i
] = values
[i
].substring(0, valueLength
295 totalSize
= width
+ toDo
;
298 if (width
> -1 && numOfFieldsToExpand
> 0) {
299 int availablePadding
= width
- totalSize
;
301 if (availablePadding
> 0) {
302 int padPerItem
= availablePadding
/ numOfFieldsToExpand
;
303 int remainder
= availablePadding
% numOfFieldsToExpand
;
305 for (int i
= 0; i
< values
.length
; i
++) {
306 if (expandedFields
[i
]) {
308 values
[i
] = values
[i
]
309 + new String(new char[remainder
]).replace(
313 if (padPerItem
> 0) {
314 values
[i
] = values
[i
]
315 + new String(new char[padPerItem
]).replace(
326 for (int i
= 0; i
< values
.length
; i
++) {
327 currentSize
+= addToList(str
, values
[i
], currentSize
, width
);
330 return str
.toArray(new String
[] {});
334 * Fix the size of the given {@link String} either with space-padding or by
338 * the {@link String} to fix
340 * the size of the resulting {@link String}
342 * @return the fixed {@link String} of size <i>size</i>
344 static private String
fixedString(String string
, int size
) {
345 int length
= string
.length();
348 string
= string
.substring(0, size
);
349 else if (length
< size
)
351 + new String(new char[size
- length
]).replace('\0', ' ');
357 * Add a {@link String} to the given {@link List}, but make sure it does not
358 * exceed the maximum size, and truncate it if needed to fit.
366 static private int addToList(List
<String
> list
, String add
,
367 int currentSize
, int maxSize
) {
368 if (add
== null || add
.length() == 0) {
375 if (currentSize
< maxSize
) {
376 if (currentSize
+ add
.length() >= maxSize
) {
377 add
= add
.substring(0, maxSize
- currentSize
);
384 if (add
.length() > 0) {
392 * Return a {@link String} representation of this contact, in vCard 2.1,
395 * @return the {@link String} representation
397 public String
toString() {
398 return toString(Format
.VCard21
, -1);
402 * Update the information from this contact with the information in the
403 * given contact. Non present fields will be removed, new fields will be
404 * added, BKey'ed fields will be completed with the binary information known
408 * the contact with the newer information and optional BKeys
410 public void updateFrom(Contact vc
) {
413 List
<Data
> newDatas
= new LinkedList
<Data
>(vc
.datas
);
414 for (int i
= 0; i
< newDatas
.size(); i
++) {
415 Data data
= newDatas
.get(i
);
416 int bkey
= Parser
.getBKey(data
);
418 if (binaries
.containsKey(bkey
)) {
419 newDatas
.set(i
, binaries
.get(bkey
));
424 this.datas
= newDatas
;
425 this.nextBKey
= vc
.nextBKey
;
432 * Mark all the binary fields with a BKey number.
435 * force the marking, and reset all the numbers.
437 protected void updateBKeys(boolean force
) {
439 binaries
= new HashMap
<Integer
, Data
>();
443 if (binaries
== null) {
444 binaries
= new HashMap
<Integer
, Data
>();
447 for (Data data
: datas
) {
448 if (data
.isBinary() && (data
.getB64Key() <= 0 || force
)) {
449 binaries
.put(nextBKey
, data
);
450 data
.resetB64Key(nextBKey
++);
455 public boolean isDirty() {
460 * Notify that this element has unsaved changes, and notify its parent of
463 protected void setDirty() {
465 if (this.parent
!= null)
466 this.parent
.setDirty();
469 public void setParent(Card parent
) {
470 this.parent
= parent
;
471 for (Data data
: datas
) {
472 data
.setParent(this);