package be.nikiroo.jvcard;
import java.security.InvalidParameterException;
+import java.util.LinkedList;
import java.util.List;
/**
* @return the value
*/
public String getValue() {
- return value;
+ return unescape(value);
}
/**
* 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;
}
}
+ /**
+ * Return the {@link List} of comma-listed values from this {@link Data}.
+ *
+ * @return the {@link List} of values
+ */
+ public List<String> 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<String> values) {
+ setList(values, ',');
+ }
+
+ /**
+ * Return the {@link List} of semi-column-listed fields from this
+ * {@link Data}.
+ *
+ * @return the {@link List} of values
+ */
+ public List<String> 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<String> values) {
+ setList(values, ';');
+ }
+
/**
* Return the group of this {@link Data}
*
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}
*
}
/**
- * 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<String> getList(char sep) {
+ List<String> rep = new LinkedList<String>();
+
+ 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<String> 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