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  *
10  *
11  * (C) Copyright 2003-2005, 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 org.jrobin.core.RrdException;
28 import org.jrobin.core.Util;
29 import org.jrobin.data.DataProcessor;
30
31 import java.util.regex.Matcher;
32 import java.util.regex.Pattern;
33
34 class PrintText extends CommentText {
35     static final String UNIT_MARKER = "([^%]?)%(s|S)";
36     static final Pattern UNIT_PATTERN = Pattern.compile(UNIT_MARKER);
37
38     private final String srcName, consolFun;
39     private final boolean includedInGraph;
40
41     PrintText(String srcName, String consolFun, String text, boolean includedInGraph) {
42         super(text);
43         this.srcName = srcName;
44         this.consolFun = consolFun;
45         this.includedInGraph = includedInGraph;
46     }
47
48     boolean isPrint() {
49         return !includedInGraph;
50     }
51
52     void resolveText(DataProcessor dproc, ValueScaler valueScaler) throws RrdException {
53         super.resolveText(dproc, valueScaler);
54         if (resolvedText != null) {
55             double value = dproc.getAggregate(srcName, consolFun);
56             Matcher matcher = UNIT_PATTERN.matcher(resolvedText);
57             if (matcher.find()) {
58                 // unit specified
59                 ValueScaler.Scaled scaled = valueScaler.scale(value, matcher.group(2).equals("s"));
60                 resolvedText = resolvedText.substring(0, matcher.start()) +
61                         matcher.group(1) + scaled.unit + resolvedText.substring(matcher.end());
62                 value = scaled.value;
63             }
64             resolvedText = Util.sprintf(resolvedText, value);
65             trimIfGlue();
66         }
67     }
68 }
69