| 1 | package ezvcard.io.chain; |
| 2 | |
| 3 | import java.io.IOException; |
| 4 | import java.io.InputStream; |
| 5 | import java.io.Reader; |
| 6 | import java.net.URL; |
| 7 | import java.nio.file.Path; |
| 8 | |
| 9 | import ezvcard.Ezvcard; |
| 10 | import ezvcard.io.StreamReader; |
| 11 | import ezvcard.io.html.HCardParser; |
| 12 | |
| 13 | /* |
| 14 | Copyright (c) 2012-2023, Michael Angstadt |
| 15 | All rights reserved. |
| 16 | |
| 17 | Redistribution and use in source and binary forms, with or without |
| 18 | modification, are permitted provided that the following conditions are met: |
| 19 | |
| 20 | 1. Redistributions of source code must retain the above copyright notice, this |
| 21 | list of conditions and the following disclaimer. |
| 22 | 2. Redistributions in binary form must reproduce the above copyright notice, |
| 23 | this list of conditions and the following disclaimer in the documentation |
| 24 | and/or other materials provided with the distribution. |
| 25 | |
| 26 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 27 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 28 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 29 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR |
| 30 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 31 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 32 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 33 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 34 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 35 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 36 | */ |
| 37 | |
| 38 | /** |
| 39 | * Chainer class for parsing hCards (HTML-encoded vCards). |
| 40 | * @see Ezvcard#parseHtml(InputStream) |
| 41 | * @see Ezvcard#parseHtml(Path) |
| 42 | * @see Ezvcard#parseHtml(Reader) |
| 43 | * @author Michael Angstadt |
| 44 | */ |
| 45 | public class ChainingHtmlParser<T extends ChainingHtmlParser<?>> extends ChainingParser<T> { |
| 46 | private String pageUrl; |
| 47 | private URL url; |
| 48 | |
| 49 | public ChainingHtmlParser(String string) { |
| 50 | super(string); |
| 51 | } |
| 52 | |
| 53 | public ChainingHtmlParser(InputStream in) { |
| 54 | super(in); |
| 55 | } |
| 56 | |
| 57 | public ChainingHtmlParser(Reader reader) { |
| 58 | super(reader); |
| 59 | } |
| 60 | |
| 61 | public ChainingHtmlParser(Path file) { |
| 62 | super(file); |
| 63 | } |
| 64 | |
| 65 | public ChainingHtmlParser(URL url) { |
| 66 | this.url = url; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Sets the original URL of the webpage. This is used to resolve relative |
| 71 | * links and to set the SOURCE property on the vCard. Setting this property |
| 72 | * has no effect if reading from a {@link URL}. |
| 73 | * @param pageUrl the webpage URL |
| 74 | * @return this |
| 75 | */ |
| 76 | public T pageUrl(String pageUrl) { |
| 77 | this.pageUrl = pageUrl; |
| 78 | return this_; |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | StreamReader constructReader() throws IOException { |
| 83 | if (string != null) { |
| 84 | return new HCardParser(string, pageUrl); |
| 85 | } |
| 86 | if (in != null) { |
| 87 | return new HCardParser(in, pageUrl); |
| 88 | } |
| 89 | if (reader != null) { |
| 90 | return new HCardParser(reader, pageUrl); |
| 91 | } |
| 92 | if (file != null) { |
| 93 | return new HCardParser(file, pageUrl); |
| 94 | } |
| 95 | return new HCardParser(url); |
| 96 | } |
| 97 | } |