KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jrobin > graph > Gprint


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  * Developers: Sasa Markovic (saxon@jrobin.org)
9  * Arne Vandamme (cobralord@jrobin.org)
10  *
11  * (C) Copyright 2003, by Sasa Markovic.
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 package org.jrobin.graph;
26
27 import java.util.HashMap JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.regex.Matcher JavaDoc;
30 import java.util.regex.Pattern JavaDoc;
31
32 import org.jrobin.core.RrdException;
33 import org.jrobin.core.XmlWriter;
34
35 /**
36  * <p>Represents a piece of aligned text (containing a retrieved datasource value) to be drawn on the graph.</p>
37  *
38  * @author Arne Vandamme (cobralord@jrobin.org)
39  * @author Sasa Markovic (saxon@jrobin.org)
40  */

41 class Gprint extends Comment
42 {
43     // ================================================================
44
// -- Members
45
// ================================================================
46
private static final String JavaDoc SCALE_MARKER = "@s";
47     private static final String JavaDoc UNIFORM_SCALE_MARKER = "@S";
48     private static final String JavaDoc VALUE_MARKER = "@([0-9]*\\.[0-9]{1}|[0-9]{1}|\\.[0-9]{1})";
49     private static final Pattern JavaDoc VALUE_PATTERN = Pattern.compile(VALUE_MARKER);
50     
51     private String JavaDoc sourceName;
52     private int aggregate;
53     private int numDec = 3; // Show 3 decimal values by default
54
private int strLen = -1;
55     private double baseValue = -1; // Default: use global base value
56
private boolean normalScale = false;
57     private boolean uniformScale = false;
58
59     protected ArrayList JavaDoc parsedList;
60
61
62     // ================================================================
63
// -- Constructors
64
// ================================================================
65
/**
66      * Constructs a Gprint object based on a string of text (with a specific placement
67      * marker in), a source from which to retrieve a value, and a consolidation function that
68      * specifies which value to retrieve. Possible consolidation functions are <code>AVERAGE, MAX, MIN, FIRST, LAST</code>
69      * and <code>TOTAL</code>.
70      * @param sourceName Name of the datasource from which to retrieve the consolidated value.
71      * @param consolFunc Consolidation function to use.
72      * @param text String of text with a placement marker for the resulting value.
73      * @throws RrdException Thrown in case of a JRobin specific error.
74      */

75     Gprint( String JavaDoc sourceName, String JavaDoc consolFunc, String JavaDoc text ) throws RrdException
76     {
77         this.text = text;
78         checkValuePlacement(); // First see if this GPRINT is valid
79
super.parseComment();
80
81         this.commentType = Comment.CMT_GPRINT;
82         this.sourceName = sourceName;
83         
84         if ( consolFunc.equalsIgnoreCase("AVERAGE") || consolFunc.equalsIgnoreCase("AVG") )
85             aggregate = Source.AGG_AVERAGE;
86         else if ( consolFunc.equalsIgnoreCase("MAX") || consolFunc.equalsIgnoreCase("MAXIMUM") )
87             aggregate = Source.AGG_MAXIMUM;
88         else if ( consolFunc.equalsIgnoreCase("MIN") || consolFunc.equalsIgnoreCase("MINIMUM") )
89             aggregate = Source.AGG_MINIMUM;
90         else if ( consolFunc.equalsIgnoreCase("LAST") )
91             aggregate = Source.AGG_LAST;
92         else if ( consolFunc.equalsIgnoreCase("FIRST") )
93             aggregate = Source.AGG_FIRST;
94         else if ( consolFunc.equalsIgnoreCase("TOTAL") )
95             aggregate = Source.AGG_TOTAL;
96         else
97             throw new RrdException( "Invalid consolidation function specified." );
98     }
99     
100     /**
101      * Constructs a Gprint object based on a string of text (with a specific placement
102      * marker in), a source from which to retrieve a value, and a consolidation function that
103      * specifies which value to retrieve. Possible consolidation functions are <code>AVERAGE, MAX, MIN, FIRST</code>
104      * and <code>LAST</code>.
105      * @param sourceName Name of the datasource from which to retrieve the consolidated value.
106      * @param consolFunc Consolidation function to use.
107      * @param text String of text with a placement marker for the resulting value.
108      * @param base Base value to use for formatting the value that needs to be printed.
109      * @throws RrdException Thrown in case of a JRobin specific error.
110      */

111     Gprint( String JavaDoc sourceName, String JavaDoc consolFunc, String JavaDoc text, double base ) throws RrdException
112     {
113         this( sourceName, consolFunc, text );
114         
115         baseValue = base;
116     }
117     
118     // ================================================================
119
// -- Protected methods
120
// ================================================================
121
/**
122      * Sets the consolidated value based on the internal sourceName and consolFunc, and the
123      * provided list of datasources with lookup table. The retrieved value will be formatted
124      * to a string using a <code>ValueFormatter</code> object.
125      * @param sources Source table containing all datasources necessary to create the final graph.
126      * @param sourceIndex HashMap containing the sourcename - index keypairs, to retrieve the index
127      * in the Source table based on the sourcename.
128      * @param vFormat ValueFormatter object used to retrieve a formatted string of the requested value.
129      * @throws RrdException Thrown in case of a JRobin specific error.
130      */

131     void setValue( Source[] sources, HashMap JavaDoc sourceIndex, ValueFormatter vFormat ) throws RrdException
132     {
133         try
134         {
135             double value = sources[ ((Integer JavaDoc) sourceIndex.get(sourceName)).intValue() ].getAggregate( aggregate );
136                         
137             // See if we need to use a specific value for the formatting
138
double oldBase = vFormat.getBase();
139             if ( baseValue != -1 && baseValue != vFormat.getBase() )
140                 vFormat.setBase( baseValue );
141             
142             vFormat.setFormat( value, numDec, strLen );
143             vFormat.setScaling( normalScale, uniformScale );
144             
145             String JavaDoc valueStr = vFormat.getFormattedValue();
146             String JavaDoc prefix = vFormat.getPrefix();
147
148             // Create a copy of the token/pair list
149
parsedList = new ArrayList JavaDoc( oList );
150
151             // Replace all values
152
for (int i = 0; i < oList.size(); i += 2 )
153             {
154                 String JavaDoc str = (String JavaDoc) oList.get(i);
155                 
156                 str = str.replaceAll(VALUE_MARKER, valueStr);
157                 if ( normalScale ) str = str.replaceAll(SCALE_MARKER, prefix);
158                 if ( uniformScale ) str = str.replaceAll(UNIFORM_SCALE_MARKER, prefix);
159                 
160                 parsedList.set( i, str );
161             }
162             
163             // Reset the base value of the formatter
164
if ( baseValue != -1 )
165                 vFormat.setBase( oldBase );
166         }
167         catch (Exception JavaDoc e) {
168             throw new RrdException( "Could not find datasource: " + sourceName );
169         }
170     }
171
172     /**
173      * Retrieves a <code>ArrayList</code> containing all string/token pairs in order of <code>String</code> - <code>Byte</code>.
174      * @return ArrayList containing all string/token pairs of this Comment.
175      */

176     ArrayList JavaDoc getTokens()
177     {
178         return parsedList;
179     }
180
181
182     // ================================================================
183
// -- Private methods
184
// ================================================================
185
/**
186      * Checks value placement by finding placeholder, checks for uniform or regular scaling and
187      * checks for the number of decimals to allow and the complete value string length.
188      * @throws RrdException Thrown in case of a JRobin specific error.
189      */

190     private void checkValuePlacement() throws RrdException
191     {
192         Matcher JavaDoc m = VALUE_PATTERN.matcher(text);
193         
194         if ( m.find() )
195         {
196             normalScale = (text.indexOf(SCALE_MARKER) >= 0);
197             uniformScale = (text.indexOf(UNIFORM_SCALE_MARKER) >= 0);
198             
199             if ( normalScale && uniformScale )
200                 throw new RrdException( "Can't specify normal scaling and uniform scaling at the same time." );
201             
202             String JavaDoc[] group = m.group(1).split("\\.");
203             strLen = -1;
204             numDec = 0;
205     
206             if ( group.length > 1 )
207             {
208                 if ( group[0].length() > 0 ) {
209                     strLen = Integer.parseInt(group[0]);
210                     numDec = Integer.parseInt(group[1]);
211                 }
212                 else
213                     numDec = Integer.parseInt(group[1]);
214             }
215             else
216                 numDec = Integer.parseInt(group[0]);
217         }
218         else
219             throw new RrdException( "Could not find where to place value. No @ placeholder found." );
220     }
221     
222     void exportXmlTemplate(XmlWriter xml) {
223         xml.startTag("gprint");
224         xml.writeTag("datasource", sourceName);
225         xml.writeTag("cf", Source.aggregates[aggregate]);
226         xml.writeTag("format", text);
227         if ( baseValue != -1 )
228             xml.writeTag( "base", baseValue );
229         xml.closeTag(); // gprint
230
}
231
232 }
233
Popular Tags