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: SubstituteFunction.java 2752 2007-04-10 14:10:41Z taqua $
028     * ------------
029     * (C) Copyright 2006-2007, by Pentaho Corporation.
030     */
031    package org.jfree.formula.function.text;
032    
033    import org.jfree.formula.EvaluationException;
034    import org.jfree.formula.FormulaContext;
035    import org.jfree.formula.LibFormulaErrorValue;
036    import org.jfree.formula.function.Function;
037    import org.jfree.formula.function.ParameterCallback;
038    import org.jfree.formula.lvalues.TypeValuePair;
039    import org.jfree.formula.typing.Type;
040    import org.jfree.formula.typing.TypeRegistry;
041    import org.jfree.formula.typing.coretypes.TextType;
042    
043    /**
044     * This function returns text where an old text is substituted with a new text.
045     *
046     * @author Cedric Pronzato
047     */
048    public class SubstituteFunction implements Function
049    {
050      public SubstituteFunction()
051      {
052      }
053    
054      public TypeValuePair evaluate(final FormulaContext context,
055                                    final ParameterCallback parameters)
056          throws EvaluationException
057      {
058        final int parameterCount = parameters.getParameterCount();
059        if (parameterCount < 3 || parameterCount > 4)
060        {
061          throw new EvaluationException(LibFormulaErrorValue.ERROR_ARGUMENTS_VALUE);
062        }
063        final TypeRegistry typeRegistry = context.getTypeRegistry();
064    
065        final Type newTextType = parameters.getType(2);
066        final Object newTextValue = parameters.getValue(2);
067        final Type textType = parameters.getType(0);
068        final Object textValue = parameters.getValue(0);
069        final Type oldTextType = parameters.getType(1);
070        final Object oldTextValue = parameters.getValue(1);
071    
072        final String newText = typeRegistry.convertToText(newTextType, newTextValue);
073        final String text = typeRegistry.convertToText(textType, textValue);
074        final String oldText = typeRegistry.convertToText(oldTextType, oldTextValue);
075    
076        if (parameterCount == 3)
077        {
078          int index = text.indexOf(oldText);
079          if (index == -1)
080          {
081            return new TypeValuePair(TextType.TYPE, text);
082          }
083    
084          String result = text;
085          while (index >= 0)
086          {
087            final StringBuffer buffer = new StringBuffer(result);
088            buffer.replace(index, index + oldText.length(), newText);
089            result = buffer.toString();
090            index = result.indexOf(oldText, index + newText.length());
091          }
092          return new TypeValuePair(TextType.TYPE, result);
093        }
094    
095        // Instead of replacing all occurences, the user only requested to replace
096        // a specific one.
097        final Type whichType = parameters.getType(3);
098        final Object whichValue = parameters.getValue(3);
099        final Number n = typeRegistry.convertToNumber(whichType, whichValue);
100        if (n.doubleValue() < 1)
101        {
102          throw new EvaluationException(
103              LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE);
104        }
105    
106        final int nthOccurence = n.intValue();
107    
108        int index = text.indexOf(oldText);
109        if (index == -1)
110        {
111          return new TypeValuePair(TextType.TYPE, text);
112        }
113    
114        String result = text;
115        int counter = 1;
116        while (index >= 0)
117        {
118          if (counter == nthOccurence)
119          {
120            final StringBuffer buffer = new StringBuffer(result);
121            buffer.replace(index, index + oldText.length(), newText);
122            result = buffer.toString();
123            index = result.indexOf(oldText, index + newText.length());
124          }
125          else
126          {
127            index = result.indexOf(oldText, index + 1);
128          }
129          counter += 1;
130        }
131        return new TypeValuePair(TextType.TYPE, result);
132      }
133    
134      public String getCanonicalName()
135      {
136        return "SUBSTITUTE";
137      }
138    
139    }