New SAVE_TO option
[jvcard.git] / src / be / nikiroo / jvcard / parsers / AbookParser.java
index cb4f667119be22b0b8cf8713a1b7f439511c30a4..17080225b34cc5d9fa9cbc7c1a58fd69b5da8d6c 100644 (file)
@@ -1,5 +1,6 @@
 package be.nikiroo.jvcard.parsers;
 
+import java.io.IOException;
 import java.util.Arrays;
 import java.util.LinkedList;
 import java.util.List;
@@ -57,11 +58,12 @@ public class AbookParser {
         * 
         * @param startingBKey
         *            the starting BKey number (all the other will follow) or -1 for
-        *            no BKey
+        *            no BKey (it is actually not used in this mode)
         * 
         * @return the {@link String} representation
         */
-       public static List<String> toStrings(Contact contact, int startingBKey) {
+       public static List<String> toStrings(Contact contact,
+                       @SuppressWarnings("unused") int startingBKey) {
                // BKey is not used in pine mode
 
                StringBuilder builder = new StringBuilder();
@@ -108,10 +110,13 @@ public class AbookParser {
                        builder.append(notes);
                }
 
+               // abook format = one line per contact
+               String out = builder.toString().replace("\n", "\\n");
+
                // note: save as pine means normal LN, nor CRLN
-               builder.append('\n');
+               out = out + "\n";
 
-               return Arrays.asList(new String[] { builder.toString() });
+               return Arrays.asList(new String[] { out });
        }
 
        /**
@@ -132,4 +137,42 @@ public class AbookParser {
 
                return lines;
        }
+
+       /**
+        * Write the given {@link Contact} in the {@link Appendable}.
+        * 
+        * @param writer
+        *            the {@link Appendable}
+        * @param contact
+        *            the {@link Contact} to write
+        * @param startingBKey
+        *            the starting BKey number (all the other will follow) or -1 for
+        *            no BKey
+        * 
+        * @throws IOException
+        *             in case of IO error
+        */
+       public static void write(Appendable writer, Contact contact,
+                       int startingBKey) throws IOException {
+               for (String s : toStrings(contact, startingBKey)) {
+                       writer.append(s);
+               }
+       }
+
+       /**
+        * Write the given {@link Card} in the {@link Appendable}.
+        * 
+        * @param writer
+        *            the {@link Appendable}
+        * @param card
+        *            the {@link Card} to write
+        * 
+        * @throws IOException
+        *             in case of IO error
+        */
+       public static void write(Appendable writer, Card card) throws IOException {
+               for (String s : toStrings(card)) {
+                       writer.append(s);
+               }
+       }
 }