1fa7c5749805746d58e41f9fae52e575e2cb529d
[jvcard.git] / src / be / nikiroo / jvcard / parsers / Parser.java
1 package be.nikiroo.jvcard.parsers;
2
3 import java.io.BufferedReader;
4 import java.io.File;
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;
11
12 import be.nikiroo.jvcard.Card;
13 import be.nikiroo.jvcard.Contact;
14 import be.nikiroo.jvcard.Data;
15
16 public class Parser {
17
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 */
31 public static List<Contact> parse(File file, Format format)
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
49 return parse(lines, format);
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 */
62 public static List<Contact> parse(List<String> lines, Format format) {
63 switch (format) {
64 case VCard21:
65 return Vcard21Parser.parse(lines);
66 case Abook:
67 return AbookParser.parse(lines);
68
69 default:
70 throw new InvalidParameterException("Unknown format: "
71 + format.toString());
72 }
73 }
74
75 /**
76 * Return a {@link String} representation of the given {@link Card}, line by
77 * line.
78 *
79 * @param card
80 * the card to convert
81 *
82 * @param startingBKey
83 * the starting BKey number (all the other will follow) or -1 for
84 * no BKey
85 *
86 * @param format
87 * the output {@link Format} to use
88 *
89 * @return the {@link String} representation
90 */
91 public static List<String> toStrings(Card card, Format format) {
92 switch (format) {
93 case VCard21:
94 return Vcard21Parser.toStrings(card);
95 case Abook:
96 return AbookParser.toStrings(card);
97
98 default:
99 throw new InvalidParameterException("Unknown format: "
100 + format.toString());
101 }
102 }
103
104 /**
105 * Return a {@link String} representation of the given {@link Card}, line by
106 * line.
107 *
108 * @param card
109 * the card to convert
110 *
111 * @param startingBKey
112 * the starting BKey number (all the other will follow) or -1 for
113 * no BKey
114 *
115 * @param format
116 * the output {@link Format} to use
117 *
118 * @return the {@link String} representation
119 */
120 public static List<String> toStrings(Contact contact, Format format,
121 int startingBKey) {
122 switch (format) {
123 case VCard21:
124 return Vcard21Parser.toStrings(contact, startingBKey);
125 case Abook:
126 return AbookParser.toStrings(contact, startingBKey);
127
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 {
138 int bkey = Integer.parseInt(data.getValue()
139 .replace("<HIDDEN_", "").replace(">", ""));
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 }