Class XMLWriter


  • public class XMLWriter
    extends java.lang.Object
    A class to facilitate writing XML via a stream.
    • Constructor Summary

      Constructors 
      Constructor Description
      XMLWriter​(java.io.Writer out)
      Create an XMLWriter object, using a default header.
      XMLWriter​(java.io.Writer out, I18NResourceBundle i18n)
      Create an XMLWriter object, using a specified bundle for l0calizing messages.
      XMLWriter​(java.io.Writer out, java.lang.String docType)
      Create an XMLWriter object, using a specified doctype header.
      XMLWriter​(java.io.Writer out, java.lang.String docType, I18NResourceBundle i18n)
      Create an XMLWriter 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.
      void endTag​(java.lang.String tag)
      Finish an XML tag.
      void flush()
      Flush the stream, and the underlying output stream.
      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 XML 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, java.lang.String value)
      Write an attribute for a tag.
      void writeDate​(long millis)
      Write a formatted date.
      void writeDate​(java.lang.String tag, long millis)
      Write a formatted date surrounded by a markup tag.
      void writeDate​(java.lang.String tag, java.util.Date date)
      Write a formatted date surrounded by a markup tag.
      void writeDate​(java.util.Date date)
      Write a formatted date.
      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 writeLine​(java.lang.String text)
      Write a line of text, followed by a newline.
      • Methods inherited from class java.lang.Object

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

      • XMLWriter

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

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

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

        public XMLWriter​(java.io.Writer out,
                         java.lang.String docType,
                         I18NResourceBundle i18n)
                  throws java.io.IOException
        Create an XMLWriter object, using a specified doctype header and using a specified bundle for localizing messages.
        Parameters:
        out - a Writer to which to write the generated XML
        docType - a string containing a doctype header for the XML 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 closing the underlying stream
      • startTag

        public void startTag​(java.lang.String tag)
                      throws java.io.IOException
        Start an XML 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 closing 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 XML 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 closing 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 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 closing 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
      • writeDate

        public void writeDate​(long millis)
                       throws java.io.IOException
        Write a formatted date.
        Parameters:
        millis - date represented in milliseconds
        Throws:
        java.io.IOException - if a exception occurs during writing
        See Also:
        Date
      • writeDate

        public void writeDate​(java.lang.String tag,
                              long millis)
                       throws java.io.IOException
        Write a formatted date surrounded by a markup tag.
        Parameters:
        tag - tag to open and close before and after the date is inserted
        millis - date represented in milliseconds
        Throws:
        java.io.IOException - if a exception occurs during writing
        See Also:
        Date
      • writeDate

        public void writeDate​(java.util.Date date)
                       throws java.io.IOException
        Write a formatted date.
        Parameters:
        date - the date to print
        Throws:
        java.io.IOException - if a exception occurs during writing
      • writeDate

        public void writeDate​(java.lang.String tag,
                              java.util.Date date)
                       throws java.io.IOException
        Write a formatted date surrounded by a markup tag.
        Parameters:
        tag - tag to open and close before and after the date is inserted
        date - the date to print
        Throws:
        java.io.IOException - if a exception occurs during writing
        See Also:
        Date
      • 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.
        Parameters:
        text - the text to be written
        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