View Javadoc
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   * An Apache Velocity tool that provides utility methods to manipulate HTML code using
20   * <a href="http://jsoup.org/">jsoup</a> HTML5 parser.
21   *
22   * @author Michael E. Cotterell
23   */
24  @DefaultKey("soup")
25  public class JsoupTool extends SafeConfig {
26  
27      private Charset outputEncoding = UTF_8;
28  
29      /**
30       * Construct a {@code JsoupTool} object.
31       */
32      public JsoupTool() {
33          setSafeMode(false);
34          System.err.println("constructing JsoupTool");
35      } // JsoupTool
36  
37      /**
38       * Get the output encoding.
39       *
40       * @return the output encoding.
41       */
42      public Charset getOutputEncoding() {
43          return this.outputEncoding;
44      } // getOutputEncoding
45  
46      /**
47       * Set the output encoding.
48       *
49       * @param outputEncoding the desired output encoding
50       */
51      protected void setOutputEncoding(Charset outputEncoding) {
52          this.outputEncoding = outputEncoding;
53      } // setOutputEncoding
54  
55      /**
56       * Parse the supplied HTML {@code content} string using jsoup.
57       *
58       * @param content HTML content string
59       * @return An {@link org.jsoup.nodes.Element} that wraps the supplied {@code content}.
60       */
61      public Element parse(String content) {
62          Document doc = Jsoup.parseBodyFragment(content);
63          doc.outputSettings().charset(outputEncoding);
64          return doc.body();
65      } // parse
66  
67      /**
68       * Select elements in the supplied HTML {@code content} string that match the
69       * selector CSS query, with this element as the starting context. Matched
70       * elements may include this element, or any of its children.
71       *
72       * @param content HTML content string
73       * @param cssQuery a selector CSS-like query
74       * @return The selected elements.
75       */
76      public Elements select(String content, String cssQuery) {
77          Element elem = parse(content);
78          return elem.select(cssQuery);
79      } // select
80  
81  } // JsoupTool