org.pentaho.reporting.libraries.formula.EvaluationException Java Examples

The following examples show how to use org.pentaho.reporting.libraries.formula.EvaluationException. 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: 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 #2
Source File: AbsFunction.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 Number result = context.getTypeRegistry().convertToNumber( type1, value1 );
  if ( result == null ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE );
  }

  final BigDecimal num = NumberUtil.getAsBigDecimal( result );
  return new TypeValuePair( NumberType.GENERIC_NUMBER, num.abs() );
}
 
Example #3
Source File: EqualsFunction.java    From pentaho-metadata 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 length = parameters.getParameterCount();
  if ( length != 2 ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_ARGUMENTS_VALUE );
  }

  final TypeRegistry typeRegistry = context.getTypeRegistry();
  final Object value1Raw = parameters.getValue( 0 );
  final Object value2Raw = parameters.getValue( 1 );
  if ( value1Raw == null || value2Raw == null ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_NA_VALUE );
  }

  final Type type1 = parameters.getType( 0 );
  final Type type2 = parameters.getType( 1 );
  final ExtendedComparator comparator = typeRegistry.getComparator( type1, type2 );
  final boolean result = comparator.isEqual( type1, value1Raw, type2, value2Raw );
  if ( result ) {
    return RETURN_TRUE;
  } else {
    return RETURN_FALSE;
  }

}
 
Example #4
Source File: FixedFunctionTest.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( parameterType );
    when( parameter.getValue( eq( i ) ) ).thenReturn( parameterValue );
    ++i;
  }

  return parameter;
}
 
Example #5
Source File: PowerFunction.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 != 2 ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_ARGUMENTS_VALUE );
  }
  final Type type1 = parameters.getType( 0 );
  final Object value1 = parameters.getValue( 0 );
  final Type type2 = parameters.getType( 1 );
  final Object value2 = parameters.getValue( 1 );

  final Number result1 = context.getTypeRegistry().convertToNumber( type1, value1 );
  final Number result2 = context.getTypeRegistry().convertToNumber( type2, value2 );
  if ( result1 == null || result2 == null ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE );
  }


  final double power = Math.pow( result1.doubleValue(), result2.doubleValue() );
  return new TypeValuePair( NumberType.GENERIC_NUMBER, new BigDecimal( power ) );
}
 
Example #6
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 #7
Source File: IsNumberFunctionTest.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Test
public void runAdditionalTest() throws EvaluationException {
	IsNumberFunction function = new IsNumberFunction();

	ParameterCallback cbIntMock = mock(ParameterCallback.class);
	when(cbIntMock.getParameterCount()).thenReturn(1);
	when(cbIntMock.getType(anyInt())).thenReturn(NumberType.GENERIC_NUMBER);
	when(cbIntMock.getValue(anyInt())).thenReturn(new Long(55));

	ParameterCallback cbStrMock = mock(ParameterCallback.class);
	when(cbStrMock.getParameterCount()).thenReturn(1);
	when(cbStrMock.getType(anyInt())).thenReturn(TextType.TYPE);
	when(cbStrMock.getValue(anyInt())).thenReturn(new String("55"));

	TypeValuePair result = function.evaluate(getContext(), cbIntMock);
	assertEquals(result.getValue(), Boolean.TRUE);

	result = function.evaluate(getContext(), cbStrMock);
	assertEquals(result.getValue(), Boolean.FALSE);
}
 
Example #8
Source File: AcoshFunction.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
public TypeValuePair evaluate( FormulaContext context, 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 Number result = context.getTypeRegistry().convertToNumber( type1, value1 );
  if ( result == null ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE );
  }
  final double d = result.doubleValue();
  if ( d < 1.0 ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE );
  }
  return new TypeValuePair( NumberType.GENERIC_NUMBER, new BigDecimal( Math.log( d + Math.sqrt( d * d - 1.0 ) ) ) );
}
 
Example #9
Source File: DayFunction.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 {
  if ( parameters.getParameterCount() != 1 ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_ARGUMENTS_VALUE );
  }

  final TypeRegistry typeRegistry = context.getTypeRegistry();

  final Date d = typeRegistry.convertToDate( parameters.getType( 0 ), parameters.getValue( 0 ) );
  if ( d == null ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE );
  }

  final Calendar gc = DateUtil.createCalendar( d, context.getLocalizationContext() );

  final int dayOfMonth = gc.get( Calendar.DAY_OF_MONTH );
  //noinspection UnpredictableBigDecimalConstructorCall
  return new TypeValuePair( NumberType.GENERIC_NUMBER, new BigDecimal( (double) dayOfMonth ) );
}
 
Example #10
Source File: IntFunction.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 {
  if ( parameters.getParameterCount() != 1 ) {
    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 Number ret = NumberUtil.performIntRounding( NumberUtil.getAsBigDecimal( result ) );
  return new TypeValuePair( NumberType.GENERIC_NUMBER, ret );
}
 
Example #11
Source File: RowCountFunction.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 ReportFormulaContext rfc = (ReportFormulaContext) context;
  final int groupStart;
  if ( parameterCount == 0 ) {
    groupStart = rfc.getRuntime().getGroupStartRow( -1 );
  } else {
    final String groupName =
        context.getTypeRegistry().convertToText( parameters.getType( 0 ), parameters.getValue( 0 ) );
    groupStart = rfc.getRuntime().getGroupStartRow( groupName );
  }
  final int row = rfc.getRuntime().getCurrentRow();
  // noinspection UnpredictableBigDecimalConstructorCall
  return new TypeValuePair( NumberType.GENERIC_NUMBER, new BigDecimal( (double) ( row - groupStart ) ) );
}
 
Example #12
Source File: UpperFunction.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.toUpperCase( context.getLocalizationContext().getLocale() ) );
}
 
Example #13
Source File: ResourceLookupFunctionTest.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Before
public void before() throws IOException, EvaluationException {
  rbfactory = mock( ResourceBundleFactory.class );
  InputStream stream = new ByteArrayInputStream( ( "key=TRANSLATED" ).getBytes( StandardCharsets.UTF_8 ) );
  ResourceBundle rb = new PropertyResourceBundle( stream );
  when( rbfactory.getResourceBundle( eq( resourceId ) ) ).thenReturn( rb );

  when( parameters.getParameterCount() ).thenReturn( 2 );
  when( parameters.getType( eq( 0 ) ) ).thenReturn( TextType.TYPE );
  when( parameters.getValue( eq( 0 ) ) ).thenReturn( resourceId );

  when( parameters.getType( eq( 1 ) ) ).thenReturn( TextType.TYPE );
  when( parameters.getValue( eq( 1 ) ) ).thenReturn( "key" );

  when( context.getTypeRegistry() ).thenReturn( new DefaultTypeRegistry() );

  when( context.getProcessingContext() ).thenReturn( pcontext );
  when( pcontext.getResourceBundleFactory() ).thenReturn( rbfactory );
}
 
Example #14
Source File: IsTextFunction.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 {
  try {
    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 );
    if ( type1.isFlagSet( Type.TEXT_TYPE ) || value1 instanceof String ) {
      return RETURN_TRUE;
    }

    return RETURN_FALSE;
  } catch ( EvaluationException e ) {
    return RETURN_FALSE;
  }
}
 
Example #15
Source File: AscFunction.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 TypeRegistry typeRegistry = context.getTypeRegistry();

  final String result = typeRegistry.convertToText( type1, value1 );
  if ( result == null ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE );
  }

  final char[] chars = result.toCharArray();
  final StringBuffer b = new StringBuffer( chars.length );
  for ( int i = 0; i < chars.length; i++ ) {
    final char c = chars[ i ];
    convert( c, b );
  }
  return new TypeValuePair( TextType.TYPE, b.toString() );
}
 
Example #16
Source File: DefaultTypeRegistry.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
public Sequence convertToSequence( final Type type, final Object value ) throws EvaluationException {
  // scalar
  if ( type.isFlagSet( Type.SCALAR_TYPE ) ) {
    return new AnySequence( new StaticValue( value, type ), context );
  } else if ( type.isFlagSet( Type.SEQUENCE_TYPE ) ) {
    // already a sequence
    if ( value instanceof Sequence ) {
      return (Sequence) value;
    } else {
      logger.warn( "Assertation failure: Type declared to be a sequence, but no sequence found inside." );
      throw TypeConversionException.getInstance();
    }
  } else if ( type.isFlagSet( Type.ARRAY_TYPE ) ) {
    // an array source
    if ( value instanceof ArrayCallback ) {
      return new AnySequence( (ArrayCallback) value, context );
    } else if ( value instanceof Object[] ) {
      return new AnySequence( convertToArray( type, value ), context );
    } else {
      logger.warn( "Assertation failure: Type declared to be array, but no array callback found inside." );
      throw TypeConversionException.getInstance();
    }
  }
  throw TypeConversionException.getInstance();
}
 
Example #17
Source File: EndsWithFunction.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 != 2 ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_ARGUMENTS_VALUE );
  }
  final TypeRegistry typeRegistry = context.getTypeRegistry();

  final Type textType1 = parameters.getType( 0 );
  final Object textValue1 = parameters.getValue( 0 );
  final Type textType2 = parameters.getType( 1 );
  final Object textValue2 = parameters.getValue( 1 );

  final String text = typeRegistry.convertToText( textType1, textValue1 );
  final String substring = typeRegistry.convertToText( textType2, textValue2 );

  return text.endsWith( substring ) ? RETURN_TRUE : RETURN_FALSE;
}
 
Example #18
Source File: LikeFunction.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 != 2 ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_ARGUMENTS_VALUE );
  }
  final TypeRegistry typeRegistry = context.getTypeRegistry();

  final Type textType1 = parameters.getType( 0 );
  final Object textValue1 = parameters.getValue( 0 );
  final Type patternType = parameters.getType( 1 );
  final Object patternValue = parameters.getValue( 1 );

  final String text = typeRegistry.convertToText( textType1, textValue1 );

  String regex = typeRegistry.convertToText( patternType, patternValue );

  // replace any * or % with .*
  regex = regex.replaceAll( "\\*", ".*" ).replaceAll( "%", ".*" );

  final Pattern p = Pattern.compile( regex );
  final Matcher m = p.matcher( text );

  return m.find() ? RETURN_TRUE : RETURN_FALSE;
}
 
Example #19
Source File: MonthFunction.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 {
  if ( parameters.getParameterCount() != 1 ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_ARGUMENTS_VALUE );
  }

  final TypeRegistry typeRegistry = context.getTypeRegistry();
  final Date d = typeRegistry.convertToDate( parameters.getType( 0 ), parameters.getValue( 0 ) );

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

  final Calendar gc = DateUtil.createCalendar( d, context.getLocalizationContext() );

  final int month = gc.get( Calendar.MONTH ) + 1;
  return new TypeValuePair( NumberType.GENERIC_NUMBER, new BigDecimal( month ) );
}
 
Example #20
Source File: LikeFunction.java    From pentaho-metadata 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 != 2 ) {
    throw new EvaluationException( LibFormulaErrorValue.ERROR_ARGUMENTS_VALUE );
  }
  final TypeRegistry typeRegistry = context.getTypeRegistry();

  final Type textType1 = parameters.getType( 0 );
  final Object textValue1 = parameters.getValue( 0 );
  final Type textType2 = parameters.getType( 1 );
  final Object textValue2 = parameters.getValue( 1 );

  final String text = typeRegistry.convertToText( textType1, textValue1 );

  String regex = typeRegistry.convertToText( textType2, textValue2 );

  // replace any * or % with .*
  regex = regex.replaceAll( "\\*", ".*" ).replaceAll( "%", ".*" );

  Pattern p = Pattern.compile( regex );
  Matcher m = p.matcher( text );

  return m.find() ? RETURN_TRUE : RETURN_FALSE;
}
 
Example #21
Source File: NotEqualOperator.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 TypeValuePair value1, final TypeValuePair value2 )
  throws EvaluationException {
  final TypeRegistry typeRegistry = context.getTypeRegistry();

  final ExtendedComparator comparator =
    typeRegistry.getComparator( value1.getType(), value2.getType() );
  final boolean result = comparator.isEqual
    ( value1.getType(), value1.getValue(),
      value2.getType(), value2.getValue() );

  if ( result == false ) {
    return RETURN_TRUE;
  } else {
    return RETURN_FALSE;
  }
}
 
Example #22
Source File: BeginsWithFunction.java    From pentaho-metadata 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 != 2 ) {
    throw new EvaluationException( LibFormulaErrorValue.ERROR_ARGUMENTS_VALUE );
  }
  final TypeRegistry typeRegistry = context.getTypeRegistry();

  final Type textType1 = parameters.getType( 0 );
  final Object textValue1 = parameters.getValue( 0 );
  final Type textType2 = parameters.getType( 1 );
  final Object textValue2 = parameters.getValue( 1 );

  final String text = typeRegistry.convertToText( textType1, textValue1 );
  final String substring = typeRegistry.convertToText( textType2, textValue2 );

  return text.startsWith( substring ) ? RETURN_TRUE : RETURN_FALSE;
}
 
Example #23
Source File: IsRefFunction.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 );
  }

  // we want error values propagated so we need to evaluate the parameter
  parameters.getValue( 0 );

  final LValue raw = parameters.getRaw( 0 );
  if ( raw instanceof ContextLookup ) {
    return RETURN_TRUE;
  }

  return RETURN_FALSE;
}
 
Example #24
Source File: ParseDateFunction.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
private Locale parseLocale( final String s ) throws EvaluationException {
  final StringTokenizer strtok = new StringTokenizer( s.trim(), "_" );
  if ( strtok.hasMoreElements() == false ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_UNEXPECTED_VALUE );
  }
  final String language = strtok.nextToken();
  String country = "";
  if ( strtok.hasMoreTokens() ) {
    country = strtok.nextToken();
  }
  String variant = "";
  if ( strtok.hasMoreTokens() ) {
    variant = strtok.nextToken();
  }
  return new Locale( language, country, variant );
}
 
Example #25
Source File: DefaultTypeRegistry.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
private StringBuilder unwrap( final Object retval, final StringBuilder b ) throws EvaluationException {
  if ( retval.getClass().isArray() ) {
    return unwrapArray( retval, b );
  }
  if ( retval instanceof Sequence ) {
    return unwrapSequence( (Sequence) retval, b );
  }
  if ( retval instanceof ArrayCallback ) {
    return unwrapArrayCallback( (ArrayCallback) retval, b );
  }
  if ( retval instanceof Collection ) {
    return unwrapCollection( (Collection<?>) retval, b );
  }
  return b.append( retval );
}
 
Example #26
Source File: Term.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
public TypeValuePair evaluate() throws EvaluationException {
  TypeValuePair result = optimizedHeadValue.evaluate();
  for ( int i = 0; i < operandsArray.length; i++ ) {
    final LValue value = operandsArray[ i ];
    final InfixOperator op = operatorArray[ i ];
    result = op.evaluate( getContext(), result, value.evaluate() );
  }
  return result;
}
 
Example #27
Source File: ConcatenateFunction.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 StringBuffer computedResult = new StringBuffer( 512 );
  final int parameterCount = parameters.getParameterCount();

  if ( parameterCount == 0 ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_ARGUMENTS_VALUE );
  }

  final TypeRegistry typeRegistry = context.getTypeRegistry();
  for ( int paramIdx = 0; paramIdx < parameterCount; paramIdx++ ) {
    final Type type = parameters.getType( paramIdx );
    final Object value = parameters.getValue( paramIdx );
    final Sequence sequence = typeRegistry.convertToSequence( type, value );

    while ( sequence.hasNext() ) {
      final LValue lValue = sequence.nextRawValue();
      final TypeValuePair pair = lValue.evaluate();
      final Type type1 = pair.getType();
      final Object o = pair.getValue();
      final String str = typeRegistry.convertToText( type1, o );
      computedResult.append( str );
    }
  }

  return new TypeValuePair( TextType.TYPE, computedResult.toString() );
}
 
Example #28
Source File: ExportTypeFunction.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 {
  if ( context instanceof ReportFormulaContext == false ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_UNEXPECTED_VALUE );
  }
  final ReportFormulaContext rfc = (ReportFormulaContext) context;
  return new TypeValuePair( TextType.TYPE, rfc.getExportType() );
}
 
Example #29
Source File: RightFunction.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 TypeRegistry typeRegistry = context.getTypeRegistry();

  final Type textType = parameters.getType( 0 );
  final Object textValue = parameters.getValue( 0 );

  final String text = typeRegistry.convertToText( textType, textValue );
  final int length;
  if ( parameterCount == 2 ) {
    final Number lengthVal = typeRegistry.convertToNumber( parameters.getType( 1 ), parameters.getValue( 1 ) );
    if ( lengthVal.doubleValue() < 0 ) {
      throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE );
    }
    length = lengthVal.intValue();
  } else {
    length = 1;
  }

  if ( text == null || length < 0 ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE );
  }

  int s = text.length() - length + 1;
  if ( s < 1 ) {
    s = 1;
  }
  return new TypeValuePair( TextType.TYPE, MidFunction.process( text, s, length ) );
}
 
Example #30
Source File: ContextLookup.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
public TypeValuePair evaluate() throws EvaluationException {
  final FormulaContext context = getContext();
  final Type type = context.resolveReferenceType( name );
  final Object value = context.resolveReference( name );
  if ( value == null ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_NA_VALUE );
  }
  return new TypeValuePair( type, value );
}