Source for org.jfree.formula.function.text.URLEncodeFunction

   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: URLEncodeFunction.java 3521 2007-10-16 10:55:14Z tmorgner $
  28:  * ------------
  29:  * (C) Copyright 2006-2007, by Pentaho Corporation.
  30:  */
  31: 
  32: package org.jfree.formula.function.text;
  33: 
  34: import java.io.UnsupportedEncodingException;
  35: 
  36: import org.jfree.formula.EvaluationException;
  37: import org.jfree.formula.FormulaContext;
  38: import org.jfree.formula.LibFormulaErrorValue;
  39: import org.jfree.formula.function.Function;
  40: import org.jfree.formula.function.ParameterCallback;
  41: import org.jfree.formula.lvalues.TypeValuePair;
  42: import org.jfree.formula.typing.Type;
  43: import org.jfree.formula.typing.coretypes.TextType;
  44: import org.jfree.formula.util.URLEncoder;
  45: 
  46: /**
  47:  * This function encodes a given text using the URL-Encoding schema. An optional
  48:  * second parameter can be given to specify the character encoding that should
  49:  * be used when converting text to bytes.
  50:  *
  51:  * @author Cedric Pronzato
  52:  */
  53: public class URLEncodeFunction implements Function
  54: {
  55:   public URLEncodeFunction()
  56:   {
  57:   }
  58: 
  59:   public TypeValuePair evaluate(final FormulaContext context, final ParameterCallback parameters) throws EvaluationException
  60:   {
  61:     final int parameterCount = parameters.getParameterCount();
  62:     if (parameterCount < 1)
  63:     {
  64:       throw new EvaluationException(LibFormulaErrorValue.ERROR_ARGUMENTS_VALUE);
  65:     }
  66:     final Type textType = parameters.getType(0);
  67:     final Object textValue = parameters.getValue(0);
  68:     final String textResult =
  69:         context.getTypeRegistry().convertToText(textType, textValue);
  70: 
  71:     if(textResult == null)
  72:     {
  73:       throw new EvaluationException(LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE);
  74:     }
  75: 
  76:     String encodingResult = null;
  77:     if(parameterCount == 2)
  78:     {
  79:       final Type encodingType = parameters.getType(1);
  80:       final Object encodingValue = parameters.getValue(1);
  81:       encodingResult = context.getTypeRegistry().convertToText(encodingType, encodingValue);
  82:       if(encodingResult == null)
  83:       {
  84:         throw new EvaluationException(LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE);
  85:       }
  86:     }
  87:     try
  88:     {
  89:       if (encodingResult == null)
  90:       {
  91:         return new TypeValuePair
  92:             (TextType.TYPE, URLEncoder.encode(textResult, "ISO-8859-1"));
  93:       }
  94:       return new TypeValuePair
  95:           (TextType.TYPE, URLEncoder.encode(textResult, encodingResult));
  96: 
  97:     }
  98:     catch(UnsupportedEncodingException use)
  99:     {
 100:       throw new EvaluationException(LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE);
 101:     }
 102:   }
 103: 
 104:   public String getCanonicalName()
 105:   {
 106:     return "URLENCODE";
 107:   }
 108: 
 109: }