Java Code Examples for org.pentaho.reporting.libraries.formula.typing.coretypes.TextType#TYPE

The following examples show how to use org.pentaho.reporting.libraries.formula.typing.coretypes.TextType#TYPE . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MidFunction.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
public TypeValuePair evaluate( final FormulaContext context,
                               final ParameterCallback parameters ) throws EvaluationException {
  final int parameterCount = parameters.getParameterCount();
  if ( parameterCount != 3 ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_ARGUMENTS_VALUE );
  }
  final TypeRegistry typeRegistry = context.getTypeRegistry();

  final Type textType = parameters.getType( 0 );
  final Object textValue = parameters.getValue( 0 );
  final Type startType = parameters.getType( 1 );
  final Object startValue = parameters.getValue( 1 );
  final Type lengthType = parameters.getType( 2 );
  final Object lengthValue = parameters.getValue( 2 );

  final String text = typeRegistry.convertToText( textType, textValue );
  final Number start = typeRegistry.convertToNumber( startType, startValue );
  final Number length = typeRegistry.convertToNumber( lengthType, lengthValue );

  if ( length.doubleValue() < 0 || start.doubleValue() < 1 ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE );
  }

  return new TypeValuePair( TextType.TYPE, process( text, start.intValue(), length.intValue() ) );
}
 
Example 2
Source File: LowerFunction.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
public TypeValuePair evaluate( final FormulaContext context, final ParameterCallback parameters )
  throws EvaluationException {
  final int parameterCount = parameters.getParameterCount();
  if ( parameterCount != 1 ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_ARGUMENTS_VALUE );
  }
  final Type type1 = parameters.getType( 0 );
  final Object value1 = parameters.getValue( 0 );
  final String result = context.getTypeRegistry().convertToText( type1, value1 );

  if ( result == null ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE );
  }

  return new TypeValuePair( TextType.TYPE, result.toLowerCase( context.getLocalizationContext().getLocale() ) );
}
 
Example 3
Source File: AcoshFunctionTest.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
private ParameterCallback getMockedParameterCallback( Object... parameterValues ) throws EvaluationException {
  ParameterCallback parameter = mock( ParameterCallback.class );
  when( parameter.getParameterCount() ).thenReturn( parameterValues.length );
  int i = 0;
  for ( Object parameterValue : parameterValues ) {
    Type parameterType = AnyType.TYPE;
    if ( parameterValue instanceof BigDecimal ) {
      parameterType = NumberType.GENERIC_NUMBER;
    } else if ( parameterValue instanceof Boolean ) {
      parameterType = LogicalType.TYPE;
    } else if ( parameterValue instanceof String ) {
      parameterType = TextType.TYPE;
    }
    when( parameter.getType( eq( i ) ) ).thenReturn( NumberType.GENERIC_NUMBER );
    when( parameter.getValue( eq( i ) ) ).thenReturn( parameterValue );
    ++i;
  }

  return parameter;
}
 
Example 4
Source File: URLEncodeFunction.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
public TypeValuePair evaluate( final FormulaContext context, final ParameterCallback parameters )
  throws EvaluationException {
  final int parameterCount = parameters.getParameterCount();
  if ( parameterCount < 1 ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_ARGUMENTS_VALUE );
  }
  final Type textType = parameters.getType( 0 );
  final Object textValue = parameters.getValue( 0 );
  final String textResult =
    context.getTypeRegistry().convertToText( textType, textValue );

  if ( textResult == null ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE );
  }

  final String encodingResult;
  if ( parameterCount == 2 ) {
    final Type encodingType = parameters.getType( 1 );
    final Object encodingValue = parameters.getValue( 1 );
    encodingResult = context.getTypeRegistry().convertToText( encodingType, encodingValue );
    if ( encodingResult == null ) {
      throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE );
    }
  } else {
    encodingResult = context.getConfiguration().getConfigProperty
      ( "org.pentaho.reporting.libraries.formula.URLEncoding", "UTF-8" );
  }
  try {
    return new TypeValuePair
      ( TextType.TYPE, URLEncoder.encode( textResult, encodingResult ) );

  } catch ( final UnsupportedEncodingException use ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE );
  }
}
 
Example 5
Source File: RightFunctionDescription.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
public Type getParameterType( final int position ) {
  if ( position == 0 ) {
    return TextType.TYPE;
  }

  return NumberType.GENERIC_NUMBER;
}
 
Example 6
Source File: DollarFunction.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
public TypeValuePair evaluate( final FormulaContext context, final ParameterCallback parameters )
  throws EvaluationException {
  final int parameterCount = parameters.getParameterCount();
  if ( parameterCount < 1 || parameterCount > 2 ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_ARGUMENTS_VALUE );
  }
  final Type type1 = parameters.getType( 0 );
  final Object value1 = parameters.getValue( 0 );
  final Number result = context.getTypeRegistry().convertToNumber( type1, value1 );
  if ( result == null ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE );
  }

  final NumberFormat currencyInstance =
    NumberFormat.getCurrencyInstance( context.getLocalizationContext().getLocale() );

  if ( parameterCount == 2 ) {
    final Type typeDecimals = parameters.getType( 1 );
    final Object valueDecimals = parameters.getValue( 1 );
    final Number resultDecimals = context.getTypeRegistry().convertToNumber( typeDecimals, valueDecimals );
    if ( resultDecimals == null ) {
      throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE );
    }
    currencyInstance.setMaximumFractionDigits( resultDecimals.intValue() );
    currencyInstance.setMinimumFractionDigits( resultDecimals.intValue() );
  }

  return new TypeValuePair( TextType.TYPE, currencyInstance.format( result ) );
}
 
Example 7
Source File: CsvArrayFunctionDescription.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Returns the parameter type at the given position using the function metadata. The first parameter is at the
 * position 0;
 *
 * @param position The parameter index.
 * @return The parameter type.
 */
public Type getParameterType( final int position ) {
  if ( position == 1 ) {
    return LogicalType.TYPE;
  }

  return TextType.TYPE;
}
 
Example 8
Source File: URLBuilderFunctionDescription.java    From pentaho-reporting with GNU Lesser General Public License v2.1 4 votes vote down vote up
public Type getValueType() {
  return TextType.TYPE;
}
 
Example 9
Source File: ResourceLookupFunctionDescription.java    From pentaho-reporting with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override public Type getParameterType( int position ) {
  return TextType.TYPE;
}
 
Example 10
Source File: LookupFunctionDescription.java    From pentaho-reporting with GNU Lesser General Public License v2.1 4 votes vote down vote up
public Type getParameterType( final int position ) {
  return TextType.TYPE;
}
 
Example 11
Source File: UpperFunctionDescription.java    From pentaho-reporting with GNU Lesser General Public License v2.1 4 votes vote down vote up
public Type getValueType() {
  return TextType.TYPE;
}
 
Example 12
Source File: ReplaceFunctionDescription.java    From pentaho-reporting with GNU Lesser General Public License v2.1 4 votes vote down vote up
public Type getParameterType( final int position ) {
  if ( position == 0 || position == 3 ) {
    return TextType.TYPE;
  }
  return NumberType.GENERIC_NUMBER;
}
 
Example 13
Source File: LowerFunctionDescription.java    From pentaho-reporting with GNU Lesser General Public License v2.1 4 votes vote down vote up
public Type getValueType() {
  return TextType.TYPE;
}
 
Example 14
Source File: URLEncodeFunctionDescription.java    From pentaho-reporting with GNU Lesser General Public License v2.1 4 votes vote down vote up
public Type getParameterType( final int position ) {
  return TextType.TYPE;
}
 
Example 15
Source File: AscFunctionDescription.java    From pentaho-reporting with GNU Lesser General Public License v2.1 4 votes vote down vote up
public Type getParameterType( final int position ) {
  return TextType.TYPE;
}
 
Example 16
Source File: MParameterTextFunction.java    From pentaho-reporting with GNU Lesser General Public License v2.1 4 votes vote down vote up
public TypeValuePair evaluate( final FormulaContext context, final ParameterCallback parameters )
  throws EvaluationException {
  final int parameterCount = parameters.getParameterCount();
  if ( parameterCount < 2 || parameterCount > 4 ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_ARGUMENTS_VALUE );
  }

  Object rawValue = parameters.getValue( 0 );
  if ( rawValue instanceof Object[] == false ) {
    rawValue = TypeUtil.normalize( context.getTypeRegistry().convertToSequence( parameters.getType( 0 ), rawValue ) );
  }
  final String parameterName =
      context.getTypeRegistry().convertToText( parameters.getType( 1 ), parameters.getValue( 1 ) );

  final String encodingResult;
  final boolean urlEncode;
  if ( parameterCount > 2 ) {
    urlEncode =
        !( Boolean.FALSE.equals( context.getTypeRegistry().convertToLogical( parameters.getType( 2 ),
            parameters.getValue( 2 ) ) ) );

    if ( parameterCount == 4 ) {
      final Type encodingType = parameters.getType( 3 );
      final Object encodingValue = parameters.getValue( 3 );
      encodingResult = context.getTypeRegistry().convertToText( encodingType, encodingValue );
      if ( encodingResult == null ) {
        throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE );
      }
    } else {
      encodingResult =
          context.getConfiguration().getConfigProperty( "org.pentaho.reporting.libraries.formula.URLEncoding",
              "UTF-8" );
    }
  } else {
    urlEncode = true;
    encodingResult =
        context.getConfiguration().getConfigProperty( "org.pentaho.reporting.libraries.formula.URLEncoding", "UTF-8" );
  }

  try {
    final Object[] value = (Object[]) rawValue;
    final StringBuffer b = new StringBuffer( 1000 );
    for ( int i = 0; i < value.length; i++ ) {
      if ( i != 0 ) {
        b.append( "&" );
        b.append( URLEncoder.encode( parameterName, encodingResult ) );
        b.append( "=" );
      }

      final String s;
      try {
        final Object o = value[i];
        s = ConverterRegistry.toAttributeValue( o );
      } catch ( BeanException e ) {
        // ok, so what. Log and return error
        logger.warn( "MPARAMETERTEXT: Failed to convert value " + rawValue + " (" + rawValue.getClass() + ")", e );
        throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_UNEXPECTED_VALUE );
      }

      if ( s == null ) {
        continue;
      }

      if ( urlEncode ) {
        b.append( URLEncoder.encode( s, encodingResult ) );
      } else {
        b.append( s );
      }
    }

    return new TypeValuePair( TextType.TYPE, b.toString() );

  } catch ( final UnsupportedEncodingException use ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE );
  }
}
 
Example 17
Source File: ConcatenateFunctionDescription.java    From pentaho-reporting with GNU Lesser General Public License v2.1 4 votes vote down vote up
public Type getValueType() {
  return TextType.TYPE;
}
 
Example 18
Source File: CleanFunctionDescription.java    From pentaho-reporting with GNU Lesser General Public License v2.1 4 votes vote down vote up
public Type getParameterType( final int position ) {
  return TextType.TYPE;
}
 
Example 19
Source File: EndsWithFunctionDescription.java    From pentaho-metadata with GNU Lesser General Public License v2.1 4 votes vote down vote up
public Type getParameterType( final int position ) {
  return TextType.TYPE;
}
 
Example 20
Source File: ExportTypeFunctionDescription.java    From pentaho-reporting with GNU Lesser General Public License v2.1 2 votes vote down vote up
/**
 * Returns the expected value type. This function returns a LogicalType.
 *
 * @return LogicalType.TYPE
 */
public Type getValueType() {
  return TextType.TYPE;
}