Commit | Line | Data |
---|---|---|
a3b510ab NR |
1 | package be.nikiroo.jvcard.parsers; |
2 | ||
59597d59 | 3 | import java.io.IOException; |
cf77cb35 | 4 | import java.util.Arrays; |
a3b510ab NR |
5 | import java.util.LinkedList; |
6 | import java.util.List; | |
7 | ||
8 | import be.nikiroo.jvcard.Card; | |
9 | import be.nikiroo.jvcard.Contact; | |
10 | import be.nikiroo.jvcard.Data; | |
11 | ||
12 | public class AbookParser { | |
0b6140e4 NR |
13 | /** |
14 | * Load the given data from under the given {@link Format}. | |
15 | * | |
16 | * @param lines | |
17 | * the input to load from | |
18 | * @param format | |
19 | * the {@link Format} to load as | |
20 | * | |
21 | * @return the list of elements | |
22 | */ | |
23 | public static List<Contact> parseContact(List<String> lines) { | |
a3b510ab | 24 | List<Contact> contacts = new LinkedList<Contact>(); |
78e4af97 | 25 | |
a3b510ab NR |
26 | for (String line : lines) { |
27 | List<Data> content = new LinkedList<Data>(); | |
28 | ||
29 | String tab[] = line.split("\t"); | |
30 | ||
31 | if (tab.length >= 1) | |
32 | content.add(new Data(null, "NICKNAME", tab[0].trim(), null)); | |
33 | if (tab.length >= 2) | |
34 | content.add(new Data(null, "FN", tab[1].trim(), null)); | |
35 | if (tab.length >= 3) | |
36 | content.add(new Data(null, "EMAIL", tab[2].trim(), null)); | |
37 | if (tab.length >= 4) | |
38 | content.add(new Data(null, "X-FCC", tab[3].trim(), null)); | |
39 | if (tab.length >= 5) | |
40 | content.add(new Data(null, "NOTE", tab[4].trim(), null)); | |
41 | ||
42 | contacts.add(new Contact(content)); | |
43 | } | |
44 | ||
45 | return contacts; | |
46 | } | |
47 | ||
cf77cb35 NR |
48 | /** |
49 | * Return a {@link String} representation of the given {@link Card}, line by | |
50 | * line. | |
51 | * | |
52 | * <p> | |
53 | * Note that the BKey is actually not used in Pine mode. | |
54 | * </p> | |
55 | * | |
56 | * @param card | |
57 | * the card to convert | |
58 | * | |
59 | * @param startingBKey | |
60 | * the starting BKey number (all the other will follow) or -1 for | |
d5260eeb | 61 | * no BKey (it is actually not used in this mode) |
cf77cb35 NR |
62 | * |
63 | * @return the {@link String} representation | |
64 | */ | |
d5260eeb NR |
65 | public static List<String> toStrings(Contact contact, |
66 | @SuppressWarnings("unused") int startingBKey) { | |
a3b510ab NR |
67 | // BKey is not used in pine mode |
68 | ||
69 | StringBuilder builder = new StringBuilder(); | |
70 | ||
71 | String nick = contact.getPreferredDataValue("NICKNAME"); | |
72 | if (nick != null) { | |
73 | nick = nick.replaceAll(" ", "_"); | |
74 | nick = nick.replaceAll(",", "-"); | |
75 | nick = nick.replaceAll("@", "(a)"); | |
76 | nick = nick.replaceAll("\"", "'"); | |
77 | nick = nick.replaceAll(";", "."); | |
78 | nick = nick.replaceAll(":", "="); | |
79 | nick = nick.replaceAll("[()\\[\\]<>\\\\]", "/"); | |
80 | ||
81 | builder.append(nick); | |
82 | } | |
83 | ||
84 | builder.append('\t'); | |
85 | ||
86 | String fn = contact.getPreferredDataValue("FN"); | |
87 | if (fn != null) | |
88 | builder.append(fn); | |
89 | ||
90 | builder.append('\t'); | |
91 | ||
92 | String email = contact.getPreferredDataValue("EMAIL"); | |
93 | if (email != null) | |
94 | builder.append(email); | |
95 | ||
96 | // optional fields follow: | |
97 | ||
98 | String xfcc = contact.getPreferredDataValue("X-FCC"); | |
99 | if (xfcc != null) { | |
100 | builder.append('\t'); | |
101 | builder.append(xfcc); | |
102 | } | |
103 | ||
104 | String notes = contact.getPreferredDataValue("NOTE"); | |
105 | if (notes != null) { | |
106 | if (xfcc == null) | |
107 | builder.append('\t'); | |
108 | ||
109 | builder.append('\t'); | |
110 | builder.append(notes); | |
111 | } | |
112 | ||
812124d1 NR |
113 | // abook format = one line per contact |
114 | String out = builder.toString().replace("\n", "\\n"); | |
115 | ||
a3b510ab | 116 | // note: save as pine means normal LN, nor CRLN |
812124d1 | 117 | out = out + "\n"; |
78e4af97 | 118 | |
812124d1 | 119 | return Arrays.asList(new String[] { out }); |
a3b510ab NR |
120 | } |
121 | ||
cf77cb35 NR |
122 | /** |
123 | * Return a {@link String} representation of the given {@link Card}, line by | |
124 | * line. | |
125 | * | |
126 | * @param card | |
127 | * the card to convert | |
128 | * | |
129 | * @return the {@link String} representation | |
130 | */ | |
131 | public static List<String> toStrings(Card card) { | |
132 | List<String> lines = new LinkedList<String>(); | |
a3b510ab | 133 | |
78e4af97 | 134 | for (int index = 0; index < card.size(); index++) { |
cf77cb35 | 135 | lines.addAll(toStrings(card.get(index), -1)); |
a3b510ab NR |
136 | } |
137 | ||
cf77cb35 | 138 | return lines; |
a3b510ab | 139 | } |
59597d59 NR |
140 | |
141 | /** | |
142 | * Write the given {@link Contact} in the {@link Appendable}. | |
143 | * | |
144 | * @param writer | |
145 | * the {@link Appendable} | |
146 | * @param contact | |
147 | * the {@link Contact} to write | |
148 | * @param startingBKey | |
149 | * the starting BKey number (all the other will follow) or -1 for | |
150 | * no BKey | |
151 | * | |
152 | * @throws IOException | |
153 | * in case of IO error | |
154 | */ | |
155 | public static void write(Appendable writer, Contact contact, | |
156 | int startingBKey) throws IOException { | |
157 | for (String s : toStrings(contact, startingBKey)) { | |
158 | writer.append(s); | |
59597d59 NR |
159 | } |
160 | } | |
161 | ||
162 | /** | |
163 | * Write the given {@link Card} in the {@link Appendable}. | |
164 | * | |
165 | * @param writer | |
166 | * the {@link Appendable} | |
167 | * @param card | |
168 | * the {@link Card} to write | |
169 | * | |
170 | * @throws IOException | |
171 | * in case of IO error | |
172 | */ | |
173 | public static void write(Appendable writer, Card card) throws IOException { | |
174 | for (String s : toStrings(card)) { | |
175 | writer.append(s); | |
59597d59 NR |
176 | } |
177 | } | |
a3b510ab | 178 | } |