java.lang.Object
org.rrd4j.core.XmlWriter
All Implemented Interfaces:
AutoCloseable

public class XmlWriterextends Object implements AutoCloseable
Extremely simple utility class used to create XML documents.
  • Field Details

    • INDENT_STR

      static final String INDENT_STR
      Indentation string for XML output
      See Also:
    • STYLE

      private static final String STYLE
      Attribute name carrying the RRD tool style.
      See Also:
    • ISOLIKE

      private static final java.time.format.DateTimeFormatter ISOLIKE
      Timestamp format written into the XML, in UTC.
    • DEFAULT_NAN_STRING

      private static final String DEFAULT_NAN_STRING
      How NaN is written when no other form is given.
    • writer

      private final PrintWriter writer
      Where the XML is written.
    • indent

      private final StringBuilder indent
      Indentation of the element currently being written.
    • openTags

      private final Deque<String> openTags
      Elements not yet closed, innermost last.
    • timeFormatter

      private final java.time.format.DateTimeFormatter timeFormatter
      Time formatter
    • doubleFormatter

      private final XmlWriter.DoubleFormater doubleFormatter
      Double formatter
  • Constructor Details

    • XmlWriter

      private XmlWriter(PrintWriter writer, java.time.format.DateTimeFormatter timeFormatter, XmlWriter.DoubleFormater doubleFormatter)
      Creates a writer for one XML document.
      Parameters:
      writer - where the XML is written
      timeFormatter - how timestamps are written
      doubleFormatter - how numbers are written
    • XmlWriter

      public XmlWriter(OutputStream stream)
      Creates XmlWriter with the specified OutputStream to send XML code to.
      Parameters:
      stream - OutputStream which receives XML code
    • XmlWriter

      public XmlWriter(OutputStream stream, boolean autoFlush)
      Creates XmlWriter with the specified output stream to send XML code to.
      Parameters:
      stream - Output stream which receives XML code
      autoFlush - is the stream to be flushed automatically
    • XmlWriter

      public XmlWriter(PrintWriter stream)
      Creates XmlWriter with the specified PrintWriter to send XML code to.
      Parameters:
      stream - PrintWriter which receives XML code
  • Method Details

    • withTimeZone

      public XmlWriter withTimeZone(java.time.ZoneId zid)
      Return a new XmlWriter that will format time stamp as ISO 8601 with this explicit time zone ZoneId
      Parameters:
      zid - the time zone the returned writer renders time stamps in
      Returns:
      a new writer sharing this one's stream, unused otherwise
    • withDoubleFormatter

      public XmlWriter withDoubleFormatter(XmlWriter.DoubleFormater doubleFormatter)
      Return a new XmlWriter that will format time stamp using this ZoneId
      Parameters:
      doubleFormatter - the formatter the returned writer renders DS values with
      Returns:
      a new writer sharing this one's stream, unused otherwise
    • startTag

      public void startTag(String tag)
      Opens XML tag
      Parameters:
      tag - XML tag name
    • closeTag

      public void closeTag()
      Closes the corresponding XML tag
    • writeTag

      public void writeTag(String tag, Object value)
      Writes <tag>value</tag> to output stream
      Parameters:
      tag - XML tag name
      value - value to be placed between <tag> and </tag>
    • writeTag

      public void writeTag(String tag, int value)
      Writes <tag>value</tag> to output stream
      Parameters:
      tag - XML tag name
      value - value to be placed between <tag> and </tag>
    • writeTag

      public void writeTag(String tag, long value)
      Writes <tag>value</tag> to output stream
      Parameters:
      tag - XML tag name
      value - value to be placed between <tag> and </tag>
    • writeTag

      public void writeTag(String tag, double value, String nanString)
      Writes <tag>value</tag> to output stream
      Parameters:
      tag - XML tag name
      value - value to be placed between <tag> and </tag>
      nanString - a String object.
    • writeTag

      public void writeTag(String tag, double value)
      Writes <tag>value</tag> to output stream
      Parameters:
      tag - XML tag name
      value - value to be placed between <tag> and </tag>
    • writeTag

      public void writeTag(String tag, boolean value)
      Writes <tag>value</tag> to output stream
      Parameters:
      tag - XML tag name
      value - value to be placed between <tag> and </tag>
    • writeTag

      public void writeTag(String tag, Color value)
      Writes <tag>value</tag> to output stream
      Parameters:
      tag - XML tag name
      value - value to be placed between <tag> and </tag>
    • writeTag

      public void writeTag(String tag, Font value)
      Writes <tag>value</tag> to output stream
      Parameters:
      tag - XML tag name
      value - value to be placed between <tag> and </tag>
    • writeTag

      public void writeTag(String tag, File value)
      Writes <tag>value</tag> to output stream
      Parameters:
      tag - XML tag name
      value - value to be placed between <tag> and </tag>
    • flush

      public void flush()
      Flushes the output stream
    • writeComment

      public void writeComment(Object comment)
      Writes XML comment to output stream
      Parameters:
      comment - comment string
    • writeComment

      public void writeComment(long timestamp)
      Writes a timestamp using the configured DateTimeFormatter as an XML comment to output stream.
      Parameters:
      timestamp - the timestamp to write
    • formatTimestamp

      public String formatTimestamp(long timestamp)
      Formats a timestamp using the configured DateTimeFormatter.
      Parameters:
      timestamp - the timestamp to format
      Returns:
      the formatted timestamp
    • escape

      private static String escape(String s)
      Escapes XML special characters in a string.
      Parameters:
      s - the string to escape
      Returns:
      the escaped string
    • close

      public void close()
      Flushes the underlying writer, then closes it.
      Specified by:
      close in interface AutoCloseable