001 /**
002 * =========================================
003 * LibFormula : a free Java formula library
004 * =========================================
005 *
006 * Project Info: http://reporting.pentaho.org/libformula/
007 *
008 * (C) Copyright 2006-2007, by Pentaho Corporation and Contributors.
009 *
010 * This library is free software; you can redistribute it and/or modify it under the terms
011 * of the GNU Lesser General Public License as published by the Free Software Foundation;
012 * either version 2.1 of the License, or (at your option) any later version.
013 *
014 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
015 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016 * See the GNU Lesser General Public License for more details.
017 *
018 * You should have received a copy of the GNU Lesser General Public License along with this
019 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020 * Boston, MA 02111-1307, USA.
021 *
022 * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
023 * in the United States and other countries.]
024 *
025 *
026 * ------------
027 * $Id: DefaultFormulaContext.java 3521 2007-10-16 10:55:14Z tmorgner $
028 * ------------
029 * (C) Copyright 2006-2007, by Pentaho Corporation.
030 */
031 package org.jfree.formula;
032
033 import java.util.HashMap;
034
035 import org.jfree.formula.function.DefaultFunctionRegistry;
036 import org.jfree.formula.function.FunctionRegistry;
037 import org.jfree.formula.operators.DefaultOperatorFactory;
038 import org.jfree.formula.operators.OperatorFactory;
039 import org.jfree.formula.typing.DefaultTypeRegistry;
040 import org.jfree.formula.typing.Type;
041 import org.jfree.formula.typing.TypeRegistry;
042 import org.jfree.formula.typing.coretypes.AnyType;
043 import org.jfree.util.Configuration;
044
045 /**
046 * Creation-Date: 31.10.2006, 16:32:32
047 *
048 * @author Thomas Morgner
049 */
050 public class DefaultFormulaContext implements FormulaContext
051 {
052 private DefaultTypeRegistry typeRegistry;
053 private DefaultFunctionRegistry functionRegistry;
054 private OperatorFactory operatorFactory;
055 private DefaultLocalizationContext localizationContext;
056 private Configuration config;
057 private HashMap references;
058
059 public DefaultFormulaContext()
060 {
061 this(LibFormulaBoot.getInstance().getGlobalConfig());
062 }
063
064 public DefaultFormulaContext(final Configuration config)
065 {
066 this.config = config;
067 localizationContext = new DefaultLocalizationContext();
068 localizationContext.initialize(config);
069 typeRegistry = new DefaultTypeRegistry();
070 typeRegistry.initialize(config, this);
071 functionRegistry = new DefaultFunctionRegistry();
072 functionRegistry.initialize(config);
073 operatorFactory = new DefaultOperatorFactory();
074 operatorFactory.initalize(config);
075 }
076
077 public OperatorFactory getOperatorFactory()
078 {
079 return operatorFactory;
080 }
081
082 public void defineReference(final Object name, final Object value)
083 {
084 if (references == null)
085 {
086 references = new HashMap();
087 }
088 references.put(name, value);
089 }
090
091 public Object resolveReference(final Object name)
092 {
093 if (references == null)
094 {
095 return null;
096 }
097 return references.get(name);
098 }
099
100 public Configuration getConfiguration()
101 {
102 return config;
103 }
104
105 public FunctionRegistry getFunctionRegistry()
106 {
107 return functionRegistry;
108 }
109
110 public Type resolveReferenceType(final Object name)
111 {
112 return AnyType.TYPE;
113 }
114
115 public TypeRegistry getTypeRegistry()
116 {
117 return typeRegistry;
118 }
119
120 public LocalizationContext getLocalizationContext()
121 {
122 return localizationContext;
123 }
124
125 public boolean isReferenceDirty(final Object name)
126 {
127 return true;
128 }
129 }