1 package be
.nikiroo
.jvcard
.parsers
;
3 import java
.util
.Arrays
;
4 import java
.util
.LinkedList
;
7 import be
.nikiroo
.jvcard
.Card
;
8 import be
.nikiroo
.jvcard
.Contact
;
9 import be
.nikiroo
.jvcard
.Data
;
11 public class AbookParser
{
13 * Load the given data from under the given {@link Format}.
16 * the input to load from
18 * the {@link Format} to load as
20 * @return the list of elements
22 public static List
<Contact
> parseContact(List
<String
> lines
) {
23 List
<Contact
> contacts
= new LinkedList
<Contact
>();
25 for (String line
: lines
) {
26 List
<Data
> content
= new LinkedList
<Data
>();
28 String tab
[] = line
.split("\t");
31 content
.add(new Data(null, "NICKNAME", tab
[0].trim(), null));
33 content
.add(new Data(null, "FN", tab
[1].trim(), null));
35 content
.add(new Data(null, "EMAIL", tab
[2].trim(), null));
37 content
.add(new Data(null, "X-FCC", tab
[3].trim(), null));
39 content
.add(new Data(null, "NOTE", tab
[4].trim(), null));
41 contacts
.add(new Contact(content
));
48 * Return a {@link String} representation of the given {@link Card}, line by
52 * Note that the BKey is actually not used in Pine mode.
59 * the starting BKey number (all the other will follow) or -1 for
62 * @return the {@link String} representation
64 public static List
<String
> toStrings(Contact contact
, int startingBKey
) {
65 // BKey is not used in pine mode
67 StringBuilder builder
= new StringBuilder();
69 String nick
= contact
.getPreferredDataValue("NICKNAME");
71 nick
= nick
.replaceAll(" ", "_");
72 nick
= nick
.replaceAll(",", "-");
73 nick
= nick
.replaceAll("@", "(a)");
74 nick
= nick
.replaceAll("\"", "'");
75 nick
= nick
.replaceAll(";", ".");
76 nick
= nick
.replaceAll(":", "=");
77 nick
= nick
.replaceAll("[()\\[\\]<>\\\\]", "/");
84 String fn
= contact
.getPreferredDataValue("FN");
90 String email
= contact
.getPreferredDataValue("EMAIL");
92 builder
.append(email
);
94 // optional fields follow:
96 String xfcc
= contact
.getPreferredDataValue("X-FCC");
102 String notes
= contact
.getPreferredDataValue("NOTE");
105 builder
.append('\t');
107 builder
.append('\t');
108 builder
.append(notes
);
111 // note: save as pine means normal LN, nor CRLN
112 builder
.append('\n');
114 return Arrays
.asList(new String
[] { builder
.toString() });
118 * Return a {@link String} representation of the given {@link Card}, line by
122 * the card to convert
124 * @return the {@link String} representation
126 public static List
<String
> toStrings(Card card
) {
127 List
<String
> lines
= new LinkedList
<String
>();
129 for (int index
= 0; index
< card
.size(); index
++) {
130 lines
.addAll(toStrings(card
.get(index
), -1));