Frames | No Frames |
1: /** 2: * ========================================= 3: * LibFormula : a free Java formula library 4: * ========================================= 5: * 6: * Project Info: http://reporting.pentaho.org/libformula/ 7: * 8: * (C) Copyright 2006-2007, by Pentaho Corporation and Contributors. 9: * 10: * This library is free software; you can redistribute it and/or modify it under the terms 11: * of the GNU Lesser General Public License as published by the Free Software Foundation; 12: * either version 2.1 of the License, or (at your option) any later version. 13: * 14: * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 15: * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 16: * See the GNU Lesser General Public License for more details. 17: * 18: * You should have received a copy of the GNU Lesser General Public License along with this 19: * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, 20: * Boston, MA 02111-1307, USA. 21: * 22: * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 23: * in the United States and other countries.] 24: * 25: * 26: * ------------ 27: * $Id: FormulaParser.java,v 1.8 2007/06/10 13:35:47 taqua Exp $ 28: * ------------ 29: * (C) Copyright 2006-2007, by Pentaho Corporation. 30: */ 31: package org.jfree.formula.parser; 32: 33: import java.io.StringReader; 34: 35: import org.jfree.formula.DefaultFormulaContext; 36: import org.jfree.formula.EvaluationException; 37: import org.jfree.formula.LibFormulaBoot; 38: import org.jfree.formula.lvalues.LValue; 39: import org.jfree.formula.operators.DefaultOperatorFactory; 40: import org.jfree.formula.operators.OperatorFactory; 41: 42: public class FormulaParser extends GeneratedFormulaParser 43: { 44: // This is my parser class 45: private OperatorFactory operatorFactory; 46: 47: public FormulaParser() 48: { 49: super(new StringReader("")); 50: operatorFactory = new DefaultOperatorFactory(); 51: operatorFactory.initalize(LibFormulaBoot.getInstance().getGlobalConfig()); 52: } 53: 54: protected OperatorFactory getOperatorFactory() 55: { 56: return operatorFactory; 57: } 58: 59: public LValue parse(String formula) throws ParseException 60: { 61: if (formula == null) 62: { 63: throw new NullPointerException("Formula-text given must not be null."); 64: } 65: ReInit(new StringReader(formula)); 66: return getExpression(); 67: } 68: 69: public static void main(String[] args) 70: throws ParseException, EvaluationException 71: { 72: FormulaParser parser = new FormulaParser(); 73: 74: LValue x; 75: // x = parser.parse("1 * 2 + 3 * 4"); 76: // x.initialize(new DefaultFormulaContext()); 77: // System.out.println(x); 78: // 79: // x = parser.parse("[a] * [b] + [c] * [d]"); 80: // x.initialize(new DefaultFormulaContext()); 81: // System.out.println(x); 82: // 83: // x = parser.parse("IF([A];[B];[C])"); 84: // x.initialize(new DefaultFormulaContext()); 85: // System.out.println(x); 86: // 87: // x = parser.parse("1 + ( 2+ (3 + (400 + 200)))"); 88: // x.initialize(new DefaultFormulaContext()); 89: // System.out.println(x); 90: 91: x = parser.parse("(1)()"); 92: x.initialize(new DefaultFormulaContext()); 93: System.out.println(x); 94: } 95: }