java.lang.Object
org.rrd4j.core.XmlTemplate
Direct Known Subclasses:
RrdDefTemplate

public abstract class XmlTemplateextends Object
Class used as a base class for various XML template related classes. Class provides methods for XML source parsing and XML tree traversing. XML source may have unlimited number of placeholders (variables) in the format ${variable_name}. Methods are provided to specify variable values at runtime. Note that this class has limited functionality: XML source gets parsed, and variable values are collected. You have to extend this class to do something more useful.
  • Field Details

    • PATTERN_STRING

      private static final String PATTERN_STRING
      Matches a ${name} placeholder.
      See Also:
    • PATTERN

      private static final Pattern PATTERN
      Compiled form of the placeholder pattern.
    • root

      protected Element root
      the parsed root element of the template
    • valueMap

      private final HashMap<String,Object> valueMap
      Replacement value for each placeholder name.
    • validatedNodes

      private final HashSet<Node> validatedNodes
      Nodes already checked against the template.
  • Constructor Details

    • XmlTemplate

      protected XmlTemplate(InputSource xmlSource) throws IOException
      Parse an XML template from a source.
      Parameters:
      xmlSource - the source to read the template from
      Throws:
      IOException - if the source cannot be read or is not well-formed XML
    • XmlTemplate

      protected XmlTemplate(String xmlString) throws IOException
      Parse an XML template from a string.
      Parameters:
      xmlString - the template text to parse
      Throws:
      IOException - if the text is not well-formed XML
    • XmlTemplate

      protected XmlTemplate(File xmlFile) throws IOException
      Parse an XML template from a file.
      Parameters:
      xmlFile - the file to read the template from
      Throws:
      IOException - if the file cannot be read or does not hold well-formed XML
  • Method Details

    • clearValues

      public void clearValues()
      Removes all placeholder-value mappings.
    • setVariable

      public void setVariable(String name, String value)
      Sets value for a single XML template variable. Variable name should be specified without leading '${' and ending '}' placeholder markers. For example, for a placeholder ${start}, specify start for the name parameter.
      Parameters:
      name - the placeholder name, without the leading '${' or trailing '}'
      value - the text substituted for the placeholder
    • setVariable

      public void setVariable(String name, int value)
      Sets value for a single XML template variable. Variable name should be specified without leading '${' and ending '}' placeholder markers. For example, for a placeholder ${start}, specify start for the name parameter.
      Parameters:
      name - the placeholder name, without the leading '${' or trailing '}'
      value - the text substituted for the placeholder
    • setVariable

      public void setVariable(String name, long value)
      Sets value for a single XML template variable. Variable name should be specified without leading '${' and ending '}' placeholder markers. For example, for a placeholder ${start}, specify start for the name parameter.
      Parameters:
      name - the placeholder name, without the leading '${' or trailing '}'
      value - the text substituted for the placeholder
    • setVariable

      public void setVariable(String name, double value)
      Sets value for a single XML template variable. Variable name should be specified without leading '${' and ending '}' placeholder markers. For example, for a placeholder ${start}, specify start for the name parameter.
      Parameters:
      name - the placeholder name, without the leading '${' or trailing '}'
      value - the text substituted for the placeholder
    • setVariable

      public void setVariable(String name, Color value)
      Sets value for a single XML template variable. Variable name should be specified without leading '${' and ending '}' placeholder markers. For example, for a placeholder ${start}, specify start for the name parameter.
      Parameters:
      name - the placeholder name, without the leading '${' or trailing '}'
      value - the text substituted for the placeholder
    • byteToHex

      private String byteToHex(int i)
      Format one channel of a colour as two lowercase hex digits.
      Parameters:
      i - the colour channel value, 0 to 255
      Returns:
      the two-digit hex form of a colour channel, 0 to 255
    • setVariable

      public void setVariable(String name, Date value)
      Sets value for a single XML template variable. Variable name should be specified without leading '${' and ending '}' placeholder markers. For example, for a placeholder ${start}, specify start for the name parameter.
      Parameters:
      name - the placeholder name, without the leading '${' or trailing '}'
      value - the text substituted for the placeholder
    • setVariable

      public void setVariable(String name, Calendar value)
      Sets value for a single XML template variable. Variable name should be specified without leading '${' and ending '}' placeholder markers. For example, for a placeholder ${start}, specify start for the name parameter.
      Parameters:
      name - the placeholder name, without the leading '${' or trailing '}'
      value - the text substituted for the placeholder
    • setVariable

      public void setVariable(String name, boolean value)
      Sets value for a single XML template variable. Variable name should be specified without leading '${' and ending '}' placeholder markers. For example, for a placeholder ${start}, specify start for the name parameter.
      Parameters:
      name - the placeholder name, without the leading '${' or trailing '}'
      value - the text substituted for the placeholder
    • hasVariables

      public boolean hasVariables()
      Searches the XML template to see if there are variables in there that will need to be set.
      Returns:
      True if variables were detected, false if not.
    • getVariables

      public String[] getVariables()
      Returns the list of variables that should be set in this template.
      Returns:
      Array of variable names as an array of strings.
    • getChildNodes

      protected static Node[] getChildNodes(Node parentNode, String childName)
      Find the child nodes of the given name.
      Parameters:
      parentNode - the node whose children to search
      childName - the element name to match
      Returns:
      an array of the matching child nodes, empty if there are none
    • getChildNodes

      protected static Node[] getChildNodes(Node parentNode)
      Find every child node of the given node.
      Parameters:
      parentNode - the node whose children to return
      Returns:
      an array of all child nodes, empty if there are none
    • getFirstChildNode

      protected static Node getFirstChildNode(Node parentNode, String childName)
      Find the first child node of the given name.
      Parameters:
      parentNode - the node whose children to search
      childName - the element name to match
      Returns:
      the first matching child node, or null if there is none
    • hasChildNode

      protected boolean hasChildNode(Node parentNode, String childName)
      Test whether a node has a child of the given name.
      Parameters:
      parentNode - the node whose children to search
      childName - the element name to match
      Returns:
      true if a matching child exists, false otherwise
    • getChildValue

      protected String getChildValue(Node parentNode, String childName)
      Read a named child element's text with its placeholders resolved.
      Parameters:
      parentNode - the node whose child to read
      childName - the element name to match
      Returns:
      the resolved text, or null if no such child exists
    • getChildValue

      protected String getChildValue(Node parentNode, String childName, boolean trim)
      Read a named child element's text with its placeholders resolved.
      Parameters:
      parentNode - the node whose child to read
      childName - the element name to match
      trim - when true, strip leading and trailing whitespace from the text
      Returns:
      the resolved text, or null if no such child exists
    • getValue

      protected String getValue(Node parentNode)
      Read a node's own text with its placeholders resolved.
      Parameters:
      parentNode - the node whose text to read
      Returns:
      the resolved text, or null if the node has none
    • getValue

      protected String getValue(Node parentNode, boolean trim)
      Read a node's own text with its placeholders resolved.
      Parameters:
      parentNode - the node whose text to read
      trim - when true, strip leading and trailing whitespace from the text
      Returns:
      the resolved text, or null if the node has none
    • resolveMappings

      private String resolveMappings(String templateValue)
      Substitute every ${name} placeholder in a value from the recorded mappings.
      Parameters:
      templateValue - the raw text to resolve, or null
      Returns:
      the resolved text, or null if templateValue was null
    • getChildValueAsInt

      protected int getChildValueAsInt(Node parentNode, String childName)
      Read a named child element's text as an int.
      Parameters:
      parentNode - the node whose child to read
      childName - the element name to match
      Returns:
      the parsed integer
    • getValueAsInt

      protected int getValueAsInt(Node parentNode)
      Read a node's own text as an int.
      Parameters:
      parentNode - the node whose text to read
      Returns:
      the parsed integer
    • getChildValueAsLong

      protected long getChildValueAsLong(Node parentNode, String childName)
      Read a named child element's text as a long.
      Parameters:
      parentNode - the node whose child to read
      childName - the element name to match
      Returns:
      the parsed long
    • getValueAsLong

      protected long getValueAsLong(Node parentNode)
      Read a node's own text as a long.
      Parameters:
      parentNode - the node whose text to read
      Returns:
      the parsed long
    • getChildValueAsDouble

      protected double getChildValueAsDouble(Node parentNode, String childName)
      Read a named child element's text as a double.
      Parameters:
      parentNode - the node whose child to read
      childName - the element name to match
      Returns:
      the parsed double
    • getValueAsDouble

      protected double getValueAsDouble(Node parentNode)
      Read a node's own text as a double.
      Parameters:
      parentNode - the node whose text to read
      Returns:
      the parsed double
    • getChildValueAsBoolean

      protected boolean getChildValueAsBoolean(Node parentNode, String childName)
      Read a named child element's text as a boolean.
      Parameters:
      parentNode - the node whose child to read
      childName - the element name to match
      Returns:
      the parsed boolean
    • getValueAsBoolean

      protected boolean getValueAsBoolean(Node parentNode)
      Read a node's own text as a boolean.
      Parameters:
      parentNode - the node whose text to read
      Returns:
      the parsed boolean
    • getValueAsColor

      protected Paint getValueAsColor(Node parentNode)
      Read a node's own text as a colour.
      Parameters:
      parentNode - the node whose text to read
      Returns:
      the parsed colour, or null if the text does not name one
    • isEmptyNode

      protected boolean isEmptyNode(Node node)
      Test whether a node carries no renderable content.
      Parameters:
      node - the node to test
      Returns:
      true if the node is a comment or is whitespace-only text, false otherwise
    • validateTagsOnlyOnce

      protected void validateTagsOnlyOnce(Node parentNode, String[] allowedChildNames)
      Reject any child element not named in the allowed list, once per node.
      Parameters:
      parentNode - the node whose children to check
      allowedChildNames - the permitted element names; a name suffixed with "*" permits repeats, and the array is consumed as names are matched