| 1 | package be.nikiroo.jvcard.parsers; |
| 2 | |
| 3 | import java.util.Arrays; |
| 4 | import java.util.LinkedList; |
| 5 | import java.util.List; |
| 6 | |
| 7 | import be.nikiroo.jvcard.Card; |
| 8 | import be.nikiroo.jvcard.Contact; |
| 9 | import be.nikiroo.jvcard.Data; |
| 10 | |
| 11 | public class AbookParser { |
| 12 | /** |
| 13 | * Load the given data from under the given {@link Format}. |
| 14 | * |
| 15 | * @param lines |
| 16 | * the input to load from |
| 17 | * @param format |
| 18 | * the {@link Format} to load as |
| 19 | * |
| 20 | * @return the list of elements |
| 21 | */ |
| 22 | public static List<Contact> parseContact(List<String> lines) { |
| 23 | List<Contact> contacts = new LinkedList<Contact>(); |
| 24 | |
| 25 | for (String line : lines) { |
| 26 | List<Data> content = new LinkedList<Data>(); |
| 27 | |
| 28 | String tab[] = line.split("\t"); |
| 29 | |
| 30 | if (tab.length >= 1) |
| 31 | content.add(new Data(null, "NICKNAME", tab[0].trim(), null)); |
| 32 | if (tab.length >= 2) |
| 33 | content.add(new Data(null, "FN", tab[1].trim(), null)); |
| 34 | if (tab.length >= 3) |
| 35 | content.add(new Data(null, "EMAIL", tab[2].trim(), null)); |
| 36 | if (tab.length >= 4) |
| 37 | content.add(new Data(null, "X-FCC", tab[3].trim(), null)); |
| 38 | if (tab.length >= 5) |
| 39 | content.add(new Data(null, "NOTE", tab[4].trim(), null)); |
| 40 | |
| 41 | contacts.add(new Contact(content)); |
| 42 | } |
| 43 | |
| 44 | return contacts; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Return a {@link String} representation of the given {@link Card}, line by |
| 49 | * line. |
| 50 | * |
| 51 | * <p> |
| 52 | * Note that the BKey is actually not used in Pine mode. |
| 53 | * </p> |
| 54 | * |
| 55 | * @param card |
| 56 | * the card to convert |
| 57 | * |
| 58 | * @param startingBKey |
| 59 | * the starting BKey number (all the other will follow) or -1 for |
| 60 | * no BKey |
| 61 | * |
| 62 | * @return the {@link String} representation |
| 63 | */ |
| 64 | public static List<String> toStrings(Contact contact, int startingBKey) { |
| 65 | // BKey is not used in pine mode |
| 66 | |
| 67 | StringBuilder builder = new StringBuilder(); |
| 68 | |
| 69 | String nick = contact.getPreferredDataValue("NICKNAME"); |
| 70 | if (nick != null) { |
| 71 | nick = nick.replaceAll(" ", "_"); |
| 72 | nick = nick.replaceAll(",", "-"); |
| 73 | nick = nick.replaceAll("@", "(a)"); |
| 74 | nick = nick.replaceAll("\"", "'"); |
| 75 | nick = nick.replaceAll(";", "."); |
| 76 | nick = nick.replaceAll(":", "="); |
| 77 | nick = nick.replaceAll("[()\\[\\]<>\\\\]", "/"); |
| 78 | |
| 79 | builder.append(nick); |
| 80 | } |
| 81 | |
| 82 | builder.append('\t'); |
| 83 | |
| 84 | String fn = contact.getPreferredDataValue("FN"); |
| 85 | if (fn != null) |
| 86 | builder.append(fn); |
| 87 | |
| 88 | builder.append('\t'); |
| 89 | |
| 90 | String email = contact.getPreferredDataValue("EMAIL"); |
| 91 | if (email != null) |
| 92 | builder.append(email); |
| 93 | |
| 94 | // optional fields follow: |
| 95 | |
| 96 | String xfcc = contact.getPreferredDataValue("X-FCC"); |
| 97 | if (xfcc != null) { |
| 98 | builder.append('\t'); |
| 99 | builder.append(xfcc); |
| 100 | } |
| 101 | |
| 102 | String notes = contact.getPreferredDataValue("NOTE"); |
| 103 | if (notes != null) { |
| 104 | if (xfcc == null) |
| 105 | builder.append('\t'); |
| 106 | |
| 107 | builder.append('\t'); |
| 108 | builder.append(notes); |
| 109 | } |
| 110 | |
| 111 | // note: save as pine means normal LN, nor CRLN |
| 112 | builder.append('\n'); |
| 113 | |
| 114 | return Arrays.asList(new String[] { builder.toString() }); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Return a {@link String} representation of the given {@link Card}, line by |
| 119 | * line. |
| 120 | * |
| 121 | * @param card |
| 122 | * the card to convert |
| 123 | * |
| 124 | * @return the {@link String} representation |
| 125 | */ |
| 126 | public static List<String> toStrings(Card card) { |
| 127 | List<String> lines = new LinkedList<String>(); |
| 128 | |
| 129 | for (int index = 0; index < card.size(); index++) { |
| 130 | lines.addAll(toStrings(card.get(index), -1)); |
| 131 | } |
| 132 | |
| 133 | return lines; |
| 134 | } |
| 135 | } |