KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jrobin > core > XmlReader


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
26 package org.jrobin.core;
27
28 import org.w3c.dom.Element JavaDoc;
29 import org.w3c.dom.Node JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.io.File JavaDoc;
32
33 class XmlReader extends DataImporter {
34
35     private Element JavaDoc root;
36     private Node JavaDoc[] dsNodes, arcNodes;
37
38     XmlReader(String JavaDoc xmlFilePath) throws IOException JavaDoc, RrdException {
39         root = Util.Xml.getRootElement(new File JavaDoc(xmlFilePath));
40         dsNodes = Util.Xml.getChildNodes(root, "ds");
41         arcNodes = Util.Xml.getChildNodes(root, "rra");
42     }
43
44     String JavaDoc getVersion() throws RrdException {
45         return Util.Xml.getChildValue(root, "version");
46     }
47
48     long getLastUpdateTime() throws RrdException {
49         return Util.Xml.getChildValueAsLong(root, "lastupdate");
50     }
51
52     long getStep() throws RrdException {
53         return Util.Xml.getChildValueAsLong(root, "step");
54     }
55
56     int getDsCount() {
57         return dsNodes.length;
58     }
59
60     int getArcCount() {
61         return arcNodes.length;
62     }
63
64     String JavaDoc getDsName(int dsIndex) throws RrdException {
65         return Util.Xml.getChildValue(dsNodes[dsIndex], "name");
66     }
67
68     String JavaDoc getDsType(int dsIndex) throws RrdException {
69         return Util.Xml.getChildValue(dsNodes[dsIndex], "type");
70     }
71
72     long getHeartbeat(int dsIndex) throws RrdException {
73         return Util.Xml.getChildValueAsLong(dsNodes[dsIndex], "minimal_heartbeat");
74     }
75
76     double getMinValue(int dsIndex) throws RrdException {
77         return Util.Xml.getChildValueAsDouble(dsNodes[dsIndex], "min");
78     }
79
80     double getMaxValue(int dsIndex) throws RrdException {
81         return Util.Xml.getChildValueAsDouble(dsNodes[dsIndex], "max");
82     }
83
84     double getLastValue(int dsIndex) throws RrdException {
85         return Util.Xml.getChildValueAsDouble(dsNodes[dsIndex], "last_ds");
86     }
87
88     double getAccumValue(int dsIndex) throws RrdException {
89         return Util.Xml.getChildValueAsDouble(dsNodes[dsIndex], "value");
90     }
91
92     long getNanSeconds(int dsIndex) throws RrdException {
93         return Util.Xml.getChildValueAsLong(dsNodes[dsIndex], "unknown_sec");
94     }
95
96     String JavaDoc getConsolFun(int arcIndex) throws RrdException {
97         return Util.Xml.getChildValue(arcNodes[arcIndex], "cf");
98     }
99
100     double getXff(int arcIndex) throws RrdException {
101         return Util.Xml.getChildValueAsDouble(arcNodes[arcIndex], "xff");
102     }
103
104     int getSteps(int arcIndex) throws RrdException {
105         return Util.Xml.getChildValueAsInt(arcNodes[arcIndex], "pdp_per_row");
106     }
107
108     double getStateAccumValue(int arcIndex, int dsIndex) throws RrdException {
109         Node JavaDoc cdpNode = Util.Xml.getFirstChildNode(arcNodes[arcIndex], "cdp_prep");
110         Node JavaDoc[] dsNodes = Util.Xml.getChildNodes(cdpNode, "ds");
111         return Util.Xml.getChildValueAsDouble(dsNodes[dsIndex], "value");
112     }
113
114     int getStateNanSteps(int arcIndex, int dsIndex) throws RrdException {
115         Node JavaDoc cdpNode = Util.Xml.getFirstChildNode(arcNodes[arcIndex], "cdp_prep");
116         Node JavaDoc[] dsNodes = Util.Xml.getChildNodes(cdpNode, "ds");
117         return Util.Xml.getChildValueAsInt(dsNodes[dsIndex], "unknown_datapoints");
118     }
119
120     int getRows(int arcIndex) throws RrdException {
121         Node JavaDoc dbNode = Util.Xml.getFirstChildNode(arcNodes[arcIndex], "database");
122         Node JavaDoc[] rows = Util.Xml.getChildNodes(dbNode, "row");
123         return rows.length;
124     }
125
126     double[] getValues(int arcIndex, int dsIndex) throws RrdException {
127         Node JavaDoc dbNode = Util.Xml.getFirstChildNode(arcNodes[arcIndex], "database");
128         Node JavaDoc[] rows = Util.Xml.getChildNodes(dbNode, "row");
129         double[] values = new double[rows.length];
130         for(int i = 0; i < rows.length; i++) {
131             Node JavaDoc[] vNodes = Util.Xml.getChildNodes(rows[i], "v");
132             Node JavaDoc vNode = vNodes[dsIndex];
133             values[i] = Util.parseDouble(vNode.getFirstChild().getNodeValue().trim());
134         }
135         return values;
136     }
137 }
Popular Tags