1 package be
.nikiroo
.jvcard
.parsers
;
3 import java
.io
.BufferedReader
;
5 import java
.io
.FileInputStream
;
6 import java
.io
.IOException
;
7 import java
.io
.InputStreamReader
;
8 import java
.security
.InvalidParameterException
;
9 import java
.util
.LinkedList
;
10 import java
.util
.List
;
12 import be
.nikiroo
.jvcard
.Card
;
13 import be
.nikiroo
.jvcard
.Contact
;
14 import be
.nikiroo
.jvcard
.Data
;
19 * Load the data from the given {@link File} under the given {@link Format}.
22 * the input to load from
24 * the {@link Format} to load as
26 * @return the list of elements
31 public static List
<Contact
> parseContact(File file
, Format format
)
33 List
<String
> lines
= null;
35 if (file
!= null && file
.exists()) {
36 BufferedReader buffer
= new BufferedReader(new InputStreamReader(
37 new FileInputStream(file
), "UTF-8"));
38 lines
= new LinkedList
<String
>();
39 for (String line
= buffer
.readLine(); line
!= null; line
= buffer
47 return new LinkedList
<Contact
>();
49 return parseContact(lines
, format
);
53 * Load the given data from under the given {@link Format}.
56 * the input to load from
58 * the {@link Format} to load as
60 * @return the list of elements
62 public static List
<Contact
> parseContact(List
<String
> lines
, Format format
) {
65 return Vcard21Parser
.parseContact(lines
);
67 return AbookParser
.parseContact(lines
);
70 throw new InvalidParameterException("Unknown format: "
76 * Write the given {@link Card} in the {@link Appendable}.
79 * the {@link Appendable}
81 * the {@link Card} to write
83 * the {@link Format} to export to
88 public static void write(Appendable writer
, Format format
, Card card
)
92 Vcard21Parser
.write(writer
, card
);
95 AbookParser
.write(writer
, card
);
98 throw new InvalidParameterException("Unknown format: "
104 * Write the given {@link Contact} in the {@link Appendable}.
107 * the {@link Appendable}
109 * the {@link Contact} to write
110 * @param startingBKey
111 * the starting BKey number (all the other will follow) or -1 for
114 * the {@link Format} to export to
116 * @throws IOException
117 * in case of IO error
119 public static void write(Appendable writer
, Contact contact
, Format format
,
120 int startingBKey
) throws IOException
{
123 Vcard21Parser
.write(writer
, contact
, startingBKey
);
126 AbookParser
.write(writer
, contact
, startingBKey
);
129 throw new InvalidParameterException("Unknown format: "
130 + format
.toString());
134 // return -1 if no bkey
135 public static int getBKey(Data data
) {
136 if (data
.isBinary() && data
.getValue().startsWith("<HIDDEN_")) {
138 int bkey
= Integer
.parseInt(data
.getValue()
139 .replace("<HIDDEN_", "").replace(">", ""));
141 throw new InvalidParameterException(
142 "All bkeys MUST be positive");
144 } catch (NumberFormatException nfe
) {
151 static String
generateBKeyString(int bkey
) {
153 throw new InvalidParameterException("All bkeys MUST be positive");
155 return "<HIDDEN_" + bkey
+ ">";