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: DefaultOperatorFactory.java 3521 2007-10-16 10:55:14Z tmorgner $
028 * ------------
029 * (C) Copyright 2006-2007, by Pentaho Corporation.
030 */
031 package org.jfree.formula.operators;
032
033 import java.util.HashMap;
034 import java.util.Iterator;
035
036 import org.jfree.util.Configuration;
037 import org.jfree.util.ObjectUtilities;
038
039 /**
040 * Creation-Date: 02.11.2006, 12:29:27
041 *
042 * @author Thomas Morgner
043 */
044 public class DefaultOperatorFactory implements OperatorFactory
045 {
046 private static final String INFIX_PREFIX = "org.jfree.formula.operators.infix.";
047 private static final String PREFIX_PREFIX = "org.jfree.formula.operators.prefix.";
048 private static final String POSTFIX_PREFIX = "org.jfree.formula.operators.postfix.";
049
050 private HashMap infixOperators;
051 private HashMap prefixOperators;
052 private HashMap postfixOperators;
053
054 public DefaultOperatorFactory()
055 {
056 infixOperators = new HashMap();
057 prefixOperators = new HashMap();
058 postfixOperators = new HashMap();
059 }
060
061 public void initalize(final Configuration configuration)
062 {
063 loadInfixOperators(configuration);
064 loadPrefixOperators(configuration);
065 loadPostfixOperators(configuration);
066 }
067
068 private void loadInfixOperators(final Configuration configuration)
069 {
070 final Iterator infixKeys = configuration.findPropertyKeys(INFIX_PREFIX);
071 while (infixKeys.hasNext())
072 {
073 final String configKey = (String) infixKeys.next();
074 if (configKey.endsWith(".class") == false)
075 {
076 continue;
077 }
078 final String operatorClass = configuration.getConfigProperty(configKey);
079 if (operatorClass == null)
080 {
081 continue;
082 }
083 if (operatorClass.length() == 0)
084 {
085 continue;
086 }
087 final String tokenKey = configKey.substring
088 (0, configKey.length() - ".class".length()) + ".token";
089 final String token = configuration.getConfigProperty(tokenKey);
090 if (token == null)
091 {
092 continue;
093 }
094 final String tokenTrimmed = token.trim();
095
096 // this assumption was breaking >=, <=, and <>
097 // if (tokenTrimmed.length() != 1)
098 // {
099 // continue;
100 // }
101
102 final Object operator = ObjectUtilities.loadAndInstantiate
103 (operatorClass, DefaultOperatorFactory.class, InfixOperator.class);
104 if (operator instanceof InfixOperator)
105 {
106 infixOperators.put (tokenTrimmed, operator);
107 }
108 }
109 }
110
111 private void loadPrefixOperators(final Configuration configuration)
112 {
113 final Iterator infixKeys = configuration.findPropertyKeys(PREFIX_PREFIX);
114 final int infixLength = PREFIX_PREFIX.length();
115 while (infixKeys.hasNext())
116 {
117 final String configKey = (String) infixKeys.next();
118 if (configKey.endsWith(".class") == false)
119 {
120 continue;
121 }
122 final String operatorClass = configuration.getConfigProperty(configKey);
123 if (operatorClass == null)
124 {
125 continue;
126 }
127 if (operatorClass.length() == 0)
128 {
129 continue;
130 }
131 final String tokenKey = configKey.substring
132 (0, configKey.length() - ".class".length()) + ".token";
133 final String token = configuration.getConfigProperty(tokenKey);
134 if (token == null)
135 {
136 continue;
137 }
138 final String tokenTrimmed = token.trim();
139
140 // this is an invalid assumption
141 // if (tokenTrimmed.length() != 1)
142 // {
143 // continue;
144 // }
145
146 final Object operator = ObjectUtilities.loadAndInstantiate
147 (operatorClass, DefaultOperatorFactory.class, PrefixOperator.class);
148 if (operator instanceof PrefixOperator)
149 {
150 prefixOperators.put (tokenTrimmed, operator);
151 }
152 }
153 }
154
155 private void loadPostfixOperators(final Configuration configuration)
156 {
157 final Iterator infixKeys = configuration.findPropertyKeys(POSTFIX_PREFIX);
158 final int infixLength = POSTFIX_PREFIX.length();
159 while (infixKeys.hasNext())
160 {
161 final String configKey = (String) infixKeys.next();
162 if (configKey.endsWith(".class") == false)
163 {
164 continue;
165 }
166 final String operatorClass = configuration.getConfigProperty(configKey);
167 if (operatorClass == null)
168 {
169 continue;
170 }
171 if (operatorClass.length() == 0)
172 {
173 continue;
174 }
175 final String tokenKey = configKey.substring
176 (0, configKey.length() - ".class".length()) + ".token";
177 final String token = configuration.getConfigProperty(tokenKey);
178 if (token == null)
179 {
180 continue;
181 }
182 final String tokenTrimmed = token.trim();
183 // this is an invalid assumption
184 // if (tokenTrimmed.length() != 1)
185 // {
186 // continue;
187 // }
188
189 final Object operator = ObjectUtilities.loadAndInstantiate
190 (operatorClass, DefaultOperatorFactory.class, PostfixOperator.class);
191 if (operator instanceof PostfixOperator)
192 {
193 postfixOperators.put (tokenTrimmed, operator);
194 }
195 }
196 }
197
198 public InfixOperator createInfixOperator(final String operator)
199 {
200 return (InfixOperator) infixOperators.get(operator);
201 }
202
203 public PostfixOperator createPostfixOperator(final String operator)
204 {
205 return (PostfixOperator) postfixOperators.get(operator);
206 }
207
208 public PrefixOperator createPrefixOperator(final String operator)
209 {
210 return (PrefixOperator) prefixOperators.get(operator);
211 }
212 }