From: Arne V. <cob...@us...> - 2004-07-11 22:04:23
|
Update of /cvsroot/jrobin/src/org/jrobin/graph In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv7100/org/jrobin/graph Modified Files: ExportData.java Gprint.java RrdExportDef.java RrdExporter.java RrdGraphDef.java RrdGraphDefTemplate.java Sdef.java Source.java Added Files: Print.java RrdExportDefTemplate.java TimeText.java Log Message: JRobin 1.4.0 - Several tweaks - Added print() option to ExportData - Added time() option to RrdGraphDef - Added variables options to XmlTemplate - Added RrdExportDefTemplate --- NEW FILE: TimeText.java --- /* ============================================================ * JRobin : Pure java implementation of RRDTool's functionality * ============================================================ * * Project Info: http://www.jrobin.org * Project Lead: Sasa Markovic (sa...@jr...) * * Developers: Sasa Markovic (sa...@jr...) * Arne Vandamme (cob...@jr...) * * (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 org.jrobin.graph; import org.jrobin.core.RrdException; import org.jrobin.core.Util; import org.jrobin.core.XmlWriter; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.Calendar; /** * <p>Comment object containing a formatted timestamp (current time of timestamp given).</p> */ public class TimeText extends Comment { private static final String TIME_MARKER = "@t"; private Date textDate = null; private DateFormat dateFormat; private ArrayList parsedList; TimeText( String text, String pattern ) throws RrdException { this( text, new SimpleDateFormat( pattern ) ); } TimeText( String text, DateFormat dateFormat ) throws RrdException { super( text ); this.dateFormat = dateFormat; // Check if we can locate the placeholder for the timestamp (@t) if ( text.indexOf(TIME_MARKER) < 0 ) throw new RrdException( "Could not find where to place timestamp. No @t placeholder found."); } TimeText( String text, String pattern, long timestamp ) throws RrdException { this( text, new SimpleDateFormat( pattern ), new Date( timestamp * 1000 ) ); } TimeText( String text, DateFormat dateFormat, long timestamp ) throws RrdException { this( text, dateFormat, new Date( timestamp * 1000 ) ); } TimeText( String text, String pattern, Date date ) throws RrdException { this( text, new SimpleDateFormat( pattern ), date ); } TimeText( String text, DateFormat dateFormat, Date date ) throws RrdException { super( text ); this.textDate = date; this.dateFormat = dateFormat; // Check if we can locate the placeholder for the timestamp (@t) if ( text.indexOf(TIME_MARKER) < 0 ) throw new RrdException( "Could not find where to place timestamp. No @t placeholder found."); } TimeText( String text, String pattern, Calendar cal ) throws RrdException { this( text, new SimpleDateFormat( pattern ), cal.getTime() ); } TimeText( String text, DateFormat dateFormat, Calendar cal ) throws RrdException { this( text, dateFormat, cal.getTime() ); } ArrayList getTokens() { parsedList = new ArrayList( oList ); // Create time string String timeStr = dateFormat.format( (textDate != null ? textDate : new Date()) ); // Replace all values for (int i = 0; i < oList.size(); i += 2 ) { String str = (String) oList.get(i); str = str.replaceAll(TIME_MARKER, timeStr); parsedList.set( i, str ); } return parsedList; } void exportXmlTemplate(XmlWriter xml) { xml.startTag("time"); xml.writeTag( "format", text ); if ( dateFormat instanceof SimpleDateFormat ) xml.writeTag( "pattern", ((SimpleDateFormat) dateFormat).toPattern()); else xml.writeTag( "pattern", "" ); // A custom DateFormat can't be exported if ( textDate != null ) xml.writeTag( "value", Util.getTimestamp(textDate) ); xml.closeTag(); } } Index: Gprint.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/graph/Gprint.java,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** Gprint.java 9 Jun 2004 07:35:36 -0000 1.6 --- Gprint.java 11 Jul 2004 22:04:13 -0000 1.7 *************** *** 79,84 **** super.parseComment(); ! this.commentType = Comment.CMT_GPRINT; ! this.sourceName = sourceName; if ( consolFunc.equalsIgnoreCase("AVERAGE") || consolFunc.equalsIgnoreCase("AVG") ) --- 79,84 ---- super.parseComment(); ! this.commentType = Comment.CMT_GPRINT; ! this.sourceName = sourceName; if ( consolFunc.equalsIgnoreCase("AVERAGE") || consolFunc.equalsIgnoreCase("AVG") ) *************** *** 116,120 **** } - // ================================================================ // -- Protected methods --- 116,119 ---- *************** *** 138,142 **** // See if we need to use a specific value for the formatting double oldBase = vFormat.getBase(); ! if ( baseValue != -1 ) vFormat.setBase( baseValue ); --- 137,141 ---- // See if we need to use a specific value for the formatting double oldBase = vFormat.getBase(); ! if ( baseValue != -1 && baseValue != vFormat.getBase() ) vFormat.setBase( baseValue ); Index: Sdef.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/graph/Sdef.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** Sdef.java 9 Jul 2004 12:22:15 -0000 1.2 --- Sdef.java 11 Jul 2004 22:04:14 -0000 1.3 *************** *** 84,87 **** --- 84,88 ---- values = new double[ numPoints ]; this.aggregatePoints = aggregatePoints; + calculated = false; } Index: Source.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/graph/Source.java,v retrieving revision 1.10 retrieving revision 1.11 diff -C2 -d -r1.10 -r1.11 *** Source.java 10 Jul 2004 00:07:03 -0000 1.10 --- Source.java 11 Jul 2004 22:04:14 -0000 1.11 *************** *** 57,64 **** --- 57,66 ---- protected long step = 0; + private long lastPreciseTime = 0; // Last time requested private long lastTime = 0; private long totalTime = 0; private int stPos = 0; + private int lastStPos = 0; // Last value position requested // ================================================================ *************** *** 120,126 **** { // Return the next value if we fetched it before ! if ( !Double.isInfinite(nextValue) ) return nextValue; // Try to fetch the next value for ( int i = pos + 1; i < values.length; i++ ) --- 122,130 ---- { // Return the next value if we fetched it before ! if ( !Double.isInfinite(nextValue) && pos >= lastStPos ) return nextValue; + lastStPos = pos; + // Try to fetch the next value for ( int i = pos + 1; i < values.length; i++ ) *************** *** 142,145 **** --- 146,151 ---- nextValue = Double.POSITIVE_INFINITY; + lastStPos = pos; + return values[pos]; } *************** *** 147,152 **** double get( long preciseTime, long[] reducedTimestamps ) { ! long t = Util.normalize( preciseTime, step ); ! t = ( t < preciseTime ? t + step : t ); while ( stPos < reducedTimestamps.length - 1 ) --- 153,163 ---- double get( long preciseTime, long[] reducedTimestamps ) { ! long t = Util.normalize( preciseTime, step ); ! t = ( t < preciseTime ? t + step : t ); ! ! if ( preciseTime < lastPreciseTime ) // Backward fetching is weird, start over, we prolly in a new iteration ! stPos = 0; ! ! lastPreciseTime = preciseTime; while ( stPos < reducedTimestamps.length - 1 ) Index: RrdGraphDefTemplate.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/graph/RrdGraphDefTemplate.java,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** RrdGraphDefTemplate.java 30 May 2004 19:49:56 -0000 1.7 --- RrdGraphDefTemplate.java 11 Jul 2004 22:04:14 -0000 1.8 *************** *** 345,349 **** private void resolveGraphElements(Node graphNode) throws RrdException { validateTagsOnlyOnce(graphNode, new String[] { ! "area*", "line*", "stack*", "gprint*", "hrule*", "vrule*", "comment*" }); Node[] childs = getChildNodes(graphNode); --- 345,349 ---- private void resolveGraphElements(Node graphNode) throws RrdException { validateTagsOnlyOnce(graphNode, new String[] { ! "area*", "line*", "stack*", "gprint*", "hrule*", "vrule*", "comment*", "time*" }); Node[] childs = getChildNodes(graphNode); *************** *** 379,382 **** --- 379,390 ---- rrdGraphDef.gprint( datasource, consolFun, format, getChildValueAsDouble(childs[i], "base") ); } + else if(nodeName.equals("time")) { + validateTagsOnlyOnce(childs[i], new String[] { "format", "pattern", "value" }); + String format = getChildValue(childs[i], "format", false ); + String pattern = getChildValue(childs[i], "pattern"); + String timestamp = getChildValue(childs[i], "value"); + + rrdGraphDef.time( format, pattern, Util.getGregorianCalendar(timestamp) ); + } else if(nodeName.equals("hrule")) { validateTagsOnlyOnce(childs[i], new String[] { "value", "color", "legend", "width" }); *************** *** 482,493 **** Node[] nodes = getChildNodes(datasourceNode, "def"); for(int i = 0; i < nodes.length; i++) { ! if(hasChildNode(nodes[i], "rrd")) { // RRD datasource ! validateTagsOnlyOnce(nodes[i], new String[] {"name", "rrd", "source", "cf"}); ! String name = getChildValue(nodes[i], "name"); ! String rrd = getChildValue(nodes[i], "rrd"); ! String dsName = getChildValue(nodes[i], "source"); String consolFun = getChildValue(nodes[i], "cf"); ! rrdGraphDef.datasource(name, rrd, dsName, consolFun); } else if(hasChildNode(nodes[i], "rpn")) { --- 490,509 ---- Node[] nodes = getChildNodes(datasourceNode, "def"); for(int i = 0; i < nodes.length; i++) { ! if(hasChildNode(nodes[i], "rrd")) ! { // RRD datasource ! validateTagsOnlyOnce(nodes[i], new String[] {"name", "rrd", "source", "cf", "backend"}); ! String name = getChildValue(nodes[i], "name"); ! String rrd = getChildValue(nodes[i], "rrd"); ! String dsName = getChildValue(nodes[i], "source"); String consolFun = getChildValue(nodes[i], "cf"); ! ! if ( Util.Xml.hasChildNode(nodes[i], "backend") ) ! { ! String backend = getChildValue( nodes[i], "backend" ); ! rrdGraphDef.datasource( name, rrd, dsName, consolFun, backend ); ! } ! else ! rrdGraphDef.datasource(name, rrd, dsName, consolFun); } else if(hasChildNode(nodes[i], "rpn")) { Index: ExportData.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/graph/ExportData.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** ExportData.java 10 Jul 2004 00:07:03 -0000 1.2 --- ExportData.java 11 Jul 2004 22:04:13 -0000 1.3 *************** *** 50,53 **** --- 50,54 ---- private Source[] sources; + private Print printer; // ================================================================ *************** *** 296,299 **** --- 297,357 ---- /** + * <p>Calculate the chosen consolidation function <code>consolFun</code> over + * the <code>sourceName</code> and returns the result as a string using the + * specified <code>format</code>.</p> + * + * <p>In the format string there should be a + * <code>@n.d</code> marker (replace <code>n</code> with the total number of spaces the + * value should at minimum take up, and replace <code>d</code> with the desired number of decimals) + * in the place where the number should be printed. If an additional <code>@s</code> is + * found in the format, the value will be scaled and an appropriate SI magnitude + * unit will be printed in place of the <code>@s</code> marker. If you specify + * <code>@S</code> instead of <code>@s</code>, the value will be scaled with the scale + * factor used in the last gprint directive (uniform value scaling).</p> + * + * @param sourceName Source name + * @param consolFun Consolidation function to be used for calculation ("AVERAGE", + * "MIN", "MAX", "LAST" or "TOTAL" (since 1.3.1) + * @param format Format string. For example: "speed is @5.2 @sbits/sec@c", + * "temperature = @0 degrees" + * @throws RrdException Thrown in case of JRobin specific error + */ + public String print( String sourceName, String consolFun, String format ) throws RrdException { + return print( sourceName, consolFun, format, ValueFormatter.DEFAULT_BASE ); + } + + /** + * <p>Calculate the chosen consolidation function <code>consolFun</code> over + * the <code>sourceName</code> and returns the result as a string using the + * specified <code>format</code>.</p> + * + * <p>In the format string there should be a + * <code>@n.d</code> marker (replace <code>n</code> with the total number of spaces the + * value should at minimum take up, and replace <code>d</code> with the desired number of decimals) + * in the place where the number should be printed. If an additional <code>@s</code> is + * found in the format, the value will be scaled and an appropriate SI magnitude + * unit will be printed in place of the <code>@s</code> marker. If you specify + * <code>@S</code> instead of <code>@s</code>, the value will be scaled with the scale + * factor used in the last gprint directive (uniform value scaling).</p> + * + * @param sourceName Source name + * @param consolFun Consolidation function to be used for calculation ("AVERAGE", + * "MIN", "MAX", "LAST" or "TOTAL" (since 1.3.1) + * @param format Format string. For example: "speed is @5.2 @sbits/sec@c", + * "temperature = @0 degrees" + * @param base Base value used to calculate the appriopriate scaling SI magnitude. + * @throws RrdException Thrown in case of JRobin specific error + */ + public String print( String sourceName, String consolFun, String format, double base ) throws RrdException + { + double value = getAggregate( sourceName, consolFun ); + + if ( printer == null ) + printer = new Print( base, ValueFormatter.NO_SCALE ); + + return printer.getFormattedString( value, format, base ); + } + + /** * Imports a export XML string and maps it back to this ExportData object. * The XML can be from either a JRobin or RRDtool export. *************** *** 398,403 **** for ( int i = 0; i < sources.length; i++ ) sourceByName.put( sources[i].getName(), sources[i] ); - - } --- 456,459 ---- Index: RrdExportDef.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/graph/RrdExportDef.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** RrdExportDef.java 10 Jul 2004 00:07:03 -0000 1.2 --- RrdExportDef.java 11 Jul 2004 22:04:13 -0000 1.3 *************** *** 25,29 **** package org.jrobin.graph; ! import java.io.Serializable; import java.util.ArrayList; import java.util.Date; --- 25,29 ---- package org.jrobin.graph; ! import java.io.*; import java.util.ArrayList; import java.util.Date; *************** *** 32,35 **** --- 32,36 ---- import org.jrobin.core.Util; import org.jrobin.core.RrdException; + import org.jrobin.core.XmlWriter; /** *************** *** 367,370 **** --- 368,465 ---- } + /** + * Exports RrdExportDef (export definition) object in XML format to output stream. + * Generated code can be parsed with {@link RrdExportDefTemplate} class. + * + * @param stream Output stream to send XML code to. + */ + public void exportXmlTemplate( OutputStream stream ) + { + XmlWriter xml = new XmlWriter( stream ); + + xml.startTag("rrd_export_def"); + + // SPAN + xml.startTag("span"); + xml.writeTag("start", getStartTime() ); + xml.writeTag("end", getEndTime() ); + xml.closeTag(); // span + + // OPTIONS + xml.startTag( "options" ); + if ( resolution > 1 ) + xml.writeTag( "resolution", resolution ); + xml.writeTag( "strict_export", ( strict == STRICT_IMPLICIT_ON || strict == STRICT_EXPLICIT_ON ? "true" : "false" ) ); + xml.closeTag(); + + // DATASOURCES + xml.startTag("datasources"); + // defs + for ( int i = 0; i < fetchSources.size(); i++ ) + fetchSources.get( i ).exportXml(xml); + // cdefs and sdefs + for (int i = 0; i < cdefList.size(); i++ ) + { + Cdef cdef = (Cdef) cdefList.get(i); + cdef.exportXml(xml); + } + xml.closeTag(); // datasources + + // EXPORTS + xml.startTag("exports"); + String[][] list = getExportDatasources(); + for ( int i = 0; i < list.length; i++ ) + { + xml.startTag( "export" ); + xml.writeTag( "datasource", list[i][0] ); + xml.writeTag( "legend", list[i][1] ); + xml.closeTag(); + } + xml.closeTag(); // exports + + xml.closeTag(); // rrd_export_def + xml.flush(); + + xml.flush(); + } + + /** + * Exports RrdExportDef (export definition) object in XML format to string. + * Generated code can be parsed with {@link RrdExportDefTemplate} class, see + * {@link RrdExportDef#exportXmlTemplate()}. + * + * @return String representing graph definition in XML format. + */ + public String getXmlTemplate() + { + return exportXmlTemplate(); + } + + /** + * Exports RrdExportDef (export definition) object in XML format to string. + * Generated code can be parsed with {@link RrdExportDefTemplate} class. + * + * @return String representing graph definition in XML format. + */ + public String exportXmlTemplate() + { + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + exportXmlTemplate(outputStream); + return outputStream.toString(); + } + + /** + * Exports RrdExportDef (export definition) object in XML format to file. + * Generated code can be parsed with {@link RrdExportDefTemplate} class. + * + * @param filePath destination file + */ + public void exportXmlTemplate(String filePath) throws IOException + { + FileOutputStream outputStream = new FileOutputStream(filePath, false); + exportXmlTemplate(outputStream); + outputStream.close(); + } + // ================================================================ // -- Protected (package) methods Index: RrdExporter.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/graph/RrdExporter.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** RrdExporter.java 10 Jul 2004 00:07:03 -0000 1.2 --- RrdExporter.java 11 Jul 2004 22:04:13 -0000 1.3 *************** *** 147,150 **** --- 147,151 ---- long requestedStep = (long) (endTime - startTime) / maxRows; + if ( requestedStep <= 0 ) requestedStep = 1; // Shift start and endTime *************** *** 172,175 **** --- 173,177 ---- requestedStep = (long) (endTime - startTime) / maxRows; + if ( requestedStep <= 0 ) requestedStep = 1; } *************** *** 462,465 **** --- 464,470 ---- protected ExportData createExportData() throws RrdException { + if ( sources == null) + throw new RrdException( "Sources not calculated, no data to return." ); + // Now create a RrdDataSet object containing the results Source[] sourceSet; --- NEW FILE: Print.java --- package org.jrobin.graph; import org.jrobin.core.RrdException; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * <p>This is a simplified version of the Gprint class, used for simple value formatting * with scaling.</p> * * @author Arne Vandamme (cob...@jr...) */ class Print { // ================================================================ // -- Members // ================================================================ private static final String SCALE_MARKER = "@s"; private static final String UNIFORM_SCALE_MARKER = "@S"; private static final String VALUE_MARKER = "@([0-9]*\\.[0-9]{1}|[0-9]{1}|\\.[0-9]{1})"; private static final Pattern VALUE_PATTERN = Pattern.compile(VALUE_MARKER); private int numDec = 3; // Show 3 decimal values by default private int strLen = -1; private boolean normalScale = false; private boolean uniformScale = false; private ValueFormatter vFormat; // ================================================================ // -- Constructor // ================================================================ Print( double defaultBase, int scaleIndex ) { vFormat = new ValueFormatter( defaultBase, scaleIndex ); } // ================================================================ // -- Protected methods // ================================================================ String getFormattedString( double value, String format, double baseValue ) throws RrdException { // -- Parse the format checkValuePlacement( format ); // -- Generate the formatted string double oldBase = vFormat.getBase(); vFormat.setBase( baseValue ); vFormat.setFormat( value, numDec, strLen ); vFormat.setScaling( normalScale, uniformScale ); String valueStr = vFormat.getFormattedValue(); String prefix = vFormat.getPrefix(); vFormat.setBase( oldBase ); String str = format; str = str.replaceAll(VALUE_MARKER, valueStr); if ( normalScale ) str = str.replaceAll(SCALE_MARKER, prefix); if ( uniformScale ) str = str.replaceAll(UNIFORM_SCALE_MARKER, prefix); return str; } private void checkValuePlacement( String text ) throws RrdException { Matcher m = VALUE_PATTERN.matcher(text); if ( m.find() ) { normalScale = (text.indexOf(SCALE_MARKER) >= 0); uniformScale = (text.indexOf(UNIFORM_SCALE_MARKER) >= 0); if ( normalScale && uniformScale ) throw new RrdException( "Can't specify normal scaling and uniform scaling at the same time." ); String[] group = m.group(1).split("\\."); strLen = -1; numDec = 0; if ( group.length > 1 ) { if ( group[0].length() > 0 ) { strLen = Integer.parseInt(group[0]); numDec = Integer.parseInt(group[1]); } else numDec = Integer.parseInt(group[1]); } else numDec = Integer.parseInt(group[0]); } else throw new RrdException( "Could not find where to place value. No @ placeholder found." ); } } --- NEW FILE: RrdExportDefTemplate.java --- /* ============================================================ * JRobin : Pure java implementation of RRDTool's functionality * ============================================================ * * Project Info: http://www.jrobin.org * Project Lead: Sasa Markovic (sa...@jr...) * * Developers: Sasa Markovic (sa...@jr...) * Arne Vandamme (cob...@jr...) * * (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 org.jrobin.graph; import org.xml.sax.InputSource; import org.jrobin.core.RrdException; import org.jrobin.core.XmlTemplate; import org.jrobin.core.Util; import org.w3c.dom.Node; import java.io.IOException; import java.io.File; import java.util.GregorianCalendar; /** * <p>Class used to create an arbitrary number of RrdExportDef (export) objects * from a single XML template. XML template can be supplied as an XML InputSource, * XML file or XML formatted string.<p> * * @author Arne Vandamme (arn...@jr...) */ public class RrdExportDefTemplate extends XmlTemplate { private RrdExportDef def; /** * Creates template object from any parsable XML source * @param inputSource XML source * @throws java.io.IOException thrown in case of I/O error * @throws org.jrobin.core.RrdException usually thrown in case of XML related error */ public RrdExportDefTemplate(InputSource inputSource) throws IOException, RrdException { super(inputSource); } /** * Creates template object from the file containing XML template code * @param xmlFile file containing XML template * @throws IOException thrown in case of I/O error * @throws RrdException usually thrown in case of XML related error */ public RrdExportDefTemplate(File xmlFile) throws IOException, RrdException { super(xmlFile); } /** * Creates template object from the string containing XML template code * @param xmlString string containing XML template * @throws IOException thrown in case of I/O error * @throws RrdException usually thrown in case of XML related error */ public RrdExportDefTemplate(String xmlString) throws IOException, RrdException { super(xmlString); } /** * Creates RrdExportDef object which can be used to create RrdExport * object (actual JRobin export). Before this method is called, all template variables (if any) * must be resolved (replaced with real values). * See {@link XmlTemplate#setVariable(String, String) setVariable()} method information to * understand how to supply values for template variables. * * @return Export definition which can be used to create RrdExport object (actual JRobin export) * @throws RrdException Thrown if parsed XML template contains invalid (unrecognized) tags */ public RrdExportDef getRrdExportDef() throws RrdException { // basic check if( !root.getTagName().equals("rrd_export_def") ) throw new RrdException("XML definition must start with <rrd_export_def>"); validateTagsOnlyOnce( root, new String[] {"span", "options", "datasources", "exports"} ); def = new RrdExportDef(); // traverse all nodes Node[] childs = getChildNodes(root); for(int i = 0; i < childs.length; i++) { // SPAN String nodeName = childs[i].getNodeName(); if(nodeName.equals("span")) { resolveSpan(childs[i]); } // OPTIONS else if(nodeName.equals("options")) { resolveOptions(childs[i]); } // DATASOURCES else if(nodeName.equals("datasources")) { resolveDatasources(childs[i]); } // EXPORTS else if(nodeName.equals("exports")) { resolveExports(childs[i]); } } return def; } private void resolveExports(Node datasourceNode) throws RrdException { validateTagsOnlyOnce(datasourceNode, new String[] { "export*" }); Node[] nodes = getChildNodes(datasourceNode, "export"); for( int i = 0; i < nodes.length; i++ ) { validateTagsOnlyOnce( nodes[i], new String[] { "datasource", "legend" } ); String ds = getChildValue( nodes[i], "datasource" ); String legend = getChildValue( nodes[i], "legend" ); def.export( ds, legend ); } } private void resolveDatasources(Node datasourceNode) throws RrdException { validateTagsOnlyOnce(datasourceNode, new String[] { "def*" }); Node[] nodes = getChildNodes(datasourceNode, "def"); for(int i = 0; i < nodes.length; i++) { if(hasChildNode(nodes[i], "rrd")) { // RRD datasource validateTagsOnlyOnce(nodes[i], new String[] {"name", "rrd", "source", "cf", "backend"}); String name = getChildValue(nodes[i], "name"); String rrd = getChildValue(nodes[i], "rrd"); String dsName = getChildValue(nodes[i], "source"); String consolFun = getChildValue(nodes[i], "cf"); if ( Util.Xml.hasChildNode(nodes[i], "backend") ) { String backend = getChildValue( nodes[i], "backend" ); def.datasource( name, rrd, dsName, consolFun, backend ); } else def.datasource(name, rrd, dsName, consolFun); } else if(hasChildNode(nodes[i], "rpn")) { // RPN datasource validateTagsOnlyOnce(nodes[i], new String[] {"name", "rpn"}); String name = getChildValue(nodes[i], "name"); String rpn = getChildValue(nodes[i], "rpn"); def.datasource(name, rpn); } else if ( hasChildNode( nodes[i], "cf" ) || hasChildNode( nodes[i], "datasource" ) ) { // STATIC AGGREGATED DATASOURCE validateTagsOnlyOnce( nodes[i], new String[] {"name", "datasource", "cf"} ); String name = getChildValue(nodes[i], "name"); String ds = getChildValue(nodes[i], "datasource"); String cf = getChildValue(nodes[i], "cf"); def.datasource( name, ds, cf ); } else { throw new RrdException("Unrecognized <def> format"); } } } private void resolveOptions(Node rootOptionNode) throws RrdException { validateTagsOnlyOnce( rootOptionNode, new String[] { "resolution", "strict_export" }); Node[] optionNodes = getChildNodes(rootOptionNode); for( int i = 0; i < optionNodes.length; i++ ) { String option = optionNodes[i].getNodeName(); Node optionNode = optionNodes[i]; if(option.equals("strict_export")) // STRICT EXPORT def.setStrictExport( getValueAsBoolean(optionNode) ); else if(option.equals("resolution")) // RESOLUTION def.setResolution( getValueAsInt(optionNode) ); } } private void resolveSpan(Node spanNode) throws RrdException { validateTagsOnlyOnce(spanNode, new String[] {"start", "end"}); String startStr = getChildValue(spanNode, "start"); String endStr = getChildValue(spanNode, "end"); GregorianCalendar gc1 = Util.getGregorianCalendar(startStr); GregorianCalendar gc2 = Util.getGregorianCalendar(endStr); def.setTimePeriod(gc1, gc2); } } Index: RrdGraphDef.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/graph/RrdGraphDef.java,v retrieving revision 1.18 retrieving revision 1.19 diff -C2 -d -r1.18 -r1.19 *** RrdGraphDef.java 10 Jul 2004 00:07:03 -0000 1.18 --- RrdGraphDef.java 11 Jul 2004 22:04:13 -0000 1.19 *************** *** 31,34 **** --- 31,35 ---- import java.util.*; import java.text.SimpleDateFormat; + import java.text.DateFormat; import org.jrobin.core.RrdException; *************** *** 75,79 **** private boolean showLegend = true; // show legend and comments (default: yes) private boolean drawSignature = true; // show JRobin url signature (default: yes) - private boolean useExportDataFlag = false; // use ExportData instead of normal datasources private Color backColor = new Color( 245, 245, 245 ); // variation of light gray --- 76,79 ---- *************** *** 742,745 **** --- 742,746 ---- * Adds a vertical rule to the graph definition. The legend allows for the same * alignment options as <code>gprint</code> or <code>comment</code>. + * * @param timestamp Rule position (specific moment in time) * @param color Rule color. *************** *** 761,764 **** --- 762,766 ---- * marker at the very end of the string. By putting a <code>@g</code> marker instead all * whitespace inside the string at very beginning or end will be removed also. + * * @param text Comment * @throws RrdException Thrown in case of JRobin specific error. *************** *** 767,770 **** --- 769,886 ---- addComment( new Comment(text) ); } + + /** + * Adds a comment that will contain the current time, to the graph definition. Normal comment codes + * apply, but a special marker <code>@t</code> should be present for the location of the timestamp. + * The actual format of the printed timestamp is determined by the pattern parameter, for information + * on possible patterns, see <em>java.text.SimpleDateFormat</em>. + * + * @param text Comment text (must contain @t marker). + * @param pattern SimpleDateFormat pattern to format the timestamp with. + * @throws RrdException Thrown in case of JRobin specific error. + */ + public void time( String text, String pattern ) throws RrdException { + addComment( new TimeText( text, pattern ) ); + } + + /** + * Adds a comment that will contain the current time, to the graph definition. Normal comment codes + * apply, but a special marker <code>@t</code> should be present for the location of the timestamp. + * The actual format of the printed timestamp is determined by the DateFormat passed as a parameter. + * + * @param text Comment text (must contain @t marker). + * @param format DateFormat object to format the timestamp with. + * @throws RrdException Thrown in case of JRobin specific error. + */ + public void time( String text, DateFormat format ) throws RrdException { + addComment( new TimeText( text, format ) ); + } + + /** + * Adds a comment that will contain the given timestamp, to the graph definition. Normal comment codes + * apply, but a special marker <code>@t</code> should be present for the location of the timestamp. + * The actual format of the printed timestamp is determined by the pattern parameter, for information + * on possible patterns, see <em>java.text.SimpleDateFormat</em>. + * + * @param text Comment text (must contain @t marker). + * @param pattern SimpleDateFormat pattern to format the timestamp with. + * @param timestamp Timestamp (in seconds) that should be formatted. + * @throws RrdException Thrown in case of JRobin specific error. + */ + public void time( String text, String pattern, long timestamp ) throws RrdException { + addComment( new TimeText( text, pattern, timestamp ) ); + } + + /** + * Adds a comment that will contain the given timestamp, to the graph definition. Normal comment codes + * apply, but a special marker <code>@t</code> should be present for the location of the timestamp. + * The actual format of the printed timestamp is determined by the DateFormat passed as a parameter. + * + * @param text Comment text (must contain @t marker). + * @param format DateFormat object to format the timestamp with. + * @param timestamp Timestamp (in seconds) that should be formatted. + * @throws RrdException Thrown in case of JRobin specific error. + */ + public void time( String text, DateFormat format, long timestamp ) throws RrdException { + addComment( new TimeText( text, format, timestamp ) ); + } + + /** + * Adds a comment that will contain the given timestamp, to the graph definition. Normal comment codes + * apply, but a special marker <code>@t</code> should be present for the location of the timestamp. + * The actual format of the printed timestamp is determined by the pattern parameter, for information + * on possible patterns, see <em>java.text.SimpleDateFormat</em>. + * + * @param text Comment text (must contain @t marker). + * @param pattern SimpleDateFormat pattern to format the timestamp with. + * @param date Timestamp (as Date object) that should be formatted. + * @throws RrdException Thrown in case of JRobin specific error. + */ + public void time( String text, String pattern, Date date ) throws RrdException { + addComment( new TimeText( text, pattern, date ) ); + } + + /** + * Adds a comment that will contain the given timestamp, to the graph definition. Normal comment codes + * apply, but a special marker <code>@t</code> should be present for the location of the timestamp. + * The actual format of the printed timestamp is determined by the DateFormat passed as a parameter. + * + * @param text Comment text (must contain @t marker). + * @param format DateFormat object to format the timestamp with. + * @param date Timestamp (as Date object) that should be formatted. + * @throws RrdException Thrown in case of JRobin specific error. + */ + public void time( String text, DateFormat format, Date date ) throws RrdException { + addComment( new TimeText( text, format, date ) ); + } + + /** + * Adds a comment that will contain the given timestamp, to the graph definition. Normal comment codes + * apply, but a special marker <code>@t</code> should be present for the location of the timestamp. + * The actual format of the printed timestamp is determined by the pattern parameter, for information + * on possible patterns, see <em>java.text.SimpleDateFormat</em>. + * + * @param text Comment text (must contain @t marker). + * @param pattern SimpleDateFormat pattern to format the timestamp with. + * @param cal Timestamp (as Calendar) that should be formatted. + * @throws RrdException Thrown in case of JRobin specific error. + */ + public void time( String text, String pattern, Calendar cal) throws RrdException { + addComment( new TimeText( text, pattern, cal ) ); + } + + /** + * Adds a comment that will contain the given timestamp, to the graph definition. Normal comment codes + * apply, but a special marker <code>@t</code> should be present for the location of the timestamp. + * The actual format of the printed timestamp is determined by the DateFormat passed as a parameter. + * + * @param text Comment text (must contain @t marker). + * @param format DateFormat object to format the timestamp with. + * @param cal Timestamp (as Calendar) that should be formatted. + * @throws RrdException Thrown in case of JRobin specific error. + */ + public void time( String text, DateFormat format, Calendar cal) throws RrdException { + addComment( new TimeText( text, format, cal ) ); + } /** *************** *** 928,966 **** } - /** - * Exports RrdGraphDef (graph definition) object in XML format to string. - * Generated code can be parsed with {@link RrdGraphDefTemplate} class, see - * {@link RrdGraphDef#exportXmlTemplate()}. - * - * @return String representing graph definition in XML format - */ - public String getXmlTemplate() { - return exportXmlTemplate(); - } - - /** - * Exports RrdGraphDef (graph definition) object in XML format to string. - * Generated code can be parsed with {@link RrdGraphDefTemplate} class. - * - * @return String representing graph definition in XML format - */ - public String exportXmlTemplate() { - ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - exportXmlTemplate(outputStream); - return outputStream.toString(); - } - - /** - * Exports RrdGraphDef (graph definition) object in XML format to file. - * Generated code can be parsed with {@link RrdGraphDefTemplate} class. - * - * @param filePath destination file - */ - public void exportXmlTemplate(String filePath) throws IOException { - FileOutputStream outputStream = new FileOutputStream(filePath, false); - exportXmlTemplate(outputStream); - outputStream.close(); - } - // ================================================================ // -- Protected (package) methods --- 1044,1047 ---- |