📄 rrdgraphdeftemplate.java
字号:
/**
* Creates template object from any parsable XML source
* @param inputSource XML source
* @throws IOException thrown in case of I/O error
* @throws RrdException usually thrown in case of XML related error
*/
public RrdGraphDefTemplate(InputSource inputSource) throws IOException, RrdException {
super(inputSource);
}
/**
* Creates template object from the file containing XML template code
* @param xmlFile file containing XML template
* @throws IOException thrown in case of I/O error
* @throws RrdException usually thrown in case of XML related error
*/
public RrdGraphDefTemplate(File xmlFile) throws IOException, RrdException {
super(xmlFile);
}
/**
* Creates template object from the string containing XML template code
* @param xmlString string containing XML template
* @throws IOException thrown in case of I/O error
* @throws RrdException usually thrown in case of XML related error
*/
public RrdGraphDefTemplate(String xmlString) throws IOException, RrdException {
super(xmlString);
}
/**
* Creates RrdGraphDef object which can be used to create RrdGraph
* object (actual JRobin graphs). Before this method is called, all template variables (if any)
* must be resolved (replaced with real values).
* See {@link XmlTemplate#setVariable(String, String) setVariable()} method information to
* understand how to supply values for template variables.
* @return Graph definition which can be used to create RrdGraph object (actual JRobin graphs)
* @throws RrdException Thrown if parsed XML template contains invalid (unrecognized) tags
*/
public RrdGraphDef getRrdGraphDef() throws RrdException {
// basic check
if(!root.getTagName().equals("rrd_graph_def")) {
throw new RrdException("XML definition must start with <rrd_graph_def>");
}
validateTagsOnlyOnce(root, new String[] {"span", "options", "datasources", "graph"});
rrdGraphDef = new RrdGraphDef();
// traverse all nodes
Node[] childs = getChildNodes(root);
for(int i = 0; i < childs.length; i++) {
// SPAN
String nodeName = childs[i].getNodeName();
if(nodeName.equals("span")) {
resolveSpan(childs[i]);
}
// OPTIONS
else if(nodeName.equals("options")) {
resolveOptions(childs[i]);
}
// DATASOURCES
else if(nodeName.equals("datasources")) {
resolveDatasources(childs[i]);
}
// GRAPH ELEMENTS
else if(nodeName.equals("graph")) {
resolveGraphElements(childs[i]);
}
}
return rrdGraphDef;
}
private void resolveGraphElements(Node graphNode) throws RrdException {
validateTagsOnlyOnce(graphNode, new String[] {
"area*", "line*", "stack*", "gprint*", "hrule*", "vrule*", "comment*", "time*"
});
Node[] childs = getChildNodes(graphNode);
for(int i = 0; i < childs.length; i++) {
String nodeName = childs[i].getNodeName();
if(nodeName.equals("area")) {
resolveArea(childs[i]);
}
else if(nodeName.equals("line")) {
resolveLine(childs[i]);
}
else if(nodeName.equals("stack")) {
validateTagsOnlyOnce(childs[i], new String[] { "datasource", "color", "legend" });
String datasource = getChildValue(childs[i], "datasource");
String colorStr = getChildValue(childs[i], "color");
Color color = Color.decode(colorStr);
String legend = getChildValue(childs[i], "legend", false);
rrdGraphDef.stack(datasource, color, legend);
}
else if(nodeName.equals("comment")) {
String comment = getValue(childs[i], false);
rrdGraphDef.comment(comment);
}
else if(nodeName.equals("gprint")) {
validateTagsOnlyOnce(childs[i], new String[] { "datasource", "cf", "format", "base" });
String datasource = getChildValue(childs[i], "datasource");
String consolFun = getChildValue(childs[i], "cf");
String format = getChildValue(childs[i], "format", false );
if ( !hasChildNode(childs[i], "base") )
rrdGraphDef.gprint( datasource, consolFun, format );
else
rrdGraphDef.gprint( datasource, consolFun, format, getChildValueAsDouble(childs[i], "base") );
}
else if(nodeName.equals("time")) {
validateTagsOnlyOnce(childs[i], new String[] { "format", "pattern", "value" });
String format = getChildValue(childs[i], "format", false );
String pattern = getChildValue(childs[i], "pattern");
if ( Util.Xml.hasChildNode( childs[i], "value" ) )
{
String timestamp = getChildValue(childs[i], "value");
rrdGraphDef.time( format, pattern, Util.getGregorianCalendar(timestamp) );
}
else
rrdGraphDef.time( format, pattern );
}
else if(nodeName.equals("hrule")) {
validateTagsOnlyOnce(childs[i], new String[] { "value", "color", "legend", "width" });
double value = getChildValueAsDouble(childs[i], "value");
String colorStr = getChildValue(childs[i], "color");
Color color = Color.decode(colorStr);
String legend = getChildValue(childs[i], "legend", false);
int width = 1;
try {
width = getChildValueAsInt(childs[i], "width");
} catch(RrdException e) { }
rrdGraphDef.hrule(value, color, legend, width);
}
else if(nodeName.equals("vrule")) {
validateTagsOnlyOnce(childs[i], new String[] { "time", "color", "legend", "width" });
String timeStr = getChildValue(childs[i], "time");
GregorianCalendar gc = Util.getGregorianCalendar(timeStr);
String colorStr = getChildValue(childs[i], "color");
Color color = Color.decode(colorStr);
String legend = getChildValue(childs[i], "legend", false);
int width = 1;
try {
width = getChildValueAsInt(childs[i], "width");
} catch(RrdException e) { }
rrdGraphDef.vrule(gc, color, legend, width);
}
}
}
private void resolveLine(Node lineNode) throws RrdException {
if(hasChildNode(lineNode, "datasource")) {
// ordinary line definition
validateTagsOnlyOnce(lineNode, new String[] { "datasource", "color", "legend", "width" });
String datasource = getChildValue(lineNode, "datasource");
String colorStr = getChildValue(lineNode, "color");
Color color = Color.decode(colorStr);
String legend = getChildValue(lineNode, "legend", false);
// line width is not mandatory
int width = 1;
try {
width = getChildValueAsInt(lineNode, "width");
} catch(RrdException e) { }
rrdGraphDef.line(datasource, color, legend, width);
}
else if(hasChildNode(lineNode, "time1")) {
// two point definition
validateTagsOnlyOnce(lineNode, new String[] {
"time1", "time2", "value1", "value2", "color", "legend", "width"
});
String t1str = getChildValue(lineNode, "time1");
GregorianCalendar gc1 = Util.getGregorianCalendar(t1str);
String t2str = getChildValue(lineNode, "time2");
GregorianCalendar gc2 = Util.getGregorianCalendar(t2str);
double v1 = getChildValueAsDouble(lineNode, "value1");
double v2 = getChildValueAsDouble(lineNode, "value2");
String colorStr = getChildValue(lineNode, "color");
Color color = Color.decode(colorStr);
String legend = getChildValue(lineNode, "legend", false);
int width = 1;
try {
width = getChildValueAsInt(lineNode, "width");
} catch(RrdException e) { }
rrdGraphDef.line(gc1, v1, gc2, v2, color, legend, width);
}
else {
throw new RrdException("Unrecognized <line> format");
}
}
private void resolveArea(Node areaNode) throws RrdException {
if(hasChildNode(areaNode, "datasource")) {
validateTagsOnlyOnce(areaNode, new String[] { "datasource", "color", "legend" });
// ordinary area definition
String datasource = getChildValue(areaNode, "datasource");
String colorStr = getChildValue(areaNode, "color");
Color color = Color.decode(colorStr);
String legend = getChildValue(areaNode, "legend", false);
rrdGraphDef.area(datasource, color, legend);
}
else if(hasChildNode(areaNode, "time1")) {
// two point definition
validateTagsOnlyOnce(areaNode, new String[] {
"time1", "time2", "value1", "value2", "color", "legend", "width"
});
String t1str = getChildValue(areaNode, "time1");
GregorianCalendar gc1 = Util.getGregorianCalendar(t1str);
String t2str = getChildValue(areaNode, "time2");
GregorianCalendar gc2 = Util.getGregorianCalendar(t2str);
double v1 = getChildValueAsDouble(areaNode, "value1");
double v2 = getChildValueAsDouble(areaNode, "value2");
String colorStr = getChildValue(areaNode, "color");
Color color = Color.decode(colorStr);
String legend = getChildValue(areaNode, "legend", false);
rrdGraphDef.area(gc1, v1, gc2, v2, color, legend);
}
else {
throw new RrdException("Unrecognized <area> format");
}
}
private void resolveDatasources(Node datasourceNode) throws RrdException {
validateTagsOnlyOnce(datasourceNode, new String[] { "def*", "export_data*" });
Node[] nodes = getChildNodes(datasourceNode, "def");
for(int i = 0; i < nodes.length; i++) {
if(hasChildNode(nodes[i], "rrd"))
{
// RRD datasource
validateTagsOnlyOnce(nodes[i], new String[] {"name", "rrd", "source", "cf", "backend"});
String name = getChildValue(nodes[i], "name");
String rrd = getChildValue(nodes[i], "rrd");
String dsName = getChildValue(nodes[i], "source");
String consolFun = getChildValue(nodes[i], "cf");
if ( Util.Xml.hasChildNode(nodes[i], "backend") )
{
String backend = getChildValue( nodes[i], "backend" );
rrdGraphDef.datasource( name, rrd, dsName, consolFun, backend );
}
else
rrdGraphDef.datasource(name, rrd, dsName, consolFun);
}
else if(hasChildNode(nodes[i], "rpn")) {
// RPN datasource
validateTagsOnlyOnce(nodes[i], new String[] {"name", "rpn"});
String name = getChildValue(nodes[i], "name");
String rpn = getChildValue(nodes[i], "rpn");
rrdGraphDef.datasource(name, rpn);
}
else if ( hasChildNode( nodes[i], "cf" ) || hasChildNode( nodes[i], "datasource" ) ) {
// STATIC AGGREGATED DATASOURCE
validateTagsOnlyOnce( nodes[i], new String[] {"name", "datasource", "cf"} );
String name = getChildValue(nodes[i], "name");
String ds = getChildValue(nodes[i], "datasource");
String cf = getChildValue(nodes[i], "cf");
rrdGraphDef.datasource( name, ds, cf );
}
else {
throw new RrdException("Unrecognized <def> format");
}
}
nodes = getChildNodes(datasourceNode, "export_data");
for ( int i = 0; i < nodes.length; i++ )
{
validateTagsOnlyOnce( nodes[i], new String[] {"file", "ds_name_prefix", "use_legend_names"} );
String file = getChildValue( nodes[i], "file" );
String prefix = "d";
boolean use_legends = false;
if ( Util.Xml.hasChildNode( nodes[i], "ds_name_prefix" ) )
prefix = getChildValue(nodes[i], "ds_name_prefix");
if ( Util.Xml.hasChildNode( nodes[i], "use_legend_names" ) )
use_legends = getChildValueAsBoolean(nodes[i], "use_legend_names");
try
{
if ( !prefix.equals("d") )
rrdGraphDef.addExportData( new ExportData( new File(file), prefix ) );
else
rrdGraphDef.addExportData( new ExportData( new File(file), use_legends ) );
}
catch ( IOException ioe ) {
throw new RrdException( ioe );
}
}
}
private void resolveOptions(Node rootOptionNode) throws RrdException {
validateTagsOnlyOnce(rootOptionNode, new String[] {
"anti_aliasing", "arrow_color", "axis_color", "back_color", "background",
"base_value", "canvas", "left_padding", "default_font", "default_font_color",
"frame_color", "front_grid", "grid_range", "grid_x", "grid_y", "border",
"major_grid_color", "major_grid_x", "major_grid_y",
"minor_grid_color", "minor_grid_x", "minor_grid_y",
"overlay", "show_legend", "show_signature", "time_axis", "time_axis_label",
"title", "title_font", "title_font_color", "units_exponent", "value_axis",
"vertical_label", "strict_export", "resolution", "lower_limit"
});
Node[] optionNodes = getChildNodes(rootOptionNode);
for(int i = 0; i < optionNodes.length; i++) {
String option = optionNodes[i].getNodeName();
Node optionNode = optionNodes[i];
// ANTI ALIASING
if(option.equals("anti_aliasing")) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -