Class HtmlDomUtil

java.lang.Object
com.google.gerrit.httpd.HtmlDomUtil

public class HtmlDomUtil extends Object
Utility functions to deal with HTML using W3C DOM operations.
  • Field Details

    • ENC

      public static final Charset ENC
      Standard character encoding we prefer (UTF-8).
    • HTML_STRICT

      public static final String HTML_STRICT
      DOCTYPE for a standards mode HTML document.
      See Also:
  • Constructor Details

    • HtmlDomUtil

      public HtmlDomUtil()
  • Method Details

    • toUTF8

      public static byte[] toUTF8(Document hostDoc) throws IOException
      Convert a document to a UTF-8 byte sequence.
      Throws:
      IOException
    • compress

      public static byte[] compress(byte[] raw) throws IOException
      Compress the document.
      Throws:
      IOException
    • toString

      public static String toString(Document hostDoc) throws IOException
      Convert a document to a String, assuming later encoding to UTF-8.
      Throws:
      IOException
    • find

      public static Element find(Node parent, String name)
      Find an element by its "id" attribute; null if no element is found.
    • addHidden

      public static void addHidden(Element form, String name, String value)
      Append an HTML <input type="hidden"> to the form.
    • newDocument

      public static Document newDocument()
      Construct a new empty document.
    • clone

      public static Document clone(Document doc) throws IOException
      Clone a document so it can be safely modified on a per-request basis.
      Throws:
      IOException
    • parseFile

      public static Document parseFile(Class<?> context, String name) throws IOException
      Parse an XHTML file from our CLASSPATH and return the instance.
      Throws:
      IOException
    • readFile

      public static String readFile(Class<?> context, String name) throws IOException
      Read a Read a UTF-8 text file from our CLASSPATH and return it.
      Throws:
      IOException
    • parseFile

      public static Document parseFile(Path path) throws IOException
      Parse an XHTML file from the local drive and return the instance.
      Throws:
      IOException
    • readFile

      public static String readFile(Path parentDir, String name) throws IOException
      Read a UTF-8 text file from the local drive.
      Throws:
      IOException
    • attachNonce

      public static Optional<String> attachNonce(String html, String nonce)
      Attaches nonce to all script elements in html.

      The returned html is not guaranteed to have the same formatting as the input.

      Returns:
      Updated html or {#link Optional.empty()} if parsing failed.