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
63 * @return the {@link String} representation
65 public static List
<String
> toStrings(Contact contact
, int startingBKey
) {
66 // BKey is not used in pine mode
68 StringBuilder builder
= new StringBuilder();
70 String nick
= contact
.getPreferredDataValue("NICKNAME");
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("[()\\[\\]<>\\\\]", "/");
85 String fn
= contact
.getPreferredDataValue("FN");
91 String email
= contact
.getPreferredDataValue("EMAIL");
93 builder
.append(email
);
95 // optional fields follow:
97 String xfcc
= contact
.getPreferredDataValue("X-FCC");
100 builder
.append(xfcc
);
103 String notes
= contact
.getPreferredDataValue("NOTE");
106 builder
.append('\t');
108 builder
.append('\t');
109 builder
.append(notes
);
112 // note: save as pine means normal LN, nor CRLN
113 builder
.append('\n');
115 return Arrays
.asList(new String
[] { builder
.toString() });
119 * Return a {@link String} representation of the given {@link Card}, line by
123 * the card to convert
125 * @return the {@link String} representation
127 public static List
<String
> toStrings(Card card
) {
128 List
<String
> lines
= new LinkedList
<String
>();
130 for (int index
= 0; index
< card
.size(); index
++) {
131 lines
.addAll(toStrings(card
.get(index
), -1));
138 * Write the given {@link Contact} in the {@link Appendable}.
141 * the {@link Appendable}
143 * the {@link Contact} to write
144 * @param startingBKey
145 * the starting BKey number (all the other will follow) or -1 for
148 * @throws IOException
149 * in case of IO error
151 public static void write(Appendable writer
, Contact contact
,
152 int startingBKey
) throws IOException
{
153 for (String s
: toStrings(contact
, startingBKey
)) {
160 * Write the given {@link Card} in the {@link Appendable}.
163 * the {@link Appendable}
165 * the {@link Card} to write
167 * @throws IOException
168 * in case of IO error
170 public static void write(Appendable writer
, Card card
) throws IOException
{
171 for (String s
: toStrings(card
)) {