001package ezvcard.io.chain;
002
003import java.io.IOException;
004import java.io.InputStream;
005import java.io.Reader;
006import java.net.URL;
007import java.nio.file.Path;
008
009import ezvcard.Ezvcard;
010import ezvcard.io.StreamReader;
011import ezvcard.io.html.HCardParser;
012
013/*
014 Copyright (c) 2012-2023, Michael Angstadt
015 All rights reserved.
016
017 Redistribution and use in source and binary forms, with or without
018 modification, are permitted provided that the following conditions are met: 
019
020 1. Redistributions of source code must retain the above copyright notice, this
021 list of conditions and the following disclaimer. 
022 2. Redistributions in binary form must reproduce the above copyright notice,
023 this list of conditions and the following disclaimer in the documentation
024 and/or other materials provided with the distribution. 
025
026 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
027 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
028 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
029 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
030 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
031 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
032 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
033 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
034 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
035 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
036 */
037
038/**
039 * Chainer class for parsing hCards (HTML-encoded vCards).
040 * @see Ezvcard#parseHtml(InputStream)
041 * @see Ezvcard#parseHtml(Path)
042 * @see Ezvcard#parseHtml(Reader)
043 * @author Michael Angstadt
044 */
045public class ChainingHtmlParser<T extends ChainingHtmlParser<?>> extends ChainingParser<T> {
046        private String pageUrl;
047        private URL url;
048
049        public ChainingHtmlParser(String string) {
050                super(string);
051        }
052
053        public ChainingHtmlParser(InputStream in) {
054                super(in);
055        }
056
057        public ChainingHtmlParser(Reader reader) {
058                super(reader);
059        }
060
061        public ChainingHtmlParser(Path file) {
062                super(file);
063        }
064
065        public ChainingHtmlParser(URL url) {
066                this.url = url;
067        }
068
069        /**
070         * Sets the original URL of the webpage. This is used to resolve relative
071         * links and to set the SOURCE property on the vCard. Setting this property
072         * has no effect if reading from a {@link URL}.
073         * @param pageUrl the webpage URL
074         * @return this
075         */
076        public T pageUrl(String pageUrl) {
077                this.pageUrl = pageUrl;
078                return this_;
079        }
080
081        @Override
082        StreamReader constructReader() throws IOException {
083                if (string != null) {
084                        return new HCardParser(string, pageUrl);
085                }
086                if (in != null) {
087                        return new HCardParser(in, pageUrl);
088                }
089                if (reader != null) {
090                        return new HCardParser(reader, pageUrl);
091                }
092                if (file != null) {
093                        return new HCardParser(file, pageUrl);
094                }
095                return new HCardParser(url);
096        }
097}