KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jrobin > graph > RrdGraphDefTemplate


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.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 JavaDoc;
31 import org.w3c.dom.Node JavaDoc;
32
33 import java.io.IOException JavaDoc;
34 import java.io.File JavaDoc;
35 import java.awt.*;
36 import java.util.GregorianCalendar JavaDoc;
37
38 /**
39  * Class used to create an arbitrary number of RrdGraphDef (graph definition) objects
40  * from a single XML template. XML template can be supplied as an XML InputSource,
41  * XML file or XML formatted string.<p>
42  *
43  * Here is an example of a properly formatted XML template with all available options in it
44  * (unwanted options can be removed):<p>
45  * <pre>
46  * &lt;rrd_graph_def&gt;
47  * &lt;span&gt;
48  * &lt;!-- ISO FORMAT: yyyy-MM-dd HH:mm:ss --&gt;
49  * &lt;start&gt;2004-02-27 13:35:00&lt;/start&gt;
50  * &lt;!-- timestamp in seconds is also allowed --&gt;
51  * &lt;end&gt;1234567890&lt;/end&gt;
52  * &lt;/span&gt;
53  * &lt;options&gt;
54  * &lt;anti_aliasing&gt;true&lt;/anti_aliasing&gt;
55  * &lt;arrow_color&gt;#FF0000&lt;/arrow_color&gt;
56  * &lt;axis_color&gt;#00FF00&lt;/axis_color&gt;
57  * &lt;back_color&gt;#00FF00&lt;/back_color&gt;
58  * &lt;background&gt;#FFFFFF&lt;/background&gt;
59  * &lt;base_value&gt;1024&lt;/base_value&gt;
60  * &lt;canvas&gt;#112211&lt;/canvas&gt;
61  * &lt;left_padding&gt;55&lt;/left_padding&gt;
62  * &lt;default_font&gt;
63  * &lt;name&gt;Times&lt;/name&gt;
64  * &lt;style&gt;BOLD ITALIC&lt;/style&gt;
65  * &lt;size&gt;15&lt;/size&gt;
66  * &lt;/default_font&gt;
67  * &lt;default_font_color&gt;#000000&lt;/default_font_color&gt;
68  * &lt;frame_color&gt;#0000FF&lt;/frame_color&gt;
69  * &lt;front_grid&gt;true&lt;/front_grid&gt;
70  * &lt;grid_range&gt;
71  * &lt;lower&gt;100&lt;/lower&gt;
72  * &lt;upper&gt;200&lt;/upper&gt;
73  * &lt;rigid&gt;false&lt;/rigid&gt;
74  * &lt;/grid_range&gt;
75  * &lt;grid_x&gt;true&lt;/grid_x&gt;
76  * &lt;grid_y&gt;false&lt;/grid_y&gt;
77  * &lt;border&gt;
78  * &lt;color&gt;#00FFFF&lt;/color&gt;
79  * &lt;width&gt;2&lt;/width&gt;
80  * &lt;/border&gt;
81  * &lt;major_grid_color&gt;#00FF00&lt;/major_grid_color&gt;
82  * &lt;major_grid_x&gt;true&lt;/major_grid_x&gt;
83  * &lt;major_grid_y&gt;false&lt;/major_grid_y&gt;
84  * &lt;minor_grid_color&gt;#00FFFF&lt;/minor_grid_color&gt;
85  * &lt;minor_grid_x&gt;true&lt;/minor_grid_x&gt;
86  * &lt;minor_grid_y&gt;false&lt;/minor_grid_y&gt;
87  * &lt;overlay&gt;overlay_image.png&lt;/overlay&gt;
88  * &lt;show_legend&gt;true&lt;/show_legend&gt;
89  * &lt;show_signature&gt;false&lt;/show_signature&gt;
90  * &lt;time_axis&gt;
91  * &lt;!-- ALLOWED TIME UNITS: SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, YEAR --&gt;
92  * &lt;min_grid_time_unit&gt;HOUR&lt;/min_grid_time_unit&gt;
93  * &lt;min_grid_unit_steps&gt;4&lt;/min_grid_unit_steps&gt;
94  * &lt;maj_grid_time_unit&gt;DAY&lt;/maj_grid_time_unit&gt;
95  * &lt;maj_grid_unit_steps&gt;2&lt;/maj_grid_unit_steps&gt;
96  * &lt;date_format&gt;HH:mm&lt;/date_format&gt;
97  * &lt;center_labels&gt;true&lt;/center_labels&gt;
98  * &lt;!-- ALLOWED DAYS OF WEEK: MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY --&gt;
99  * &lt;first_day_of_week&gt;MONDAY&lt;/first_day_of_week&gt;
100  * &lt;/time_axis&gt;
101  * &lt;time_axis_label&gt;time&lt;/time_axis_label&gt;
102  * &lt;title&gt;Graph title&lt;/title&gt;
103  * &lt;title_font&gt;
104  * &lt;name&gt;Verdana&lt;/name&gt;
105  * &lt;style&gt;BOLD&lt;/style&gt;
106  * &lt;size&gt;17&lt;/size&gt;
107  * &lt;/title_font&gt;
108  * &lt;title_font_color&gt;#FF0000&lt;/title_font_color&gt;
109  * &lt;units_exponent&gt;6&lt;/units_exponent&gt;
110  * &lt;value_axis&gt;
111  * &lt;grid_step&gt;100&lt;/grid_step&gt;
112  * &lt;label_step&gt;200&lt;/label_step&gt;
113  * &lt;/value_axis&gt;
114  * &lt;vertical_label&gt;voltage [V]&lt;/vertical_label&gt;
115  * &lt;/options&gt;
116  * &lt;datasources&gt;
117  * &lt;def&gt;
118  * &lt;name&gt;input&lt;/name&gt;
119  * &lt;rrd&gt;test1.rrd&lt;/rrd&gt;
120  * &lt;source&gt;inOctets&lt;/source&gt;
121  * &lt;cf&gt;AVERAGE&lt;/cf&gt;
122  * &lt;/def&gt;
123  * &lt;def&gt;
124  * &lt;name&gt;output&lt;/name&gt;
125  * &lt;rrd&gt;test2.rrd&lt;/rrd&gt;
126  * &lt;source&gt;outOctets&lt;/source&gt;
127  * &lt;cf&gt;MAX&lt;/cf&gt;
128  * &lt;backend&gt;FILE&lt;/backend&gt;
129  * &lt;/def&gt;
130  * &lt;def&gt;
131  * &lt;name&gt;input8&lt;/name&gt;
132  * &lt;rpn&gt;input,8,*&lt;/rpn&gt;
133  * &lt;/def&gt;
134  * &lt;def&gt;
135  * &lt;name&gt;output8&lt;/name&gt;
136  * &lt;rpn&gt;output,8,*,-1,*&lt;/rpn&gt;
137  * &lt;/def&gt;
138  * &lt;def&gt;
139  * &lt;name&gt;avgOutput8&lt;/name&gt;
140  * &lt;datasource&gt;output8&lt;/datasource&gt;
141  * &lt;cf&gt;AVERAGE&lt;/cf&gt;
142  * &lt;/def&gt;
143  * &lt;export_data&gt;
144  * &lt;file&gt;exportdata1.xml&lt;/file&gt;
145  * &lt;ds_name_prefix&gt;traffic&lt;/ds_name_prefix&gt;
146  * &lt;/export_data&gt;
147  * &lt;export_data&gt;
148  * &lt;file&gt;exportdata2.xml&lt;/file&gt;
149  * &lt;use_legend_names&gt;true&lt;/use_legend_names&gt;
150  * &lt;/export_data&gt;
151  * &lt;export_data&gt;
152  * &lt;file&gt;exportdata3.xml&lt;/file&gt;
153  * &lt;/export_data&gt;
154  * &lt;/datasources&gt;
155  * &lt;graph&gt;
156  * &lt;area&gt;
157  * &lt;datasource&gt;input&lt;/datasource&gt;
158  * &lt;color&gt;#FF0000&lt;/color&gt;
159  * &lt;legend&gt;Input traffic&lt;/legend&gt;
160  * &lt;/area&gt;
161  * &lt;area&gt;
162  * &lt;datasource&gt;output&lt;/datasource&gt;
163  * &lt;color&gt;#00FF00&lt;/color&gt;
164  * &lt;legend&gt;Output traffic&lt;/legend&gt;
165  * &lt;/area&gt;
166  * &lt;stack&gt;
167  * &lt;datasource&gt;input8&lt;/datasource&gt;
168  * &lt;color&gt;#AA00AA&lt;/color&gt;
169  * &lt;legend&gt;Stacked input@r&lt;/legend&gt;
170  * &lt;/stack&gt;
171  * &lt;line&gt;
172  * &lt;datasource&gt;input&lt;/datasource&gt;
173  * &lt;color&gt;#AB7777&lt;/color&gt;
174  * &lt;legend&gt;Input traffic@l&lt;/legend&gt;
175  * &lt;/line&gt;
176  * &lt;line&gt;
177  * &lt;datasource&gt;output&lt;/datasource&gt;
178  * &lt;color&gt;#AA00AA&lt;/color&gt;
179  * &lt;legend&gt;Output traffic@r&lt;/legend&gt;
180  * &lt;width&gt;2&lt;/width&gt;
181  * &lt;/line&gt;
182  * &lt;area&gt;
183  * &lt;time1&gt;2004-02-25 12:00:01&lt;/time1&gt;
184  * &lt;time2&gt;1000222333&lt;/time2&gt;
185  * &lt;value1&gt;1001.23&lt;/value1&gt;
186  * &lt;value2&gt;2765.45&lt;/value2&gt;
187  * &lt;color&gt;#AABBCC&lt;/color&gt;
188  * &lt;legend&gt;simeple two point area&lt;/legend&gt;
189  * &lt;/area&gt;
190  * &lt;line&gt;
191  * &lt;time1&gt;1000111444&lt;/time1&gt;
192  * &lt;time2&gt;2004-02-25 12:00:01&lt;/time2&gt;
193  * &lt;value1&gt;1009.23&lt;/value1&gt;
194  * &lt;value2&gt;9002.45&lt;/value2&gt;
195  * &lt;color&gt;#AABB33&lt;/color&gt;
196  * &lt;legend&gt;simple two point line&lt;/legend&gt;
197  * &lt;width&gt;5&lt;/width&gt;
198  * &lt;/line&gt;
199  * &lt;gprint&gt;
200  * &lt;datasource&gt;input&lt;/datasource&gt;
201  * &lt;cf&gt;AVERAGE&lt;/cf&gt;
202  * &lt;format&gt;Average input: @2@c&lt;/format&gt;
203  * &lt;/gprint&gt;
204  * &lt;gprint&gt;
205  * &lt;datasource&gt;output&lt;/datasource&gt;
206  * &lt;cf&gt;TOTAL&lt;/cf&gt;
207  * &lt;format&gt;total output: @2@r&lt;/format&gt;
208  * &lt;-- optional base value --&gt;
209  * &lt;base&gt;1024&lt;/base&gt;
210  * &lt;/gprint&gt;
211  * &lt;hrule&gt;
212  * &lt;value&gt;1234.5678&lt;/value&gt;
213  * &lt;color&gt;#112233&lt;/color&gt;
214  * &lt;legend&gt;horizontal rule&lt;/legend&gt;
215  * &lt;width&gt;3&lt;/width&gt;
216  * &lt;/hrule&gt;
217  * &lt;vrule&gt;
218  * &lt;time&gt;2004-02-22 17:43:57&lt;/time&gt;
219  * &lt;color&gt;#112299&lt;/color&gt;
220  * &lt;legend&gt;vertical rule&lt;/legend&gt;
221  * &lt;width&gt;6&lt;/width&gt;
222  * &lt;/vrule&gt;
223  * &lt;time&gt;
224  * &lt;format&gt;Current time: @t&lt;/format&gt;
225  * &lt;pattern&gt;MMM dd, yyyy HH:mm:ss&lt;/pattern&gt;
226  * &lt;/time&gt;
227  * &lt;time&gt;
228  * &lt;format&gt;Month: @t&lt;/format&gt;
229  * &lt;pattern&gt;MMMM yyyy&lt;/pattern&gt;
230  * &lt;value&gt;2004-01-01 12:00:00&lt;/value&gt;
231  * &lt;/time&gt;
232  * &lt;comment&gt;Created with JRobin&lt;/comment&gt;
233  * &lt;/graph&gt;
234  * &lt;/rrd_graph_def&gt;
235  * </pre>
236  * Notes on the template syntax:<p>
237  * <ul>
238  * <li>There is a strong relation between the XML template syntax and the syntax of
239  * {@link RrdGraphDef} class methods. If you are not sure what some XML tag means, check javadoc
240  * for the corresponding class.
241  * <li>hard-coded timestamps in templates should be long integeres
242  * (like: 1000243567) or ISO formatted strings (like: 2004-02-21 12:25:45)
243  * <li>whitespaces are not harmful, however, whitespaces do get special treatment in case of printed text: it is not
244  * necessary to use CDATA blocks to specify fixed whitespace with <code>comment</code>,<code>title</code>, <code>time</code>,
245  * <code>gprint</code> or <code>legend</code> tags
246  * <li>use <code>true</code>, <code>on</code>, <code>yes</code>, <code>y</code>,
247  * or <code>1</code> to specify boolean <code>true</code> value (anything else will
248  * be treated as <code>false</code>).
249  * <li>floating point values: anything that cannot be parsed will be treated as Double.NaN
250  * (like: U, unknown, 12r.23)
251  * <li>use #RRGGBB format to specify colors.
252  * <li>valid font styles are: PLAIN, ITALIC, BOLD, BOLD ITALIC
253  * <li>comments are allowed.
254  * </ul>
255  * Any template value (text between <code>&lt;some_tag&gt;</code> and
256  * <code>&lt;/some_tag&gt;</code>) can be replaced with
257  * a variable of the following form: <code>${variable_name}</code>. Use
258  * {@link XmlTemplate#setVariable(String, String) setVariable()}
259  * methods from the base class to replace
260  * template variables with real values at runtime.<p>
261  *
262  * Typical usage scenario:<p>
263  * <ul>
264  * <li>Create your XML template and save it to a file (template.xml, for example)
265  * <li>Replace template values with variables if you want to change them during runtime.
266  * For example, time span should not be hard-coded in the template - you probably want to create
267  * many different graphs with different time spans from the same XML template.
268  * For example, your XML template could start with:
269  * <pre>
270  * &lt;rrd_graph_def&gt;
271  * &lt;span&gt;
272  * &lt;start&gt;${start}&lt;/start&gt;
273  * &lt;end&gt;${end}&lt;/end&gt;
274  * &lt;/span&gt;
275  * ...
276  * </pre>
277  * <li>In your Java code, create RrdGraphDefTemplate object using your XML template file:
278  * <pre>
279  * RrdGraphDefTemplate t = new RrdGraphDefTemplate(new File(template.xml));
280  * </pre>
281  * <li>Then, specify real values for template variables:
282  * <pre>
283  * t.setVariable("start", new GregorianCalendar(2004, 2, 25));
284  * t.setVariable("end", new GregorianCalendar(2004, 2, 26));
285  * </pre>
286  * <li>Once all template variables are set, just use the template object to create RrdGraphDef
287  * object. This object is actually used to create JRobin grahps:
288  * <pre>
289  * RrdGraphDef gdef = t.getRrdGraphDef();
290  * RrdGraph g = new RrdGraph(gdef);
291  * g.saveAsPNG("graph.png");
292  * </pre>
293  * </ul>
294  * You should create new RrdGraphDefTemplate object only once for each XML template. Single template
295  * object can be reused to create as many RrdGraphDef objects as needed, with different values
296  * specified for template variables. XML synatax check is performed only once - the first graph
297  * definition object gets created relatively slowly, but it will be created much faster next time.
298  */

299 public class RrdGraphDefTemplate extends XmlTemplate {
300
301     private RrdGraphDef rrdGraphDef;
302
303     /**
304      * Creates template object from any parsable XML source
305      * @param inputSource XML source
306      * @throws IOException thrown in case of I/O error
307      * @throws RrdException usually thrown in case of XML related error
308      */

309     public RrdGraphDefTemplate(InputSource JavaDoc inputSource) throws IOException JavaDoc, RrdException {
310         super(inputSource);
311     }
312
313     /**
314      * Creates template object from the file containing XML template code
315      * @param xmlFile file containing XML template
316      * @throws IOException thrown in case of I/O error
317      * @throws RrdException usually thrown in case of XML related error
318      */

319     public RrdGraphDefTemplate(File JavaDoc xmlFile) throws IOException JavaDoc, RrdException {
320         super(xmlFile);
321     }
322
323     /**
324      * Creates template object from the string containing XML template code
325      * @param xmlString string containing XML template
326      * @throws IOException thrown in case of I/O error
327      * @throws RrdException usually thrown in case of XML related error
328      */

329     public RrdGraphDefTemplate(String JavaDoc xmlString) throws IOException JavaDoc, RrdException {
330         super(xmlString);
331     }
332
333     /**
334      * Creates RrdGraphDef object which can be used to create RrdGraph
335      * object (actual JRobin graphs). Before this method is called, all template variables (if any)
336      * must be resolved (replaced with real values).
337      * See {@link XmlTemplate#setVariable(String, String) setVariable()} method information to
338      * understand how to supply values for template variables.
339      * @return Graph definition which can be used to create RrdGraph object (actual JRobin graphs)
340      * @throws RrdException Thrown if parsed XML template contains invalid (unrecognized) tags
341      */

342     public RrdGraphDef getRrdGraphDef() throws RrdException {
343         // basic check
344
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 JavaDoc[] {"span", "options", "datasources", "graph"});
348         rrdGraphDef = new RrdGraphDef();
349         // traverse all nodes
350
Node JavaDoc[] childs = getChildNodes(root);
351         for(int i = 0; i < childs.length; i++) {
352             // SPAN
353
String JavaDoc nodeName = childs[i].getNodeName();
354             if(nodeName.equals("span")) {
355                 resolveSpan(childs[i]);
356             }
357             // OPTIONS
358
else if(nodeName.equals("options")) {
359                 resolveOptions(childs[i]);
360             }
361             // DATASOURCES
362
else if(nodeName.equals("datasources")) {
363                 resolveDatasources(childs[i]);
364             }
365             // GRAPH ELEMENTS
366
else if(nodeName.equals("graph")) {
367                 resolveGraphElements(childs[i]);
368             }
369         }
370         return rrdGraphDef;
371     }
372
373     private void resolveGraphElements(Node JavaDoc graphNode) throws RrdException {
374         validateTagsOnlyOnce(graphNode, new String JavaDoc[] {
375             "area*", "line*", "stack*", "gprint*", "hrule*", "vrule*", "comment*", "time*"
376         });
377         Node JavaDoc[] childs = getChildNodes(graphNode);
378         for(int i = 0; i < childs.length; i++) {
379             String JavaDoc 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 JavaDoc[] { "datasource", "color", "legend" });
388                 String JavaDoc datasource = getChildValue(childs[i], "datasource");
389                 String JavaDoc colorStr = getChildValue(childs[i], "color");
390                 Color color = Color.decode(colorStr);
391                 String JavaDoc legend = getChildValue(childs[i], "legend", false);
392                 rrdGraphDef.stack(datasource, color, legend);
393             }
394             else if(nodeName.equals("comment")) {
395                 String JavaDoc comment = getValue(childs[i], false);
396                 rrdGraphDef.comment(comment);
397             }
398             else if(nodeName.equals("gprint")) {
399                 validateTagsOnlyOnce(childs[i], new String JavaDoc[] { "datasource", "cf", "format", "base" });
400                 String JavaDoc datasource = getChildValue(childs[i], "datasource");
401                 String JavaDoc consolFun = getChildValue(childs[i], "cf");
402                 String JavaDoc 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 JavaDoc[] { "format", "pattern", "value" });
411                 String JavaDoc format = getChildValue(childs[i], "format", false );
412                 String JavaDoc pattern = getChildValue(childs[i], "pattern");
413
414                 if ( Util.Xml.hasChildNode( childs[i], "value" ) )
415                 {
416                     String JavaDoc 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 JavaDoc[] { "value", "color", "legend", "width" });
424                 double value = getChildValueAsDouble(childs[i], "value");
425                 String JavaDoc colorStr = getChildValue(childs[i], "color");
426                 Color color = Color.decode(colorStr);
427                 String JavaDoc 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 JavaDoc[] { "time", "color", "legend", "width" });
436                 String JavaDoc timeStr = getChildValue(childs[i], "time");
437                 GregorianCalendar JavaDoc gc = Util.getGregorianCalendar(timeStr);
438                 String JavaDoc colorStr = getChildValue(childs[i], "color");
439                 Color color = Color.decode(colorStr);
440                 String JavaDoc 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 JavaDoc lineNode) throws RrdException {
451         if(hasChildNode(lineNode, "datasource")) {
452             // ordinary line definition
453
validateTagsOnlyOnce(lineNode, new String JavaDoc[] { "datasource", "color", "legend", "width" });
454             String JavaDoc datasource = getChildValue(lineNode, "datasource");
455             String JavaDoc colorStr = getChildValue(lineNode, "color");
456             Color color = Color.decode(colorStr);
457             String JavaDoc legend = getChildValue(lineNode, "legend", false);
458             // line width is not mandatory
459
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             // two point definition
467
validateTagsOnlyOnce(lineNode, new String JavaDoc[] {
468                 "time1", "time2", "value1", "value2", "color", "legend", "width"
469             });
470             String JavaDoc t1str = getChildValue(lineNode, "time1");
471             GregorianCalendar JavaDoc gc1 = Util.getGregorianCalendar(t1str);
472             String JavaDoc t2str = getChildValue(lineNode, "time2");
473             GregorianCalendar JavaDoc gc2 = Util.getGregorianCalendar(t2str);
474             double v1 = getChildValueAsDouble(lineNode, "value1");
475             double v2 = getChildValueAsDouble(lineNode, "value2");
476             String JavaDoc colorStr = getChildValue(lineNode, "color");
477             Color color = Color.decode(colorStr);
478             String JavaDoc 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 JavaDoc areaNode) throws RrdException {
491         if(hasChildNode(areaNode, "datasource")) {
492             validateTagsOnlyOnce(areaNode, new String JavaDoc[] { "datasource", "color", "legend" });
493             // ordinary area definition
494
String JavaDoc datasource = getChildValue(areaNode, "datasource");
495             String JavaDoc colorStr = getChildValue(areaNode, "color");
496             Color color = Color.decode(colorStr);
497             String JavaDoc legend = getChildValue(areaNode, "legend", false);
498             rrdGraphDef.area(datasource, color, legend);
499         }
500         else if(hasChildNode(areaNode, "time1")) {
501             // two point definition
502
validateTagsOnlyOnce(areaNode, new String JavaDoc[] {
503                 "time1", "time2", "value1", "value2", "color", "legend", "width"
504             });
505             String JavaDoc t1str = getChildValue(areaNode, "time1");
506             GregorianCalendar JavaDoc gc1 = Util.getGregorianCalendar(t1str);
507             String JavaDoc t2str = getChildValue(areaNode, "time2");
508             GregorianCalendar JavaDoc gc2 = Util.getGregorianCalendar(t2str);
509             double v1 = getChildValueAsDouble(areaNode, "value1");
510             double v2 = getChildValueAsDouble(areaNode, "value2");
511             String JavaDoc colorStr = getChildValue(areaNode, "color");
512             Color color = Color.decode(colorStr);
513             String JavaDoc 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 JavaDoc datasourceNode) throws RrdException {
522         validateTagsOnlyOnce(datasourceNode, new String JavaDoc[] { "def*", "export_data*" });
523         Node JavaDoc[] nodes = getChildNodes(datasourceNode, "def");
524         for(int i = 0; i < nodes.length; i++) {
525             if(hasChildNode(nodes[i], "rrd"))
526             {
527                 // RRD datasource
528
validateTagsOnlyOnce(nodes[i], new String JavaDoc[] {"name", "rrd", "source", "cf", "backend"});
529                 String JavaDoc name = getChildValue(nodes[i], "name");
530                 String JavaDoc rrd = getChildValue(nodes[i], "rrd");
531                 String JavaDoc dsName = getChildValue(nodes[i], "source");
532                 String JavaDoc consolFun = getChildValue(nodes[i], "cf");
533
534                 if ( Util.Xml.hasChildNode(nodes[i], "backend") )
535                 {
536                     String JavaDoc 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                 // RPN datasource
544
validateTagsOnlyOnce(nodes[i], new String JavaDoc[] {"name", "rpn"});
545                 String JavaDoc name = getChildValue(nodes[i], "name");
546                 String JavaDoc rpn = getChildValue(nodes[i], "rpn");
547                 rrdGraphDef.datasource(name, rpn);
548             }
549             else if ( hasChildNode( nodes[i], "cf" ) || hasChildNode( nodes[i], "datasource" ) ) {
550                 // STATIC AGGREGATED DATASOURCE
551
validateTagsOnlyOnce( nodes[i], new String JavaDoc[] {"name", "datasource", "cf"} );
552                 String JavaDoc name = getChildValue(nodes[i], "name");
553                 String JavaDoc ds = getChildValue(nodes[i], "datasource");
554                 String JavaDoc 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 JavaDoc[] {"file", "ds_name_prefix", "use_legend_names"} );
566             String JavaDoc file = getChildValue( nodes[i], "file" );
567             String JavaDoc 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 JavaDoc(file), prefix ) );
580                 else
581                     rrdGraphDef.addExportData( new ExportData( new File JavaDoc(file), use_legends ) );
582             }
583             catch ( IOException JavaDoc ioe ) {
584                 throw new RrdException( ioe );
585             }
586         }
587     }
588
589     private void resolveOptions(Node JavaDoc rootOptionNode) throws RrdException {
590         validateTagsOnlyOnce(rootOptionNode, new String JavaDoc[] {
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 JavaDoc[] optionNodes = getChildNodes(rootOptionNode);
601         for(int i = 0; i < optionNodes.length; i++) {
602             String JavaDoc option = optionNodes[i].getNodeName();
603             Node JavaDoc optionNode = optionNodes[i];
604             // ANTI ALIASING
605
if(option.equals("anti_aliasing")) {
606                 boolean antiAliasing = getValueAsBoolean(optionNode);
607                 rrdGraphDef.setAntiAliasing(antiAliasing);
608             }
609             // ARROW COLOR
610
else if(option.equals("arrow_color")) {
611                 String JavaDoc colorStr = getValue(optionNode);
612                 rrdGraphDef.setArrowColor(Color.decode(colorStr));
613             }
614             // AXIS COLOR
615
else if(option.equals("axis_color")) {
616                 String JavaDoc colorStr = getValue(optionNode);
617                 rrdGraphDef.setAxisColor(Color.decode(colorStr));
618             }
619             // BACK COLOR
620
else if(option.equals("back_color")) {
621                 String JavaDoc colorStr = getValue(optionNode);
622                 rrdGraphDef.setBackColor(Color.decode(colorStr));
623             }
624             // BACKGROUND
625
else if(option.equals("background")) {
626                 String JavaDoc backgroundFile = getValue(optionNode);
627                 rrdGraphDef.setBackground(backgroundFile);
628             }
629             // BASE VALUE
630
else if(option.equals("base_value")) {
631                 double baseValue = getValueAsDouble(optionNode);
632                 rrdGraphDef.setBaseValue(baseValue);
633             }
634             // CANVAS
635
else if(option.equals("canvas")) {
636                 String JavaDoc colorStr = getValue(optionNode);
637                 rrdGraphDef.setCanvasColor(Color.decode(colorStr));
638             }
639             // LEFT PADDING
640
else if(option.equals("left_padding")) {
641                 int padding = getValueAsInt(optionNode);
642                 rrdGraphDef.setChartLeftPadding(padding);
643             }
644             // DEFAULT FONT
645
else if(option.equals("default_font")) {
646                 Font f = resolveFont(optionNode);
647                 rrdGraphDef.setDefaultFont(f);
648             }
649             // DEFAULT FONT COLOR
650
else if(option.equals("default_font_color")) {
651                 String JavaDoc colorStr = getValue(optionNode);
652                 rrdGraphDef.setDefaultFontColor(Color.decode(colorStr));
653             }
654             // FRAME COLOR
655
else if(option.equals("frame_color")) {
656                 String JavaDoc colorStr = getValue(optionNode);
657                 rrdGraphDef.setFrameColor(Color.decode(colorStr));
658             }
659             // FRONT GRID
660
else if(option.equals("front_grid")) {
661                 boolean frontGrid = getValueAsBoolean(optionNode);
662                 rrdGraphDef.setFrontGrid(frontGrid);
663             }
664             // GRID RANGE
665
else if(option.equals("grid_range")) {
666                 validateTagsOnlyOnce(optionNode, new String JavaDoc[] { "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             // LOWER LIMIT
673
else if(option.equals("lower_limit")) {
674                 double lower = getValueAsDouble(optionNode);
675                 rrdGraphDef.setLowerLimit( lower );
676             }
677             // GRID X?
678
else if(option.equals("grid_x")) {
679                 boolean gx = getValueAsBoolean(optionNode);
680                 rrdGraphDef.setGridX(gx);
681             }
682             // GRID Y?
683
else if(option.equals("grid_y")) {
684                 boolean gy = getValueAsBoolean(optionNode);
685                 rrdGraphDef.setGridY(gy);
686             }
687             // BORDER
688
else if(option.equals("border")) {
689                 validateTagsOnlyOnce(optionNode, new String JavaDoc[] {"color", "width"});
690                 String JavaDoc colorStr = getChildValue(optionNode, "color");
691                 int width = getChildValueAsInt(optionNode, "width");
692                 rrdGraphDef.setImageBorder(Color.decode(colorStr), width);
693             }
694             // MAJOR GRID COLOR
695
else if(option.equals("major_grid_color")) {
696                 String JavaDoc colorStr = getValue(optionNode);
697                 rrdGraphDef.setMajorGridColor(Color.decode(colorStr));
698             }
699             // MAJOR GRID X?
700
else if(option.equals("major_grid_x")) {
701                 boolean gx = getValueAsBoolean(optionNode);
702                 rrdGraphDef.setMajorGridX(gx);
703             }
704             // MAJOR GRID Y?
705
else if(option.equals("major_grid_y")) {
706                 boolean gy = getValueAsBoolean(optionNode);
707                 rrdGraphDef.setMajorGridY(gy);
708             }
709             // MINOR GRID COLOR
710
else if(option.equals("minor_grid_color")) {
711                 String JavaDoc colorStr = getValue(optionNode);
712                 rrdGraphDef.setMinorGridColor(Color.decode(colorStr));
713             }
714             // MINOR GRID X?
715
else if(option.equals("minor_grid_x")) {
716                 boolean gx = getValueAsBoolean(optionNode);
717                 rrdGraphDef.setMinorGridX(gx);
718             }
719             // MINOR GRID Y?
720
else if(option.equals("minor_grid_y")) {
721                 boolean gy = getValueAsBoolean(optionNode);
722                 rrdGraphDef.setMinorGridY(gy);
723             }
724             // OVERLAY
725
else if(option.equals("overlay")) {
726                 String JavaDoc overlay = getValue(optionNode);
727                 rrdGraphDef.setOverlay(overlay);
728             }
729             // SHOW LEGEND?
730
else if(option.equals("show_legend")) {
731                 boolean show = getValueAsBoolean(optionNode);
732                 rrdGraphDef.setShowLegend(show);
733             }
734             // SHOW SIGNATURE?
735
else if(option.equals("show_signature")) {
736                 boolean show = getValueAsBoolean(optionNode);
737                 rrdGraphDef.setShowSignature(show);
738             }
739             // TIME AXIS
740
else if(option.equals("time_axis")) {
741                 validateTagsOnlyOnce(optionNode, new String JavaDoc[] {
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 JavaDoc format = getChildValue(optionNode, "date_format");
753                     boolean center = getChildValueAsBoolean(optionNode, "center_labels");
754                     rrdGraphDef.setTimeAxis(unit1, step1, unit2, step2, format, center);
755                 }
756
757                 // Determine first day of the week
758
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             // TIME AXIS LABEL
765
else if(option.equals("time_axis_label")) {
766                 String JavaDoc label = getValue(optionNode);
767                 rrdGraphDef.setTimeAxisLabel(label);
768             }
769             // TITLE
770
else if(option.equals("title")) {
771                 String JavaDoc title = getValue(optionNode);
772                 rrdGraphDef.setTitle(title);
773             }
774             // TITLE FONT
775
else if(option.equals("title_font")) {
776                 Font f = resolveFont(optionNode);
777                 rrdGraphDef.setTitleFont(f);
778             }
779             // TITLE FONT COLOR
780
else if(option.equals("title_font_color")) {
781                 String JavaDoc colorStr = getValue(optionNode);
782                 rrdGraphDef.setTitleFontColor(Color.decode(colorStr));
783             }
784             // UNITS EXPONENT
785
else if(option.equals("units_exponent")) {
786                 int exp = getValueAsInt(optionNode);
787                 rrdGraphDef.setUnitsExponent(exp);
788             }
789             // VALUE AXIS
790
else if(option.equals("value_axis")) {
791                 validateTagsOnlyOnce(optionNode, new String JavaDoc[] {"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             // VERTICAL LABEL
797
else if(option.equals("vertical_label")) {
798                 String JavaDoc label = getValue(optionNode);
799                 rrdGraphDef.setVerticalLabel(label);
800             }
801             // STRICT EXPORT
802
else if(option.equals("strict_export")) {
803                 rrdGraphDef.setStrictExport( getValueAsBoolean(optionNode) );
804             }
805             // RESOLUTION
806
else if(option.equals("resolution")) {
807                 rrdGraphDef.setResolution( getValueAsInt(optionNode) );
808             }
809         }
810     }
811
812     private int resolveUnit(String JavaDoc 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 JavaDoc("Invalid unit specified: " + unit);
836         }
837     }
838
839     private int resolveDayUnit( String JavaDoc 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 JavaDoc( "Invalid day unit specified: " + unit );
863         }
864
865     }
866
867     private void resolveSpan(Node JavaDoc spanNode) throws RrdException {
868         validateTagsOnlyOnce(spanNode, new String JavaDoc[] {"start", "end"});
869         String JavaDoc startStr = getChildValue(spanNode, "start");
870         String JavaDoc endStr = getChildValue(spanNode, "end");
871         GregorianCalendar JavaDoc gc1 = Util.getGregorianCalendar(startStr);
872         GregorianCalendar JavaDoc gc2 = Util.getGregorianCalendar(endStr);
873         rrdGraphDef.setTimePeriod(gc1, gc2);
874     }
875
876     private Font resolveFont(Node JavaDoc fontNode) throws RrdException {
877         validateTagsOnlyOnce(fontNode, new String JavaDoc[] {"name", "style", "size"});
878         String JavaDoc name = getChildValue(fontNode, "name");
879         String JavaDoc 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 /*
898     public static void main(String[] args) throws IOException, RrdException {
899         File fileTemplate = new File("work/test2.xml");
900         RrdGraphDefTemplate template = new RrdGraphDefTemplate(fileTemplate);
901         template.setVariable("comment1", "[comment 1]");
902         template.setVariable("comment2", "[comment 2]");
903         System.out.println(template.getRrdGraphDef().exportXmlTemplate());
904     }
905 */

906 }
907
Popular Tags