1 25 package org.jrobin.graph; 26 27 import java.util.HashMap ; 28 import java.util.ArrayList ; 29 import java.util.regex.Matcher ; 30 import java.util.regex.Pattern ; 31 32 import org.jrobin.core.RrdException; 33 import org.jrobin.core.XmlWriter; 34 35 41 class Gprint extends Comment 42 { 43 private static final String SCALE_MARKER = "@s"; 47 private static final String UNIFORM_SCALE_MARKER = "@S"; 48 private static final String VALUE_MARKER = "@([0-9]*\\.[0-9]{1}|[0-9]{1}|\\.[0-9]{1})"; 49 private static final Pattern VALUE_PATTERN = Pattern.compile(VALUE_MARKER); 50 51 private String sourceName; 52 private int aggregate; 53 private int numDec = 3; private int strLen = -1; 55 private double baseValue = -1; private boolean normalScale = false; 57 private boolean uniformScale = false; 58 59 protected ArrayList parsedList; 60 61 62 75 Gprint( String sourceName, String consolFunc, String text ) throws RrdException 76 { 77 this.text = text; 78 checkValuePlacement(); 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 111 Gprint( String sourceName, String consolFunc, String text, double base ) throws RrdException 112 { 113 this( sourceName, consolFunc, text ); 114 115 baseValue = base; 116 } 117 118 131 void setValue( Source[] sources, HashMap sourceIndex, ValueFormatter vFormat ) throws RrdException 132 { 133 try 134 { 135 double value = sources[ ((Integer ) sourceIndex.get(sourceName)).intValue() ].getAggregate( aggregate ); 136 137 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 valueStr = vFormat.getFormattedValue(); 146 String prefix = vFormat.getPrefix(); 147 148 parsedList = new ArrayList ( oList ); 150 151 for (int i = 0; i < oList.size(); i += 2 ) 153 { 154 String str = (String ) 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 if ( baseValue != -1 ) 165 vFormat.setBase( oldBase ); 166 } 167 catch (Exception e) { 168 throw new RrdException( "Could not find datasource: " + sourceName ); 169 } 170 } 171 172 176 ArrayList getTokens() 177 { 178 return parsedList; 179 } 180 181 182 190 private void checkValuePlacement() throws RrdException 191 { 192 Matcher 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 [] 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(); } 231 232 } 233 | Popular Tags |