package be.nikiroo.jvcard.parsers;
+import java.io.IOException;
+import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import be.nikiroo.jvcard.Data;
public class AbookParser {
- public static List<Contact> parse(List<String> 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<Contact> parseContact(List<String> lines) {
List<Contact> contacts = new LinkedList<Contact>();
-
+
for (String line : lines) {
List<Data> content = new LinkedList<Data>();
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.
+ *
+ * <p>
+ * Note that the BKey is actually not used in Pine mode.
+ * </p>
+ *
+ * @param card
+ * the card to convert
+ *
+ * @param startingBKey
+ * the starting BKey number (all the other will follow) or -1 for
+ * no BKey (it is actually not used in this mode)
+ *
+ * @return the {@link String} representation
+ */
+ public static List<String> toStrings(Contact contact,
+ @SuppressWarnings("unused") int startingBKey) {
// BKey is not used in pine mode
StringBuilder builder = new StringBuilder();
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');
-
- return builder.toString();
+ out = out + "\n";
+
+ return Arrays.asList(new String[] { out });
}
- 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<String> toStrings(Card card) {
+ List<String> lines = new LinkedList<String>();
+
+ for (int index = 0; index < card.size(); index++) {
+ lines.addAll(toStrings(card.get(index), -1));
+ }
- for (Contact contact : card.getContactsList()) {
- builder.append(toString(contact, -1));
+ 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);
}
+ }
- return builder.toString();
+ /**
+ * 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);
+ }
}
}