| 1 | package be.nikiroo.jvcard; |
| 2 | |
| 3 | import java.security.InvalidParameterException; |
| 4 | import java.util.LinkedList; |
| 5 | import java.util.List; |
| 6 | |
| 7 | /** |
| 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. |
| 10 | * |
| 11 | * @author niki |
| 12 | * |
| 13 | */ |
| 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 |
| 20 | // Address |
| 21 | } |
| 22 | |
| 23 | private String name; |
| 24 | private String value; |
| 25 | private String group; |
| 26 | private int b64; // -1 = no, 0 = still not ordered, the rest is order |
| 27 | |
| 28 | /** |
| 29 | * Create a new {@link Data} with the given values. |
| 30 | * |
| 31 | * @param types |
| 32 | * the types of this {@link Data} |
| 33 | * @param name |
| 34 | * its name (<b>MUST NOT</b> be NULL) |
| 35 | * @param value |
| 36 | * its value (<b>MUST NOT</b> be NULL) |
| 37 | * @param group |
| 38 | * its group if any (or NULL if none) |
| 39 | */ |
| 40 | public Data(List<TypeInfo> types, String name, String value, String group) { |
| 41 | super(types); |
| 42 | |
| 43 | this.name = name.toUpperCase(); |
| 44 | this.value = value.toString(); // crash NOW if null |
| 45 | this.group = group; |
| 46 | |
| 47 | b64 = -1; |
| 48 | for (TypeInfo type : this) { |
| 49 | if (type.getName().equals("ENCODING") |
| 50 | && type.getValue().equals("b")) { |
| 51 | b64 = 0; |
| 52 | break; |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Return the name of this {@link Data} |
| 59 | * |
| 60 | * @return the name |
| 61 | */ |
| 62 | public String getName() { |
| 63 | return name; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Return the value of this {@link Data} |
| 68 | * |
| 69 | * @return the value |
| 70 | */ |
| 71 | public String getValue() { |
| 72 | return unescape(value); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Change the value of this {@link Data} |
| 77 | * |
| 78 | * @param value |
| 79 | * the new value |
| 80 | */ |
| 81 | public void setValue(String value) { |
| 82 | setRawValue(escape(value)); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Return the raw value of this {@link Data} |
| 87 | * |
| 88 | * @return the raw value |
| 89 | */ |
| 90 | public String getRawValue() { |
| 91 | return value; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Change the raw value of this {@link Data} |
| 96 | * |
| 97 | * @param value |
| 98 | * the new raw value |
| 99 | */ |
| 100 | public void setRawValue(String value) { |
| 101 | if ((value == null && this.value != null) |
| 102 | || (value != null && !value.equals(this.value))) { |
| 103 | this.value = value; |
| 104 | setDirty(); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Return the {@link List} of comma-listed values from this {@link Data}. |
| 110 | * |
| 111 | * @return the {@link List} of values |
| 112 | */ |
| 113 | public List<String> getValues() { |
| 114 | return getList(','); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Set the {@link List} of comma-listed values from this {@link Data}. |
| 119 | * |
| 120 | * @param values |
| 121 | * the {@link List} of values |
| 122 | */ |
| 123 | public void setValues(List<String> values) { |
| 124 | setList(values, ','); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Return the {@link List} of semi-column-listed fields from this |
| 129 | * {@link Data}. |
| 130 | * |
| 131 | * @return the {@link List} of values |
| 132 | */ |
| 133 | public List<String> getFields() { |
| 134 | return getList(';'); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Set the {@link List} of comma-listed values from this {@link Data}. |
| 139 | * |
| 140 | * @param values |
| 141 | * the {@link List} of values |
| 142 | */ |
| 143 | public void setFields(List<String> values) { |
| 144 | setList(values, ';'); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Return the group of this {@link Data} |
| 149 | * |
| 150 | * @return the group |
| 151 | */ |
| 152 | public String getGroup() { |
| 153 | return group; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Change the group of this {@link Data} |
| 158 | * |
| 159 | * @param group |
| 160 | * the new group |
| 161 | */ |
| 162 | public void setGroup(String group) { |
| 163 | if ((group == null && this.group != null) |
| 164 | || (group != null && !group.equals(this.group))) { |
| 165 | this.group = group; |
| 166 | setDirty(); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Return the bkey number of this {@link Data} or -1 if it is not binary. |
| 172 | * |
| 173 | * @return the bkey or -1 |
| 174 | */ |
| 175 | public int getB64Key() { |
| 176 | return b64; |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Check if this {@link Data} is binary |
| 181 | * |
| 182 | * @return TRUE if it is |
| 183 | */ |
| 184 | public boolean isBinary() { |
| 185 | return b64 >= 0; |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Return the preferred value of this {@link Data}, or |
| 190 | * {@link Integer#MAX_VALUE} if none. |
| 191 | * |
| 192 | * @return the preferred value |
| 193 | */ |
| 194 | public int getPreferred() { |
| 195 | for (TypeInfo type : this) { |
| 196 | if (type.getName().equals("PRE")) { |
| 197 | try { |
| 198 | return Integer.parseInt(type.getValue()); |
| 199 | } catch (NumberFormatException e) { |
| 200 | e.printStackTrace(); |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | return Integer.MAX_VALUE; |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Change the bkey of this {@link Data} |
| 210 | * |
| 211 | * @param i |
| 212 | * the new bkey |
| 213 | * |
| 214 | * @throw InvalidParameterException if the {@link Data} is not binary or if |
| 215 | * it is but you try to set a negative bkey |
| 216 | */ |
| 217 | void resetB64Key(int i) { |
| 218 | if (!isBinary()) |
| 219 | throw new InvalidParameterException( |
| 220 | "Cannot add a BKey on a non-binary object"); |
| 221 | if (i < 0) |
| 222 | throw new InvalidParameterException( |
| 223 | "Cannot remove the BKey on a binary object"); |
| 224 | |
| 225 | b64 = i; |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Return the {@link List} of sep-listed values from this {@link String} |
| 230 | * data. |
| 231 | * |
| 232 | * @param value |
| 233 | * the data |
| 234 | * |
| 235 | * @param the |
| 236 | * separator |
| 237 | * |
| 238 | * @return the {@link List} of values |
| 239 | */ |
| 240 | private List<String> getList(char sep) { |
| 241 | List<String> rep = new LinkedList<String>(); |
| 242 | |
| 243 | if (value != null && value.length() > 0) { |
| 244 | int last = 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)); |
| 249 | last = i + 1; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | if (last < value.length()) |
| 254 | rep.add(value.substring(last)); |
| 255 | } |
| 256 | |
| 257 | return rep; |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * Create the {@link String}-encoded {@link List} of sep-listed values from |
| 262 | * the given values. |
| 263 | * |
| 264 | * @param values |
| 265 | * the {@link List} of values |
| 266 | * |
| 267 | * @param sep |
| 268 | * the separator |
| 269 | * |
| 270 | * @return the {@link String} |
| 271 | */ |
| 272 | private void setList(List<String> values, char sep) { |
| 273 | StringBuilder builder = new StringBuilder(); |
| 274 | boolean first = true; |
| 275 | for (String value : values) { |
| 276 | if (!first) |
| 277 | builder.append(sep); |
| 278 | |
| 279 | builder.append(escape(value)); |
| 280 | |
| 281 | first = false; |
| 282 | } |
| 283 | |
| 284 | value = builder.toString(); |
| 285 | } |
| 286 | |
| 287 | @Override |
| 288 | public String getId() { |
| 289 | return "" + name; |
| 290 | } |
| 291 | |
| 292 | @Override |
| 293 | public String getState() { |
| 294 | return ("" + name + value + group).replace(' ', '_'); |
| 295 | } |
| 296 | } |