java.lang.Object
org.rrd4j.core.XmlTemplate
- Direct Known Subclasses:
RrdDefTemplate
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 Summary
FieldsModifier and TypeFieldDescriptionprivate static final PatternCompiled form of the placeholder pattern.private static final StringMatches a ${name} placeholder.protected Elementthe parsed root element of the templateNodes already checked against the template.Replacement value for each placeholder name.Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedXmlTemplate(File xmlFile) Parse an XML template from a file.protectedXmlTemplate(String xmlString) Parse an XML template from a string.protectedXmlTemplate(InputSource xmlSource) Parse an XML template from a source.Method Summary
Modifier and TypeMethodDescriptionprivate StringbyteToHex(int i) Format one channel of a colour as two lowercase hex digits.voidRemoves all placeholder-value mappings.protected static Node[]getChildNodes(Node parentNode) Find every child node of the given node.protected static Node[]getChildNodes(Node parentNode, String childName) Find the child nodes of the given name.protected StringgetChildValue(Node parentNode, String childName) Read a named child element's text with its placeholders resolved.protected StringgetChildValue(Node parentNode, String childName, boolean trim) Read a named child element's text with its placeholders resolved.protected booleangetChildValueAsBoolean(Node parentNode, String childName) Read a named child element's text as a boolean.protected doublegetChildValueAsDouble(Node parentNode, String childName) Read a named child element's text as a double.protected intgetChildValueAsInt(Node parentNode, String childName) Read a named child element's text as an int.protected longgetChildValueAsLong(Node parentNode, String childName) Read a named child element's text as a long.protected static NodegetFirstChildNode(Node parentNode, String childName) Find the first child node of the given name.protected StringRead a node's own text with its placeholders resolved.protected StringRead a node's own text with its placeholders resolved.protected booleangetValueAsBoolean(Node parentNode) Read a node's own text as a boolean.protected PaintgetValueAsColor(Node parentNode) Read a node's own text as a colour.protected doublegetValueAsDouble(Node parentNode) Read a node's own text as a double.protected intgetValueAsInt(Node parentNode) Read a node's own text as an int.protected longgetValueAsLong(Node parentNode) Read a node's own text as a long.String[]Returns the list of variables that should be set in this template.protected booleanhasChildNode(Node parentNode, String childName) Test whether a node has a child of the given name.booleanSearches the XML template to see if there are variables in there that will need to be set.protected booleanisEmptyNode(Node node) Test whether a node carries no renderable content.private StringresolveMappings(String templateValue) Substitute every ${name} placeholder in a value from the recorded mappings.voidsetVariable(String name, boolean value) Sets value for a single XML template variable.voidsetVariable(String name, double value) Sets value for a single XML template variable.voidsetVariable(String name, int value) Sets value for a single XML template variable.voidsetVariable(String name, long value) Sets value for a single XML template variable.voidsetVariable(String name, Color value) Sets value for a single XML template variable.voidsetVariable(String name, String value) Sets value for a single XML template variable.voidsetVariable(String name, Calendar value) Sets value for a single XML template variable.voidsetVariable(String name, Date value) Sets value for a single XML template variable.protected voidvalidateTagsOnlyOnce(Node parentNode, String[] allowedChildNames) Reject any child element not named in the allowed list, once per node.
Field Details
PATTERN_STRING
Matches a ${name} placeholder.- See Also:
PATTERN
Compiled form of the placeholder pattern.root
the parsed root element of the templatevalueMap
validatedNodes
Constructor Details
XmlTemplate
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
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
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
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}, specifystartfor thenameparameter.- Parameters:
name- the placeholder name, without the leading '${' or trailing '}'value- the text substituted for the placeholder
setVariable
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}, specifystartfor thenameparameter.- Parameters:
name- the placeholder name, without the leading '${' or trailing '}'value- the text substituted for the placeholder
setVariable
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}, specifystartfor thenameparameter.- Parameters:
name- the placeholder name, without the leading '${' or trailing '}'value- the text substituted for the placeholder
setVariable
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}, specifystartfor thenameparameter.- Parameters:
name- the placeholder name, without the leading '${' or trailing '}'value- the text substituted for the placeholder
setVariable
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}, specifystartfor thenameparameter.- Parameters:
name- the placeholder name, without the leading '${' or trailing '}'value- the text substituted for the placeholder
byteToHex
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
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}, specifystartfor thenameparameter.- Parameters:
name- the placeholder name, without the leading '${' or trailing '}'value- the text substituted for the placeholder
setVariable
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}, specifystartfor thenameparameter.- Parameters:
name- the placeholder name, without the leading '${' or trailing '}'value- the text substituted for the placeholder
setVariable
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}, specifystartfor thenameparameter.- 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
Returns the list of variables that should be set in this template.- Returns:
- Array of variable names as an array of strings.
getChildNodes
getChildNodes
getFirstChildNode
hasChildNode
getChildValue
getChildValue
Read a named child element's text with its placeholders resolved.- Parameters:
parentNode- the node whose child to readchildName- the element name to matchtrim- when true, strip leading and trailing whitespace from the text- Returns:
- the resolved text, or null if no such child exists
getValue
getValue
resolveMappings
getChildValueAsInt
getValueAsInt
Read a node's own text as an int.- Parameters:
parentNode- the node whose text to read- Returns:
- the parsed integer
getChildValueAsLong
getValueAsLong
Read a node's own text as a long.- Parameters:
parentNode- the node whose text to read- Returns:
- the parsed long
getChildValueAsDouble
getValueAsDouble
Read a node's own text as a double.- Parameters:
parentNode- the node whose text to read- Returns:
- the parsed double
getChildValueAsBoolean
getValueAsBoolean
Read a node's own text as a boolean.- Parameters:
parentNode- the node whose text to read- Returns:
- the parsed boolean
getValueAsColor
isEmptyNode
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
Reject any child element not named in the allowed list, once per node.- Parameters:
parentNode- the node whose children to checkallowedChildNames- the permitted element names; a name suffixed with "*" permits repeats, and the array is consumed as names are matched