1 25 package org.jrobin.graph; 26 27 import org.jrobin.core.XmlTemplate; 28 import org.jrobin.core.RrdException; 29 import org.jrobin.core.Util; 30 import org.xml.sax.InputSource ; 31 import org.w3c.dom.Node ; 32 33 import java.io.IOException ; 34 import java.io.File ; 35 import java.awt.*; 36 import java.util.GregorianCalendar ; 37 38 299 public class RrdGraphDefTemplate extends XmlTemplate { 300 301 private RrdGraphDef rrdGraphDef; 302 303 309 public RrdGraphDefTemplate(InputSource inputSource) throws IOException , RrdException { 310 super(inputSource); 311 } 312 313 319 public RrdGraphDefTemplate(File xmlFile) throws IOException , RrdException { 320 super(xmlFile); 321 } 322 323 329 public RrdGraphDefTemplate(String xmlString) throws IOException , RrdException { 330 super(xmlString); 331 } 332 333 342 public RrdGraphDef getRrdGraphDef() throws RrdException { 343 if(!root.getTagName().equals("rrd_graph_def")) { 345 throw new RrdException("XML definition must start with <rrd_graph_def>"); 346 } 347 validateTagsOnlyOnce(root, new String [] {"span", "options", "datasources", "graph"}); 348 rrdGraphDef = new RrdGraphDef(); 349 Node [] childs = getChildNodes(root); 351 for(int i = 0; i < childs.length; i++) { 352 String nodeName = childs[i].getNodeName(); 354 if(nodeName.equals("span")) { 355 resolveSpan(childs[i]); 356 } 357 else if(nodeName.equals("options")) { 359 resolveOptions(childs[i]); 360 } 361 else if(nodeName.equals("datasources")) { 363 resolveDatasources(childs[i]); 364 } 365 else if(nodeName.equals("graph")) { 367 resolveGraphElements(childs[i]); 368 } 369 } 370 return rrdGraphDef; 371 } 372 373 private void resolveGraphElements(Node graphNode) throws RrdException { 374 validateTagsOnlyOnce(graphNode, new String [] { 375 "area*", "line*", "stack*", "gprint*", "hrule*", "vrule*", "comment*", "time*" 376 }); 377 Node [] childs = getChildNodes(graphNode); 378 for(int i = 0; i < childs.length; i++) { 379 String nodeName = childs[i].getNodeName(); 380 if(nodeName.equals("area")) { 381 resolveArea(childs[i]); 382 } 383 else if(nodeName.equals("line")) { 384 resolveLine(childs[i]); 385 } 386 else if(nodeName.equals("stack")) { 387 validateTagsOnlyOnce(childs[i], new String [] { "datasource", "color", "legend" }); 388 String datasource = getChildValue(childs[i], "datasource"); 389 String colorStr = getChildValue(childs[i], "color"); 390 Color color = Color.decode(colorStr); 391 String legend = getChildValue(childs[i], "legend", false); 392 rrdGraphDef.stack(datasource, color, legend); 393 } 394 else if(nodeName.equals("comment")) { 395 String comment = getValue(childs[i], false); 396 rrdGraphDef.comment(comment); 397 } 398 else if(nodeName.equals("gprint")) { 399 validateTagsOnlyOnce(childs[i], new String [] { "datasource", "cf", "format", "base" }); 400 String datasource = getChildValue(childs[i], "datasource"); 401 String consolFun = getChildValue(childs[i], "cf"); 402 String format = getChildValue(childs[i], "format", false ); 403 404 if ( !hasChildNode(childs[i], "base") ) 405 rrdGraphDef.gprint( datasource, consolFun, format ); 406 else 407 rrdGraphDef.gprint( datasource, consolFun, format, getChildValueAsDouble(childs[i], "base") ); 408 } 409 else if(nodeName.equals("time")) { 410 validateTagsOnlyOnce(childs[i], new String [] { "format", "pattern", "value" }); 411 String format = getChildValue(childs[i], "format", false ); 412 String pattern = getChildValue(childs[i], "pattern"); 413 414 if ( Util.Xml.hasChildNode( childs[i], "value" ) ) 415 { 416 String timestamp = getChildValue(childs[i], "value"); 417 rrdGraphDef.time( format, pattern, Util.getGregorianCalendar(timestamp) ); 418 } 419 else 420 rrdGraphDef.time( format, pattern ); 421 } 422 else if(nodeName.equals("hrule")) { 423 validateTagsOnlyOnce(childs[i], new String [] { "value", "color", "legend", "width" }); 424 double value = getChildValueAsDouble(childs[i], "value"); 425 String colorStr = getChildValue(childs[i], "color"); 426 Color color = Color.decode(colorStr); 427 String legend = getChildValue(childs[i], "legend", false); 428 int width = 1; 429 try { 430 width = getChildValueAsInt(childs[i], "width"); 431 } catch(RrdException e) { } 432 rrdGraphDef.hrule(value, color, legend, width); 433 } 434 else if(nodeName.equals("vrule")) { 435 validateTagsOnlyOnce(childs[i], new String [] { "time", "color", "legend", "width" }); 436 String timeStr = getChildValue(childs[i], "time"); 437 GregorianCalendar gc = Util.getGregorianCalendar(timeStr); 438 String colorStr = getChildValue(childs[i], "color"); 439 Color color = Color.decode(colorStr); 440 String legend = getChildValue(childs[i], "legend", false); 441 int width = 1; 442 try { 443 width = getChildValueAsInt(childs[i], "width"); 444 } catch(RrdException e) { } 445 rrdGraphDef.vrule(gc, color, legend, width); 446 } 447 } 448 } 449 450 private void resolveLine(Node lineNode) throws RrdException { 451 if(hasChildNode(lineNode, "datasource")) { 452 validateTagsOnlyOnce(lineNode, new String [] { "datasource", "color", "legend", "width" }); 454 String datasource = getChildValue(lineNode, "datasource"); 455 String colorStr = getChildValue(lineNode, "color"); 456 Color color = Color.decode(colorStr); 457 String legend = getChildValue(lineNode, "legend", false); 458 int width = 1; 460 try { 461 width = getChildValueAsInt(lineNode, "width"); 462 } catch(RrdException e) { } 463 rrdGraphDef.line(datasource, color, legend, width); 464 } 465 else if(hasChildNode(lineNode, "time1")) { 466 validateTagsOnlyOnce(lineNode, new String [] { 468 "time1", "time2", "value1", "value2", "color", "legend", "width" 469 }); 470 String t1str = getChildValue(lineNode, "time1"); 471 GregorianCalendar gc1 = Util.getGregorianCalendar(t1str); 472 String t2str = getChildValue(lineNode, "time2"); 473 GregorianCalendar gc2 = Util.getGregorianCalendar(t2str); 474 double v1 = getChildValueAsDouble(lineNode, "value1"); 475 double v2 = getChildValueAsDouble(lineNode, "value2"); 476 String colorStr = getChildValue(lineNode, "color"); 477 Color color = Color.decode(colorStr); 478 String legend = getChildValue(lineNode, "legend", false); 479 int width = 1; 480 try { 481 width = getChildValueAsInt(lineNode, "width"); 482 } catch(RrdException e) { } 483 rrdGraphDef.line(gc1, v1, gc2, v2, color, legend, width); 484 } 485 else { 486 throw new RrdException("Unrecognized <line> format"); 487 } 488 } 489 490 private void resolveArea(Node areaNode) throws RrdException { 491 if(hasChildNode(areaNode, "datasource")) { 492 validateTagsOnlyOnce(areaNode, new String [] { "datasource", "color", "legend" }); 493 String datasource = getChildValue(areaNode, "datasource"); 495 String colorStr = getChildValue(areaNode, "color"); 496 Color color = Color.decode(colorStr); 497 String legend = getChildValue(areaNode, "legend", false); 498 rrdGraphDef.area(datasource, color, legend); 499 } 500 else if(hasChildNode(areaNode, "time1")) { 501 validateTagsOnlyOnce(areaNode, new String [] { 503 "time1", "time2", "value1", "value2", "color", "legend", "width" 504 }); 505 String t1str = getChildValue(areaNode, "time1"); 506 GregorianCalendar gc1 = Util.getGregorianCalendar(t1str); 507 String t2str = getChildValue(areaNode, "time2"); 508 GregorianCalendar gc2 = Util.getGregorianCalendar(t2str); 509 double v1 = getChildValueAsDouble(areaNode, "value1"); 510 double v2 = getChildValueAsDouble(areaNode, "value2"); 511 String colorStr = getChildValue(areaNode, "color"); 512 Color color = Color.decode(colorStr); 513 String legend = getChildValue(areaNode, "legend", false); 514 rrdGraphDef.area(gc1, v1, gc2, v2, color, legend); 515 } 516 else { 517 throw new RrdException("Unrecognized <area> format"); 518 } 519 } 520 521 private void resolveDatasources(Node datasourceNode) throws RrdException { 522 validateTagsOnlyOnce(datasourceNode, new String [] { "def*", "export_data*" }); 523 Node [] nodes = getChildNodes(datasourceNode, "def"); 524 for(int i = 0; i < nodes.length; i++) { 525 if(hasChildNode(nodes[i], "rrd")) 526 { 527 validateTagsOnlyOnce(nodes[i], new String [] {"name", "rrd", "source", "cf", "backend"}); 529 String name = getChildValue(nodes[i], "name"); 530 String rrd = getChildValue(nodes[i], "rrd"); 531 String dsName = getChildValue(nodes[i], "source"); 532 String consolFun = getChildValue(nodes[i], "cf"); 533 534 if ( Util.Xml.hasChildNode(nodes[i], "backend") ) 535 { 536 String backend = getChildValue( nodes[i], "backend" ); 537 rrdGraphDef.datasource( name, rrd, dsName, consolFun, backend ); 538 } 539 else 540 rrdGraphDef.datasource(name, rrd, dsName, consolFun); 541 } 542 else if(hasChildNode(nodes[i], "rpn")) { 543 validateTagsOnlyOnce(nodes[i], new String [] {"name", "rpn"}); 545 String name = getChildValue(nodes[i], "name"); 546 String rpn = getChildValue(nodes[i], "rpn"); 547 rrdGraphDef.datasource(name, rpn); 548 } 549 else if ( hasChildNode( nodes[i], "cf" ) || hasChildNode( nodes[i], "datasource" ) ) { 550 validateTagsOnlyOnce( nodes[i], new String [] {"name", "datasource", "cf"} ); 552 String name = getChildValue(nodes[i], "name"); 553 String ds = getChildValue(nodes[i], "datasource"); 554 String cf = getChildValue(nodes[i], "cf"); 555 rrdGraphDef.datasource( name, ds, cf ); 556 } 557 else { 558 throw new RrdException("Unrecognized <def> format"); 559 } 560 } 561 562 nodes = getChildNodes(datasourceNode, "export_data"); 563 for ( int i = 0; i < nodes.length; i++ ) 564 { 565 validateTagsOnlyOnce( nodes[i], new String [] {"file", "ds_name_prefix", "use_legend_names"} ); 566 String file = getChildValue( nodes[i], "file" ); 567 String prefix = "d"; 568 boolean use_legends = false; 569 570 if ( Util.Xml.hasChildNode( nodes[i], "ds_name_prefix" ) ) 571 prefix = getChildValue(nodes[i], "ds_name_prefix"); 572 573 if ( Util.Xml.hasChildNode( nodes[i], "use_legend_names" ) ) 574 use_legends = getChildValueAsBoolean(nodes[i], "use_legend_names"); 575 576 try 577 { 578 if ( !prefix.equals("d") ) 579 rrdGraphDef.addExportData( new ExportData( new File (file), prefix ) ); 580 else 581 rrdGraphDef.addExportData( new ExportData( new File (file), use_legends ) ); 582 } 583 catch ( IOException ioe ) { 584 throw new RrdException( ioe ); 585 } 586 } 587 } 588 589 private void resolveOptions(Node rootOptionNode) throws RrdException { 590 validateTagsOnlyOnce(rootOptionNode, new String [] { 591 "anti_aliasing", "arrow_color", "axis_color", "back_color", "background", 592 "base_value", "canvas", "left_padding", "default_font", "default_font_color", 593 "frame_color", "front_grid", "grid_range", "grid_x", "grid_y", "border", 594 "major_grid_color", "major_grid_x", "major_grid_y", 595 "minor_grid_color", "minor_grid_x", "minor_grid_y", 596 "overlay", "show_legend", "show_signature", "time_axis", "time_axis_label", 597 "title", "title_font", "title_font_color", "units_exponent", "value_axis", 598 "vertical_label", "strict_export", "resolution", "lower_limit" 599 }); 600 Node [] optionNodes = getChildNodes(rootOptionNode); 601 for(int i = 0; i < optionNodes.length; i++) { 602 String option = optionNodes[i].getNodeName(); 603 Node optionNode = optionNodes[i]; 604 if(option.equals("anti_aliasing")) { 606 boolean antiAliasing = getValueAsBoolean(optionNode); 607 rrdGraphDef.setAntiAliasing(antiAliasing); 608 } 609 else if(option.equals("arrow_color")) { 611 String colorStr = getValue(optionNode); 612 rrdGraphDef.setArrowColor(Color.decode(colorStr)); 613 } 614 else if(option.equals("axis_color")) { 616 String colorStr = getValue(optionNode); 617 rrdGraphDef.setAxisColor(Color.decode(colorStr)); 618 } 619 else if(option.equals("back_color")) { 621 String colorStr = getValue(optionNode); 622 rrdGraphDef.setBackColor(Color.decode(colorStr)); 623 } 624 else if(option.equals("background")) { 626 String backgroundFile = getValue(optionNode); 627 rrdGraphDef.setBackground(backgroundFile); 628 } 629 else if(option.equals("base_value")) { 631 double baseValue = getValueAsDouble(optionNode); 632 rrdGraphDef.setBaseValue(baseValue); 633 } 634 else if(option.equals("canvas")) { 636 String colorStr = getValue(optionNode); 637 rrdGraphDef.setCanvasColor(Color.decode(colorStr)); 638 } 639 else if(option.equals("left_padding")) { 641 int padding = getValueAsInt(optionNode); 642 rrdGraphDef.setChartLeftPadding(padding); 643 } 644 else if(option.equals("default_font")) { 646 Font f = resolveFont(optionNode); 647 rrdGraphDef.setDefaultFont(f); 648 } 649 else if(option.equals("default_font_color")) { 651 String colorStr = getValue(optionNode); 652 rrdGraphDef.setDefaultFontColor(Color.decode(colorStr)); 653 } 654 else if(option.equals("frame_color")) { 656 String colorStr = getValue(optionNode); 657 rrdGraphDef.setFrameColor(Color.decode(colorStr)); 658 } 659 else if(option.equals("front_grid")) { 661 boolean frontGrid = getValueAsBoolean(optionNode); 662 rrdGraphDef.setFrontGrid(frontGrid); 663 } 664 else if(option.equals("grid_range")) { 666 validateTagsOnlyOnce(optionNode, new String [] { "lower", "upper", "rigid" }); 667 double lower = getChildValueAsDouble(optionNode, "lower"); 668 double upper = getChildValueAsDouble(optionNode, "upper"); 669 boolean rigid = getChildValueAsBoolean(optionNode, "rigid"); 670 rrdGraphDef.setGridRange(lower, upper, rigid); 671 } 672 else if(option.equals("lower_limit")) { 674 double lower = getValueAsDouble(optionNode); 675 rrdGraphDef.setLowerLimit( lower ); 676 } 677 else if(option.equals("grid_x")) { 679 boolean gx = getValueAsBoolean(optionNode); 680 rrdGraphDef.setGridX(gx); 681 } 682 else if(option.equals("grid_y")) { 684 boolean gy = getValueAsBoolean(optionNode); 685 rrdGraphDef.setGridY(gy); 686 } 687 else if(option.equals("border")) { 689 validateTagsOnlyOnce(optionNode, new String [] {"color", "width"}); 690 String colorStr = getChildValue(optionNode, "color"); 691 int width = getChildValueAsInt(optionNode, "width"); 692 rrdGraphDef.setImageBorder(Color.decode(colorStr), width); 693 } 694 else if(option.equals("major_grid_color")) { 696 String colorStr = getValue(optionNode); 697 rrdGraphDef.setMajorGridColor(Color.decode(colorStr)); 698 } 699 else if(option.equals("major_grid_x")) { 701 boolean gx = getValueAsBoolean(optionNode); 702 rrdGraphDef.setMajorGridX(gx); 703 } 704 else if(option.equals("major_grid_y")) { 706 boolean gy = getValueAsBoolean(optionNode); 707 rrdGraphDef.setMajorGridY(gy); 708 } 709 else if(option.equals("minor_grid_color")) { 711 String colorStr = getValue(optionNode); 712 rrdGraphDef.setMinorGridColor(Color.decode(colorStr)); 713 } 714 else if(option.equals("minor_grid_x")) { 716 boolean gx = getValueAsBoolean(optionNode); 717 rrdGraphDef.setMinorGridX(gx); 718 } 719 else if(option.equals("minor_grid_y")) { 721 boolean gy = getValueAsBoolean(optionNode); 722 rrdGraphDef.setMinorGridY(gy); 723 } 724 else if(option.equals("overlay")) { 726 String overlay = getValue(optionNode); 727 rrdGraphDef.setOverlay(overlay); 728 } 729 else if(option.equals("show_legend")) { 731 boolean show = getValueAsBoolean(optionNode); 732 rrdGraphDef.setShowLegend(show); 733 } 734 else if(option.equals("show_signature")) { 736 boolean show = getValueAsBoolean(optionNode); 737 rrdGraphDef.setShowSignature(show); 738 } 739 else if(option.equals("time_axis")) { 741 validateTagsOnlyOnce(optionNode, new String [] { 742 "min_grid_time_unit", "min_grid_unit_steps", "maj_grid_time_unit", 743 "maj_grid_unit_steps", "date_format", "center_labels", "first_day_of_week" 744 }); 745 746 if ( hasChildNode( optionNode, "min_grid_time_unit" ) ) 747 { 748 int unit1 = resolveUnit(getChildValue(optionNode, "min_grid_time_unit")); 749 int step1 = getChildValueAsInt(optionNode, "min_grid_unit_steps"); 750 int unit2 = resolveUnit(getChildValue(optionNode, "maj_grid_time_unit")); 751 int step2 = getChildValueAsInt(optionNode, "maj_grid_unit_steps"); 752 String format = getChildValue(optionNode, "date_format"); 753 boolean center = getChildValueAsBoolean(optionNode, "center_labels"); 754 rrdGraphDef.setTimeAxis(unit1, step1, unit2, step2, format, center); 755 } 756 757 if ( hasChildNode( optionNode, "first_day_of_week" ) ) 759 { 760 int dow = resolveDayUnit( getChildValue(optionNode, "first_day_of_week") ); 761 rrdGraphDef.setFirstDayOfWeek( dow ); 762 } 763 } 764 else if(option.equals("time_axis_label")) { 766 String label = getValue(optionNode); 767 rrdGraphDef.setTimeAxisLabel(label); 768 } 769 else if(option.equals("title")) { 771 String title = getValue(optionNode); 772 rrdGraphDef.setTitle(title); 773 } 774 else if(option.equals("title_font")) { 776 Font f = resolveFont(optionNode); 777 rrdGraphDef.setTitleFont(f); 778 } 779 else if(option.equals("title_font_color")) { 781 String colorStr = getValue(optionNode); 782 rrdGraphDef.setTitleFontColor(Color.decode(colorStr)); 783 } 784 else if(option.equals("units_exponent")) { 786 int exp = getValueAsInt(optionNode); 787 rrdGraphDef.setUnitsExponent(exp); 788 } 789 else if(option.equals("value_axis")) { 791 validateTagsOnlyOnce(optionNode, new String [] {"grid_step", "label_step"}); 792 double gridStep = getChildValueAsDouble(optionNode, "grid_step"); 793 double labelStep = getChildValueAsDouble(optionNode, "label_step"); 794 rrdGraphDef.setValueAxis(gridStep, labelStep); 795 } 796 else if(option.equals("vertical_label")) { 798 String label = getValue(optionNode); 799 rrdGraphDef.setVerticalLabel(label); 800 } 801 else if(option.equals("strict_export")) { 803 rrdGraphDef.setStrictExport( getValueAsBoolean(optionNode) ); 804 } 805 else if(option.equals("resolution")) { 807 rrdGraphDef.setResolution( getValueAsInt(optionNode) ); 808 } 809 } 810 } 811 812 private int resolveUnit(String unit) { 813 if(unit.equalsIgnoreCase("second")) { 814 return TimeAxisUnit.SECOND; 815 } 816 else if(unit.equalsIgnoreCase("minute")) { 817 return TimeAxisUnit.MINUTE; 818 } 819 else if(unit.equalsIgnoreCase("hour")) { 820 return TimeAxisUnit.HOUR; 821 } 822 else if(unit.equalsIgnoreCase("day")) { 823 return TimeAxisUnit.DAY; 824 } 825 else if(unit.equalsIgnoreCase("week")) { 826 return TimeAxisUnit.WEEK; 827 } 828 else if(unit.equalsIgnoreCase("month")) { 829 return TimeAxisUnit.MONTH; 830 } 831 else if(unit.equalsIgnoreCase("year")) { 832 return TimeAxisUnit.YEAR; 833 } 834 else { 835 throw new IllegalArgumentException ("Invalid unit specified: " + unit); 836 } 837 } 838 839 private int resolveDayUnit( String unit ) { 840 if ( unit.equalsIgnoreCase("monday") ) { 841 return TimeAxisUnit.MONDAY; 842 } 843 else if ( unit.equalsIgnoreCase("tuesday") ) { 844 return TimeAxisUnit.TUESDAY; 845 } 846 else if ( unit.equalsIgnoreCase("wednesday") ) { 847 return TimeAxisUnit.WEDNESDAY; 848 } 849 else if ( unit.equalsIgnoreCase("thursday") ) { 850 return TimeAxisUnit.THURSDAY; 851 } 852 else if ( unit.equalsIgnoreCase("friday") ) { 853 return TimeAxisUnit.FRIDAY; 854 } 855 else if ( unit.equalsIgnoreCase("saturday") ) { 856 return TimeAxisUnit.SATURDAY; 857 } 858 else if ( unit.equalsIgnoreCase("sunday") ) { 859 return TimeAxisUnit.SUNDAY; 860 } 861 else { 862 throw new IllegalArgumentException ( "Invalid day unit specified: " + unit ); 863 } 864 865 } 866 867 private void resolveSpan(Node spanNode) throws RrdException { 868 validateTagsOnlyOnce(spanNode, new String [] {"start", "end"}); 869 String startStr = getChildValue(spanNode, "start"); 870 String endStr = getChildValue(spanNode, "end"); 871 GregorianCalendar gc1 = Util.getGregorianCalendar(startStr); 872 GregorianCalendar gc2 = Util.getGregorianCalendar(endStr); 873 rrdGraphDef.setTimePeriod(gc1, gc2); 874 } 875 876 private Font resolveFont(Node fontNode) throws RrdException { 877 validateTagsOnlyOnce(fontNode, new String [] {"name", "style", "size"}); 878 String name = getChildValue(fontNode, "name"); 879 String style = getChildValue(fontNode, "style"); 880 int size = getChildValueAsInt(fontNode, "size"); 881 int stl = Font.PLAIN; 882 if(style.equalsIgnoreCase("BOLD")) { 883 stl = Font.BOLD; 884 } 885 else if(style.equalsIgnoreCase("ITALIC")) { 886 stl = Font.ITALIC; 887 } 888 else if(style.equalsIgnoreCase("BOLDITALIC") || 889 style.equalsIgnoreCase("ITALICBOLD") || 890 style.equalsIgnoreCase("BOLD ITALIC") || 891 style.equalsIgnoreCase("ITALIC BOLD")) { 892 stl = Font.ITALIC + Font.BOLD; 893 } 894 return new Font(name, stl, size); 895 } 896 897 906 } 907 | Popular Tags |