X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Fjvcard%2Fparsers%2FAbookParser.java;h=b8e6e58e47c2187acf5fd80e8b3c046acacf6d33;hb=59597d59aa262e31c2e1b7f66b4cb299f88ebd1b;hp=913db9b70528066c08ee990f392c66285f348302;hpb=78e4af97505df331618f9c13dd5d98440d364764;p=jvcard.git diff --git a/src/be/nikiroo/jvcard/parsers/AbookParser.java b/src/be/nikiroo/jvcard/parsers/AbookParser.java index 913db9b..b8e6e58 100644 --- a/src/be/nikiroo/jvcard/parsers/AbookParser.java +++ b/src/be/nikiroo/jvcard/parsers/AbookParser.java @@ -1,5 +1,7 @@ package be.nikiroo.jvcard.parsers; +import java.io.IOException; +import java.util.Arrays; import java.util.LinkedList; import java.util.List; @@ -8,7 +10,17 @@ import be.nikiroo.jvcard.Contact; import be.nikiroo.jvcard.Data; public class AbookParser { - public static List parse(List lines) { + /** + * Load the given data from under the given {@link Format}. + * + * @param lines + * the input to load from + * @param format + * the {@link Format} to load as + * + * @return the list of elements + */ + public static List parseContact(List lines) { List contacts = new LinkedList(); for (String line : lines) { @@ -33,8 +45,24 @@ public class AbookParser { return contacts; } - // -1 = no bkeys - public static String toString(Contact contact, int startingBKey) { + /** + * Return a {@link String} representation of the given {@link Card}, line by + * line. + * + *

+ * Note that the BKey is actually not used in Pine mode. + *

+ * + * @param card + * the card to convert + * + * @param startingBKey + * the starting BKey number (all the other will follow) or -1 for + * no BKey + * + * @return the {@link String} representation + */ + public static List toStrings(Contact contact, int startingBKey) { // BKey is not used in pine mode StringBuilder builder = new StringBuilder(); @@ -84,16 +112,65 @@ public class AbookParser { // note: save as pine means normal LN, nor CRLN builder.append('\n'); - return builder.toString(); + return Arrays.asList(new String[] { builder.toString() }); } - public static String toString(Card card) { - StringBuilder builder = new StringBuilder(); + /** + * Return a {@link String} representation of the given {@link Card}, line by + * line. + * + * @param card + * the card to convert + * + * @return the {@link String} representation + */ + public static List toStrings(Card card) { + List lines = new LinkedList(); for (int index = 0; index < card.size(); index++) { - builder.append(toString(card.get(index), -1)); + lines.addAll(toStrings(card.get(index), -1)); } - return builder.toString(); + 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); + writer.append('\n'); + } + } + + /** + * 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); + writer.append('\n'); + } } }