| 1 | package ezvcard.util; |
| 2 | |
| 3 | import org.jsoup.Jsoup; |
| 4 | import org.jsoup.nodes.Document; |
| 5 | import org.jsoup.nodes.Element; |
| 6 | import org.jsoup.select.Elements; |
| 7 | |
| 8 | /* |
| 9 | Copyright (c) 2012-2023, Michael Angstadt |
| 10 | All rights reserved. |
| 11 | |
| 12 | Redistribution and use in source and binary forms, with or without |
| 13 | modification, are permitted provided that the following conditions are met: |
| 14 | |
| 15 | 1. Redistributions of source code must retain the above copyright notice, this |
| 16 | list of conditions and the following disclaimer. |
| 17 | 2. Redistributions in binary form must reproduce the above copyright notice, |
| 18 | this list of conditions and the following disclaimer in the documentation |
| 19 | and/or other materials provided with the distribution. |
| 20 | |
| 21 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 22 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 23 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 24 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR |
| 25 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 26 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 27 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 28 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 30 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | |
| 32 | The views and conclusions contained in the software and documentation are those |
| 33 | of the authors and should not be interpreted as representing official policies, |
| 34 | either expressed or implied, of the FreeBSD Project. |
| 35 | */ |
| 36 | |
| 37 | /** |
| 38 | * Generic HTML utility methods. |
| 39 | * @author Michael Angstadt |
| 40 | */ |
| 41 | public class HtmlUtils { |
| 42 | /** |
| 43 | * Determines whether the given element is a child of one of the given |
| 44 | * parent elements. |
| 45 | * @param child the child element |
| 46 | * @param possibleParents the possible parents |
| 47 | * @return true if it is a child, false if not |
| 48 | */ |
| 49 | public static boolean isChildOf(Element child, Elements possibleParents) { |
| 50 | for (Element parent : child.parents()) { |
| 51 | if (possibleParents.contains(parent)) { |
| 52 | return true; |
| 53 | } |
| 54 | } |
| 55 | return false; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Converts an HTML string to an HTML element. |
| 60 | * @param html the HTML |
| 61 | * @return the HTML element |
| 62 | */ |
| 63 | public static Element toElement(String html) { |
| 64 | return toElement(html, null); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Converts an HTML string to an HTML element. |
| 69 | * @param html the HTML |
| 70 | * @param baseUrl the base URL |
| 71 | * @return the HTML element |
| 72 | */ |
| 73 | public static Element toElement(String html, String baseUrl) { |
| 74 | Document d = (baseUrl == null) ? Jsoup.parse(html) : Jsoup.parse(html, baseUrl); |
| 75 | return d.getElementsByTag("body").first().children().first(); |
| 76 | } |
| 77 | |
| 78 | private HtmlUtils() { |
| 79 | //hide |
| 80 | } |
| 81 | } |