1 package org.jrobin.graph; 2 3 import org.jrobin.core.RrdException; 4 5 import java.util.regex.Matcher ; 6 import java.util.regex.Pattern ; 7 8 14 class Print 15 { 16 private static final String SCALE_MARKER = "@s"; 20 private static final String UNIFORM_SCALE_MARKER = "@S"; 21 private static final String VALUE_MARKER = "@([0-9]*\\.[0-9]{1}|[0-9]{1}|\\.[0-9]{1})"; 22 private static final Pattern VALUE_PATTERN = Pattern.compile(VALUE_MARKER); 23 24 private int numDec = 3; private int strLen = -1; 26 private boolean normalScale = false; 27 private boolean uniformScale = false; 28 29 private ValueFormatter vFormat; 30 31 32 Print( double defaultBase, int scaleIndex ) 36 { 37 vFormat = new ValueFormatter( defaultBase, scaleIndex ); 38 } 39 40 41 String getFormattedString( double value, String format, double baseValue ) throws RrdException 45 { 46 checkValuePlacement( format ); 48 49 double oldBase = vFormat.getBase(); 51 vFormat.setBase( baseValue ); 52 53 vFormat.setFormat( value, numDec, strLen ); 54 vFormat.setScaling( normalScale, uniformScale ); 55 56 String valueStr = vFormat.getFormattedValue(); 57 String prefix = vFormat.getPrefix(); 58 59 vFormat.setBase( oldBase ); 60 61 String 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 text ) throws RrdException 71 { 72 Matcher 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 [] 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 |