1 package io.github.cs1302uga;
2
3 import static java.nio.charset.StandardCharsets.UTF_8;
4
5 import java.nio.charset.Charset;
6 import java.util.Optional;
7
8 import org.apache.velocity.tools.ToolContext;
9 import org.apache.velocity.tools.config.DefaultKey;
10 import org.apache.velocity.tools.generic.SafeConfig;
11 import org.apache.velocity.tools.generic.ValueParser;
12
13 import org.jsoup.Jsoup;
14 import org.jsoup.nodes.Document;
15 import org.jsoup.nodes.Element;
16 import org.jsoup.select.Elements;
17
18
19
20
21
22
23
24 @DefaultKey("soup")
25 public class JsoupTool extends SafeConfig {
26
27 private Charset outputEncoding = UTF_8;
28
29
30
31
32 public JsoupTool() {
33 setSafeMode(false);
34 System.err.println("constructing JsoupTool");
35 }
36
37
38
39
40
41
42 public Charset getOutputEncoding() {
43 return this.outputEncoding;
44 }
45
46
47
48
49
50
51 protected void setOutputEncoding(Charset outputEncoding) {
52 this.outputEncoding = outputEncoding;
53 }
54
55
56
57
58
59
60
61 public Element parse(String content) {
62 Document doc = Jsoup.parseBodyFragment(content);
63 doc.outputSettings().charset(outputEncoding);
64 return doc.body();
65 }
66
67
68
69
70
71
72
73
74
75
76 public Elements select(String content, String cssQuery) {
77 Element elem = parse(content);
78 return elem.select(cssQuery);
79 }
80
81 }