Debian Codegraph @

src/main/java/ezvcard/io/html/HCardParser.java

raw symbols SCIP
1 package ezvcard.io.html;
2
3 import static ezvcard.util.HtmlUtils.isChildOf;
4
5 import java.io.IOException;
6 import java.io.InputStream;
7 import java.io.Reader;
8 import java.io.UncheckedIOException;
9 import java.net.MalformedURLException;
10 import java.net.URL;
11 import java.nio.file.Path;
12 import java.util.ArrayList;
13 import java.util.Iterator;
14 import java.util.List;
15 import java.util.Set;
16
17 import org.jsoup.Jsoup;
18 import org.jsoup.nodes.Document;
19 import org.jsoup.nodes.Element;
20 import org.jsoup.select.Elements;
21
22 import ezvcard.VCard;
23 import ezvcard.VCardVersion;
24 import ezvcard.io.CannotParseException;
25 import ezvcard.io.EmbeddedVCardException;
26 import ezvcard.io.ParseWarning;
27 import ezvcard.io.SkipMeException;
28 import ezvcard.io.StreamReader;
29 import ezvcard.io.scribe.RawPropertyScribe;
30 import ezvcard.io.scribe.VCardPropertyScribe;
31 import ezvcard.property.Categories;
32 import ezvcard.property.Email;
33 import ezvcard.property.Impp;
34 import ezvcard.property.Label;
35 import ezvcard.property.Nickname;
36 import ezvcard.property.RawProperty;
37 import ezvcard.property.Telephone;
38 import ezvcard.property.Url;
39 import ezvcard.property.VCardProperty;
40 import ezvcard.util.Gobble;
41 import ezvcard.util.IOUtils;
42
43 /*
44 Copyright (c) 2012-2023, Michael Angstadt
45 All rights reserved.
46
47 Redistribution and use in source and binary forms, with or without
48 modification, are permitted provided that the following conditions are met:
49
50 1. Redistributions of source code must retain the above copyright notice, this
51 list of conditions and the following disclaimer.
52 2. Redistributions in binary form must reproduce the above copyright notice,
53 this list of conditions and the following disclaimer in the documentation
54 and/or other materials provided with the distribution.
55
56 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
57 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
58 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
59 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
60 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
61 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
62 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
63 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
64 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
65 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
66
67 The views and conclusions contained in the software and documentation are those
68 of the authors and should not be interpreted as representing official policies,
69 either expressed or implied, of the FreeBSD Project.
70 */
71
72 /**
73 * <p>
74 * Parses {@link VCard} objects from an HTML page (hCard format).
75 * </p>
76 * <p>
77 * <b>Example:</b>
78 * </p>
79 *
80 * <pre class="brush:java">
81 * URL url = new URL("http://example.com");
82 * HCardParser parser = new HCardParser(url);
83 * List&lt;VCard&gt; vcards = parser.parseAll();
84 * </pre>
85 * @author Michael Angstadt
86 * @see <a href="http://microformats.org/wiki/hcard">http://microformats.org/
87 * wiki/hcard</a>
88 */
89 public class HCardParser extends StreamReader {
90 private final String pageUrl;
91 private final Elements vcardElements;
92 private final Iterator<Element> vcardElementsIt;
93 private final List<Label> labels = new ArrayList<>();
94
95 private VCard vcard;
96 private Elements embeddedVCards = new Elements();
97 private Nickname nickname;
98 private Categories categories;
99
100 private final String urlPropertyName = index.getPropertyScribe(Url.class).getPropertyName().toLowerCase();
101 private final String categoriesName = index.getPropertyScribe(Categories.class).getPropertyName().toLowerCase();
102 private final String emailName = index.getPropertyScribe(Email.class).getPropertyName().toLowerCase();
103 private final String telName = index.getPropertyScribe(Telephone.class).getPropertyName().toLowerCase();
104
105 /**
106 * Creates an hCard document.
107 * @param url the URL of the webpage
108 * @throws IOException if there's a problem loading the webpage
109 */
110 public HCardParser(URL url) throws IOException {
111 this(Jsoup.parse(url, 30000), url.toString());
112 }
113
114 /**
115 * Creates an hCard document.
116 * @param in the input stream to the HTML page
117 * @throws IOException if there's a problem reading the HTML page
118 */
119 public HCardParser(InputStream in) throws IOException {
120 this(in, null);
121 }
122
123 /**
124 * Creates an hCard document.
125 * @param in the input stream to the HTML page
126 * @param pageUrl the original URL of the HTML page (used to resolve
127 * relative links)
128 * @throws IOException if there's a problem reading the HTML page
129 */
130 public HCardParser(InputStream in, String pageUrl) throws IOException {
131 this((pageUrl == null) ? Jsoup.parse(in, null, "") : Jsoup.parse(in, null, pageUrl), pageUrl);
132 }
133
134 /**
135 * Creates an hCard document.
136 * @param file the HTML file
137 * @throws IOException if there's a problem reading the HTML file
138 */
139 public HCardParser(Path file) throws IOException {
140 this(file, null);
141 }
142
143 /**
144 * Creates an hCard document.
145 * @param file the HTML file
146 * @param pageUrl the original URL of the HTML page (used to resolve
147 * relative links)
148 * @throws IOException if there's a problem reading the HTML file
149 */
150 public HCardParser(Path file, String pageUrl) throws IOException {
151 this((pageUrl == null) ? Jsoup.parse(file.toFile(), null, "") : Jsoup.parse(file.toFile(), null, pageUrl), pageUrl);
152 }
153
154 /**
155 * Creates an hCard document.
156 * @param reader the input stream to the HTML page
157 * @throws IOException if there's a problem reading the HTML page
158 */
159 public HCardParser(Reader reader) throws IOException {
160 this(reader, null);
161 }
162
163 /**
164 * Creates an hCard document.
165 * @param reader the input stream to the HTML page
166 * @param pageUrl the original URL of the HTML page (used to resolve
167 * relative links)
168 * @throws IOException if there's a problem reading the HTML page
169 */
170 public HCardParser(Reader reader, String pageUrl) throws IOException {
171 this(new Gobble(reader).asString(), pageUrl);
172 }
173
174 /**
175 * Creates an hCard document.
176 * @param html the HTML page
177 */
178 public HCardParser(String html) {
179 this(html, null);
180 }
181
182 /**
183 * Creates an hCard document.
184 * @param html the HTML page
185 * @param pageUrl the original URL of the HTML page (used to resolve
186 * relative links)
187 */
188 public HCardParser(String html, String pageUrl) {
189 this((pageUrl == null) ? Jsoup.parse(html) : Jsoup.parse(html, pageUrl), pageUrl);
190 }
191
192 /**
193 * Creates an hCard document.
194 * @param document the HTML page
195 */
196 public HCardParser(Document document) {
197 this(document, null);
198 }
199
200 /**
201 * Creates an hCard document.
202 * @param document the HTML page
203 * @param pageUrl the original URL of the HTML page (used to resolve
204 * relative links)
205 */
206 public HCardParser(Document document, String pageUrl) {
207 this.pageUrl = pageUrl;
208
209 String anchor = null;
210 if (pageUrl != null) {
211 try {
212 URL url = new URL(pageUrl);
213 anchor = url.getRef();
214 } catch (MalformedURLException e) {
215 anchor = null;
216 }
217 }
218
219 Element searchUnder = null;
220 if (anchor != null) {
221 searchUnder = document.getElementById(anchor);
222 }
223 if (searchUnder == null) {
224 searchUnder = document;
225 }
226
227 vcardElements = searchUnder.getElementsByClass("vcard");
228
229 //remove nested vcard elements
230 Iterator<Element> it = vcardElements.iterator();
231 while (it.hasNext()) {
232 Element element = it.next();
233 if (isChildOf(element, vcardElements)) {
234 it.remove();
235 }
236 }
237
238 vcardElementsIt = vcardElements.iterator();
239 }
240
241 /**
242 * Constructor for reading embedded vCards.
243 * @param embeddedVCard the HTML element of the embedded vCard
244 * @param pageUrl the original URL of the HTML page
245 */
246 private HCardParser(Element embeddedVCard, String pageUrl) {
247 this.pageUrl = pageUrl;
248 vcardElements = new Elements(embeddedVCard);
249 vcardElementsIt = vcardElements.iterator();
250 }
251
252 @Override
253 public VCard readNext() {
254 try {
255 return super.readNext();
256 } catch (IOException e) {
257 //will not be thrown because reading from DOM
258 throw new UncheckedIOException(e);
259 }
260 }
261
262 @Override
263 protected VCard _readNext() {
264 if (!vcardElementsIt.hasNext()) {
265 return null;
266 }
267
268 context.setVersion(VCardVersion.V3_0);
269 parseVCardElement(vcardElementsIt.next());
270 return vcard;
271 }
272
273 private void parseVCardElement(Element vcardElement) {
274 labels.clear();
275 nickname = null;
276 categories = null;
277
278 vcard = new VCard();
279 vcard.setVersion(VCardVersion.V3_0);
280 if (pageUrl != null) {
281 vcard.addSource(pageUrl);
282 }
283
284 //visit all descendant nodes, depth-first
285 for (Element child : vcardElement.children()) {
286 visit(child);
287 }
288
289 //assign labels to their addresses
290 assignLabels(vcard, labels);
291 }
292
293 private void visit(Element element) {
294 boolean visitChildren = true;
295 Set<String> classNames = element.classNames();
296 for (String className : classNames) {
297 className = className.toLowerCase();
298
299 //give special treatment to certain URLs
300 if (urlPropertyName.equals(className)) {
301 String href = element.attr("href");
302 if (href.length() > 0) {
303 if (!classNames.contains(emailName) && href.matches("(?i)mailto:.*")) {
304 className = emailName;
305 } else if (!classNames.contains(telName) && href.matches("(?i)tel:.*")) {
306 className = telName;
307 } else {
308 //try parsing as IMPP
309 VCardPropertyScribe<? extends VCardProperty> scribe = index.getPropertyScribe(Impp.class);
310
311 context.getWarnings().clear();
312 context.setPropertyName(scribe.getPropertyName());
313 try {
314 VCardProperty property = scribe.parseHtml(new HCardElement(element), context);
315 vcard.addProperty(property);
316 warnings.addAll(context.getWarnings());
317 continue;
318 } catch (SkipMeException e) {
319 //URL is not an instant messenger URL
320 } catch (CannotParseException e) {
321 //URL is not an instant messenger URL
322 }
323 }
324 }
325 }
326
327 //hCard uses a different name for the CATEGORIES property
328 if ("category".equals(className)) {
329 className = categoriesName;
330 }
331
332 VCardPropertyScribe<? extends VCardProperty> scribe = index.getPropertyScribe(className);
333 if (scribe == null) {
334 //if no scribe is found, and the class name doesn't start with "x-", then it must be an arbitrary CSS class that has nothing to do with vCard
335 if (!className.startsWith("x-")) {
336 continue;
337 }
338 scribe = new RawPropertyScribe(className);
339 }
340
341 context.getWarnings().clear();
342 context.setPropertyName(scribe.getPropertyName());
343
344 VCardProperty property;
345 try {
346 property = scribe.parseHtml(new HCardElement(element), context);
347 warnings.addAll(context.getWarnings());
348
349 //LABELs must be treated specially so they can be matched up with their ADRs
350 if (property instanceof Label) {
351 labels.add((Label) property);
352 continue;
353 }
354
355 //add all NICKNAMEs to the same type object
356 if (property instanceof Nickname) {
357 Nickname nn = (Nickname) property;
358 if (nickname == null) {
359 nickname = nn;
360 vcard.addProperty(nickname);
361 } else {
362 nickname.getValues().addAll(nn.getValues());
363 }
364 continue;
365 }
366
367 //add all CATEGORIES to the same type object
368 if (property instanceof Categories) {
369 Categories c = (Categories) property;
370 if (categories == null) {
371 categories = c;
372 vcard.addProperty(categories);
373 } else {
374 categories.getValues().addAll(c.getValues());
375 }
376 continue;
377 }
378 } catch (SkipMeException e) {
379 //@formatter:off
380 warnings.add(new ParseWarning.Builder(context)
381 .message(22, e.getMessage())
382 .build()
383 );
384 //@formatter:on
385 continue;
386 } catch (CannotParseException e) {
387 //@formatter:off
388 warnings.add(new ParseWarning.Builder(context)
389 .message(e)
390 .build()
391 );
392 //@formatter:on
393
394 property = new RawProperty(className, element.outerHtml());
395 } catch (EmbeddedVCardException e) {
396 if (isChildOf(element, embeddedVCards)) {
397 //prevents multiple-nested embedded elements from overwriting each other
398 continue;
399 }
400
401 property = e.getProperty();
402
403 embeddedVCards.add(element);
404 HCardParser embeddedReader = new HCardParser(element, pageUrl);
405 try {
406 VCard embeddedVCard = embeddedReader.readNext();
407 e.injectVCard(embeddedVCard);
408 } finally {
409 warnings.addAll(embeddedReader.getWarnings());
410 IOUtils.closeQuietly(embeddedReader);
411 }
412 visitChildren = false;
413 }
414
415 vcard.addProperty(property);
416 }
417
418 if (visitChildren) {
419 for (Element child : element.children()) {
420 visit(child);
421 }
422 }
423 }
424
425 public void close() {
426 //empty
427 }
428 }