Performance improvement:
[jvcard.git] / src / be / nikiroo / jvcard / parsers / Parser.java
CommitLineData
a3b510ab
NR
1package be.nikiroo.jvcard.parsers;
2
1c03abaf
NR
3import java.io.BufferedReader;
4import java.io.File;
5import java.io.FileInputStream;
6import java.io.IOException;
7import java.io.InputStreamReader;
a3b510ab 8import java.security.InvalidParameterException;
1c03abaf 9import java.util.LinkedList;
a3b510ab
NR
10import java.util.List;
11
12import be.nikiroo.jvcard.Card;
13import be.nikiroo.jvcard.Contact;
14import be.nikiroo.jvcard.Data;
15
16public class Parser {
17
1c03abaf
NR
18 /**
19 * Load the data from the given {@link File} under the given {@link Format}.
20 *
21 * @param file
22 * the input to load from
23 * @param format
24 * the {@link Format} to load as
25 *
26 * @return the list of elements
27 *
28 * @throws IOException
29 * in case of IO error
30 */
0b6140e4 31 public static List<Contact> parseContact(File file, Format format)
1c03abaf
NR
32 throws IOException {
33 List<String> lines = null;
34
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
40 .readLine()) {
41 lines.add(line);
42 }
43 buffer.close();
44 }
45
46 if (lines == null)
47 return new LinkedList<Contact>();
48
0b6140e4 49 return parseContact(lines, format);
1c03abaf
NR
50 }
51
52 /**
53 * Load the given data from under the given {@link Format}.
54 *
55 * @param lines
56 * the input to load from
57 * @param format
58 * the {@link Format} to load as
59 *
60 * @return the list of elements
61 */
0b6140e4 62 public static List<Contact> parseContact(List<String> lines, Format format) {
a3b510ab
NR
63 switch (format) {
64 case VCard21:
0b6140e4 65 return Vcard21Parser.parseContact(lines);
a3b510ab 66 case Abook:
0b6140e4 67 return AbookParser.parseContact(lines);
a3b510ab
NR
68
69 default:
70 throw new InvalidParameterException("Unknown format: "
71 + format.toString());
72 }
73 }
74
cf77cb35 75 /**
59597d59 76 * Write the given {@link Card} in the {@link Appendable}.
cf77cb35 77 *
59597d59
NR
78 * @param writer
79 * the {@link Appendable}
cf77cb35 80 * @param card
59597d59 81 * the {@link Card} to write
cf77cb35 82 * @param format
59597d59 83 * the {@link Format} to export to
cf77cb35 84 *
59597d59
NR
85 * @throws IOException
86 * in case of IO error
cf77cb35 87 */
59597d59
NR
88 public static void write(Appendable writer, Format format, Card card)
89 throws IOException {
a3b510ab
NR
90 switch (format) {
91 case VCard21:
59597d59
NR
92 Vcard21Parser.write(writer, card);
93 break;
a3b510ab 94 case Abook:
59597d59
NR
95 AbookParser.write(writer, card);
96 break;
a3b510ab
NR
97 default:
98 throw new InvalidParameterException("Unknown format: "
99 + format.toString());
100 }
101 }
102
cf77cb35 103 /**
59597d59 104 * Write the given {@link Contact} in the {@link Appendable}.
cf77cb35 105 *
59597d59
NR
106 * @param writer
107 * the {@link Appendable}
108 * @param contact
109 * the {@link Contact} to write
cf77cb35
NR
110 * @param startingBKey
111 * the starting BKey number (all the other will follow) or -1 for
112 * no BKey
cf77cb35 113 * @param format
59597d59 114 * the {@link Format} to export to
cf77cb35 115 *
59597d59
NR
116 * @throws IOException
117 * in case of IO error
cf77cb35 118 */
59597d59
NR
119 public static void write(Appendable writer, Contact contact, Format format,
120 int startingBKey) throws IOException {
a3b510ab
NR
121 switch (format) {
122 case VCard21:
59597d59
NR
123 Vcard21Parser.write(writer, contact, startingBKey);
124 break;
a3b510ab 125 case Abook:
59597d59
NR
126 AbookParser.write(writer, contact, startingBKey);
127 break;
a3b510ab
NR
128 default:
129 throw new InvalidParameterException("Unknown format: "
130 + format.toString());
131 }
132 }
133
134 // return -1 if no bkey
135 public static int getBKey(Data data) {
136 if (data.isBinary() && data.getValue().startsWith("<HIDDEN_")) {
137 try {
b9f192ed
NR
138 int bkey = Integer.parseInt(data.getValue()
139 .replace("<HIDDEN_", "").replace(">", ""));
a3b510ab
NR
140 if (bkey < 0)
141 throw new InvalidParameterException(
142 "All bkeys MUST be positive");
143 return bkey;
144 } catch (NumberFormatException nfe) {
145 }
146 }
147
148 return -1;
149 }
150
151 static String generateBKeyString(int bkey) {
152 if (bkey < 0)
153 throw new InvalidParameterException("All bkeys MUST be positive");
154
155 return "<HIDDEN_" + bkey + ">";
156 }
157}