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