1 package be
.nikiroo
.jvcard
;
3 import java
.security
.InvalidParameterException
;
4 import java
.util
.LinkedList
;
8 * A 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.
14 public class Data
extends BaseClass
<TypeInfo
> {
15 public enum DataPart
{
16 FN_FAMILY
, FN_GIVEN
, FN_ADDITIONAL
, // Name
17 FN_PRE
, FN_POST
, // Pre/Post
18 BDAY_YYYY
, BDAY_MM
, BDAY_DD
, // BDay
19 ADR_PBOX
, ADR_EXTENDED
, ADR_STREET
, ADR_CITY
, ADR_REGION
, ADR_POSTAL_CODE
, ADR_COUNTRY
26 private int b64
; // -1 = no, 0 = still not ordered, the rest is order
29 * Create a new {@link Data} with the given values.
32 * the types of this {@link Data}
34 * its name (<b>MUST NOT</b> be NULL)
36 * its value (<b>MUST NOT</b> be NULL)
38 * its group if any (or NULL if none)
40 public Data(List
<TypeInfo
> types
, String name
, String value
, String group
) {
43 this.name
= name
.toUpperCase();
44 this.value
= value
.toString(); // crash NOW if null
48 for (TypeInfo type
: this) {
49 if (type
.getName().equals("ENCODING")
50 && type
.getValue().equals("b")) {
58 * Return the name of this {@link Data}
62 public String
getName() {
67 * Return the value of this {@link Data}
71 public String
getValue() {
72 return unescape(value
);
76 * Change the value of this {@link Data}
81 public void setValue(String value
) {
82 setRawValue(escape(value
));
86 * Return the raw value of this {@link Data}
88 * @return the raw value
90 public String
getRawValue() {
95 * Change the raw value of this {@link Data}
100 public void setRawValue(String value
) {
101 if ((value
== null && this.value
!= null)
102 || (value
!= null && !value
.equals(this.value
))) {
109 * Return the {@link List} of comma-listed values from this {@link Data}.
111 * @return the {@link List} of values
113 public List
<String
> getValues() {
118 * Set the {@link List} of comma-listed values from this {@link Data}.
121 * the {@link List} of values
123 public void setValues(List
<String
> values
) {
124 setList(values
, ',');
128 * Return the {@link List} of semi-column-listed fields from this
131 * @return the {@link List} of values
133 public List
<String
> getFields() {
138 * Set the {@link List} of comma-listed values from this {@link Data}.
141 * the {@link List} of values
143 public void setFields(List
<String
> values
) {
144 setList(values
, ';');
148 * Return the group of this {@link Data}
152 public String
getGroup() {
157 * Change the group of this {@link Data}
162 public void setGroup(String group
) {
163 if ((group
== null && this.group
!= null)
164 || (group
!= null && !group
.equals(this.group
))) {
171 * Return the bkey number of this {@link Data} or -1 if it is not binary.
173 * @return the bkey or -1
175 public int getB64Key() {
180 * Check if this {@link Data} is binary
182 * @return TRUE if it is
184 public boolean isBinary() {
189 * Check if this {@link Data} has the "preferred" flag.
191 * @return TRUE if it has
193 public boolean isPreferred() {
194 for (TypeInfo type
: this) {
195 if (type
.getName().equals("TYPE") && type
.getValue().equals("pref")) {
204 * Change the bkey of this {@link Data}
209 * @throw InvalidParameterException if the {@link Data} is not binary or if
210 * it is but you try to set a negative bkey
212 void resetB64Key(int i
) {
214 throw new InvalidParameterException(
215 "Cannot add a BKey on a non-binary object");
217 throw new InvalidParameterException(
218 "Cannot remove the BKey on a binary object");
224 * Return the {@link List} of sep-listed values from this {@link String}
233 * @return the {@link List} of values
235 private List
<String
> getList(char sep
) {
236 List
<String
> rep
= new LinkedList
<String
>();
238 if (value
!= null && value
.length() > 0) {
240 for (int i
= 0; i
< value
.length(); i
++) {
241 if (value
.charAt(i
) == sep
242 && (i
== 0 || value
.charAt(i
- 1) != '\\')) {
243 rep
.add(value
.substring(last
, i
));
248 if (last
< value
.length())
249 rep
.add(value
.substring(last
));
256 * Create the {@link String}-encoded {@link List} of sep-listed values from
260 * the {@link List} of values
265 * @return the {@link String}
267 private void setList(List
<String
> values
, char sep
) {
268 StringBuilder builder
= new StringBuilder();
269 boolean first
= true;
270 for (String value
: values
) {
274 builder
.append(escape(value
));
279 value
= builder
.toString();
283 public String
getId() {
288 public String
getState() {
289 return ("" + name
+ value
+ group
).replace(' ', '_');