1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 package org.openoffice.xmerge.converter.xml.sxc.pexcel.records.formula; 25 26 import org.openoffice.xmerge.util.Debug; 27 28 /** 29 * This is the Factory class responsible for creating a <code>Token</code>. 30 * It has three methods for returning three different types of Tokens 31 * (Operator, Operand and Function). 32 * This utility class is used by either the <code>FormulaParser</code> or the 33 * <code>FormulaDecoder</code>. 34 */ 35 public class TokenFactory { 36 37 private OperatorLookup operatorLookup; 38 private OperandLookup operandLookup; 39 private FunctionLookup fl; 40 41 /** 42 * Default Constructor 43 */ TokenFactory()44 public TokenFactory() { 45 operatorLookup = new OperatorLookup(); 46 operandLookup = new OperandLookup(); 47 fl = new FunctionLookup(); 48 } 49 50 /** 51 * The Factory method for creating function Tokens 52 * 53 * @return The created <code>Token</code> 54 */ getFunctionToken(String s, int args)55 public Token getFunctionToken(String s, int args) { 56 Token t = null; 57 // We will have to fix this later to include fixed function tokens 58 // Also will need to handle errors where functions names are incorrect??? 59 Debug.log(Debug.TRACE,"TokenFactory creating function Token : " + s); 60 try { 61 t = new Token(s, ParseToken.TOKEN_FUNCTION_VARIABLE, fl.getIDFromString(s), args); 62 } catch (UnsupportedFunctionException eFn) { 63 64 Debug.log(Debug.ERROR, eFn.getMessage()); 65 } 66 return t; 67 } 68 69 /** 70 * The Factory method for creating operator Tokens 71 * 72 * @return The created <code>Token</code> 73 */ getOperatorToken(String s, int args)74 public Token getOperatorToken(String s, int args) { 75 76 Token t = null; 77 78 Debug.log(Debug.TRACE,"TokenFactory creating operator Token : " + s); 79 try { 80 if(args==1) { 81 if(s.equals("+")) { 82 t = new Token(s, ParseToken.TOKEN_OPERATOR, operatorLookup.getIDFromString("UNARY_PLUS"), args); 83 } else if (s.equals("-")) { 84 t = new Token(s, ParseToken.TOKEN_OPERATOR, operatorLookup.getIDFromString("UNARY_MINUS"), args); 85 } else { 86 t = new Token(s, ParseToken.TOKEN_OPERATOR, operatorLookup.getIDFromString(s), args); 87 } 88 } else { 89 t = new Token(s, ParseToken.TOKEN_OPERATOR, operatorLookup.getIDFromString(s), args); 90 } 91 } catch (UnsupportedFunctionException eFn) { 92 Debug.log(Debug.ERROR, eFn.getMessage()); 93 } 94 return t; 95 } 96 97 /** 98 * The Factory method for creating Operand Tokens 99 * 100 * @return The created <code>Token</code> 101 */ getOperandToken(String s, String type)102 public Token getOperandToken(String s, String type) { 103 Token t = null; 104 105 Debug.log(Debug.TRACE,"TokenFactory creating operand (" + type + ") Token : " + s); 106 try { 107 t = new Token(s, ParseToken.TOKEN_OPERAND, operandLookup.getIDFromString(type), 0); 108 } catch (UnsupportedFunctionException eFn) { 109 Debug.log(Debug.ERROR, eFn.getMessage()); 110 } 111 112 return t; 113 } 114 } 115