1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.devacfr.maven.skins.reflow;
17
18 import static java.util.Objects.requireNonNull;
19
20 import java.nio.charset.StandardCharsets;
21 import javax.annotation.Nonnull;
22 import org.jsoup.Jsoup;
23 import org.jsoup.nodes.Document;
24 import org.jsoup.nodes.Element;
25 import org.jsoup.nodes.Node;
26 import org.jsoup.parser.Parser;
27 import org.jsoup.parser.Tag;
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 public final class JsoupUtils {
48
49 private JsoupUtils() {
50
51 }
52
53
54
55
56
57
58
59
60 @Nonnull
61 public static Document createHtmlDocument(@Nonnull String html) {
62 requireNonNull(html);
63 Document doc = Jsoup.parse(html, "", createHtmlParser());
64
65
66
67
68
69
70 return doc;
71 }
72
73 @Nonnull
74 public static Document createXmlDocument(@Nonnull String html) {
75 requireNonNull(html);
76 Document doc = Jsoup.parse(html, "", createXmlParser());
77 doc.outputSettings()
78 .outline(false)
79 .syntax(Document.OutputSettings.Syntax.xml)
80 .prettyPrint(false)
81 .escapeMode(org.jsoup.nodes.Entities.EscapeMode.xhtml)
82 .charset(StandardCharsets.UTF_8);
83 return doc;
84 }
85
86 @Nonnull
87 public static Parser createHtmlParser() {
88 Parser parser = Parser.htmlParser();
89 parser.tagSet().onNewTag(tag -> {
90 if (!tag.isKnownTag())
91 tag.set(Tag.SelfClose);
92 });
93 return parser;
94 }
95
96 @Nonnull
97 public static Parser createXmlParser() {
98 Parser parser = Parser.xmlParser();
99 return parser;
100 }
101
102 public static Element link(Document doc,
103 String href,
104 String name,
105 String target,
106 String img,
107 String icon,
108 String className) {
109 Element a = doc.createElement("a");
110 if (href != null && !href.isEmpty()) {
111 a.attr("href", href);
112 }
113 if (name != null && !name.isEmpty()) {
114 a.attr("title", name);
115 }
116 if (target != null && !target.isEmpty()) {
117 a.attr("target", target);
118 }
119 if (className != null && !className.isEmpty()) {
120 a.addClass(className);
121 }
122 if (img != null && !img.isEmpty()) {
123
124 Element image = image(doc, img, "", "", "", "");
125 a.appendChild(image);
126 a.appendText(name);
127 } else if (icon != null && !icon.isEmpty()) {
128
129 a.appendText(name);
130 Element i = doc.createElement("i");
131 i.addClass(icon);
132 a.appendChild(i);
133 } else {
134
135 a.appendText(name);
136 }
137 return a;
138 }
139
140 public static Element image(Document doc, String src, String alt, String border, String width, String height) {
141 Element image = doc.createElement("img");
142 image.addClass("image-link");
143 image.attr("src", src);
144 if (alt != null && !alt.isEmpty()) {
145 image.attr("alt", alt);
146 }
147 if (border != null && !border.isEmpty()) {
148 image.attr("border", border);
149 }
150 if (width != null && !width.isEmpty()) {
151 image.attr("width", width);
152 }
153 if (height != null && !height.isEmpty()) {
154 image.attr("height", height);
155 }
156 return image;
157 }
158
159 public static String getNodeName(Node node) {
160 if (node instanceof Element) {
161 return ((Element) node).tagName();
162 } else {
163 return node.nodeName();
164 }
165 }
166
167 public static boolean hasTextNode(Node node) {
168 return node.childNodes().stream().anyMatch(n -> {
169 boolean match = n.nodeName().equals("#text") && !n.toString().trim().isEmpty();
170 return match;
171 });
172 }
173
174 }