Add more warnings source to 1.6) and fix warnings
[jvcard.git] / src / be / nikiroo / jvcard / parsers / AbookParser.java
CommitLineData
a3b510ab
NR
1package be.nikiroo.jvcard.parsers;
2
59597d59 3import java.io.IOException;
cf77cb35 4import java.util.Arrays;
a3b510ab
NR
5import java.util.LinkedList;
6import java.util.List;
7
8import be.nikiroo.jvcard.Card;
9import be.nikiroo.jvcard.Contact;
10import be.nikiroo.jvcard.Data;
11
12public 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
113 // note: save as pine means normal LN, nor CRLN
114 builder.append('\n');
78e4af97 115
cf77cb35 116 return Arrays.asList(new String[] { builder.toString() });
a3b510ab
NR
117 }
118
cf77cb35
NR
119 /**
120 * Return a {@link String} representation of the given {@link Card}, line by
121 * line.
122 *
123 * @param card
124 * the card to convert
125 *
126 * @return the {@link String} representation
127 */
128 public static List<String> toStrings(Card card) {
129 List<String> lines = new LinkedList<String>();
a3b510ab 130
78e4af97 131 for (int index = 0; index < card.size(); index++) {
cf77cb35 132 lines.addAll(toStrings(card.get(index), -1));
a3b510ab
NR
133 }
134
cf77cb35 135 return lines;
a3b510ab 136 }
59597d59
NR
137
138 /**
139 * Write the given {@link Contact} in the {@link Appendable}.
140 *
141 * @param writer
142 * the {@link Appendable}
143 * @param contact
144 * the {@link Contact} to write
145 * @param startingBKey
146 * the starting BKey number (all the other will follow) or -1 for
147 * no BKey
148 *
149 * @throws IOException
150 * in case of IO error
151 */
152 public static void write(Appendable writer, Contact contact,
153 int startingBKey) throws IOException {
154 for (String s : toStrings(contact, startingBKey)) {
155 writer.append(s);
156 writer.append('\n');
157 }
158 }
159
160 /**
161 * Write the given {@link Card} in the {@link Appendable}.
162 *
163 * @param writer
164 * the {@link Appendable}
165 * @param card
166 * the {@link Card} to write
167 *
168 * @throws IOException
169 * in case of IO error
170 */
171 public static void write(Appendable writer, Card card) throws IOException {
172 for (String s : toStrings(card)) {
173 writer.append(s);
174 writer.append('\n');
175 }
176 }
a3b510ab 177}