Update of /cvsroot/jrobin/src/jrobin/core In directory sc8-pr-cvs1:/tmp/cvs-serv26350/jrobin/core Modified Files: ArcState.java Archive.java Datasource.java Header.java RrdDb.java Util.java Added Files: XmlWriter.java Log Message: Removed DOM model for XML export --- NEW FILE: XmlWriter.java --- /* ============================================================ * JRobin : Pure java implementation of RRDTool's functionality * ============================================================ * * Project Info: http://www.sourceforge.net/projects/jrobin * Project Lead: Sasa Markovic (sa...@eu...); * * (C) Copyright 2003, by Sasa Markovic. * * This library is free software; you can redistribute it and/or modify it under the terms * of the GNU Lesser General Public License as published by the Free Software Foundation; * either version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License along with this * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, * Boston, MA 02111-1307, USA. */ package jrobin.core; import java.io.OutputStream; import java.io.PrintWriter; import java.util.ArrayList; import java.text.DecimalFormat; class XmlWriter { static final DecimalFormat df = new DecimalFormat("0.0000000000E00"); static final String INDENT_STR = " "; private PrintWriter writer; private StringBuffer indent = new StringBuffer(""); private Stack openTags = new Stack(); XmlWriter(OutputStream stream) { writer = new PrintWriter(stream); } void startTag(String tag) { writer.println(indent + "<" + tag + ">"); openTags.push(tag); indent.append(INDENT_STR); } void closeTag() { String tag = openTags.pop(); indent.setLength(indent.length() - INDENT_STR.length()); writer.println(indent + "</" + tag + ">"); } void writeTag(String tag, Object value) { writer.println(indent + "<" + tag + ">" + value + "</" + tag + ">"); } void writeTag(String tag, int value) { writeTag(tag, "" + value); } void writeTag(String tag, long value) { writeTag(tag, "" + value); } void writeTag(String tag, double value, String nanString) { if(Double.isNaN(value)) { writeTag(tag, nanString); } else { writeTag(tag, df.format(value)); } } void writeTag(String tag, double value) { writeTag(tag, value, "" + Double.NaN); } void finish() { writer.flush(); } protected void finalize() { writer.close(); } void writeComment(Object comment) { writer.println(indent + "<!-- " + comment + " -->"); } private class Stack { private ArrayList stack = new ArrayList(); void push(String tag) { stack.add(tag); } String pop() { int last = stack.size() - 1; if(last >= 0) { String tag = (String) stack.get(last); stack.remove(last); return tag; } return null; } } } Index: ArcState.java =================================================================== RCS file: /cvsroot/jrobin/src/jrobin/core/ArcState.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** ArcState.java 4 Sep 2003 13:26:41 -0000 1.1 --- ArcState.java 24 Sep 2003 11:32:24 -0000 1.2 *************** *** 23,29 **** package jrobin.core; - import org.w3c.dom.Document; - import org.w3c.dom.Element; - import java.io.IOException; --- 23,26 ---- *************** *** 79,92 **** } ! void appendXml(Element parent) throws IOException { ! Document doc = parent.getOwnerDocument(); ! Element dsElem = doc.createElement("ds"); ! Element valueElem = doc.createElement("value"); ! valueElem.appendChild(doc.createTextNode(Util.formatDoubleXml(accumValue.get()))); ! Element unknownElem = doc.createElement("unknown_datapoints"); ! unknownElem.appendChild(doc.createTextNode("" + nanSteps.get())); ! parent.appendChild(dsElem); ! dsElem.appendChild(valueElem); ! dsElem.appendChild(unknownElem); } --- 76,84 ---- } ! void appendXml(XmlWriter writer) throws IOException { ! writer.startTag("ds"); ! writer.writeTag("value", accumValue.get()); ! writer.writeTag("unknown_datapoints", nanSteps.get()); ! writer.closeTag(); // ds } Index: Archive.java =================================================================== RCS file: /cvsroot/jrobin/src/jrobin/core/Archive.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** Archive.java 4 Sep 2003 13:26:41 -0000 1.1 --- Archive.java 24 Sep 2003 11:32:24 -0000 1.2 *************** *** 23,30 **** package jrobin.core; - import org.w3c.dom.Document; - import org.w3c.dom.Element; - import org.w3c.dom.Node; - import java.io.IOException; --- 23,26 ---- *************** *** 246,288 **** } ! void appendXml(Element parent) throws IOException { ! Document doc = parent.getOwnerDocument(); ! Element rraElem = doc.createElement("rra"); ! Element cfElem = doc.createElement("cf"); ! cfElem.appendChild(doc.createTextNode(consolFun.get())); ! long arcStep = getArcStep(); ! Node pdpComment = doc.createComment(arcStep + " seconds"); ! Element pdpElem = doc.createElement("pdp_per_row"); ! pdpElem.appendChild(doc.createTextNode("" + steps.get())); ! Element xffElem = doc.createElement("xff"); ! xffElem.appendChild(doc.createTextNode("" + Util.formatDoubleXml(xff.get()))); ! // dump state ! Element cdpElem = doc.createElement("cdp_prep"); for(int i = 0; i < states.length; i++) { ! states[i].appendXml(cdpElem); } ! // put icing on the cake: dump database ! Element dbElem = doc.createElement("database"); long startTime = getStartTime(); for(int i = 0; i < rows.get(); i++) { ! long time = startTime + i * arcStep; ! Node rowComment = doc.createComment(Util.getDate(time) + " / " + time); ! dbElem.appendChild(rowComment); ! Element rowElem = doc.createElement("row"); for(int j = 0; j < robins.length; j++) { ! Element vElem = doc.createElement("v"); ! vElem.appendChild(doc.createTextNode(Util.formatDoubleXml(robins[j].getValue(i)))); ! rowElem.appendChild(vElem); } ! dbElem.appendChild(rowElem); } ! // compose everything ! parent.appendChild(rraElem); ! rraElem.appendChild(cfElem); ! rraElem.appendChild(pdpComment); ! rraElem.appendChild(pdpElem); ! rraElem.appendChild(xffElem); ! rraElem.appendChild(cdpElem); ! rraElem.appendChild(dbElem); } --- 242,269 ---- } ! void appendXml(XmlWriter writer) throws IOException { ! writer.startTag("rra"); ! writer.writeTag("cf", consolFun.get()); ! writer.writeComment(getArcStep() + " seconds"); ! writer.writeTag("pdp_per_row", steps.get()); ! writer.writeTag("xff", xff.get()); ! writer.startTag("cdp_prep"); for(int i = 0; i < states.length; i++) { ! states[i].appendXml(writer); } ! writer.closeTag(); // cdp_prep ! writer.startTag("database"); long startTime = getStartTime(); for(int i = 0; i < rows.get(); i++) { ! long time = startTime + i * getArcStep(); ! writer.writeComment(Util.getDate(time) + " / " + time); ! writer.startTag("row"); for(int j = 0; j < robins.length; j++) { ! writer.writeTag("v", robins[j].getValue(i)); } ! writer.closeTag(); // row } ! writer.closeTag(); // database ! writer.closeTag(); // rra } Index: Datasource.java =================================================================== RCS file: /cvsroot/jrobin/src/jrobin/core/Datasource.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** Datasource.java 4 Sep 2003 13:26:41 -0000 1.1 --- Datasource.java 24 Sep 2003 11:32:24 -0000 1.2 *************** *** 23,30 **** package jrobin.core; - import org.w3c.dom.Document; - import org.w3c.dom.Element; - import org.w3c.dom.Node; - import java.io.IOException; --- 23,26 ---- *************** *** 207,241 **** } ! void appendXml(Element parent) throws IOException { ! Document doc = parent.getOwnerDocument(); ! Element dsElem = doc.createElement("ds"); ! Element nameElem = doc.createElement("name"); ! nameElem.appendChild(doc.createTextNode(dsName.get())); ! Element typeElem = doc.createElement("type"); ! typeElem.appendChild(doc.createTextNode(dsType.get())); ! Element hearbeatElem = doc.createElement("minimal_heartbeat"); ! hearbeatElem.appendChild(doc.createTextNode("" + heartbeat.get())); ! Element minElem = doc.createElement("min"); ! minElem.appendChild(doc.createTextNode(Util.formatDoubleXml(minValue.get()))); ! Element maxElem = doc.createElement("max"); ! maxElem.appendChild(doc.createTextNode(Util.formatDoubleXml(maxValue.get()))); ! Node stateComment = doc.createComment("PDP Status"); ! Element lastDsElem = doc.createElement("last_ds"); ! lastDsElem.appendChild(doc.createTextNode("" + Util.formatDoubleXml(lastValue.get()))); ! Element valueElem = doc.createElement("value"); ! valueElem.appendChild(doc.createTextNode("" + Util.formatDoubleXml(accumValue.get()))); ! Element unknownElem = doc.createElement("unknown_sec"); ! unknownElem.appendChild(doc.createTextNode("" + nanSeconds.get())); ! // compose ! parent.appendChild(dsElem); ! dsElem.appendChild(nameElem); ! dsElem.appendChild(typeElem); ! dsElem.appendChild(hearbeatElem); ! dsElem.appendChild(minElem); ! dsElem.appendChild(maxElem); ! dsElem.appendChild(stateComment); ! dsElem.appendChild(lastDsElem); ! dsElem.appendChild(valueElem); ! dsElem.appendChild(unknownElem); } --- 203,218 ---- } ! void appendXml(XmlWriter writer) throws IOException { ! writer.startTag("ds"); ! writer.writeTag("name", dsName.get()); ! writer.writeTag("type", dsType.get()); ! writer.writeTag("minimal_heartbeat", heartbeat.get()); ! writer.writeTag("min", minValue.get()); ! writer.writeTag("max", maxValue.get()); ! writer.writeComment("PDP Status"); ! writer.writeTag("last_ds", lastValue.get(), "UNKN"); ! writer.writeTag("value", accumValue.get()); ! writer.writeTag("unknown_sec", nanSeconds.get()); ! writer.closeTag(); // ds } Index: Header.java =================================================================== RCS file: /cvsroot/jrobin/src/jrobin/core/Header.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** Header.java 4 Sep 2003 13:26:41 -0000 1.1 --- Header.java 24 Sep 2003 11:32:24 -0000 1.2 *************** *** 23,30 **** package jrobin.core; - import org.w3c.dom.Document; - import org.w3c.dom.Element; - import org.w3c.dom.Node; - import java.io.IOException; --- 23,26 ---- *************** *** 111,129 **** } ! void appendXml(Element parent) throws IOException { ! Document doc = parent.getOwnerDocument(); ! Element versionElem = doc.createElement("version"); ! versionElem.appendChild(doc.createTextNode(RRDTOOL_VERSION)); ! Element stepElem = doc.createElement("step"); ! stepElem.appendChild(doc.createTextNode("" + step.get())); ! Node stepComment = doc.createComment("Seconds"); ! Element lastElem = doc.createElement("lastupdate"); ! lastElem.appendChild(doc.createTextNode("" + lastUpdateTime.get())); ! Node lastComment = doc.createComment("" + Util.getDate(lastUpdateTime.get())); ! parent.appendChild(versionElem); ! parent.appendChild(stepComment); ! parent.appendChild(stepElem); ! parent.appendChild(lastComment); ! parent.appendChild(lastElem); } --- 107,116 ---- } ! void appendXml(XmlWriter writer) throws IOException { ! writer.writeTag("version", RRDTOOL_VERSION); ! writer.writeComment("Seconds"); ! writer.writeTag("step", step.get()); ! writer.writeComment(Util.getDate(lastUpdateTime.get())); ! writer.writeTag("lastupdate", lastUpdateTime.get()); } Index: RrdDb.java =================================================================== RCS file: /cvsroot/jrobin/src/jrobin/core/RrdDb.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** RrdDb.java 23 Sep 2003 13:39:20 -0000 1.3 --- RrdDb.java 24 Sep 2003 11:32:24 -0000 1.4 *************** *** 23,36 **** package jrobin.core; - import org.w3c.dom.DOMException; - import org.w3c.dom.Document; - import org.w3c.dom.Element; - - import javax.xml.parsers.DocumentBuilder; - import javax.xml.parsers.DocumentBuilderFactory; - import javax.xml.parsers.ParserConfigurationException; - import javax.xml.transform.*; - import javax.xml.transform.dom.DOMSource; - import javax.xml.transform.stream.StreamResult; import java.io.*; --- 23,26 ---- *************** *** 470,519 **** * @param destination Output stream to receive XML data * @throws IOException Thrown in case of I/O related error - * @throws RrdException Thrown in case of JRobin specific error */ ! public synchronized void dumpXml(OutputStream destination) throws IOException, RrdException { ! // create XML document ! DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); ! factory.setValidating(false); ! factory.setNamespaceAware(false); ! try { ! DocumentBuilder builder = factory.newDocumentBuilder(); ! Document doc = builder.newDocument(); ! // create root element ! Element root = doc.createElement("rrd"); ! doc.appendChild(root); ! // dump header ! header.appendXml(root); ! // dump datasources ! for(int i = 0; i < datasources.length; i++) { ! datasources[i].appendXml(root); ! } ! // dump archives ! for(int i = 0; i < archives.length; i++) { ! archives[i].appendXml(root); ! } ! // serialize DOM object ! TransformerFactory tFactory = TransformerFactory.newInstance(); ! Transformer transformer = tFactory.newTransformer(); ! transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); ! transformer.setOutputProperty(OutputKeys.METHOD, "xml"); ! transformer.setOutputProperty(OutputKeys.INDENT, "yes"); ! transformer.setOutputProperty(OutputKeys.MEDIA_TYPE, "text/xml"); ! transformer.setOutputProperty(OutputKeys.STANDALONE, "yes"); ! ! DOMSource source = new DOMSource(root); ! StreamResult result = new StreamResult(destination); ! transformer.transform(source, result); ! } catch (ParserConfigurationException e) { ! throw new RrdException("XML Error: " + e); ! } catch (DOMException e) { ! throw new RrdException("XML Error: " + e); ! } catch (TransformerFactoryConfigurationError e) { ! throw new RrdException("XML Error: " + e); ! } catch (TransformerException e) { ! throw new RrdException("XML Error: " + e); ! } catch (IllegalArgumentException e) { ! throw new RrdException("XML Error: " + e); } } --- 460,479 ---- * @param destination Output stream to receive XML data * @throws IOException Thrown in case of I/O related error */ ! public synchronized void dumpXml(OutputStream destination) throws IOException { ! XmlWriter writer = new XmlWriter(destination); ! writer.startTag("rrd"); ! // dump header ! header.appendXml(writer); ! // dump datasources ! for(int i = 0; i < datasources.length; i++) { ! datasources[i].appendXml(writer); ! } ! // dump archives ! for(int i = 0; i < archives.length; i++) { ! archives[i].appendXml(writer); } + writer.closeTag(); + writer.finish(); } *************** *** 553,557 **** public synchronized void dumpXml(String filename) throws IOException, RrdException { - // OutputStream destination = new BufferedOutputStream(new FileOutputStream(filename, false)); OutputStream destination = new BufferedOutputStream(new FileOutputStream(filename, false)); dumpXml(destination); --- 513,516 ---- Index: Util.java =================================================================== RCS file: /cvsroot/jrobin/src/jrobin/core/Util.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** Util.java 23 Sep 2003 08:31:34 -0000 1.2 --- Util.java 24 Sep 2003 11:32:24 -0000 1.3 *************** *** 94,104 **** } - static String formatDoubleXml(double x) { - if(Double.isNaN(x)) { - return "" + Double.NaN; - } - return df.format(x); - } - static void debug(String message) { if(RrdDb.DEBUG) { --- 94,97 ---- |