Commit | Line | Data |
---|---|---|
a3b510ab NR |
1 | package be.nikiroo.jvcard.parsers; |
2 | ||
bcb54330 | 3 | import java.util.Iterator; |
a3b510ab NR |
4 | import java.util.LinkedList; |
5 | import java.util.List; | |
6 | ||
7 | import be.nikiroo.jvcard.Card; | |
8 | import be.nikiroo.jvcard.Contact; | |
9 | import be.nikiroo.jvcard.Data; | |
10 | import be.nikiroo.jvcard.TypeInfo; | |
11 | ||
12 | public class Vcard21Parser { | |
bcb54330 NR |
13 | public static List<Contact> parse(Iterable<String> textData) { |
14 | Iterator<String> lines = textData.iterator(); | |
a3b510ab NR |
15 | List<Contact> contacts = new LinkedList<Contact>(); |
16 | List<Data> datas = null; | |
17 | ||
bcb54330 NR |
18 | String nextRawLine = null; |
19 | if (lines.hasNext()) { | |
20 | nextRawLine = lines.next(); | |
21 | while (lines.hasNext() && isContinuation(nextRawLine)) { | |
22 | // BAD INPUT FILE. IGNORE. | |
23 | System.err | |
24 | .println("VCARD Parser warning: CONTINUATION line seen before any data line"); | |
25 | nextRawLine = lines.next(); | |
26 | } | |
27 | } | |
28 | ||
29 | while (nextRawLine != null) { | |
30 | StringBuilder rawLine = new StringBuilder(nextRawLine.trim()); | |
31 | if (lines.hasNext()) | |
32 | nextRawLine = lines.next(); | |
33 | else | |
34 | nextRawLine = null; | |
35 | ||
36 | while (isContinuation(nextRawLine)) { | |
37 | rawLine.append(nextRawLine.trim()); | |
38 | if (lines.hasNext()) | |
39 | nextRawLine = lines.next(); | |
40 | else | |
41 | nextRawLine = null; | |
42 | } | |
43 | ||
44 | String line = rawLine.toString(); | |
a3b510ab NR |
45 | if (line.equals("BEGIN:VCARD")) { |
46 | datas = new LinkedList<Data>(); | |
47 | } else if (line.equals("END:VCARD")) { | |
48 | if (datas == null) { | |
49 | // BAD INPUT FILE. IGNORE. | |
50 | System.err | |
51 | .println("VCARD Parser warning: END:VCARD seen before any VCARD:BEGIN"); | |
52 | } else { | |
53 | contacts.add(new Contact(datas)); | |
54 | } | |
55 | } else { | |
56 | if (datas == null) { | |
57 | // BAD INPUT FILE. IGNORE. | |
58 | System.err | |
59 | .println("VCARD Parser warning: data seen before any VCARD:BEGIN"); | |
60 | } else { | |
61 | List<TypeInfo> types = new LinkedList<TypeInfo>(); | |
62 | String name = ""; | |
63 | String value = ""; | |
64 | String group = ""; | |
65 | ||
66 | if (line.contains(":")) { | |
adf9c449 NR |
67 | int colIndex = line.indexOf(':'); |
68 | String rest = line.substring(0, colIndex); | |
69 | value = line.substring(colIndex + 1); | |
a3b510ab NR |
70 | |
71 | if (rest.contains(";")) { | |
72 | String tab[] = rest.split(";"); | |
73 | name = tab[0]; | |
74 | ||
75 | for (int i = 1; i < tab.length; i++) { | |
76 | if (tab[i].contains("=")) { | |
adf9c449 NR |
77 | int equIndex = tab[i].indexOf('='); |
78 | String tname = tab[i] | |
79 | .substring(0, equIndex); | |
80 | String tvalue = tab[i] | |
81 | .substring(equIndex + 1); | |
a3b510ab NR |
82 | types.add(new TypeInfo(tname, tvalue)); |
83 | } else { | |
84 | types.add(new TypeInfo(tab[i], "")); | |
85 | } | |
86 | } | |
87 | } else { | |
88 | name = rest; | |
89 | } | |
90 | } else { | |
91 | name = line; | |
92 | } | |
93 | ||
94 | if (name.contains(".")) { | |
adf9c449 NR |
95 | int dotIndex = name.indexOf('.'); |
96 | group = name.substring(0, dotIndex); | |
97 | name = name.substring(dotIndex + 1); | |
a3b510ab NR |
98 | } |
99 | ||
100 | datas.add(new Data(types, name, value, group)); | |
101 | } | |
102 | } | |
103 | } | |
104 | ||
105 | return contacts; | |
106 | } | |
107 | ||
cf77cb35 NR |
108 | /** |
109 | * Return a {@link String} representation of the given {@link Card}, line by | |
110 | * line. | |
111 | * | |
112 | * @param card | |
113 | * the card to convert | |
114 | * | |
115 | * @param startingBKey | |
116 | * the starting BKey number (all the other will follow) or -1 for | |
117 | * no BKey | |
118 | * | |
119 | * @return the {@link String} representation | |
120 | */ | |
121 | public static List<String> toStrings(Contact contact, int startingBKey) { | |
122 | List<String> lines = new LinkedList<String>(); | |
123 | ||
124 | lines.add("BEGIN:VCARD"); | |
125 | lines.add("VERSION:2.1"); | |
1c03abaf NR |
126 | for (Data data : contact) { |
127 | StringBuilder dataBuilder = new StringBuilder(); | |
a3b510ab | 128 | if (data.getGroup() != null && !data.getGroup().trim().equals("")) { |
1c03abaf NR |
129 | dataBuilder.append(data.getGroup().trim()); |
130 | dataBuilder.append('.'); | |
a3b510ab | 131 | } |
1c03abaf NR |
132 | dataBuilder.append(data.getName()); |
133 | for (TypeInfo type : data) { | |
134 | dataBuilder.append(';'); | |
135 | dataBuilder.append(type.getName()); | |
a3b510ab NR |
136 | if (type.getValue() != null |
137 | && !type.getValue().trim().equals("")) { | |
1c03abaf NR |
138 | dataBuilder.append('='); |
139 | dataBuilder.append(type.getValue()); | |
a3b510ab NR |
140 | } |
141 | } | |
1c03abaf | 142 | dataBuilder.append(':'); |
bcb54330 NR |
143 | |
144 | // TODO: bkey! | |
1c03abaf NR |
145 | dataBuilder.append(data.getValue()); |
146 | ||
147 | // RFC says: Content lines SHOULD be folded to a maximum width of 75 | |
148 | // octets -> since it is SHOULD, we will just cut it as 74/75 chars | |
149 | // depending if the last one fits in one char (note: chars != octet) | |
150 | boolean continuation = false; | |
151 | while (dataBuilder.length() > 0) { | |
152 | int stop = 74; | |
153 | if (continuation) | |
154 | stop--; // the space takes 1 | |
155 | if (dataBuilder.length() > stop) { | |
156 | char car = dataBuilder.charAt(stop - 1); | |
157 | // RFC forbids cutting a character in 2 | |
158 | if (Character.isHighSurrogate(car)) { | |
159 | stop++; | |
160 | } | |
161 | } | |
162 | ||
163 | stop = Math.min(stop, dataBuilder.length()); | |
cf77cb35 NR |
164 | if (continuation) { |
165 | lines.add(' ' + dataBuilder.substring(0, stop)); | |
166 | } else { | |
167 | lines.add(dataBuilder.substring(0, stop)); | |
168 | } | |
1c03abaf NR |
169 | dataBuilder.delete(0, stop); |
170 | ||
171 | continuation = true; | |
172 | } | |
a3b510ab | 173 | } |
cf77cb35 | 174 | lines.add("END:VCARD"); |
a3b510ab | 175 | |
cf77cb35 | 176 | return lines; |
a3b510ab NR |
177 | } |
178 | ||
cf77cb35 NR |
179 | /** |
180 | * Return a {@link String} representation of the given {@link Card}, line by | |
181 | * line. | |
182 | * | |
183 | * @param card | |
184 | * the card to convert | |
185 | * | |
186 | * @return the {@link String} representation | |
187 | */ | |
188 | public static List<String> toStrings(Card card) { | |
189 | List<String> lines = new LinkedList<String>(); | |
a3b510ab | 190 | |
1c03abaf | 191 | for (Contact contact : card) { |
cf77cb35 | 192 | lines.addAll(toStrings(contact, -1)); |
a3b510ab | 193 | } |
78e4af97 | 194 | |
cf77cb35 | 195 | return lines; |
a3b510ab | 196 | } |
bcb54330 NR |
197 | |
198 | /** | |
199 | * Check if the given line is a continuation line or not. | |
200 | * | |
201 | * @param line | |
202 | * the line to check | |
203 | * | |
204 | * @return TRUE if the line is a continuation line | |
205 | */ | |
206 | private static boolean isContinuation(String line) { | |
207 | if (line != null && line.length() > 0) | |
208 | return (line.charAt(0) == ' ' || line.charAt(0) == '\t'); | |
209 | return false; | |
210 | } | |
a3b510ab | 211 | } |