|
From: <sa...@us...> - 2004-02-23 16:02:15
|
Update of /cvsroot/jrobin/src/org/jrobin/core In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv11365/org/jrobin/core Modified Files: RrdToolkit.java Util.java XmlReader.java Log Message: XML templates revisited Index: RrdToolkit.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/core/RrdToolkit.java,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** RrdToolkit.java 19 Feb 2004 10:48:12 -0000 1.7 --- RrdToolkit.java 23 Feb 2004 15:48:41 -0000 1.8 *************** *** 25,35 **** package org.jrobin.core; - import org.xml.sax.InputSource; - import org.xml.sax.SAXException; - import org.w3c.dom.Element; - import org.w3c.dom.Node; - import javax.xml.parsers.FactoryConfigurationError; - import javax.xml.parsers.ParserConfigurationException; import java.io.*; /** * <p>Class used to perform various complex operations on RRD files. Use an instance of the --- 25,30 ---- package org.jrobin.core; import java.io.*; + /** * <p>Class used to perform various complex operations on RRD files. Use an instance of the *************** *** 73,80 **** public void addDatasource(String sourcePath, String destPath, DsDef newDatasource) throws IOException, RrdException { ! if(Util.sameFilePath(sourcePath, destPath)) { throw new RrdException("Source and destination paths are the same"); } ! RrdDb rrdSource = new RrdDb(sourcePath); RrdDef rrdDef = rrdSource.getRrdDef(); rrdDef.setPath(destPath); --- 68,75 ---- public void addDatasource(String sourcePath, String destPath, DsDef newDatasource) throws IOException, RrdException { ! if (Util.sameFilePath(sourcePath, destPath)) { throw new RrdException("Source and destination paths are the same"); } ! RrdDb rrdSource = new RrdDb(sourcePath); RrdDef rrdDef = rrdSource.getRrdDef(); rrdDef.setPath(destPath); *************** *** 121,128 **** public void removeDatasource(String sourcePath, String destPath, String dsName) throws IOException, RrdException { ! if(Util.sameFilePath(sourcePath, destPath)) { throw new RrdException("Source and destination paths are the same"); } ! RrdDb rrdSource = new RrdDb(sourcePath); RrdDef rrdDef = rrdSource.getRrdDef(); rrdDef.setPath(destPath); --- 116,123 ---- public void removeDatasource(String sourcePath, String destPath, String dsName) throws IOException, RrdException { ! if (Util.sameFilePath(sourcePath, destPath)) { throw new RrdException("Source and destination paths are the same"); } ! RrdDb rrdSource = new RrdDb(sourcePath); RrdDef rrdDef = rrdSource.getRrdDef(); rrdDef.setPath(destPath); *************** *** 169,176 **** public void addArchive(String sourcePath, String destPath, ArcDef newArchive) throws IOException, RrdException { ! if(Util.sameFilePath(sourcePath, destPath)) { throw new RrdException("Source and destination paths are the same"); } ! RrdDb rrdSource = new RrdDb(sourcePath); RrdDef rrdDef = rrdSource.getRrdDef(); rrdDef.setPath(destPath); --- 164,171 ---- public void addArchive(String sourcePath, String destPath, ArcDef newArchive) throws IOException, RrdException { ! if (Util.sameFilePath(sourcePath, destPath)) { throw new RrdException("Source and destination paths are the same"); } ! RrdDb rrdSource = new RrdDb(sourcePath); RrdDef rrdDef = rrdSource.getRrdDef(); rrdDef.setPath(destPath); *************** *** 218,225 **** public void removeArchive(String sourcePath, String destPath, String consolFun, int steps) throws IOException, RrdException { ! if(Util.sameFilePath(sourcePath, destPath)) { throw new RrdException("Source and destination paths are the same"); } ! RrdDb rrdSource = new RrdDb(sourcePath); RrdDef rrdDef = rrdSource.getRrdDef(); rrdDef.setPath(destPath); --- 213,220 ---- public void removeArchive(String sourcePath, String destPath, String consolFun, int steps) throws IOException, RrdException { ! if (Util.sameFilePath(sourcePath, destPath)) { throw new RrdException("Source and destination paths are the same"); } ! RrdDb rrdSource = new RrdDb(sourcePath); RrdDef rrdDef = rrdSource.getRrdDef(); rrdDef.setPath(destPath); *************** *** 259,272 **** File source = new File(sourcePath); File dest = new File(destPath); ! if(saveBackup) { String backupPath = destPath + ".bak"; File backup = new File(backupPath); deleteFile(backup); ! if(!dest.renameTo(backup)) { throw new IOException("Could not create backup file " + backupPath); } } deleteFile(dest); ! if(!source.renameTo(dest)) { throw new IOException("Could not create file " + destPath + " from " + sourcePath); } --- 254,267 ---- File source = new File(sourcePath); File dest = new File(destPath); ! if (saveBackup) { String backupPath = destPath + ".bak"; File backup = new File(backupPath); deleteFile(backup); ! if (!dest.renameTo(backup)) { throw new IOException("Could not create backup file " + backupPath); } } deleteFile(dest); ! if (!source.renameTo(dest)) { throw new IOException("Could not create file " + destPath + " from " + sourcePath); } *************** *** 282,286 **** */ public void setDsHeartbeat(String sourcePath, String datasourceName, ! long newHeartbeat) throws RrdException, IOException { RrdDb rrd = new RrdDb(sourcePath); Datasource ds = rrd.getDatasource(datasourceName); --- 277,281 ---- */ public void setDsHeartbeat(String sourcePath, String datasourceName, ! long newHeartbeat) throws RrdException, IOException { RrdDb rrd = new RrdDb(sourcePath); Datasource ds = rrd.getDatasource(datasourceName); *************** *** 300,304 **** */ public void setDsMinValue(String sourcePath, String datasourceName, ! double newMinValue, boolean filterArchivedValues) throws RrdException, IOException { RrdDb rrd = new RrdDb(sourcePath); Datasource ds = rrd.getDatasource(datasourceName); --- 295,299 ---- */ public void setDsMinValue(String sourcePath, String datasourceName, ! double newMinValue, boolean filterArchivedValues) throws RrdException, IOException { RrdDb rrd = new RrdDb(sourcePath); Datasource ds = rrd.getDatasource(datasourceName); *************** *** 318,322 **** */ public void setDsMaxValue(String sourcePath, String datasourceName, ! double newMaxValue, boolean filterArchivedValues) throws RrdException, IOException { RrdDb rrd = new RrdDb(sourcePath); Datasource ds = rrd.getDatasource(datasourceName); --- 313,317 ---- */ public void setDsMaxValue(String sourcePath, String datasourceName, ! double newMaxValue, boolean filterArchivedValues) throws RrdException, IOException { RrdDb rrd = new RrdDb(sourcePath); Datasource ds = rrd.getDatasource(datasourceName); *************** *** 337,341 **** */ public void setDsMinMaxValue(String sourcePath, String datasourceName, ! double newMinValue, double newMaxValue, boolean filterArchivedValues) throws RrdException, IOException { RrdDb rrd = new RrdDb(sourcePath); --- 332,336 ---- */ public void setDsMinMaxValue(String sourcePath, String datasourceName, ! double newMinValue, double newMaxValue, boolean filterArchivedValues) throws RrdException, IOException { RrdDb rrd = new RrdDb(sourcePath); *************** *** 355,360 **** */ public void setArcXff(String sourcePath, String consolFun, int steps, ! double newXff) throws RrdException, IOException { ! RrdDb rrd = new RrdDb(sourcePath); Archive arc = rrd.getArchive(consolFun, steps); arc.setXff(newXff); --- 350,355 ---- */ public void setArcXff(String sourcePath, String consolFun, int steps, ! double newXff) throws RrdException, IOException { ! RrdDb rrd = new RrdDb(sourcePath); Archive arc = rrd.getArchive(consolFun, steps); arc.setXff(newXff); *************** *** 377,390 **** int numSteps, int newRows) throws IOException, RrdException { ! if(Util.sameFilePath(sourcePath, destPath)) { throw new RrdException("Source and destination paths are the same"); } ! if(newRows < 2) { throw new RrdException("New arcihve size must be at least 2"); } ! RrdDb rrdSource = new RrdDb(sourcePath); RrdDef rrdDef = rrdSource.getRrdDef(); ArcDef arcDef = rrdDef.findArchive(consolFun, numSteps); ! if(arcDef.getRows() != newRows) { arcDef.setRows(newRows); rrdDef.setPath(destPath); --- 372,385 ---- int numSteps, int newRows) throws IOException, RrdException { ! if (Util.sameFilePath(sourcePath, destPath)) { throw new RrdException("Source and destination paths are the same"); } ! if (newRows < 2) { throw new RrdException("New arcihve size must be at least 2"); } ! RrdDb rrdSource = new RrdDb(sourcePath); RrdDef rrdDef = rrdSource.getRrdDef(); ArcDef arcDef = rrdDef.findArchive(consolFun, numSteps); ! if (arcDef.getRows() != newRows) { arcDef.setRows(newRows); rrdDef.setPath(destPath); *************** *** 417,600 **** private static void deleteFile(File file) throws IOException { ! if(file.exists() && !file.delete()) { throw new IOException("Could not delete file: " + file.getCanonicalPath()); } } - - /** - * Creates RrdDef object from its XML string equivalent. The format of the input string is - * the same as the format described in - * {@link #createRrdDefFromXmlFile createRrdDefFromXmlFile} method. - * @param xmlString XML formatted string containing complete RRD definition - * @return RrdDef object which can be used to create new RrdDb object - * @throws RrdException thrown in case of bad XML format or bad RRD definition parameters - * @throws IOException thrown in case of I/O error - */ - public RrdDef createRrdDefFromXmlString(String xmlString) throws RrdException, IOException { - return createRrdDefFromXmlSource(new InputSource(new StringReader(xmlString))); - } - - /** - * Creates RrdDef object from the file containing its XML equivalent. Here is an example - * of a properly formatted XML file:</p> - * <pre><code> - * <rrd_def> - * <path>test.rrd</path> - * <!-- not mandatory --> - * <start>1000123123</start> - * <!-- not mandatory --> - * <step>150</step> - * <!-- at least one datasource must be supplied --> - * <datasource> - * <name>input</name> - * <type>COUNTER</type> - * <heartbeat>300</heartbeat> - * <min>0</min> - * <max>U</max> - * </datasource> - * <datasource> - * <name>temperature</name> - * <type>GAUGE</type> - * <heartbeat>400</heartbeat> - * <min>U</min> - * <max>1000</max> - * </datasource> - * <!-- at least one archive must be supplied --> - * <archive> - * <cf>AVERAGE</cf> - * <xff>0.5</xff> - * <steps>1</steps> - * <rows>700</rows> - * </archive> - * <archive> - * <cf>MAX</cf> - * <xff>0.6</xff> - * <steps>6</steps> - * <rows>7000</rows> - * </archive> - * </rrd_def> - * </code></pre> - * - * @param filepath path containing XML formatted RRD definition (like the one given above) - * @return RrdDef object which can be used to create new RrdDb object - * @throws RrdException thrown in case of bad XML format or bad RRD definition parameters - * @throws IOException thrown in case of I/O error - */ - public RrdDef createRrdDefFromXmlFile(String filepath) throws RrdException, IOException { - FileReader fileReader = null; - try { - fileReader = new FileReader(filepath); - return createRrdDefFromXmlSource(new InputSource(fileReader)); - } - finally { - if(fileReader != null) { - fileReader.close(); - } - } - } - - /** - * Creates RrdDef object from any parsable XML source. The format of the underlying input - * source data must conform to the format described for the - * {@link #createRrdDefFromXmlFile createRrdDefFromXmlFile} method. - * @param inputSource parsable XML source containing complete RRD definition - * @return RrdDef object which can be used to create new RrdDb object - * @throws RrdException thrown in case of bad XML format or bad RRD definition parameters - * @throws IOException thrown in case of I/O error - */ - public RrdDef createRrdDefFromXmlSource(InputSource inputSource) throws RrdException, IOException { - try { - Element root = XmlReader.getRootElement(inputSource); - // must start with <rrd_def> - if(!root.getTagName().equals("rrd_def")) { - throw new RrdException("XML definition must start with <rrd_def>"); - } - // PATH must be supplied or exception is thrown - String path = XmlReader.getChildValue(root, "path"); - RrdDef rrdDef = new RrdDef(path); - try { - // START is not mandatory - long start = XmlReader.getChildValueAsLong(root, "start"); - rrdDef.setStartTime(start); - } - catch(RrdException e) { } - try { - // STEP is not mandatory - long step = XmlReader.getChildValueAsLong(root, "step"); - rrdDef.setStep(step); - } - catch(RrdException e) { - // NOP - } - // datsources - Node[] dsNodes = XmlReader.getChildNodes(root, "datasource"); - for(int i = 0; i < dsNodes.length; i++) { - String name = XmlReader.getChildValue(dsNodes[i], "name"); - String type = XmlReader.getChildValue(dsNodes[i], "type"); - long heartbeat = XmlReader.getChildValueAsLong(dsNodes[i], "heartbeat"); - double min = XmlReader.getChildValueAsDouble(dsNodes[i], "min"); - double max = XmlReader.getChildValueAsDouble(dsNodes[i], "max"); - rrdDef.addDatasource(name, type, heartbeat, min, max); - } - // archives - Node[] arcNodes = XmlReader.getChildNodes(root, "archive"); - for(int i = 0; i < arcNodes.length; i++) { - String consolFun = XmlReader.getChildValue(arcNodes[i], "cf"); - double xff = XmlReader.getChildValueAsDouble(arcNodes[i], "xff"); - int steps = XmlReader.getChildValueAsInt(arcNodes[i], "steps"); - int rows = XmlReader.getChildValueAsInt(arcNodes[i], "rows"); - rrdDef.addArchive(consolFun, xff, steps, rows); - } - return rrdDef; - } catch (FactoryConfigurationError e) { - throw new RrdException("XML error: " + e); - } catch (ParserConfigurationException e) { - throw new RrdException("XML error: " + e); - } catch (SAXException e) { - throw new RrdException("XML error: " + e); - } catch(NumberFormatException e) { - throw new RrdException("XML error: " + e); - } - } - - /* - public static void main(String[] args) throws RrdException, IOException { - String s = - "<rrd_def> " + - " <path>test.rrd</path> " + - " <start>1000000000</start> " + - " <step>151</step> " + - " <datasource> " + - " <name>input</name> " + - " <type>COUNTER</type> " + - " <heartbeat>300</heartbeat> " + - " <min>10</min> " + - " <max>U</max> " + - " </datasource> " + - " <datasource> " + - " <name>temperature</name> " + - " <type>GAUGE</type> " + - " <heartbeat>400</heartbeat> " + - " <min>U</min> " + - " <max>1000</max> " + - " </datasource> " + - " <archive> " + - " <cf>AVERAGE</cf> " + - " <xff>0.5</xff> " + - " <steps>1</steps> " + - " <rows>700</rows> " + - " </archive> " + - " <archive> " + - " <cf>MAX</cf> " + - " <xff>0.6</xff> " + - " <steps>6</steps> " + - " <rows>7000</rows> " + - " </archive> " + - "</rrd_def> "; - RrdDef def = RrdToolkit.getInstance().createRrdDefFromXmlString(s); - RrdDb db = new RrdDb(def); - db.close(); - } - */ } --- 412,419 ---- private static void deleteFile(File file) throws IOException { ! if (file.exists() && !file.delete()) { throw new IOException("Could not delete file: " + file.getCanonicalPath()); } } } Index: Util.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/core/Util.java,v retrieving revision 1.8 retrieving revision 1.9 diff -C2 -d -r1.8 -r1.9 *** Util.java 19 Feb 2004 13:05:04 -0000 1.8 --- Util.java 23 Feb 2004 15:48:42 -0000 1.9 *************** *** 26,29 **** --- 26,39 ---- package org.jrobin.core; + import org.w3c.dom.Node; + import org.w3c.dom.NodeList; + import org.w3c.dom.Element; + import org.w3c.dom.Document; + import org.xml.sax.InputSource; + import org.xml.sax.SAXException; + + import javax.xml.parsers.ParserConfigurationException; + import javax.xml.parsers.DocumentBuilderFactory; + import javax.xml.parsers.DocumentBuilder; import java.text.DecimalFormat; import java.text.NumberFormat; *************** *** 31,36 **** import java.util.Locale; import java.util.GregorianCalendar; ! import java.io.File; ! import java.io.IOException; /** --- 41,46 ---- import java.util.Locale; import java.util.GregorianCalendar; ! import java.util.ArrayList; ! import java.io.*; /** *************** *** 245,248 **** --- 255,338 ---- return File.createTempFile("JROBIN_", ".tmp").getCanonicalPath(); } + + static class Xml { + static Node[] getChildNodes(Node parentNode, String childName) { + ArrayList nodes = new ArrayList(); + NodeList nodeList = parentNode.getChildNodes(); + for (int i = 0; i < nodeList.getLength(); i++) { + Node node = nodeList.item(i); + if (node.getNodeName().equals(childName)) { + nodes.add(node); + } + } + return (Node[]) nodes.toArray(new Node[0]); + } + + static Node getFirstChildNode(Node parentNode, String childName) throws RrdException { + Node[] childs = getChildNodes(parentNode, childName); + if (childs.length > 0) { + return childs[0]; + } + throw new RrdException("XML Error, no such child: " + childName); + } + + static String getChildValue(Node parentNode, String childName) throws RrdException { + NodeList children = parentNode.getChildNodes(); + for (int i = 0; i < children.getLength(); i++) { + Node child = children.item(i); + if (child.getNodeName().equals(childName)) { + return child.getFirstChild().getNodeValue().trim(); + } + } + throw new RrdException("XML Error, no such child: " + childName); + } + + static int getChildValueAsInt(Node parentNode, String childName) throws RrdException { + String valueStr = getChildValue(parentNode, childName); + return Integer.parseInt(valueStr); + } + + static long getChildValueAsLong(Node parentNode, String childName) throws RrdException { + String valueStr = getChildValue(parentNode, childName); + return Long.parseLong(valueStr); + } + + static double getChildValueAsDouble(Node parentNode, String childName) throws RrdException { + String valueStr = getChildValue(parentNode, childName); + return Util.parseDouble(valueStr); + } + + static Element getRootElement(InputSource inputSource) throws RrdException, IOException { + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + factory.setValidating(false); + factory.setNamespaceAware(false); + try { + DocumentBuilder builder = factory.newDocumentBuilder(); + Document doc = builder.parse(inputSource); + return doc.getDocumentElement(); + } catch (ParserConfigurationException e) { + throw new RrdException(e); + } catch (SAXException e) { + throw new RrdException(e); + } + } + + static Element getRootElement(String xmlString) throws RrdException, IOException { + return getRootElement(new InputSource(new StringReader(xmlString))); + } + + static Element getRootElement(File xmlFile) throws RrdException, IOException { + Reader reader = null; + try { + reader = new FileReader(xmlFile); + return getRootElement(new InputSource(reader)); + } + finally { + if(reader != null) { + reader.close(); + } + } + } + } } Index: XmlReader.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/core/XmlReader.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** XmlReader.java 19 Feb 2004 10:48:12 -0000 1.3 --- XmlReader.java 23 Feb 2004 15:48:42 -0000 1.4 *************** *** 26,40 **** package org.jrobin.core; - import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; - import org.w3c.dom.NodeList; - import org.xml.sax.SAXException; - import org.xml.sax.InputSource; - - import javax.xml.parsers.*; import java.io.IOException; ! import java.io.FileReader; ! import java.util.ArrayList; class XmlReader { --- 26,33 ---- package org.jrobin.core; import org.w3c.dom.Element; import org.w3c.dom.Node; import java.io.IOException; ! import java.io.File; class XmlReader { *************** *** 44,78 **** XmlReader(String xmlFilePath) throws IOException, RrdException { ! FileReader fileReader = null; ! try { ! fileReader = new FileReader(xmlFilePath); ! InputSource source = new InputSource(fileReader); ! root = getRootElement(source); ! dsNodes = getChildNodes(root, "ds"); ! arcNodes = getChildNodes(root, "rra"); ! } catch (FactoryConfigurationError e) { ! throw new RrdException("XML error: " + e); ! } catch (ParserConfigurationException e) { ! throw new RrdException("XML error: " + e); ! } catch (SAXException e) { ! throw new RrdException("XML error: " + e); ! } ! finally { ! if(fileReader != null) { ! fileReader.close(); ! } ! } } String getVersion() throws RrdException { ! return getChildValue(root, "version"); } long getLastUpdateTime() throws RrdException { ! return getChildValueAsLong(root, "lastupdate"); } long getStep() throws RrdException { ! return getChildValueAsLong(root, "step"); } --- 37,55 ---- XmlReader(String xmlFilePath) throws IOException, RrdException { ! root = Util.Xml.getRootElement(new File(xmlFilePath)); ! dsNodes = Util.Xml.getChildNodes(root, "ds"); ! arcNodes = Util.Xml.getChildNodes(root, "rra"); } String getVersion() throws RrdException { ! return Util.Xml.getChildValue(root, "version"); } long getLastUpdateTime() throws RrdException { ! return Util.Xml.getChildValueAsLong(root, "lastupdate"); } long getStep() throws RrdException { ! return Util.Xml.getChildValueAsLong(root, "step"); } *************** *** 86,156 **** String getDsName(int dsIndex) throws RrdException { ! return getChildValue(dsNodes[dsIndex], "name"); } String getDsType(int dsIndex) throws RrdException { ! return getChildValue(dsNodes[dsIndex], "type"); } long getHeartbeat(int dsIndex) throws RrdException { ! return getChildValueAsLong(dsNodes[dsIndex], "minimal_heartbeat"); } double getMinValue(int dsIndex) throws RrdException { ! return getChildValueAsDouble(dsNodes[dsIndex], "min"); } double getMaxValue(int dsIndex) throws RrdException { ! return getChildValueAsDouble(dsNodes[dsIndex], "max"); } double getLastValue(int dsIndex) throws RrdException { ! return getChildValueAsDouble(dsNodes[dsIndex], "last_ds"); } double getAccumValue(int dsIndex) throws RrdException { ! return getChildValueAsDouble(dsNodes[dsIndex], "value"); } long getNanSeconds(int dsIndex) throws RrdException { ! return getChildValueAsLong(dsNodes[dsIndex], "unknown_sec"); } String getConsolFun(int arcIndex) throws RrdException { ! return getChildValue(arcNodes[arcIndex], "cf"); } double getXff(int arcIndex) throws RrdException { ! return getChildValueAsDouble(arcNodes[arcIndex], "xff"); } int getSteps(int arcIndex) throws RrdException { ! return getChildValueAsInt(arcNodes[arcIndex], "pdp_per_row"); } double getStateAccumValue(int arcIndex, int dsIndex) throws RrdException { ! Node cdpNode = getFirstChildNode(arcNodes[arcIndex], "cdp_prep"); ! Node[] dsNodes = getChildNodes(cdpNode, "ds"); ! return getChildValueAsDouble(dsNodes[dsIndex], "value"); } int getStateNanSteps(int arcIndex, int dsIndex) throws RrdException { ! Node cdpNode = getFirstChildNode(arcNodes[arcIndex], "cdp_prep"); ! Node[] dsNodes = getChildNodes(cdpNode, "ds"); ! return getChildValueAsInt(dsNodes[dsIndex], "unknown_datapoints"); } int getRows(int arcIndex) throws RrdException { ! Node dbNode = getFirstChildNode(arcNodes[arcIndex], "database"); ! Node[] rows = getChildNodes(dbNode, "row"); return rows.length; } double[] getValues(int arcIndex, int dsIndex) throws RrdException { ! Node dbNode = getFirstChildNode(arcNodes[arcIndex], "database"); ! Node[] rows = getChildNodes(dbNode, "row"); double[] values = new double[rows.length]; for(int i = 0; i < rows.length; i++) { ! Node[] vNodes = getChildNodes(rows[i], "v"); Node vNode = vNodes[dsIndex]; values[i] = Util.parseDouble(vNode.getFirstChild().getNodeValue().trim()); --- 63,133 ---- String getDsName(int dsIndex) throws RrdException { ! return Util.Xml.getChildValue(dsNodes[dsIndex], "name"); } String getDsType(int dsIndex) throws RrdException { ! return Util.Xml.getChildValue(dsNodes[dsIndex], "type"); } long getHeartbeat(int dsIndex) throws RrdException { ! return Util.Xml.getChildValueAsLong(dsNodes[dsIndex], "minimal_heartbeat"); } double getMinValue(int dsIndex) throws RrdException { ! return Util.Xml.getChildValueAsDouble(dsNodes[dsIndex], "min"); } double getMaxValue(int dsIndex) throws RrdException { ! return Util.Xml.getChildValueAsDouble(dsNodes[dsIndex], "max"); } double getLastValue(int dsIndex) throws RrdException { ! return Util.Xml.getChildValueAsDouble(dsNodes[dsIndex], "last_ds"); } double getAccumValue(int dsIndex) throws RrdException { ! return Util.Xml.getChildValueAsDouble(dsNodes[dsIndex], "value"); } long getNanSeconds(int dsIndex) throws RrdException { ! return Util.Xml.getChildValueAsLong(dsNodes[dsIndex], "unknown_sec"); } String getConsolFun(int arcIndex) throws RrdException { ! return Util.Xml.getChildValue(arcNodes[arcIndex], "cf"); } double getXff(int arcIndex) throws RrdException { ! return Util.Xml.getChildValueAsDouble(arcNodes[arcIndex], "xff"); } int getSteps(int arcIndex) throws RrdException { ! return Util.Xml.getChildValueAsInt(arcNodes[arcIndex], "pdp_per_row"); } double getStateAccumValue(int arcIndex, int dsIndex) throws RrdException { ! Node cdpNode = Util.Xml.getFirstChildNode(arcNodes[arcIndex], "cdp_prep"); ! Node[] dsNodes = Util.Xml.getChildNodes(cdpNode, "ds"); ! return Util.Xml.getChildValueAsDouble(dsNodes[dsIndex], "value"); } int getStateNanSteps(int arcIndex, int dsIndex) throws RrdException { ! Node cdpNode = Util.Xml.getFirstChildNode(arcNodes[arcIndex], "cdp_prep"); ! Node[] dsNodes = Util.Xml.getChildNodes(cdpNode, "ds"); ! return Util.Xml.getChildValueAsInt(dsNodes[dsIndex], "unknown_datapoints"); } int getRows(int arcIndex) throws RrdException { ! Node dbNode = Util.Xml.getFirstChildNode(arcNodes[arcIndex], "database"); ! Node[] rows = Util.Xml.getChildNodes(dbNode, "row"); return rows.length; } double[] getValues(int arcIndex, int dsIndex) throws RrdException { ! Node dbNode = Util.Xml.getFirstChildNode(arcNodes[arcIndex], "database"); ! Node[] rows = Util.Xml.getChildNodes(dbNode, "row"); double[] values = new double[rows.length]; for(int i = 0; i < rows.length; i++) { ! Node[] vNodes = Util.Xml.getChildNodes(rows[i], "v"); Node vNode = vNodes[dsIndex]; values[i] = Util.parseDouble(vNode.getFirstChild().getNodeValue().trim()); *************** *** 159,218 **** } - // utility functions for DOM tree traversing - - static Node[] getChildNodes(Node parentNode, String childName) { - ArrayList nodes = new ArrayList(); - NodeList nodeList = parentNode.getChildNodes(); - for(int i = 0; i < nodeList.getLength(); i++) { - Node node = nodeList.item(i); - if(node.getNodeName().equals(childName)) { - nodes.add(node); - } - } - return (Node[]) nodes.toArray(new Node[0]); - } - - static Node getFirstChildNode(Node parentNode, String childName) throws RrdException { - Node[] childs = getChildNodes(parentNode, childName); - if(childs.length > 0) { - return childs[0]; - } - throw new RrdException("XML Error, no such child: " + childName); - } - - static String getChildValue(Node parentNode, String childName) throws RrdException { - NodeList children = parentNode.getChildNodes(); - for(int i = 0; i < children.getLength(); i++) { - Node child = children.item(i); - if(child.getNodeName().equals(childName)) { - return child.getFirstChild().getNodeValue().trim(); - } - } - throw new RrdException("XML Error, no such child: " + childName); - } - - static int getChildValueAsInt(Node parentNode, String childName) throws RrdException { - String valueStr = getChildValue(parentNode, childName); - return Integer.parseInt(valueStr); - } - - static long getChildValueAsLong(Node parentNode, String childName) throws RrdException { - String valueStr = getChildValue(parentNode, childName); - return Long.parseLong(valueStr); - } - - static double getChildValueAsDouble(Node parentNode, String childName) throws RrdException { - String valueStr = getChildValue(parentNode, childName); - return Util.parseDouble(valueStr); - } - - static Element getRootElement(InputSource inputSource) - throws ParserConfigurationException, IOException, SAXException { - DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); - factory.setValidating(false); - factory.setNamespaceAware(false); - DocumentBuilder builder = factory.newDocumentBuilder(); - Document doc = builder.parse(inputSource); - return doc.getDocumentElement(); - } } \ No newline at end of file --- 136,138 ---- |