1 package be
.nikiroo
.jvcard
;
3 import java
.security
.InvalidParameterException
;
4 import java
.util
.LinkedList
;
8 * A {@link Data} is a piece of information present in a {@link Contact}. It is
9 * basically a key/value pair with optional types and an optional group name.
11 * A {@link Data} can also be binary encoded: in this case, it has an associated
12 * BKey number to identify it.
16 public class Data
extends BaseClass
<TypeInfo
> {
17 public enum DataPart
{
18 FN_FAMILY
, FN_GIVEN
, FN_ADDITIONAL
, // Name
19 FN_PRE
, FN_POST
, // Pre/Post
20 BDAY_YYYY
, BDAY_MM
, BDAY_DD
, // BDay
22 ADR_PBOX
, ADR_EXTENDED
, ADR_STREET
, ADR_CITY
, ADR_REGION
, ADR_POSTAL_CODE
, ADR_COUNTRY
28 private int b64
; // -1 = no, 0 = still not ordered, the rest is order
31 * Create a new {@link Data} with the given values.
34 * the types of this {@link Data}
36 * its name (<b>MUST NOT</b> be NULL)
38 * its value (<b>MUST NOT</b> be NULL)
40 * its group if any (or NULL if none)
42 public Data(List
<TypeInfo
> types
, String name
, String value
, String group
) {
45 this.name
= name
.toUpperCase();
46 this.value
= value
.toString(); // crash NOW if null
50 for (TypeInfo type
: this) {
51 if (type
.getName().equals("ENCODING")
52 && type
.getValue().equals("b")) {
60 * Return the name of this {@link Data}
64 public String
getName() {
69 * Return the value of this {@link Data}
73 public String
getValue() {
74 return unescape(value
);
78 * Change the value of this {@link Data}
83 public void setValue(String value
) {
84 setRawValue(escape(value
));
88 * Return the raw value of this {@link Data}
90 * @return the raw value
92 public String
getRawValue() {
97 * Change the raw value of this {@link Data}
102 public void setRawValue(String value
) {
103 if ((value
== null && this.value
!= null)
104 || (value
!= null && !value
.equals(this.value
))) {
111 * Return the {@link List} of comma-listed values from this {@link Data}.
113 * @return the {@link List} of values
115 public List
<String
> getValues() {
120 * Set the {@link List} of comma-listed values from this {@link Data}.
123 * the {@link List} of values
125 public void setValues(List
<String
> values
) {
126 setList(values
, ',');
130 * Return the {@link List} of semi-column-listed fields from this
133 * @return the {@link List} of values
135 public List
<String
> getFields() {
140 * Set the {@link List} of comma-listed values from this {@link Data}.
143 * the {@link List} of values
145 public void setFields(List
<String
> values
) {
146 setList(values
, ';');
150 * Return the group of this {@link Data}
154 public String
getGroup() {
159 * Change the group of this {@link Data}
164 public void setGroup(String group
) {
165 if ((group
== null && this.group
!= null)
166 || (group
!= null && !group
.equals(this.group
))) {
173 * Return the bkey number of this {@link Data} or -1 if it is not binary.
175 * For binary data, as long as the BKey is not processed, it will be 0.
177 * @return the bkey, 0 or -1
179 public int getB64Key() {
184 * Check if this {@link Data} is binary (in this case, the BKey will be
187 * @return TRUE if it is
189 public boolean isBinary() {
194 * Return the preferred value of this {@link Data}, or
195 * {@link Integer#MAX_VALUE} if none.
197 * @return the preferred value
199 public int getPreferred() {
200 for (TypeInfo type
: this) {
201 if (type
.getName().equals("PRE")) {
203 return Integer
.parseInt(type
.getValue());
204 } catch (NumberFormatException e
) {
210 return Integer
.MAX_VALUE
;
214 * Change the bkey of this {@link Data}
219 * @throw InvalidParameterException if the {@link Data} is not binary or if
220 * it is but you try to set a negative bkey
222 void resetB64Key(int i
) {
224 throw new InvalidParameterException(
225 "Cannot add a BKey on a non-binary object");
227 throw new InvalidParameterException(
228 "Cannot remove the BKey on a binary object");
234 * Return the {@link List} of sep-listed values from this {@link String}
237 * Will take the backslash character into account (i.e., a backslash can
238 * escape the given separator).
246 * @return the {@link List} of values
248 private List
<String
> getList(char sep
) {
249 List
<String
> rep
= new LinkedList
<String
>();
251 if (value
!= null && value
.length() > 0) {
253 for (int i
= 0; i
< value
.length(); i
++) {
254 if (value
.charAt(i
) == sep
255 && (i
== 0 || value
.charAt(i
- 1) != '\\')) {
256 rep
.add(value
.substring(last
, i
));
261 if (last
< value
.length())
262 rep
.add(value
.substring(last
));
269 * Create the {@link String}-encoded {@link List} of sep-listed values from
273 * the {@link List} of values
278 * @return the {@link String}
280 private void setList(List
<String
> values
, char sep
) {
281 StringBuilder builder
= new StringBuilder();
282 boolean first
= true;
283 for (String value
: values
) {
287 builder
.append(escape(value
));
292 value
= builder
.toString();
296 public String
getId() {
301 public String
getState() {
302 return ("" + name
+ value
+ group
).replace(' ', '_');
306 public String
toString() {
307 String out
= name
+ ": " + value
;
308 if (group
!= null && !group
.isEmpty()) {
309 out
+= " (" + group
+ ")";
312 out
+= " [" + b64
+ "]";