jDoc, possible leak on crash, new depth option
[jvcard.git] / src / be / nikiroo / jvcard / BaseClass.java
index 1dd76442324ca845d2f603e5dd04c532bd7bdd32..42d4b0dd7f57090a056870f13280f573e6d2146c 100644 (file)
@@ -10,7 +10,7 @@ import java.util.LinkedList;
 import java.util.List;
 import java.util.ListIterator;
 
-import be.nikiroo.jvcard.resources.StringUtils;
+import be.nikiroo.utils.StringUtils;
 
 /**
  * This class is basically a List with a parent and a "dirty" state check. It
@@ -30,7 +30,7 @@ import be.nikiroo.jvcard.resources.StringUtils;
  * </p>
  * 
  * @author niki
- *
+ * 
  * @param <E>
  *            the type of the child elements
  */
@@ -275,7 +275,7 @@ public abstract class BaseClass<E extends BaseClass<?>> implements List<E> {
        public String getContentState(boolean self) {
                StringBuilder builder = new StringBuilder();
                buildContentStateRaw(builder, self);
-               return StringUtils.getHash(builder.toString());
+               return StringUtils.getMd5Hash(builder.toString());
        }
 
        /**
@@ -307,7 +307,7 @@ public abstract class BaseClass<E extends BaseClass<?>> implements List<E> {
         * 
         * @param depth
         *            the depth into which to descend (0 = only this object, not its
-        *            children)
+        *            children, negative value for infinite depth)
         * 
         * @return the debug {@link String}
         */
@@ -333,7 +333,7 @@ public abstract class BaseClass<E extends BaseClass<?>> implements List<E> {
         * 
         * <p>
         * Not that this state is <b>lossy</b>. You cannot retrieve the data from
-        * the state, it can only be used as an ID to check if thw data are
+        * the state, it can only be used as an ID to check if the data are
         * identical.
         * </p>
         * 
@@ -346,7 +346,7 @@ public abstract class BaseClass<E extends BaseClass<?>> implements List<E> {
         * Get the recursive state of the current object, i.e., its children
         * included. It represents the full state information about this object's
         * children.
-        * 
+        * <p>
         * It is not hashed.
         * 
         * @param builder
@@ -372,7 +372,7 @@ public abstract class BaseClass<E extends BaseClass<?>> implements List<E> {
         * 
         * @param depth
         *            the depth into which to descend (0 = only this object, not its
-        *            children)
+        *            children, negative value for infinite depth)
         * 
         * @param tab
         *            the current tabulation increment
@@ -382,16 +382,16 @@ public abstract class BaseClass<E extends BaseClass<?>> implements List<E> {
                        builder.append("        ");
                builder.append(getContentState(false) + "       " + getId());
 
-               if (depth > 0)
+               if (depth != 0)
                        builder.append(": [");
 
-               if (depth > 0) {
+               if (depth != 0) {
                        for (E child : this) {
                                builder.append("\n");
                                child.getDebugInfo(builder, depth - 1, tab + 1);
                        }
                }
-               if (depth > 0) {
+               if (depth != 0) {
                        builder.append("\n");
                        for (int i = 0; i < tab; i++)
                                builder.append("        ");
@@ -400,7 +400,8 @@ public abstract class BaseClass<E extends BaseClass<?>> implements List<E> {
        }
 
        /**
-        * Notify that this element has unsaved changes.
+        * Notify that this element <i>and all its parent elements</i> has unsaved
+        * changes.
         */
        void setDirty() {
                dirty = true;
@@ -421,7 +422,10 @@ public abstract class BaseClass<E extends BaseClass<?>> implements List<E> {
        }
 
        /**
-        * Set the parent of this element <i>and all its descendants</i>.
+        * Set the parent of this element.
+        * <p>
+        * Will also check and fix if needed the parent (this) of all its
+        * descendants (recursively).
         * 
         * @param parent
         *            the new parent
@@ -433,6 +437,38 @@ public abstract class BaseClass<E extends BaseClass<?>> implements List<E> {
                }
        }
 
+       /**
+        * Escape the given value to VCF standard.
+        * 
+        * @param value
+        *            the value to escape
+        * 
+        * @return the escaped value
+        */
+       protected String escape(String value) {
+               if (value == null)
+                       return null;
+
+               return value.replaceAll(",", "\\\\,").replaceAll(";", "\\\\;")
+                               .replaceAll("\n", "\\\\n");
+       }
+
+       /**
+        * Unescape the given value from the VCF standard.
+        * 
+        * @param value
+        *            the value to unescape
+        * 
+        * @return the unescaped value
+        */
+       protected String unescape(String value) {
+               if (value == null)
+                       return null;
+
+               return value.replaceAll("\\\\,", ",").replaceAll("\\\\;", ";")
+                               .replaceAll("\\\\n", "\n");
+       }
+
        /**
         * Each element that leaves the parent will pass trough here.
         *