Commit | Line | Data |
---|---|---|
a3b510ab NR |
1 | package be.nikiroo.jvcard.parsers; |
2 | ||
1c03abaf NR |
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; | |
a3b510ab | 8 | import java.security.InvalidParameterException; |
1c03abaf | 9 | import java.util.LinkedList; |
a3b510ab NR |
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 | ||
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 | */ | |
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 | */ | |
a3b510ab NR |
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 | // -1 = no bkeys | |
76 | public static String toString(Card card, Format format) { | |
77 | switch (format) { | |
78 | case VCard21: | |
79 | return Vcard21Parser.toString(card); | |
80 | case Abook: | |
81 | return AbookParser.toString(card); | |
82 | ||
83 | default: | |
84 | throw new InvalidParameterException("Unknown format: " | |
85 | + format.toString()); | |
86 | } | |
87 | } | |
88 | ||
89 | // -1 = no bkeys | |
b9f192ed NR |
90 | public static String toString(Contact contact, Format format, |
91 | int startingBKey) { | |
a3b510ab NR |
92 | switch (format) { |
93 | case VCard21: | |
94 | return Vcard21Parser.toString(contact, startingBKey); | |
95 | case Abook: | |
96 | return AbookParser.toString(contact, startingBKey); | |
97 | ||
98 | default: | |
99 | throw new InvalidParameterException("Unknown format: " | |
100 | + format.toString()); | |
101 | } | |
102 | } | |
103 | ||
104 | // return -1 if no bkey | |
105 | public static int getBKey(Data data) { | |
106 | if (data.isBinary() && data.getValue().startsWith("<HIDDEN_")) { | |
107 | try { | |
b9f192ed NR |
108 | int bkey = Integer.parseInt(data.getValue() |
109 | .replace("<HIDDEN_", "").replace(">", "")); | |
a3b510ab NR |
110 | if (bkey < 0) |
111 | throw new InvalidParameterException( | |
112 | "All bkeys MUST be positive"); | |
113 | return bkey; | |
114 | } catch (NumberFormatException nfe) { | |
115 | } | |
116 | } | |
117 | ||
118 | return -1; | |
119 | } | |
120 | ||
121 | static String generateBKeyString(int bkey) { | |
122 | if (bkey < 0) | |
123 | throw new InvalidParameterException("All bkeys MUST be positive"); | |
124 | ||
125 | return "<HIDDEN_" + bkey + ">"; | |
126 | } | |
127 | } |