001package ezvcard.util;
002
003import java.net.MalformedURLException;
004import java.net.URL;
005
006import org.jsoup.Jsoup;
007import org.jsoup.nodes.Document;
008import org.jsoup.nodes.Element;
009import org.jsoup.select.Elements;
010
011/*
012 Copyright (c) 2012-2026, Michael Angstadt
013 All rights reserved.
014
015 Redistribution and use in source and binary forms, with or without
016 modification, are permitted provided that the following conditions are met: 
017
018 1. Redistributions of source code must retain the above copyright notice, this
019 list of conditions and the following disclaimer. 
020 2. Redistributions in binary form must reproduce the above copyright notice,
021 this list of conditions and the following disclaimer in the documentation
022 and/or other materials provided with the distribution. 
023
024 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
025 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
026 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
027 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
028 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
029 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
030 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
031 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
032 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
033 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
034
035 The views and conclusions contained in the software and documentation are those
036 of the authors and should not be interpreted as representing official policies, 
037 either expressed or implied, of the FreeBSD Project.
038 */
039
040/**
041 * Generic HTML utility methods.
042 * @author Michael Angstadt
043 */
044public class HtmlUtils {
045        /**
046         * Determines whether the given element is a child of one of the given
047         * parent elements.
048         * @param child the child element
049         * @param possibleParents the possible parents
050         * @return true if it is a child, false if not
051         */
052        public static boolean isChildOf(Element child, Elements possibleParents) {
053                return child.parents().stream().anyMatch(possibleParents::contains);
054        }
055
056        /**
057         * Converts an HTML string to an HTML element.
058         * @param html the HTML
059         * @return the HTML element
060         */
061        public static Element toElement(String html) {
062                return toElement(html, null);
063        }
064
065        /**
066         * Converts an HTML string to an HTML element.
067         * @param html the HTML
068         * @param baseUrl the base URL
069         * @return the HTML element
070         */
071        public static Element toElement(String html, String baseUrl) {
072                Document d = (baseUrl == null) ? Jsoup.parse(html) : Jsoup.parse(html, baseUrl);
073                return d.getElementsByTag("body").first().children().first();
074        }
075
076        /**
077         * Extracts the anchor portion from a URL.
078         * @param url the full URL
079         * @return the anchor, or null if there is no anchor, or null if the given
080         * string is not a valid URL
081         */
082        public static String getAnchorFromUrl(String url) {
083                URL urlObj;
084                try {
085                        urlObj = new URL(url);
086                } catch (MalformedURLException e) {
087                        return null;
088                }
089
090                return urlObj.getRef();
091        }
092
093        private HtmlUtils() {
094                //hide
095        }
096}