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 | |
61 | * no BKey | |
62 | * | |
63 | * @return the {@link String} representation | |
64 | */ | |
65 | public static List<String> toStrings(Contact contact, int startingBKey) { | |
a3b510ab NR |
66 | // BKey is not used in pine mode |
67 | ||
68 | StringBuilder builder = new StringBuilder(); | |
69 | ||
70 | String nick = contact.getPreferredDataValue("NICKNAME"); | |
71 | if (nick != null) { | |
72 | nick = nick.replaceAll(" ", "_"); | |
73 | nick = nick.replaceAll(",", "-"); | |
74 | nick = nick.replaceAll("@", "(a)"); | |
75 | nick = nick.replaceAll("\"", "'"); | |
76 | nick = nick.replaceAll(";", "."); | |
77 | nick = nick.replaceAll(":", "="); | |
78 | nick = nick.replaceAll("[()\\[\\]<>\\\\]", "/"); | |
79 | ||
80 | builder.append(nick); | |
81 | } | |
82 | ||
83 | builder.append('\t'); | |
84 | ||
85 | String fn = contact.getPreferredDataValue("FN"); | |
86 | if (fn != null) | |
87 | builder.append(fn); | |
88 | ||
89 | builder.append('\t'); | |
90 | ||
91 | String email = contact.getPreferredDataValue("EMAIL"); | |
92 | if (email != null) | |
93 | builder.append(email); | |
94 | ||
95 | // optional fields follow: | |
96 | ||
97 | String xfcc = contact.getPreferredDataValue("X-FCC"); | |
98 | if (xfcc != null) { | |
99 | builder.append('\t'); | |
100 | builder.append(xfcc); | |
101 | } | |
102 | ||
103 | String notes = contact.getPreferredDataValue("NOTE"); | |
104 | if (notes != null) { | |
105 | if (xfcc == null) | |
106 | builder.append('\t'); | |
107 | ||
108 | builder.append('\t'); | |
109 | builder.append(notes); | |
110 | } | |
111 | ||
112 | // note: save as pine means normal LN, nor CRLN | |
113 | builder.append('\n'); | |
78e4af97 | 114 | |
cf77cb35 | 115 | return Arrays.asList(new String[] { builder.toString() }); |
a3b510ab NR |
116 | } |
117 | ||
cf77cb35 NR |
118 | /** |
119 | * Return a {@link String} representation of the given {@link Card}, line by | |
120 | * line. | |
121 | * | |
122 | * @param card | |
123 | * the card to convert | |
124 | * | |
125 | * @return the {@link String} representation | |
126 | */ | |
127 | public static List<String> toStrings(Card card) { | |
128 | List<String> lines = new LinkedList<String>(); | |
a3b510ab | 129 | |
78e4af97 | 130 | for (int index = 0; index < card.size(); index++) { |
cf77cb35 | 131 | lines.addAll(toStrings(card.get(index), -1)); |
a3b510ab NR |
132 | } |
133 | ||
cf77cb35 | 134 | return lines; |
a3b510ab | 135 | } |
59597d59 NR |
136 | |
137 | /** | |
138 | * Write the given {@link Contact} in the {@link Appendable}. | |
139 | * | |
140 | * @param writer | |
141 | * the {@link Appendable} | |
142 | * @param contact | |
143 | * the {@link Contact} to write | |
144 | * @param startingBKey | |
145 | * the starting BKey number (all the other will follow) or -1 for | |
146 | * no BKey | |
147 | * | |
148 | * @throws IOException | |
149 | * in case of IO error | |
150 | */ | |
151 | public static void write(Appendable writer, Contact contact, | |
152 | int startingBKey) throws IOException { | |
153 | for (String s : toStrings(contact, startingBKey)) { | |
154 | writer.append(s); | |
155 | writer.append('\n'); | |
156 | } | |
157 | } | |
158 | ||
159 | /** | |
160 | * Write the given {@link Card} in the {@link Appendable}. | |
161 | * | |
162 | * @param writer | |
163 | * the {@link Appendable} | |
164 | * @param card | |
165 | * the {@link Card} to write | |
166 | * | |
167 | * @throws IOException | |
168 | * in case of IO error | |
169 | */ | |
170 | public static void write(Appendable writer, Card card) throws IOException { | |
171 | for (String s : toStrings(card)) { | |
172 | writer.append(s); | |
173 | writer.append('\n'); | |
174 | } | |
175 | } | |
a3b510ab | 176 | } |