1 package be
.nikiroo
.jvcard
.parsers
;
3 import java
.io
.IOException
;
4 import java
.util
.Arrays
;
5 import java
.util
.LinkedList
;
8 import be
.nikiroo
.jvcard
.Card
;
9 import be
.nikiroo
.jvcard
.Contact
;
10 import be
.nikiroo
.jvcard
.Data
;
12 public class AbookParser
{
14 * Load the given data from under the given {@link Format}.
17 * the input to load from
19 * the {@link Format} to load as
21 * @return the list of elements
23 public static List
<Contact
> parseContact(List
<String
> lines
) {
24 List
<Contact
> contacts
= new LinkedList
<Contact
>();
26 for (String line
: lines
) {
27 List
<Data
> content
= new LinkedList
<Data
>();
29 String tab
[] = line
.split("\t");
32 content
.add(new Data(null, "NICKNAME", tab
[0].trim(), null));
34 content
.add(new Data(null, "FN", tab
[1].trim(), null));
36 content
.add(new Data(null, "EMAIL", tab
[2].trim(), null));
38 content
.add(new Data(null, "X-FCC", tab
[3].trim(), null));
40 content
.add(new Data(null, "NOTE", tab
[4].trim(), null));
42 contacts
.add(new Contact(content
));
49 * Return a {@link String} representation of the given {@link Card}, line by
53 * Note that the BKey is actually not used in Pine mode.
60 * the starting BKey number (all the other will follow) or -1 for
61 * no BKey (it is actually not used in this mode)
63 * @return the {@link String} representation
65 public static List
<String
> toStrings(Contact contact
,
66 @SuppressWarnings("unused") int startingBKey
) {
67 // BKey is not used in pine mode
69 StringBuilder builder
= new StringBuilder();
71 String nick
= contact
.getPreferredDataValue("NICKNAME");
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("[()\\[\\]<>\\\\]", "/");
86 String fn
= contact
.getPreferredDataValue("FN");
92 String email
= contact
.getPreferredDataValue("EMAIL");
94 builder
.append(email
);
96 // optional fields follow:
98 String xfcc
= contact
.getPreferredDataValue("X-FCC");
100 builder
.append('\t');
101 builder
.append(xfcc
);
104 String notes
= contact
.getPreferredDataValue("NOTE");
107 builder
.append('\t');
109 builder
.append('\t');
110 builder
.append(notes
);
113 // note: save as pine means normal LN, nor CRLN
114 builder
.append('\n');
116 return Arrays
.asList(new String
[] { builder
.toString() });
120 * Return a {@link String} representation of the given {@link Card}, line by
124 * the card to convert
126 * @return the {@link String} representation
128 public static List
<String
> toStrings(Card card
) {
129 List
<String
> lines
= new LinkedList
<String
>();
131 for (int index
= 0; index
< card
.size(); index
++) {
132 lines
.addAll(toStrings(card
.get(index
), -1));
139 * Write the given {@link Contact} in the {@link Appendable}.
142 * the {@link Appendable}
144 * the {@link Contact} to write
145 * @param startingBKey
146 * the starting BKey number (all the other will follow) or -1 for
149 * @throws IOException
150 * in case of IO error
152 public static void write(Appendable writer
, Contact contact
,
153 int startingBKey
) throws IOException
{
154 for (String s
: toStrings(contact
, startingBKey
)) {
161 * Write the given {@link Card} in the {@link Appendable}.
164 * the {@link Appendable}
166 * the {@link Card} to write
168 * @throws IOException
169 * in case of IO error
171 public static void write(Appendable writer
, Card card
) throws IOException
{
172 for (String s
: toStrings(card
)) {