X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Fjvcard%2FData.java;h=ba5fde5df13a9d86381b965aa1487d702ff61be8;hb=30a4aa17f2141ad80a23447ee2e6303f6c9ef995;hp=63362332004fede408a44ee37d2fc3460fa0a64d;hpb=3634193b7a8927e68a3ae3d38fff4f6bd36c4ee5;p=jvcard.git diff --git a/src/be/nikiroo/jvcard/Data.java b/src/be/nikiroo/jvcard/Data.java index 6336233..ba5fde5 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; /** @@ -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,30 @@ 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; + } + + /** + * Check if this {@link Data} has the "preferred" flag. + * + * @return TRUE if it has + */ + public boolean isPreferred() { + for (TypeInfo type : this) { + if (type.getName().equals("TYPE") && type.getValue().equals("pref")) { + return true; + } + } + + return false; + } + /** * Change the bkey of this {@link Data} * @@ -138,27 +221,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; } /** - * Check if this {@link Data} has the "preferred" flag. + * Create the {@link String}-encoded {@link List} of sep-listed values from + * the given values. * - * @return TRUE if it has + * @param values + * the {@link List} of values + * + * @param sep + * the separator + * + * @return the {@link String} */ - public boolean isPreferred() { - for (TypeInfo type : this) { - if (type.getName().equals("TYPE") && type.getValue().equals("pref")) { - return true; - } + 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; } - return false; + value = builder.toString(); } @Override