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 * Return the preferred value of this {@link Data}, or
190 * {@link Integer#MAX_VALUE} if none.
192 * @return the preferred value
194 public int getPreferred() {
195 for (TypeInfo type
: this) {
196 if (type
.getName().equals("PRE")) {
198 return Integer
.parseInt(type
.getValue());
199 } catch (NumberFormatException e
) {
205 return Integer
.MAX_VALUE
;
209 * Change the bkey of this {@link Data}
214 * @throw InvalidParameterException if the {@link Data} is not binary or if
215 * it is but you try to set a negative bkey
217 void resetB64Key(int i
) {
219 throw new InvalidParameterException(
220 "Cannot add a BKey on a non-binary object");
222 throw new InvalidParameterException(
223 "Cannot remove the BKey on a binary object");
229 * Return the {@link List} of sep-listed values from this {@link String}
238 * @return the {@link List} of values
240 private List
<String
> getList(char sep
) {
241 List
<String
> rep
= new LinkedList
<String
>();
243 if (value
!= null && value
.length() > 0) {
245 for (int i
= 0; i
< value
.length(); i
++) {
246 if (value
.charAt(i
) == sep
247 && (i
== 0 || value
.charAt(i
- 1) != '\\')) {
248 rep
.add(value
.substring(last
, i
));
253 if (last
< value
.length())
254 rep
.add(value
.substring(last
));
261 * Create the {@link String}-encoded {@link List} of sep-listed values from
265 * the {@link List} of values
270 * @return the {@link String}
272 private void setList(List
<String
> values
, char sep
) {
273 StringBuilder builder
= new StringBuilder();
274 boolean first
= true;
275 for (String value
: values
) {
279 builder
.append(escape(value
));
284 value
= builder
.toString();
288 public String
getId() {
293 public String
getState() {
294 return ("" + name
+ value
+ group
).replace(' ', '_');