Class HTMLWriter

  • Direct Known Subclasses:
    HTMLWriterEx

    public class HTMLWriter
    extends java.lang.Object
    A class to facilitate writing HTML via a stream.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String A
      The HTML "a" tag.
      static java.lang.String ALIGN
      The HTML "align" attribute.
      static java.lang.String B
      The HTML "b" tag.
      static java.lang.String BODY
      The HTML "body" tag.
      static java.lang.String BORDER
      The HTML "border" attribute.
      static java.lang.String BR
      The HTML "br" tag.
      static java.lang.String CLASSID
      The HTML "classid" attribute.
      static java.lang.String CODE
      The HTML "code" tag.
      static java.lang.String COL
      The HTML "col" attribute value.
      static java.lang.String COLOR
      The HTML "color" attribute.
      static java.lang.String FONT
      The HTML "font" tag.
      static java.lang.String H1
      The HTML "h1" tag.
      static java.lang.String H2
      The HTML "h2" tag.
      static java.lang.String H3
      The HTML "h3" tag.
      static java.lang.String H4
      The HTML "h4" tag.
      static java.lang.String HEAD
      The HTML "head" tag.
      static java.lang.String HR
      The HTML "hr" tag.
      static java.lang.String HREF
      The HTML "href" attribute.
      static java.lang.String HTML
      The HTML "html" tag.
      static java.lang.String I
      The HTML "i" tag.
      static java.lang.String ID
      The HTML "id" attribute.
      static java.lang.String IMAGE
      The HTML "image" tag.
      static java.lang.String LEFT
      The HTML "left" attribute value.
      static java.lang.String LI
      The HTML "li" tag.
      static java.lang.String LINK
      The HTML "link" tag.
      static java.lang.String NAME
      The HTML "name" attribute.
      static java.lang.String OBJECT
      The HTML "object" tag.
      static java.lang.String P
      The HTML "param" tag.
      static java.lang.String PARAM
      The HTML "p" tag.
      static java.lang.String REL
      The HTML "rel" attribute value.
      static java.lang.String RIGHT
      The HTML "right" attribute value.
      static java.lang.String ROW
      The HTML "row" attribute value.
      static java.lang.String SCOPE
      The HTML "scope" attribute.
      static java.lang.String SMALL
      The HTML "small" tag.
      static java.lang.String SRC
      The HTML "src" attribute.
      static java.lang.String STYLE
      The HTML "style" attribute.
      static java.lang.String SUMMARY
      The HTML "summary" attribute.
      static java.lang.String TABLE
      The HTML "table" tag.
      static java.lang.String TD
      The HTML "td" tag.
      static java.lang.String TEXT_LEFT  
      static java.lang.String TEXT_RIGHT  
      static java.lang.String TH
      The HTML "th" tag.
      static java.lang.String TITLE
      The HTML "title"attribute.
      static java.lang.String TOP
      The HTML "top" attribute value.
      static java.lang.String TR
      The HTML "tr" tag.
      static java.lang.String TYPE
      The HTML "type" attribute.
      static java.lang.String UL
      The HTML "ul" tag.
      static java.lang.String VALIGN
      The HTML "valign" attribute.
      static java.lang.String VALUE
      The HTML "value" attribute.
    • Constructor Summary

      Constructors 
      Constructor Description
      HTMLWriter​(java.io.Writer out)
      Create an HTMLWriter object, using a default doctype for HTML 3.2.
      HTMLWriter​(java.io.Writer out, I18NResourceBundle i18n)
      Create an HTMLWriter object, using a specified bundle for l0calizing messages.
      HTMLWriter​(java.io.Writer out, java.lang.String docType)
      Create an HTMLWriter object, using a specified doctype header.
      HTMLWriter​(java.io.Writer out, java.lang.String docType, I18NResourceBundle i18n)
      Create an HTMLWriter object, using a specified doctype header and using a specified bundle for localizing messages.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()
      Close the stream, and the underlying output stream.
      protected boolean closePrevTag()  
      void endEmptyTag​(java.lang.String tag)
      Finish an empty element tag, such as a META, BASE or LINK tag.
      void endTag​(java.lang.String tag)
      Finish an HTML tag.
      void flush()
      Flush the stream, and the underlying output stream.
      java.lang.String getCanonicalURIPath​(java.io.File file)
      Returns URI path to the specified absolute file.
      void newLine()
      Write a newline to the underlying output stream.
      void setI18NResourceBundle​(I18NResourceBundle i18n)
      Set the resource bundle to be used for localizing messages.
      void startTag​(java.lang.String tag)
      Start an HTML tag.
      void write​(I18NResourceBundle i18n, java.lang.String key)
      Write a localized message, using a specified resource bundle.
      void write​(I18NResourceBundle i18n, java.lang.String key, java.lang.Object arg)
      Write a localized message, using a specified resource bundle.
      void write​(I18NResourceBundle i18n, java.lang.String key, java.lang.Object... args)
      Write a localized message, using a specified resource bundle.
      void write​(java.lang.String text)
      Write body text, escaping it as necessary.
      void writeAttr​(java.lang.String name, int value)
      Write an attribute for a tag.
      void writeAttr​(java.lang.String name, java.lang.String value)
      Write an attribute for a tag.
      void writeEntity​(java.lang.String entity)
      Write a basic HTML entity, such as   or { .
      void writeI18N​(java.lang.String key)
      Write a localized message, using the default resource bundle.
      void writeI18N​(java.lang.String key, java.lang.Object arg)
      Write a localized message, using the default resource bundle.
      void writeI18N​(java.lang.String key, java.lang.Object... args)
      Write a localized message, using the default resource bundle.
      void writeImage​(java.lang.String imagePath)
      Write an image tag, using a specified path for the image source attribute.
      void writeImage​(java.net.URL imageURL)
      Write an image tag, using a specified path for the image source attribute.
      void writeLangAttr()
      Writes value of 'lang' attribute that is defined in resource bundle.
      void writeLine​(java.lang.String text)
      Write a line of text, followed by a newline.
      void writeLink​(java.io.File file)
      Write a hypertext link.
      void writeLink​(java.io.File file, java.lang.String body)
      Write a hypertext link.
      void writeLink​(java.lang.String anchor, java.lang.String body)
      Write a hypertext link.
      void writeLink​(java.net.URL url, java.lang.String body)
      Write a hypertext link.
      void writeLinkDestination​(java.lang.String anchor, java.lang.String body)
      Write the destination marker for a hypertext link.
      void writeParam​(java.lang.String name, java.lang.String value)
      Write a parameter tag.
      void writeRaw​(java.lang.String text)
      Write body text, escaping it as necessary.
      void writeStyleAttr​(java.lang.String value)
      Write a style attribute.
      • Methods inherited from class java.lang.Object

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

      • HTMLWriter

        public HTMLWriter​(java.io.Writer out)
                   throws java.io.IOException
        Create an HTMLWriter object, using a default doctype for HTML 3.2.
        Parameters:
        out - a Writer to which to write the generated HTML
        Throws:
        java.io.IOException - if there is a problem writing to the underlying stream
      • HTMLWriter

        public HTMLWriter​(java.io.Writer out,
                          java.lang.String docType)
                   throws java.io.IOException
        Create an HTMLWriter object, using a specified doctype header.
        Parameters:
        out - a Writer to which to write the generated HTML
        docType - a string containing a doctype header for the HTML to be generated
        Throws:
        java.io.IOException - if there is a problem writing to the underlying stream
      • HTMLWriter

        public HTMLWriter​(java.io.Writer out,
                          I18NResourceBundle i18n)
                   throws java.io.IOException
        Create an HTMLWriter object, using a specified bundle for l0calizing messages.
        Parameters:
        out - a Writer to which to write the generated HTML
        i18n - a resource bundle to use to localize messages
        Throws:
        java.io.IOException - if there is a problem writing to the underlying stream
      • HTMLWriter

        public HTMLWriter​(java.io.Writer out,
                          java.lang.String docType,
                          I18NResourceBundle i18n)
                   throws java.io.IOException
        Create an HTMLWriter object, using a specified doctype header and using a specified bundle for localizing messages.
        Parameters:
        out - a Writer to which to write the generated HTML
        docType - a string containing a doctype header for the HTML to be generated
        i18n - a resource bundle to use to localize messages
        Throws:
        java.io.IOException - if there is a problem writing to the underlying stream
    • Method Detail

      • setI18NResourceBundle

        public void setI18NResourceBundle​(I18NResourceBundle i18n)
        Set the resource bundle to be used for localizing messages.
        Parameters:
        i18n - the resource bundle to be used for localizing messages
      • flush

        public void flush()
                   throws java.io.IOException
        Flush the stream, and the underlying output stream.
        Throws:
        java.io.IOException - if there is a problem writing to the underlying stream
      • close

        public void close()
                   throws java.io.IOException
        Close the stream, and the underlying output stream.
        Throws:
        java.io.IOException - if there is a problem closing the underlying stream
      • newLine

        public void newLine()
                     throws java.io.IOException
        Write a newline to the underlying output stream.
        Throws:
        java.io.IOException - if there is a problem writing to the underlying stream
      • startTag

        public void startTag​(java.lang.String tag)
                      throws java.io.IOException
        Start an HTML tag. If a prior tag has been started, it will be closed first. Once a tag has been opened, attributes for the tag may be written out, followed by body content before finally ending the tag.
        Parameters:
        tag - the tag to be started
        Throws:
        java.io.IOException - if there is a problem writing to the underlying stream
        See Also:
        writeAttr(java.lang.String, java.lang.String), write(java.lang.String), endTag(java.lang.String)
      • endTag

        public void endTag​(java.lang.String tag)
                    throws java.io.IOException
        Finish an HTML tag. It is expected that a call to endTag will match a corresponding earlier call to startTag, but there is no formal check for this.
        Parameters:
        tag - the tag to be closed.
        Throws:
        java.io.IOException - if there is a problem writing to the underlying stream
      • endEmptyTag

        public void endEmptyTag​(java.lang.String tag)
                         throws java.io.IOException
        Finish an empty element tag, such as a META, BASE or LINK tag. This is expected to correspond with a startTag.
        Parameters:
        tag - the tag which is being closed. this is only useful for validation, it is not written out
        Throws:
        java.lang.IllegalStateException - if this call does not follow startTag (stream is not currently inside a tag)
        java.io.IOException - if there is a problem writing to the underlying stream
      • writeAttr

        public void writeAttr​(java.lang.String name,
                              java.lang.String value)
                       throws java.io.IOException
        Write an attribute for a tag. A tag must previously have been started. All tag attributes must be written before any body text is written. The value will be quoted. No check is made that the attribute is valid for the current tag.
        Parameters:
        name - the name of the attribute to be written
        value - the value of the attribute to be written
        Throws:
        java.lang.IllegalStateException - if the stream is not in a state to write attributes -- e.g. if this call does not follow startTag or other calls of writteAttr
        java.io.IOException - if there is a problem writing to the underlying stream
      • writeLangAttr

        public void writeLangAttr()
                           throws java.io.IOException
        Writes value of 'lang' attribute that is defined in resource bundle. This method is expected to be called after opening (starting) the root HTML tag.
        Throws:
        java.io.IOException - if there is a problem writing to the underlying stream
      • writeAttr

        public void writeAttr​(java.lang.String name,
                              int value)
                       throws java.io.IOException
        Write an attribute for a tag. A tag must previously have been started. All tag attributes must be written before any body text is written. The value will be quoted if necessary when writing it to the underlying stream. No check is made that the attribute is valid for the current tag.
        Parameters:
        name - the name of the attribute to be written
        value - the value of the attribute to be written
        Throws:
        java.lang.IllegalStateException - if the stream is not in a state to write attributes -- e.g. if this call does not follow startTag or other calls of writteAttr
        java.io.IOException - if there is a problem writing to the underlying stream
      • writeLine

        public void writeLine​(java.lang.String text)
                       throws java.io.IOException
        Write a line of text, followed by a newline. The text will be escaped as necessary.
        Parameters:
        text - the text to be written.
        Throws:
        java.io.IOException - if there is a problem closing the underlying stream
      • writeRaw

        public void writeRaw​(java.lang.String text)
                      throws java.io.IOException
        Write body text, escaping it as necessary. If this call follows a call of startTag, the open tag will be closed -- meaning that no more attributes can be written until another tag is started. If the text value is null, the current tag will still be closed, but no other text will be written.
        Parameters:
        text - the text to be written, may be null or zero length.
        Throws:
        java.io.IOException - if there is a problem writing to the underlying stream
      • write

        public void write​(java.lang.String text)
                   throws java.io.IOException
        Write body text, escaping it as necessary. If this call follows a call of startTag, the open tag will be closed -- meaning that no more attributes can be written until another tag is started. If the text value is null, the current tag will still be closed, but no other text will be written.
        Parameters:
        text - the text to be written, may be null or zero length.
        Throws:
        java.io.IOException - if there is a problem writing to the underlying stream
      • writeEntity

        public void writeEntity​(java.lang.String entity)
                         throws java.io.IOException
        Write a basic HTML entity, such as   or { .
        Parameters:
        entity - the entity to write
        Throws:
        java.io.IOException - if there is a problem writing to the underlying stream
      • writeImage

        public void writeImage​(java.lang.String imagePath)
                        throws java.io.IOException
        Write an image tag, using a specified path for the image source attribute.
        Parameters:
        imagePath - the path for the image source
        Throws:
        java.io.IOException - if there is a problem closing the underlying stream
      • writeImage

        public void writeImage​(java.net.URL imageURL)
                        throws java.io.IOException
        Write an image tag, using a specified path for the image source attribute.
        Parameters:
        imageURL - the url for the image source
        Throws:
        java.io.IOException - if there is a problem closing the underlying stream
      • writeLink

        public void writeLink​(java.lang.String anchor,
                              java.lang.String body)
                       throws java.io.IOException
        Write a hypertext link.
        Parameters:
        anchor - the target for the link
        body - the body text for the link
        Throws:
        java.io.IOException - if there is a problem closing the underlying stream
      • writeLink

        public void writeLink​(java.io.File file,
                              java.lang.String body)
                       throws java.io.IOException
        Write a hypertext link.
        Parameters:
        file - the target for the link
        body - the body text for the link
        Throws:
        java.io.IOException - if there is a problem closing the underlying stream
      • getCanonicalURIPath

        public java.lang.String getCanonicalURIPath​(java.io.File file)
                                             throws java.io.IOException
        Returns URI path to the specified absolute file.
        Parameters:
        file - to get URI path
        Returns:
        URI path for the file
        Throws:
        java.io.IOException - if there is a problem to encode path symbol
      • writeLink

        public void writeLink​(java.io.File file)
                       throws java.io.IOException
        Write a hypertext link.
        Parameters:
        file - the target and body for the link
        Throws:
        java.io.IOException - if there is a problem closing the underlying stream
      • writeLink

        public void writeLink​(java.net.URL url,
                              java.lang.String body)
                       throws java.io.IOException
        Write a hypertext link.
        Parameters:
        url - the target for the link
        body - the body text for the link
        Throws:
        java.io.IOException - if there is a problem closing the underlying stream
      • writeLinkDestination

        public void writeLinkDestination​(java.lang.String anchor,
                                         java.lang.String body)
                                  throws java.io.IOException
        Write the destination marker for a hypertext link.
        Parameters:
        anchor - the destination marker for hypertext links
        body - the body text for the marker
        Throws:
        java.io.IOException - if there is a problem closing the underlying stream
      • writeParam

        public void writeParam​(java.lang.String name,
                               java.lang.String value)
                        throws java.io.IOException
        Write a parameter tag.
        Parameters:
        name - the name of the parameter
        value - the value of the parameter
        Throws:
        java.io.IOException - if there is a problem closing the underlying stream
      • writeStyleAttr

        public void writeStyleAttr​(java.lang.String value)
                            throws java.io.IOException
        Write a style attribute.
        Parameters:
        value - the value for the style attribute
        Throws:
        java.io.IOException - if there is a problem closing the underlying stream
      • write

        public void write​(I18NResourceBundle i18n,
                          java.lang.String key)
                   throws java.io.IOException
        Write a localized message, using a specified resource bundle.
        Parameters:
        i18n - the resource bundle used to localize the message
        key - the key for the message to be localized
        Throws:
        java.io.IOException - if there is a problem closing the underlying stream
      • write

        public void write​(I18NResourceBundle i18n,
                          java.lang.String key,
                          java.lang.Object arg)
                   throws java.io.IOException
        Write a localized message, using a specified resource bundle.
        Parameters:
        i18n - the resource bundle used to localize the message
        key - the key for the message to be localized
        arg - an argument to be formatted into the localized message
        Throws:
        java.io.IOException - if there is a problem closing the underlying stream
      • write

        public void write​(I18NResourceBundle i18n,
                          java.lang.String key,
                          java.lang.Object... args)
                   throws java.io.IOException
        Write a localized message, using a specified resource bundle.
        Parameters:
        i18n - the resource bundle used to localize the message
        key - the key for the message to be localized
        args - arguments to be formatted into the localized message
        Throws:
        java.io.IOException - if there is a problem closing the underlying stream
      • writeI18N

        public void writeI18N​(java.lang.String key)
                       throws java.io.IOException
        Write a localized message, using the default resource bundle.
        Parameters:
        key - the key for the message to be localized
        Throws:
        java.io.IOException - if there is a problem closing the underlying stream
      • writeI18N

        public void writeI18N​(java.lang.String key,
                              java.lang.Object arg)
                       throws java.io.IOException
        Write a localized message, using the default resource bundle.
        Parameters:
        key - the key for the message to be localized
        arg - an argument to be formatted into the localized message
        Throws:
        java.io.IOException - if there is a problem closing the underlying stream
      • writeI18N

        public void writeI18N​(java.lang.String key,
                              java.lang.Object... args)
                       throws java.io.IOException
        Write a localized message, using the default resource bundle.
        Parameters:
        key - the key for the message to be localized
        args - arguments to be formatted into the localized message
        Throws:
        java.io.IOException - if there is a problem closing the underlying stream
      • closePrevTag

        protected boolean closePrevTag()
                                throws java.io.IOException
        Throws:
        java.io.IOException