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 3521 2007-10-16 10:55:14Z tmorgner $ 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(final 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(final String[] args) 70: throws ParseException, EvaluationException 71: { 72: final FormulaParser parser = new FormulaParser(); 73: 74: // x = parser.parse("1 * 2 + 3 * 4"); 75: // x.initialize(new DefaultFormulaContext()); 76: // System.out.println(x); 77: // 78: // x = parser.parse("[a] * [b] + [c] * [d]"); 79: // x.initialize(new DefaultFormulaContext()); 80: // System.out.println(x); 81: // 82: // x = parser.parse("IF([A];[B];[C])"); 83: // x.initialize(new DefaultFormulaContext()); 84: // System.out.println(x); 85: // 86: // x = parser.parse("1 + ( 2+ (3 + (400 + 200)))"); 87: // x.initialize(new DefaultFormulaContext()); 88: // System.out.println(x); 89: 90: final LValue x = parser.parse("(1)()"); 91: x.initialize(new DefaultFormulaContext()); 92: System.out.println(x); 93: } 94: }