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 | */ | |
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 NR |
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 | * | |
cf77cb35 NR |
82 | * @param format |
83 | * the output {@link Format} to use | |
84 | * | |
85 | * @return the {@link String} representation | |
86 | */ | |
87 | public static List<String> toStrings(Card card, Format format) { | |
a3b510ab NR |
88 | switch (format) { |
89 | case VCard21: | |
cf77cb35 | 90 | return Vcard21Parser.toStrings(card); |
a3b510ab | 91 | case Abook: |
cf77cb35 | 92 | return AbookParser.toStrings(card); |
a3b510ab NR |
93 | |
94 | default: | |
95 | throw new InvalidParameterException("Unknown format: " | |
96 | + format.toString()); | |
97 | } | |
98 | } | |
99 | ||
cf77cb35 NR |
100 | /** |
101 | * Return a {@link String} representation of the given {@link Card}, line by | |
102 | * line. | |
103 | * | |
104 | * @param card | |
105 | * the card to convert | |
106 | * | |
107 | * @param startingBKey | |
108 | * the starting BKey number (all the other will follow) or -1 for | |
109 | * no BKey | |
110 | * | |
111 | * @param format | |
112 | * the output {@link Format} to use | |
113 | * | |
114 | * @return the {@link String} representation | |
115 | */ | |
116 | public static List<String> toStrings(Contact contact, Format format, | |
b9f192ed | 117 | int startingBKey) { |
a3b510ab NR |
118 | switch (format) { |
119 | case VCard21: | |
cf77cb35 | 120 | return Vcard21Parser.toStrings(contact, startingBKey); |
a3b510ab | 121 | case Abook: |
cf77cb35 | 122 | return AbookParser.toStrings(contact, startingBKey); |
a3b510ab NR |
123 | |
124 | default: | |
125 | throw new InvalidParameterException("Unknown format: " | |
126 | + format.toString()); | |
127 | } | |
128 | } | |
129 | ||
130 | // return -1 if no bkey | |
131 | public static int getBKey(Data data) { | |
132 | if (data.isBinary() && data.getValue().startsWith("<HIDDEN_")) { | |
133 | try { | |
b9f192ed NR |
134 | int bkey = Integer.parseInt(data.getValue() |
135 | .replace("<HIDDEN_", "").replace(">", "")); | |
a3b510ab NR |
136 | if (bkey < 0) |
137 | throw new InvalidParameterException( | |
138 | "All bkeys MUST be positive"); | |
139 | return bkey; | |
140 | } catch (NumberFormatException nfe) { | |
141 | } | |
142 | } | |
143 | ||
144 | return -1; | |
145 | } | |
146 | ||
147 | static String generateBKeyString(int bkey) { | |
148 | if (bkey < 0) | |
149 | throw new InvalidParameterException("All bkeys MUST be positive"); | |
150 | ||
151 | return "<HIDDEN_" + bkey + ">"; | |
152 | } | |
153 | } |