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
) {
164 String
[] formatFields
= format
.split("\\|");
165 String
[] values
= new String
[formatFields
.length
];
166 Boolean
[] expandedFields
= new Boolean
[formatFields
.length
];
167 Boolean
[] fixedsizeFields
= new Boolean
[formatFields
.length
];
168 int numOfFieldsToExpand
= 0;
175 if (width
> -1 && separator
!= null && separator
.length() > 0
176 && formatFields
.length
> 1) {
177 int swidth
= (formatFields
.length
- 1) * separator
.length();
178 if (swidth
>= width
) {
180 while (str
.length() < width
) {
184 return str
.substring(0, width
);
190 for (int i
= 0; i
< formatFields
.length
; i
++) {
191 String field
= formatFields
[i
];
194 boolean binary
= false;
195 boolean expand
= false;
197 if (field
.contains("@")) {
198 String
[] opts
= field
.split("@");
201 for (int io
= 1; io
< opts
.length
; io
++) {
202 String opt
= opts
[io
];
203 if (opt
.equals("x")) {
205 } else if (opt
.equals("+")) {
207 numOfFieldsToExpand
++;
210 size
= Integer
.parseInt(opt
);
211 } catch (Exception e
) {
217 String value
= getPreferredDataValue(field
);
222 value
= fixedString(value
, size
);
225 expandedFields
[i
] = expand
;
226 fixedsizeFields
[i
] = (size
> -1);
229 if (value
!= null && !value
.equals(""))
236 totalSize
+= value
.length();
240 if (width
> -1 && totalSize
> width
) {
241 int toDo
= totalSize
- width
;
242 for (int i
= fixedsizeFields
.length
- 1; toDo
> 0 && i
>= 0; i
--) {
243 if (!fixedsizeFields
[i
]) {
244 int valueLength
= values
[i
].length();
245 if (valueLength
> 0) {
246 if (valueLength
>= toDo
) {
247 values
[i
] = values
[i
].substring(0, valueLength
258 totalSize
= width
+ toDo
;
261 if (width
> -1 && numOfFieldsToExpand
> 0) {
262 int availablePadding
= width
- totalSize
;
264 if (availablePadding
> 0) {
265 int padPerItem
= availablePadding
/ numOfFieldsToExpand
;
266 int remainder
= availablePadding
% numOfFieldsToExpand
;
268 for (int i
= 0; i
< values
.length
; i
++) {
269 if (expandedFields
[i
]) {
271 values
[i
] = values
[i
]
272 + new String(new char[remainder
]).replace(
276 if (padPerItem
> 0) {
277 values
[i
] = values
[i
]
278 + new String(new char[padPerItem
]).replace(
288 for (String field
: values
) {
292 str
+= separator
+ field
;
300 str
= fixedString(str
, width
);
307 * Fix the size of the given {@link String} either with space-padding or by
311 * the {@link String} to fix
313 * the size of the resulting {@link String}
315 * @return the fixed {@link String} of size <i>size</i>
317 static private String
fixedString(String string
, int size
) {
318 int length
= string
.length();
321 string
= string
.substring(0, size
);
322 else if (length
< size
)
324 + new String(new char[size
- length
]).replace('\0', ' ');
330 * Return a {@link String} representation of this contact, in vCard 2.1,
333 * @return the {@link String} representation
335 public String
toString() {
336 return toString(Format
.VCard21
, -1);
340 * Update the information from this contact with the information in the
341 * given contact. Non present fields will be removed, new fields will be
342 * added, BKey'ed fields will be completed with the binary information known
346 * the contact with the newer information and optional BKeys
348 public void updateFrom(Contact vc
) {
351 List
<Data
> newDatas
= new LinkedList
<Data
>(vc
.datas
);
352 for (int i
= 0; i
< newDatas
.size(); i
++) {
353 Data data
= newDatas
.get(i
);
354 int bkey
= Parser
.getBKey(data
);
356 if (binaries
.containsKey(bkey
)) {
357 newDatas
.set(i
, binaries
.get(bkey
));
362 this.datas
= newDatas
;
363 this.nextBKey
= vc
.nextBKey
;
370 * Mark all the binary fields with a BKey number.
373 * force the marking, and reset all the numbers.
375 protected void updateBKeys(boolean force
) {
377 binaries
= new HashMap
<Integer
, Data
>();
381 if (binaries
== null) {
382 binaries
= new HashMap
<Integer
, Data
>();
385 for (Data data
: datas
) {
386 if (data
.isBinary() && (data
.getB64Key() <= 0 || force
)) {
387 binaries
.put(nextBKey
, data
);
388 data
.resetB64Key(nextBKey
++);
393 public boolean isDirty() {
398 * Notify that this element has unsaved changes, and notify its parent of
401 protected void setDirty() {
403 if (this.parent
!= null)
404 this.parent
.setDirty();
407 public void setParent(Card parent
) {
408 this.parent
= parent
;
409 for (Data data
: datas
) {
410 data
.setParent(this);