KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Developers: Sasa Markovic (saxon@jrobin.org)
9  * Arne Vandamme (cobralord@jrobin.org)
10  *
11  * (C) Copyright 2003, by Sasa Markovic.
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.xml.sax.InputSource JavaDoc;
28 import org.jrobin.core.RrdException;
29 import org.jrobin.core.XmlTemplate;
30 import org.jrobin.core.Util;
31 import org.w3c.dom.Node JavaDoc;
32
33 import java.io.IOException JavaDoc;
34 import java.io.File JavaDoc;
35 import java.util.GregorianCalendar JavaDoc;
36
37 /**
38  * <p>Class used to create a RrdExportDef (export) object
39  * from a single XML template. XML template can be supplied as an XML InputSource,
40  * XML file or XML formatted string.<p>
41  *
42  * <p>Below is an exmple XML template, not all options are required:</p>
43  *
44  * <xmp>
45  * <rrd_export_def>
46  * <span>
47  * <start>${start}</start>
48  * <end>${end}</end>
49  * </span>
50  * <options>
51  * <resolution>300</resolution>
52  * <strict_export>true</strict_export>
53  * </options>
54  * <datasources>
55  * <def>
56  * <name>bytesIn</name>
57  * <rrd>${rrd}</rrd>
58  * <source>ifInOctets</source>
59  * <cf>AVERAGE</cf>
60  * </def>
61  * <def>
62  * <name>bytesOut</name>
63  * <rrd>${rrd}</rrd>
64  * <source>ifOutOctets</source>
65  * <cf>AVERAGE</cf>
66  * </def>
67  * <def>
68  * <name>bitsIn</name>
69  * <rpn>bytesIn,8,*</rpn>
70  * </def>
71  * <def>
72  * <name>bitsOut</name>
73  * <rpn>bytesOut,8,*</rpn>
74  * </def>
75  * </datasources>
76  * <exports>
77  * <export>
78  * <datasource>bitsIn</datasource>
79  * <legend>Incoming traffic</legend>
80  * </export>
81  * <export>
82  * <datasource>bitsOut</datasource>
83  * <legend>Outgoing traffic</legend>
84  * </export>
85  * </exports>
86  * </rrd_export_def>
87  * </xmp>
88  *
89  * <p><b>Note:</b> for more information on JRobin XML templates in general, refer to the {@link RrdGraphDefTemplate}</p>
90  * <p><b>Note:</b> the <code>RrdExportDefTemplate</code> <code>datasources</code> section can contain all the same options
91  * as the corresponding section in {@link RrdGraphDefTemplate}
92  * </p>
93  * @author Arne Vandamme (arne.vandamme@jrobin.org)
94  */

95 public class RrdExportDefTemplate extends XmlTemplate
96 {
97     private RrdExportDef def;
98
99     /**
100      * Creates template object from any parsable XML source
101      * @param inputSource XML source
102      * @throws java.io.IOException thrown in case of I/O error
103      * @throws org.jrobin.core.RrdException usually thrown in case of XML related error
104      */

105     public RrdExportDefTemplate(InputSource JavaDoc inputSource) throws IOException JavaDoc, RrdException {
106         super(inputSource);
107     }
108
109     /**
110      * Creates template object from the file containing XML template code
111      * @param xmlFile file containing XML template
112      * @throws IOException thrown in case of I/O error
113      * @throws RrdException usually thrown in case of XML related error
114      */

115     public RrdExportDefTemplate(File JavaDoc xmlFile) throws IOException JavaDoc, RrdException {
116         super(xmlFile);
117     }
118
119     /**
120      * Creates template object from the string containing XML template code
121      * @param xmlString string containing XML template
122      * @throws IOException thrown in case of I/O error
123      * @throws RrdException usually thrown in case of XML related error
124      */

125     public RrdExportDefTemplate(String JavaDoc xmlString) throws IOException JavaDoc, RrdException {
126         super(xmlString);
127     }
128
129     /**
130      * Creates RrdExportDef object which can be used to create RrdExport
131      * object (actual JRobin export). Before this method is called, all template variables (if any)
132      * must be resolved (replaced with real values).
133      * See {@link XmlTemplate#setVariable(String, String) setVariable()} method information to
134      * understand how to supply values for template variables.
135      *
136      * @return Export definition which can be used to create RrdExport object (actual JRobin export)
137      * @throws RrdException Thrown if parsed XML template contains invalid (unrecognized) tags
138      */

139     public RrdExportDef getRrdExportDef() throws RrdException
140     {
141         // basic check
142
if( !root.getTagName().equals("rrd_export_def") )
143             throw new RrdException("XML definition must start with <rrd_export_def>");
144
145         validateTagsOnlyOnce( root, new String JavaDoc[] {"span", "options", "datasources", "exports"} );
146         def = new RrdExportDef();
147         // traverse all nodes
148
Node JavaDoc[] childs = getChildNodes(root);
149         for(int i = 0; i < childs.length; i++) {
150             // SPAN
151
String JavaDoc nodeName = childs[i].getNodeName();
152             if(nodeName.equals("span")) {
153                 resolveSpan(childs[i]);
154             }
155             // OPTIONS
156
else if(nodeName.equals("options")) {
157                 resolveOptions(childs[i]);
158             }
159             // DATASOURCES
160
else if(nodeName.equals("datasources")) {
161                 resolveDatasources(childs[i]);
162             }
163             // EXPORTS
164
else if(nodeName.equals("exports")) {
165                 resolveExports(childs[i]);
166             }
167         }
168         return def;
169     }
170
171     private void resolveExports(Node JavaDoc datasourceNode) throws RrdException
172     {
173         validateTagsOnlyOnce(datasourceNode, new String JavaDoc[] { "export*" });
174         Node JavaDoc[] nodes = getChildNodes(datasourceNode, "export");
175         for( int i = 0; i < nodes.length; i++ )
176         {
177             validateTagsOnlyOnce( nodes[i], new String JavaDoc[] { "datasource", "legend" } );
178             String JavaDoc ds = getChildValue( nodes[i], "datasource" );
179             String JavaDoc legend = getChildValue( nodes[i], "legend" );
180
181             def.export( ds, legend );
182         }
183     }
184
185     private void resolveDatasources(Node JavaDoc datasourceNode) throws RrdException
186     {
187         validateTagsOnlyOnce(datasourceNode, new String JavaDoc[] { "def*", "export_data*" });
188         Node JavaDoc[] nodes = getChildNodes(datasourceNode, "def");
189         for(int i = 0; i < nodes.length; i++) {
190             if(hasChildNode(nodes[i], "rrd"))
191             {
192                 // RRD datasource
193
validateTagsOnlyOnce(nodes[i], new String JavaDoc[] {"name", "rrd", "source", "cf", "backend"});
194                 String JavaDoc name = getChildValue(nodes[i], "name");
195                 String JavaDoc rrd = getChildValue(nodes[i], "rrd");
196                 String JavaDoc dsName = getChildValue(nodes[i], "source");
197                 String JavaDoc consolFun = getChildValue(nodes[i], "cf");
198
199                 if ( Util.Xml.hasChildNode(nodes[i], "backend") )
200                 {
201                     String JavaDoc backend = getChildValue( nodes[i], "backend" );
202                     def.datasource( name, rrd, dsName, consolFun, backend );
203                 }
204                 else
205                     def.datasource(name, rrd, dsName, consolFun);
206             }
207             else if(hasChildNode(nodes[i], "rpn")) {
208                 // RPN datasource
209
validateTagsOnlyOnce(nodes[i], new String JavaDoc[] {"name", "rpn"});
210                 String JavaDoc name = getChildValue(nodes[i], "name");
211                 String JavaDoc rpn = getChildValue(nodes[i], "rpn");
212                 def.datasource(name, rpn);
213             }
214             else if ( hasChildNode( nodes[i], "cf" ) || hasChildNode( nodes[i], "datasource" ) ) {
215                 // STATIC AGGREGATED DATASOURCE
216
validateTagsOnlyOnce( nodes[i], new String JavaDoc[] {"name", "datasource", "cf"} );
217                 String JavaDoc name = getChildValue(nodes[i], "name");
218                 String JavaDoc ds = getChildValue(nodes[i], "datasource");
219                 String JavaDoc cf = getChildValue(nodes[i], "cf");
220                 def.datasource( name, ds, cf );
221             }
222             else {
223                 throw new RrdException("Unrecognized <def> format");
224             }
225         }
226
227         nodes = getChildNodes(datasourceNode, "export_data");
228         for ( int i = 0; i < nodes.length; i++ )
229         {
230             validateTagsOnlyOnce( nodes[i], new String JavaDoc[] {"file", "ds_name_prefix", "use_legend_names"} );
231             String JavaDoc file = getChildValue( nodes[i], "file" );
232             String JavaDoc prefix = "d";
233             boolean use_legends = false;
234
235             if ( Util.Xml.hasChildNode( nodes[i], "ds_name_prefix" ) )
236                 prefix = getChildValue(nodes[i], "ds_name_prefix");
237
238             if ( Util.Xml.hasChildNode( nodes[i], "use_legend_names" ) )
239                 use_legends = getChildValueAsBoolean(nodes[i], "use_legend_names");
240
241             try
242             {
243                 if ( !prefix.equals("d") )
244                     def.addExportData( new ExportData( new File JavaDoc(file), prefix ) );
245                 else
246                     def.addExportData( new ExportData( new File JavaDoc(file), use_legends ) );
247             }
248             catch ( IOException JavaDoc ioe ) {
249                 throw new RrdException( ioe );
250             }
251         }
252     }
253
254     private void resolveOptions(Node JavaDoc rootOptionNode) throws RrdException
255     {
256         validateTagsOnlyOnce( rootOptionNode, new String JavaDoc[] {
257             "resolution", "strict_export"
258         });
259
260         Node JavaDoc[] optionNodes = getChildNodes(rootOptionNode);
261         for( int i = 0; i < optionNodes.length; i++ )
262         {
263             String JavaDoc option = optionNodes[i].getNodeName();
264             Node JavaDoc optionNode = optionNodes[i];
265
266             if(option.equals("strict_export")) // STRICT EXPORT
267
def.setStrictExport( getValueAsBoolean(optionNode) );
268             else if(option.equals("resolution")) // RESOLUTION
269
def.setResolution( getValueAsInt(optionNode) );
270         }
271     }
272
273     private void resolveSpan(Node JavaDoc spanNode) throws RrdException
274     {
275         validateTagsOnlyOnce(spanNode, new String JavaDoc[] {"start", "end"});
276         String JavaDoc startStr = getChildValue(spanNode, "start");
277         String JavaDoc endStr = getChildValue(spanNode, "end");
278         GregorianCalendar JavaDoc gc1 = Util.getGregorianCalendar(startStr);
279         GregorianCalendar JavaDoc gc2 = Util.getGregorianCalendar(endStr);
280         def.setTimePeriod(gc1, gc2);
281     }
282 }
283
Popular Tags