1 25 package org.jrobin.graph; 26 27 import org.xml.sax.InputSource ; 28 import org.jrobin.core.RrdException; 29 import org.jrobin.core.XmlTemplate; 30 import org.jrobin.core.Util; 31 import org.w3c.dom.Node ; 32 33 import java.io.IOException ; 34 import java.io.File ; 35 import java.util.GregorianCalendar ; 36 37 95 public class RrdExportDefTemplate extends XmlTemplate 96 { 97 private RrdExportDef def; 98 99 105 public RrdExportDefTemplate(InputSource inputSource) throws IOException , RrdException { 106 super(inputSource); 107 } 108 109 115 public RrdExportDefTemplate(File xmlFile) throws IOException , RrdException { 116 super(xmlFile); 117 } 118 119 125 public RrdExportDefTemplate(String xmlString) throws IOException , RrdException { 126 super(xmlString); 127 } 128 129 139 public RrdExportDef getRrdExportDef() throws RrdException 140 { 141 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 [] {"span", "options", "datasources", "exports"} ); 146 def = new RrdExportDef(); 147 Node [] childs = getChildNodes(root); 149 for(int i = 0; i < childs.length; i++) { 150 String nodeName = childs[i].getNodeName(); 152 if(nodeName.equals("span")) { 153 resolveSpan(childs[i]); 154 } 155 else if(nodeName.equals("options")) { 157 resolveOptions(childs[i]); 158 } 159 else if(nodeName.equals("datasources")) { 161 resolveDatasources(childs[i]); 162 } 163 else if(nodeName.equals("exports")) { 165 resolveExports(childs[i]); 166 } 167 } 168 return def; 169 } 170 171 private void resolveExports(Node datasourceNode) throws RrdException 172 { 173 validateTagsOnlyOnce(datasourceNode, new String [] { "export*" }); 174 Node [] nodes = getChildNodes(datasourceNode, "export"); 175 for( int i = 0; i < nodes.length; i++ ) 176 { 177 validateTagsOnlyOnce( nodes[i], new String [] { "datasource", "legend" } ); 178 String ds = getChildValue( nodes[i], "datasource" ); 179 String legend = getChildValue( nodes[i], "legend" ); 180 181 def.export( ds, legend ); 182 } 183 } 184 185 private void resolveDatasources(Node datasourceNode) throws RrdException 186 { 187 validateTagsOnlyOnce(datasourceNode, new String [] { "def*", "export_data*" }); 188 Node [] nodes = getChildNodes(datasourceNode, "def"); 189 for(int i = 0; i < nodes.length; i++) { 190 if(hasChildNode(nodes[i], "rrd")) 191 { 192 validateTagsOnlyOnce(nodes[i], new String [] {"name", "rrd", "source", "cf", "backend"}); 194 String name = getChildValue(nodes[i], "name"); 195 String rrd = getChildValue(nodes[i], "rrd"); 196 String dsName = getChildValue(nodes[i], "source"); 197 String consolFun = getChildValue(nodes[i], "cf"); 198 199 if ( Util.Xml.hasChildNode(nodes[i], "backend") ) 200 { 201 String 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 validateTagsOnlyOnce(nodes[i], new String [] {"name", "rpn"}); 210 String name = getChildValue(nodes[i], "name"); 211 String rpn = getChildValue(nodes[i], "rpn"); 212 def.datasource(name, rpn); 213 } 214 else if ( hasChildNode( nodes[i], "cf" ) || hasChildNode( nodes[i], "datasource" ) ) { 215 validateTagsOnlyOnce( nodes[i], new String [] {"name", "datasource", "cf"} ); 217 String name = getChildValue(nodes[i], "name"); 218 String ds = getChildValue(nodes[i], "datasource"); 219 String 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 [] {"file", "ds_name_prefix", "use_legend_names"} ); 231 String file = getChildValue( nodes[i], "file" ); 232 String 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 (file), prefix ) ); 245 else 246 def.addExportData( new ExportData( new File (file), use_legends ) ); 247 } 248 catch ( IOException ioe ) { 249 throw new RrdException( ioe ); 250 } 251 } 252 } 253 254 private void resolveOptions(Node rootOptionNode) throws RrdException 255 { 256 validateTagsOnlyOnce( rootOptionNode, new String [] { 257 "resolution", "strict_export" 258 }); 259 260 Node [] optionNodes = getChildNodes(rootOptionNode); 261 for( int i = 0; i < optionNodes.length; i++ ) 262 { 263 String option = optionNodes[i].getNodeName(); 264 Node optionNode = optionNodes[i]; 265 266 if(option.equals("strict_export")) def.setStrictExport( getValueAsBoolean(optionNode) ); 268 else if(option.equals("resolution")) def.setResolution( getValueAsInt(optionNode) ); 270 } 271 } 272 273 private void resolveSpan(Node spanNode) throws RrdException 274 { 275 validateTagsOnlyOnce(spanNode, new String [] {"start", "end"}); 276 String startStr = getChildValue(spanNode, "start"); 277 String endStr = getChildValue(spanNode, "end"); 278 GregorianCalendar gc1 = Util.getGregorianCalendar(startStr); 279 GregorianCalendar gc2 = Util.getGregorianCalendar(endStr); 280 def.setTimePeriod(gc1, gc2); 281 } 282 } 283 | Popular Tags |