KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jrobin > demo > graph > GraphTemplate


1 /* ============================================================
2  * JRobin : Pure java implementation of RRDTool's functionality
3  * ============================================================
4  *
5  * Project Info: http://www.jrobin.org
6  * Project Lead: Sasa Markovic (saxon@jrobin.org);
7  *
8  * (C) Copyright 2003, by Sasa Markovic.
9  *
10  * Developers: Sasa Markovic (saxon@jrobin.org)
11  * Arne Vandamme (cobralord@jrobin.org)
12  *
13  * This library is free software; you can redistribute it and/or modify it under the terms
14  * of the GNU Lesser General Public License as published by the Free Software Foundation;
15  * either version 2.1 of the License, or (at your option) any later version.
16  *
17  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
18  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19  * See the GNU Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public License along with this
22  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
23  * Boston, MA 02111-1307, USA.
24  */

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 JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.io.BufferedReader JavaDoc;
34 import java.io.InputStreamReader JavaDoc;
35
36 /**
37  * <p>Simple command line application that allows you to generate a graph
38  * from a RrdGraphDefTemplate. Pretty straightforward in use.</p>
39  *
40  * @author Arne Vandamme (cobralord@jrobin.org)
41  */

42 public class GraphTemplate
43 {
44     private static String JavaDoc format = "gif";
45     private static int width = 0; // Auto scale
46
private static int height = 0; // Auto scale
47
private static float quality = 1.0f; // JPEG quality
48

49     private static String JavaDoc templateFile, imageName;
50
51     private static void die( String JavaDoc msg )
52     {
53         System.err.println( msg );
54         System.exit( -1 );
55     }
56
57     private static void parseArguments( String JavaDoc[] args )
58     {
59         int rpos = args.length - 1;
60
61         // Last two arguments should be templateFile and imageName
62
imageName = args[rpos--];
63         templateFile = args[rpos];
64
65         // Remaining number of parameters should be even
66
if ( rpos % 2 > 0 )
67             die( "Invalid number of arguments." );
68
69         for ( int i = 0; i < rpos; i += 2 )
70         {
71             String JavaDoc arg = args[i];
72             String JavaDoc 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 JavaDoc e ) {
86                 die( "Error with option '" + arg + "': " + e.getMessage() );
87             }
88         }
89     }
90
91     private static String JavaDoc readVariable( BufferedReader JavaDoc in, String JavaDoc name ) throws IOException JavaDoc
92     {
93         System.out.print( "Variable '" + name + "' = " );
94
95         return in.readLine();
96     }
97
98     public static void main( String JavaDoc[] 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             // -- Read the RrdGraphDefTemplate (XML format)
111
System.out.println( ">>> Reading XML template" );
112             RrdGraphDefTemplate template = new RrdGraphDefTemplate( new File JavaDoc(templateFile) );
113
114             // -- Set the parameters (if there are any)
115
System.out.println( ">>> Setting template variables" );
116             if ( template.hasVariables() )
117             {
118                 BufferedReader JavaDoc in = new BufferedReader JavaDoc( new InputStreamReader JavaDoc(System.in) );
119
120                 String JavaDoc[] 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             // -- Generate the actual graph
130
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 JavaDoc ioe ) {
147             die( "IOException occurred: " + ioe.getMessage() );
148         }
149     }
150 }
151
Popular Tags