Source for org.jfree.formula.operators.DefaultOperatorFactory

   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: DefaultOperatorFactory.java,v 1.5 2007/05/16 03:23:30 willgorman Exp $
  28:  * ------------
  29:  * (C) Copyright 2006-2007, by Pentaho Corporation.
  30:  */
  31: package org.jfree.formula.operators;
  32: 
  33: import java.util.HashMap;
  34: import java.util.Iterator;
  35: 
  36: import org.jfree.util.Configuration;
  37: import org.jfree.util.ObjectUtilities;
  38: 
  39: /**
  40:  * Creation-Date: 02.11.2006, 12:29:27
  41:  *
  42:  * @author Thomas Morgner
  43:  */
  44: public class DefaultOperatorFactory implements OperatorFactory
  45: {
  46:   private static final String INFIX_PREFIX = "org.jfree.formula.operators.infix.";
  47:   private static final String PREFIX_PREFIX = "org.jfree.formula.operators.prefix.";
  48:   private static final String POSTFIX_PREFIX = "org.jfree.formula.operators.postfix.";
  49: 
  50:   private HashMap infixOperators;
  51:   private HashMap prefixOperators;
  52:   private HashMap postfixOperators;
  53: 
  54:   public DefaultOperatorFactory()
  55:   {
  56:     infixOperators = new HashMap();
  57:     prefixOperators = new HashMap();
  58:     postfixOperators = new HashMap();
  59:   }
  60: 
  61:   public void initalize(Configuration configuration)
  62:   {
  63:     loadInfixOperators(configuration);
  64:     loadPrefixOperators(configuration);
  65:     loadPostfixOperators(configuration);
  66:   }
  67: 
  68:   private void loadInfixOperators(final Configuration configuration)
  69:   {
  70:     final Iterator infixKeys = configuration.findPropertyKeys(INFIX_PREFIX);
  71:     while (infixKeys.hasNext())
  72:     {
  73:       final String configKey = (String) infixKeys.next();
  74:       if (configKey.endsWith(".class") == false)
  75:       {
  76:         continue;
  77:       }
  78:       final String operatorClass = configuration.getConfigProperty(configKey);
  79:       if (operatorClass == null)
  80:       {
  81:         continue;
  82:       }
  83:       if (operatorClass.length() == 0)
  84:       {
  85:         continue;
  86:       }
  87:       final String tokenKey = configKey.substring
  88:           (0, configKey.length() - ".class".length()) + ".token";
  89:       final String token = configuration.getConfigProperty(tokenKey);
  90:       if (token == null)
  91:       {
  92:         continue;
  93:       }
  94:       final String tokenTrimmed = token.trim();
  95:       
  96:       // this assumption was breaking >=, <=, and <>
  97:       // if (tokenTrimmed.length() != 1)
  98:       // {
  99:       //   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(String operator)
 199:   {
 200:     return (InfixOperator) infixOperators.get(operator);
 201:   }
 202: 
 203:   public PostfixOperator createPostfixOperator(String operator)
 204:   {
 205:     return (PostfixOperator) postfixOperators.get(operator);
 206:   }
 207: 
 208:   public PrefixOperator createPrefixOperator(String operator)
 209:   {
 210:     return (PrefixOperator) prefixOperators.get(operator);
 211:   }
 212: }