Class HtmlTools


  • public class HtmlTools
    extends java.lang.Object
    The HtmlTools class defines methods to HTML handling.
    Since:
    1.0
    Author:
    Vincent Siveton
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String encodeId​(java.lang.String id)
      Construct a valid id.
      static java.lang.String encodeURL​(java.lang.String url)
      Encode an url
      static java.lang.String escapeHTML​(java.lang.String text)
      Escape special HTML characters in a String in xml mode.
      static java.lang.String escapeHTML​(java.lang.String text, boolean xmlMode)
      Escape special HTML characters in a String.
      static javax.swing.text.html.HTML.Tag getHtmlTag​(java.lang.String tagName)
      Returns a tag for a defined HTML tag name.
      static boolean isId​(java.lang.String text)
      Determines if the specified text is a valid id according to the rules laid out in encodeId(String).
      static char[] toChars​(int codePoint)
      Converts the given code point to an equivalent character array.
      static java.lang.String unescapeHTML​(java.lang.String text)
      Unescapes HTML entities in a string in non xml mode.
      static java.lang.String unescapeHTML​(java.lang.String text, boolean xmlMode)
      Unescapes HTML entities in a string.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • getHtmlTag

        public static javax.swing.text.html.HTML.Tag getHtmlTag​(java.lang.String tagName)
        Returns a tag for a defined HTML tag name. This is one of the tags defined in HtmlMarkup. If the given name does not represent one of the defined tags, then null will be returned.
        Parameters:
        tagName - the String name requested.
        Returns:
        a tag constant corresponding to the tagName, or null if not found.
        Since:
        1.1
        See Also:
        http://www.w3.org/TR/html401/index/elements.html
      • escapeHTML

        public static java.lang.String escapeHTML​(java.lang.String text)
        Escape special HTML characters in a String in xml mode. Note: this method doesn't escape non-ascii characters by numeric characters references.
        Parameters:
        text - the String to escape, may be null.
        Returns:
        The escaped text or the empty string if text == null.
        See Also:
        escapeHTML(String,boolean)
      • escapeHTML

        public static java.lang.String escapeHTML​(java.lang.String text,
                                                  boolean xmlMode)
        Escape special HTML characters in a String.
         < becomes &lt;
         > becomes &gt;
         & becomes &amp;
         " becomes &quot;
         ' becomes &apos; if xmlMode = true
         
        If xmlMode is true, every other character than the above remains unchanged, if xmlMode is false, non-ascii characters get replaced by their hex code. Note: all characters are encoded, i.e.:
         ř       = &#x159;
         𝟭 = &#x1d7ed;
         
        Parameters:
        text - The String to escape, may be null.
        xmlMode - true to replace also ' to &apos, false to replace non-ascii characters by numeric characters references.
        Returns:
        The escaped text or the empty string if text == null.
        Since:
        1.1
        See Also:
        http://www.w3.org/TR/2000/REC-xml-20001006#sec-predefined-ent, http://www.w3.org/TR/html401/charset.html#h-5.3
      • unescapeHTML

        public static java.lang.String unescapeHTML​(java.lang.String text)
        Unescapes HTML entities in a string in non xml mode.
        Parameters:
        text - the String to unescape, may be null.
        Returns:
        a new unescaped String, null if null string input.
        Since:
        1.1.1.
        See Also:
        unescapeHTML(String, boolean)
      • unescapeHTML

        public static java.lang.String unescapeHTML​(java.lang.String text,
                                                    boolean xmlMode)
        Unescapes HTML entities in a string.

        Unescapes a string containing entity escapes to a string containing the actual Unicode characters corresponding to the escapes. Supports HTML 4.0 entities.

        For example, the string "&lt;Fran&ccedil;ais&gt;" will become "<Français>".

        Note: all unicode entities are decoded, i.e.:
         &#x159;   = ř
         &#x1d7ed; = 𝟭
         
        Parameters:
        text - the String to unescape, may be null.
        xmlMode - set to true to replace &apos by '.
        Returns:
        a new unescaped String, null if null string input.
        Since:
        1.1.1.
      • encodeURL

        public static java.lang.String encodeURL​(java.lang.String url)
        Encode an url
        Parameters:
        url - the String to encode, may be null
        Returns:
        the text encoded, null if null String input
      • isId

        public static boolean isId​(java.lang.String text)
        Determines if the specified text is a valid id according to the rules laid out in encodeId(String).
        Parameters:
        text - The text to be tested.
        Returns:
        true if the text is a valid id, otherwise false.
        See Also:
        DoxiaUtils.isValidId(String)
      • toChars

        public static char[] toChars​(int codePoint)
        Converts the given code point to an equivalent character array.
        Parameters:
        codePoint - the code point to convert.
        Returns:
        If codePoint is a supplementary code point, returns a character array of length 2, otherwise a character array of length 1 containing only the original int as a char.