X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Fjvcard%2FData.java;h=fa9e7c25223d5563ce12693708b24f36b90bcbe9;hb=812124d18d08bfc270adab0cd1e289dc6126563b;hp=ef44813a0fca7d62dcaf1ed032434c97a0960be2;hpb=e253bd50bb05519f4a16fed4fb95d5b3340128ea;p=jvcard.git diff --git a/src/be/nikiroo/jvcard/Data.java b/src/be/nikiroo/jvcard/Data.java index ef44813..fa9e7c2 100644 --- a/src/be/nikiroo/jvcard/Data.java +++ b/src/be/nikiroo/jvcard/Data.java @@ -1,6 +1,7 @@ package be.nikiroo.jvcard; import java.security.InvalidParameterException; +import java.util.LinkedList; import java.util.List; /** @@ -8,7 +9,7 @@ import java.util.List; * basically a key/value pair with optional types and an optional group name. * * @author niki - * + * */ public class Data extends BaseClass { public enum DataPart { @@ -30,17 +31,17 @@ public class Data extends BaseClass { * @param types * the types of this {@link Data} * @param name - * its name + * its name (MUST NOT be NULL) * @param value - * its value + * its value (MUST NOT be NULL) * @param group - * its group if any + * its group if any (or NULL if none) */ public Data(List types, String name, String value, String group) { super(types); - this.name = name; - this.value = value; + this.name = name.toUpperCase(); + this.value = value.toString(); // crash NOW if null this.group = group; b64 = -1; @@ -68,7 +69,7 @@ public class Data extends BaseClass { * @return the value */ public String getValue() { - return value; + return unescape(value); } /** @@ -78,6 +79,25 @@ public class Data extends BaseClass { * the new value */ public void setValue(String value) { + setRawValue(escape(value)); + } + + /** + * Return the raw value of this {@link Data} + * + * @return the raw value + */ + public String getRawValue() { + return value; + } + + /** + * Change the raw value of this {@link Data} + * + * @param value + * the new raw value + */ + public void setRawValue(String value) { if ((value == null && this.value != null) || (value != null && !value.equals(this.value))) { this.value = value; @@ -85,6 +105,45 @@ public class Data extends BaseClass { } } + /** + * Return the {@link List} of comma-listed values from this {@link Data}. + * + * @return the {@link List} of values + */ + public List getValues() { + return getList(','); + } + + /** + * Set the {@link List} of comma-listed values from this {@link Data}. + * + * @param values + * the {@link List} of values + */ + public void setValues(List values) { + setList(values, ','); + } + + /** + * Return the {@link List} of semi-column-listed fields from this + * {@link Data}. + * + * @return the {@link List} of values + */ + public List getFields() { + return getList(';'); + } + + /** + * Set the {@link List} of comma-listed values from this {@link Data}. + * + * @param values + * the {@link List} of values + */ + public void setFields(List values) { + setList(values, ';'); + } + /** * Return the group of this {@link Data} * @@ -117,6 +176,35 @@ public class Data extends BaseClass { return b64; } + /** + * Check if this {@link Data} is binary + * + * @return TRUE if it is + */ + public boolean isBinary() { + return b64 >= 0; + } + + /** + * Return the preferred value of this {@link Data}, or + * {@link Integer#MAX_VALUE} if none. + * + * @return the preferred value + */ + public int getPreferred() { + for (TypeInfo type : this) { + if (type.getName().equals("PRE")) { + try { + return Integer.parseInt(type.getValue()); + } catch (NumberFormatException e) { + e.printStackTrace(); + } + } + } + + return Integer.MAX_VALUE; + } + /** * Change the bkey of this {@link Data} * @@ -138,12 +226,62 @@ public class Data extends BaseClass { } /** - * Check if this {@link Data} is binary + * Return the {@link List} of sep-listed values from this {@link String} + * data. * - * @return TRUE if it is + * @param value + * the data + * + * @param the + * separator + * + * @return the {@link List} of values */ - public boolean isBinary() { - return b64 >= 0; + private List getList(char sep) { + List rep = new LinkedList(); + + if (value != null && value.length() > 0) { + int last = 0; + for (int i = 0; i < value.length(); i++) { + if (value.charAt(i) == sep + && (i == 0 || value.charAt(i - 1) != '\\')) { + rep.add(value.substring(last, i)); + last = i + 1; + } + } + + if (last < value.length()) + rep.add(value.substring(last)); + } + + return rep; + } + + /** + * Create the {@link String}-encoded {@link List} of sep-listed values from + * the given values. + * + * @param values + * the {@link List} of values + * + * @param sep + * the separator + * + * @return the {@link String} + */ + private void setList(List values, char sep) { + StringBuilder builder = new StringBuilder(); + boolean first = true; + for (String value : values) { + if (!first) + builder.append(sep); + + builder.append(escape(value)); + + first = false; + } + + value = builder.toString(); } @Override @@ -153,6 +291,19 @@ public class Data extends BaseClass { @Override public String getState() { - return "" + name + value + group; + return ("" + name + value + group).replace(' ', '_'); + } + + @Override + public String toString() { + String out = name + ": " + value; + if (group != null && !group.isEmpty()) { + out += " (" + group + ")"; + } + if (b64 >= 0) { + out += " [" + b64 + "]"; + } + + return out; } }