1:
31:
32: package ;
33:
34: import ;
35: import ;
36: import ;
37: import ;
38: import ;
39: import ;
40:
41:
46: public class FormulaExpression extends AbstractExpression
47: {
48: private transient Formula compiledFormula;
49: private String formulaNamespace;
50: private String formulaExpression;
51: private String formula;
52:
53: public FormulaExpression()
54: {
55: }
56:
57: private synchronized FormulaContext getFormulaContext()
58: {
59: final ReportContext globalContext = getRuntime().getReportContext();
60: return globalContext.getFormulaContext();
61: }
62:
63: public String getFormula()
64: {
65: return formula;
66: }
67:
68: public String getFormulaNamespace()
69: {
70: return formulaNamespace;
71: }
72:
73: public String getFormulaExpression()
74: {
75: return formulaExpression;
76: }
77:
78: public void setFormula(final String formula)
79: {
80: this.formula = formula;
81: if (formula == null)
82: {
83: formulaNamespace = null;
84: formulaExpression = null;
85: }
86: else
87: {
88: final int separator = formula.indexOf(':');
89: if (separator <= 0 || ((separator + 1) == formula.length()))
90: {
91: if (formula.startsWith("="))
92: {
93: formulaNamespace = "report";
94: formulaExpression = formula.substring(1);
95: }
96: else
97: {
98:
99: formulaNamespace = null;
100: formulaExpression = null;
101: }
102: }
103: else
104: {
105: formulaNamespace = formula.substring(0, separator);
106: formulaExpression = formula.substring(separator + 1);
107: }
108: }
109: this.compiledFormula = null;
110: }
111:
112: private Object computeRegularValue()
113: {
114: try
115: {
116: if (compiledFormula == null)
117: {
118: compiledFormula = new Formula(formulaExpression);
119: }
120:
121: final ReportFormulaContext context =
122: new ReportFormulaContext(getFormulaContext(), getDataRow());
123: try
124: {
125: compiledFormula.initialize(context);
126: return compiledFormula.evaluate();
127: }
128: finally
129: {
130: context.setDataRow(null);
131: }
132: }
133: catch (Exception e)
134: {
135: Log.debug("Failed to compute the regular value.", e);
136: return null;
137: }
138: }
139:
140:
147: public Formula getCompiledFormula()
148: throws ParseException
149: {
150: if (compiledFormula == null)
151: {
152: compiledFormula = new Formula(formulaExpression);
153: }
154: return compiledFormula;
155: }
156:
157:
163: public Object computeValue() throws DataSourceException
164: {
165: try
166: {
167: return computeRegularValue();
168: }
169: catch (Exception e)
170: {
171: return null;
172: }
173: }
174:
175:
184: public Object clone() throws CloneNotSupportedException
185: {
186: final FormulaExpression o = (FormulaExpression) super.clone();
187: if (compiledFormula != null)
188: {
189: o.compiledFormula = (Formula) compiledFormula.clone();
190: }
191: return o;
192: }
193: }