1 25 package org.jrobin.demo.graph; 26 27 import org.jrobin.graph.RrdGraphDefTemplate; 28 import org.jrobin.graph.RrdGraph; 29 import org.jrobin.core.RrdException; 30 31 import java.io.File ; 32 import java.io.IOException ; 33 import java.io.BufferedReader ; 34 import java.io.InputStreamReader ; 35 36 42 public class GraphTemplate 43 { 44 private static String format = "gif"; 45 private static int width = 0; private static int height = 0; private static float quality = 1.0f; 49 private static String templateFile, imageName; 50 51 private static void die( String msg ) 52 { 53 System.err.println( msg ); 54 System.exit( -1 ); 55 } 56 57 private static void parseArguments( String [] args ) 58 { 59 int rpos = args.length - 1; 60 61 imageName = args[rpos--]; 63 templateFile = args[rpos]; 64 65 if ( rpos % 2 > 0 ) 67 die( "Invalid number of arguments." ); 68 69 for ( int i = 0; i < rpos; i += 2 ) 70 { 71 String arg = args[i]; 72 String val = args[i + 1]; 73 74 try 75 { 76 if ( arg.equalsIgnoreCase("-img") ) 77 format = val; 78 else if ( arg.equalsIgnoreCase("-w") ) 79 width = Integer.parseInt(val); 80 else if ( arg.equalsIgnoreCase("-h") ) 81 height = Integer.parseInt(val); 82 else if ( arg.equalsIgnoreCase("-q") ) 83 quality = Float.parseFloat(val); 84 } 85 catch ( Exception e ) { 86 die( "Error with option '" + arg + "': " + e.getMessage() ); 87 } 88 } 89 } 90 91 private static String readVariable( BufferedReader in, String name ) throws IOException 92 { 93 System.out.print( "Variable '" + name + "' = " ); 94 95 return in.readLine(); 96 } 97 98 public static void main( String [] args ) 99 { 100 if ( args.length < 2 ) 101 { 102 System.out.println( "Usage: GraphTemplate [-img (png|gif|jpg)] [-w width] [-h height] [-q jpegQuality] <template_file> <image_name>" ); 103 System.exit(0); 104 } 105 106 parseArguments( args ); 107 108 try 109 { 110 System.out.println( ">>> Reading XML template" ); 112 RrdGraphDefTemplate template = new RrdGraphDefTemplate( new File (templateFile) ); 113 114 System.out.println( ">>> Setting template variables" ); 116 if ( template.hasVariables() ) 117 { 118 BufferedReader in = new BufferedReader ( new InputStreamReader (System.in) ); 119 120 String [] variables = template.getVariables(); 121 for ( int i = 0; i < variables.length; i++ ) 122 template.setVariable( variables[i], readVariable( in, variables[i] ) ); 123 } 124 125 System.out.println( ">>> Generating graph..." ); 126 127 long start = System.currentTimeMillis(); 128 129 RrdGraph graph = new RrdGraph( template.getRrdGraphDef() ); 131 132 if ( format.equalsIgnoreCase("png") ) 133 graph.saveAsPNG( imageName, width, height ); 134 else if ( format.equalsIgnoreCase("gif") ) 135 graph.saveAsGIF( imageName, width, height ); 136 else if ( format.equalsIgnoreCase("jpg") ) 137 graph.saveAsJPEG( imageName, width, height, quality ); 138 139 long stop = System.currentTimeMillis(); 140 141 System.out.println( ">>> Graph generated and saved in " + (stop - start) + " milliseconds" ); 142 } 143 catch ( RrdException rrde ) { 144 die( "RrdException occurred: " + rrde.getMessage() ); 145 } 146 catch ( IOException ioe ) { 147 die( "IOException occurred: " + ioe.getMessage() ); 148 } 149 } 150 } 151 | Popular Tags |