Class HtmlUtils


  • public class HtmlUtils
    extends Object
    Utility class for HTML. Kudos to Adam Winer (Oracle) for much of this code.
    • Method Detail

      • writeText

        public static void writeText​(Writer out,
                                     boolean escapeUnicode,
                                     boolean escapeIsocode,
                                     char[] buffer,
                                     char[] text,
                                     boolean forXml)
                              throws IOException
        Throws:
        IOException
      • writeText

        public static void writeText​(Writer out,
                                     boolean escapeUnicode,
                                     boolean escapeIsocode,
                                     char[] buff,
                                     char[] text,
                                     int start,
                                     int length,
                                     boolean forXml)
                              throws IOException
        Write char array text.
        Throws:
        IOException
      • writeText

        public static void writeText​(Writer out,
                                     boolean escapeUnicode,
                                     boolean escapeIsocode,
                                     char[] buff,
                                     String text,
                                     char[] textBuff,
                                     boolean forXml)
                              throws IOException
        Write String text.
        Throws:
        IOException
      • writeAttribute

        public static void writeAttribute​(Writer out,
                                          boolean escapeUnicode,
                                          boolean escapeIsocode,
                                          char[] buff,
                                          String text,
                                          char[] textBuff,
                                          boolean isScriptInAttributeValueEnabled,
                                          boolean forXml)
                                   throws IOException
        Write a string attribute. Note that this code is duplicated below for character arrays - change both places if you make any changes!!!
        Throws:
        IOException
      • writeAttribute

        public static void writeAttribute​(Writer out,
                                          boolean escapeUnicode,
                                          boolean escapeIsocode,
                                          char[] buff,
                                          char[] text,
                                          int start,
                                          int length,
                                          boolean isScriptInAttributeValueEnabled,
                                          boolean forXml)
                                   throws IOException
        Write a character array attribute. Note that this code is duplicated above for string - change both places if you make any changes!!!
        Throws:
        IOException
      • writeURL

        public static void writeURL​(Writer out,
                                    String text,
                                    char[] textBuff,
                                    String queryEncoding)
                             throws IOException,
                                    UnsupportedEncodingException
        Writes a string into URL-encoded format out to a Writer. All characters before the start of the query string will be encoded using UTF-8. Characters after the start of the query string will be encoded using a client-defined encoding. You'll need to use the encoding that the server will expect. (HTML forms will generate query strings using the character encoding that the HTML itself was generated in.) All characters will be encoded as needed for URLs, with the exception of the percent symbol ("%"). Because this is the character itself used for escaping, attempting to escape this character would cause this code to double-escape some strings. It also may be necessary to pre-escape some characters. In particular, a question mark ("?") is considered the start of the query string.

        NOTE: This is method is duplicated below. The difference being the acceptance of a char[] for the text to write. Any changes made here, should be made below.

        Parameters:
        out - a Writer for the output
        text - the unencoded (or partially encoded) String
        queryEncoding - the character set encoding for after the first question mark
        Throws:
        IOException
        UnsupportedEncodingException
      • writeURL

        public static void writeURL​(Writer out,
                                    char[] textBuff,
                                    int start,
                                    int len,
                                    String queryEncoding)
                             throws IOException,
                                    UnsupportedEncodingException
        Writes a string into URL-encoded format out to a Writer. All characters before the start of the query string will be encoded using UTF-8. Characters after the start of the query string will be encoded using a client-defined encoding. You'll need to use the encoding that the server will expect. (HTML forms will generate query strings using the character encoding that the HTML itself was generated in.) All characters will be encoded as needed for URLs, with the exception of the percent symbol ("%"). Because this is the character itself used for escaping, attempting to escape this character would cause this code to double-escape some strings. It also may be necessary to pre-escape some characters. In particular, a question mark ("?") is considered the start of the query string.

        NOTE: This is method is duplicated above. The difference being the acceptance of a String for the text to write. Any changes made here, should be made above.

        Parameters:
        out - a Writer for the output
        textBuff - char[] containing the content to write
        queryEncoding - the character set encoding for after the first question mark
        Throws:
        IOException
        UnsupportedEncodingException
      • validateEncoding

        public static boolean validateEncoding​(String encoding)
      • isISO8859_1encoding

        public static boolean isISO8859_1encoding​(String encoding)
      • isUTFencoding

        public static boolean isUTFencoding​(String encoding)
      • isEmptyElement

        public static boolean isEmptyElement​(String name)