KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jrobin.graph;
2
3 import org.jrobin.core.RrdException;
4
5 import java.util.regex.Matcher JavaDoc;
6 import java.util.regex.Pattern JavaDoc;
7
8 /**
9  * <p>This is a simplified version of the Gprint class, used for simple value formatting
10  * with scaling.</p>
11  *
12  * @author Arne Vandamme (cobralord@jrobin.org)
13  */

14 class Print
15 {
16     // ================================================================
17
// -- Members
18
// ================================================================
19
private static final String JavaDoc SCALE_MARKER = "@s";
20     private static final String JavaDoc UNIFORM_SCALE_MARKER = "@S";
21     private static final String JavaDoc VALUE_MARKER = "@([0-9]*\\.[0-9]{1}|[0-9]{1}|\\.[0-9]{1})";
22     private static final Pattern JavaDoc VALUE_PATTERN = Pattern.compile(VALUE_MARKER);
23
24     private int numDec = 3; // Show 3 decimal values by default
25
private int strLen = -1;
26     private boolean normalScale = false;
27     private boolean uniformScale = false;
28
29     private ValueFormatter vFormat;
30
31
32     // ================================================================
33
// -- Constructor
34
// ================================================================
35
Print( double defaultBase, int scaleIndex )
36     {
37         vFormat = new ValueFormatter( defaultBase, scaleIndex );
38     }
39
40
41     // ================================================================
42
// -- Protected methods
43
// ================================================================
44
String JavaDoc getFormattedString( double value, String JavaDoc format, double baseValue ) throws RrdException
45     {
46         // -- Parse the format
47
checkValuePlacement( format );
48
49         // -- Generate the formatted string
50
double oldBase = vFormat.getBase();
51         vFormat.setBase( baseValue );
52
53         vFormat.setFormat( value, numDec, strLen );
54         vFormat.setScaling( normalScale, uniformScale );
55
56         String JavaDoc valueStr = vFormat.getFormattedValue();
57         String JavaDoc prefix = vFormat.getPrefix();
58
59         vFormat.setBase( oldBase );
60
61         String JavaDoc str = format;
62
63         str = str.replaceAll(VALUE_MARKER, valueStr);
64         if ( normalScale ) str = str.replaceAll(SCALE_MARKER, prefix);
65         if ( uniformScale ) str = str.replaceAll(UNIFORM_SCALE_MARKER, prefix);
66
67         return str;
68     }
69
70     private void checkValuePlacement( String JavaDoc text ) throws RrdException
71     {
72         Matcher JavaDoc m = VALUE_PATTERN.matcher(text);
73
74         if ( m.find() )
75         {
76             normalScale = (text.indexOf(SCALE_MARKER) >= 0);
77             uniformScale = (text.indexOf(UNIFORM_SCALE_MARKER) >= 0);
78
79             if ( normalScale && uniformScale )
80                 throw new RrdException( "Can't specify normal scaling and uniform scaling at the same time." );
81
82             String JavaDoc[] group = m.group(1).split("\\.");
83             strLen = -1;
84             numDec = 0;
85
86             if ( group.length > 1 )
87             {
88                 if ( group[0].length() > 0 ) {
89                     strLen = Integer.parseInt(group[0]);
90                     numDec = Integer.parseInt(group[1]);
91                 }
92                 else
93                     numDec = Integer.parseInt(group[1]);
94             }
95             else
96                 numDec = Integer.parseInt(group[0]);
97         }
98         else
99             throw new RrdException( "Could not find where to place value. No @ placeholder found." );
100     }
101 }
102
Popular Tags