java.lang.Object
org.rrd4j.core.Util.Xml
Enclosing class:
Util

public static class Util.Xmlextends Object
Various DOM utility functions.
  • Field Details

    • eh

      private static final ErrorHandler eh
      Handler reporting XML errors through the logging system.
  • Constructor Details

    • Xml

      private Xml()
      Utility class, not intended for instantiation.
  • Method Details

    • getChildNodes

      public static Node[] getChildNodes(Node parentNode)
      Returns every element child of the given node
      Parameters:
      parentNode - node whose children are collected
      Returns:
      element children in document order, empty if there are none
    • getChildNodes

      public static Node[] getChildNodes(Node parentNode, String childName)
      Returns the element children of the given node whose name matches
      Parameters:
      parentNode - node whose children are collected
      childName - element name to match, or null to accept every element
      Returns:
      matching element children in document order, empty if there are none
    • getFirstChildNode

      public static Node getFirstChildNode(Node parentNode, String childName)
      Returns the first element child carrying the requested name
      Parameters:
      parentNode - node whose children are searched
      childName - element name to match
      Returns:
      the first matching element child
      Throws:
      IllegalArgumentException - If no child carries that name
    • hasChildNode

      public static boolean hasChildNode(Node parentNode, String childName)
      Checks whether the given node has an element child of the requested name
      Parameters:
      parentNode - node whose children are searched
      childName - element name to match
      Returns:
      true if at least one child carries that name
    • getChildValue

      public static String getChildValue(Node parentNode, String childName)
      Returns the trimmed text of the first child element carrying the requested name
      Parameters:
      parentNode - node whose children are searched
      childName - element name to match
      Returns:
      text of the first matching child element, trimmed
      Throws:
      IllegalStateException - If no child carries that name
    • getChildValue

      public static String getChildValue(Node parentNode, String childName, boolean trim)
      Returns the text of the first child element carrying the requested name
      Parameters:
      parentNode - node whose children are searched
      childName - element name to match
      trim - whether to strip leading and trailing whitespace from the text
      Returns:
      text of the first matching child element, trimmed if requested
      Throws:
      IllegalStateException - If no child carries that name
    • getValue

      public static String getValue(Node node)
      Returns the trimmed text of the node's first child
      Parameters:
      node - element whose child supplies the text
      Returns:
      text of the first child, trimmed, or null if the node has no child
    • getValue

      public static String getValue(Node node, boolean trimValue)
      Returns the text of the node's first child
      Parameters:
      node - element whose child supplies the text
      trimValue - whether to strip leading and trailing whitespace from the text
      Returns:
      text of the first child, trimmed if requested, or null if the node has no child at all (an empty element such as <x/>)
    • getChildValueAsInt

      public static int getChildValueAsInt(Node parentNode, String childName)
      Reads a child element's text as an int
      Parameters:
      parentNode - node whose children are searched
      childName - element name to match
      Returns:
      the text parsed as an int, typically a row or step count
      Throws:
      IllegalStateException - If no child carries that name
      NumberFormatException - If the text is not a valid int
    • getValueAsInt

      public static int getValueAsInt(Node node)
      Reads a node's text as an int
      Parameters:
      node - element whose child supplies the text
      Returns:
      the text parsed as an int, typically a row or step count
      Throws:
      NumberFormatException - If the text is not a valid int
    • getChildValueAsLong

      public static long getChildValueAsLong(Node parentNode, String childName)
      Reads a child element's text as a long
      Parameters:
      parentNode - node whose children are searched
      childName - element name to match
      Returns:
      the text parsed as a long, typically a timestamp in seconds or a heartbeat span
      Throws:
      IllegalStateException - If no child carries that name
      NumberFormatException - If the text is not a valid long
    • getValueAsLong

      public static long getValueAsLong(Node node)
      Reads a node's text as a long
      Parameters:
      node - element whose child supplies the text
      Returns:
      the text parsed as a long, typically a timestamp in seconds or a heartbeat span
      Throws:
      NumberFormatException - If the text is not a valid long
    • getChildValueAsDouble

      public static double getChildValueAsDouble(Node parentNode, String childName)
      Reads a child element's text as a double, yielding NaN when unparsable
      Parameters:
      parentNode - node whose children are searched
      childName - element name to match
      Returns:
      the text parsed as a double, or NaN if it is not a valid double
      Throws:
      IllegalStateException - If no child carries that name
    • getValueAsDouble

      public static double getValueAsDouble(Node node)
      Reads a node's text as a double, yielding NaN when unparsable
      Parameters:
      node - element whose child supplies the text
      Returns:
      the text parsed as a double, or NaN if it is not a valid double
    • getChildValueAsBoolean

      public static boolean getChildValueAsBoolean(Node parentNode, String childName)
      Reads a child element's text as a boolean
      Parameters:
      parentNode - node whose children are searched
      childName - element name to match
      Returns:
      true for true/on/yes/y/1 (any case), false for anything else including a missing text
      Throws:
      IllegalStateException - If no child carries that name
    • getValueAsBoolean

      public static boolean getValueAsBoolean(Node node)
      Reads a node's text as a boolean
      Parameters:
      node - element whose child supplies the text
      Returns:
      true for true/on/yes/y/1 (any case), false for anything else including a missing text
    • getRootElement

      public static Element getRootElement(InputSource inputSource) throws IOException
      Parses an RRD XML document and returns its root element
      Parameters:
      inputSource - source of the XML text, for example a file or a string reader
      Returns:
      the document element of the parsed document
      Throws:
      IOException - Thrown if the source cannot be read
      RuntimeException - Thrown if the source is not well-formed XML
    • getRootElement

      public static Element getRootElement(String xmlString) throws IOException
      Parses RRD XML held in a string and returns its root element
      Parameters:
      xmlString - the XML text
      Returns:
      the document element of the parsed document
      Throws:
      IOException - Thrown if the string cannot be read
      RuntimeException - Thrown if the string is not well-formed XML
    • getRootElement

      public static Element getRootElement(File xmlFile) throws IOException
      Parses an RRD XML file, read as UTF-8, and returns its root element
      Parameters:
      xmlFile - file holding the XML text
      Returns:
      the document element of the parsed document
      Throws:
      IOException - Thrown if the file cannot be opened or read
      RuntimeException - Thrown if the file is not well-formed XML