org.pentaho.reporting.libraries.formula.typing.coretypes.TextType Java Examples

The following examples show how to use org.pentaho.reporting.libraries.formula.typing.coretypes.TextType. 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: MessageFunction.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 message = context.getTypeRegistry().convertToText( type1, value1 );

  final MessageFormat format = new MessageFormat( message, context.getLocalizationContext().getLocale() );
  final Object[] args = new Object[ parameterCount - 1 ];
  for ( int i = 1; i < parameterCount; i += 1 ) {
    args[ i - 1 ] = parameters.getValue( i );
  }
  return new TypeValuePair( TextType.TYPE, format.format( args ) );
}
 
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: 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 #4
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 #5
Source File: UrlParameterSeparatorFunction.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() == 0 ) {
    return new TypeValuePair( TextType.TYPE, "?" );
  }
  final String text = context.getTypeRegistry().convertToText( parameters.getType( 0 ), parameters.getValue( 0 ) );
  if ( text == null ) {
    return new TypeValuePair( TextType.TYPE, "?" );
  }
  if ( text.indexOf( '?' ) == -1 ) {
    return new TypeValuePair( TextType.TYPE, "?" );
  }

  if ( text.endsWith( "?" ) ) {
    return new TypeValuePair( TextType.TYPE, text );
  }

  return new TypeValuePair( TextType.TYPE, text + "&" );
}
 
Example #6
Source File: DefaultSQLFunctionGenerator.java    From pentaho-metadata with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * This method may be used by child classes to verify all params are static numbers.
 * 
 * @param f
 *          function to verify
 * @throws PentahoMetadataException
 *           if params are not numbers
 */
protected void verifyAllStaticStrings( FormulaFunction f ) throws PentahoMetadataException {

  for ( int i = 0; i < f.getChildValues().length; i++ ) {
    // this checks to see if the strings are static or if they are available as parameters
    if ( ( !( f.getChildValues()[i] instanceof StaticValue ) || !( ( (StaticValue) f.getChildValues()[i] )
        .getValueType() instanceof TextType ) )
        && ( !( f.getChildValues()[i] instanceof ContextLookup ) || !( ( (ContextLookup) f.getChildValues()[i] )
            .getName().startsWith( "param:" ) ) ) ) {
      throw new PentahoMetadataException(
          Messages
              .getErrorString(
                  "PMSFormulaContext.ERROR_0004_INVALID_PARAM_TYPE_NOT_STRING", Integer.toString( i + 1 ), f.getFunctionName() ) ); //$NON-NLS-1$
    }
  }
}
 
Example #7
Source File: UnicharFunction.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 );
  }

  return new TypeValuePair( TextType.TYPE, String.valueOf( ( (char) result.intValue() ) ) );
}
 
Example #8
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 #9
Source File: ProperFunction.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, capitalize( result ) );
}
 
Example #10
Source File: FunctionsTest.java    From pentaho-metadata with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Test
public void testInPositive() {
  InFunction bwf = new InFunction();
  StaticValue[] params = new StaticValue[5];
  params[0] = new StaticValue( "string", TextType.TYPE );
  params[1] = new StaticValue( "my favorite string", TextType.TYPE );
  params[2] = new StaticValue( "my string", TextType.TYPE );
  params[3] = new StaticValue( "my favorite", TextType.TYPE );
  params[4] = new StaticValue( "string", TextType.TYPE );
  ParameterCallback parameters = new TestParameterCallback( params );
  try {
    TypeValuePair tvp = bwf.evaluate( new DefaultFormulaContext(), parameters );
    assertTrue( tvp.getValue().equals( Boolean.TRUE ) );
  } catch ( Exception ex ) {
    ex.printStackTrace();
    fail();
  }
}
 
Example #11
Source File: FunctionsTest.java    From pentaho-metadata with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Test
public void testLikeNegative() {
  LikeFunction bwf = new LikeFunction();
  StaticValue[] params = new StaticValue[2];
  params[0] = new StaticValue( "string", TextType.TYPE );
  params[1] = new StaticValue( "bin", TextType.TYPE );
  ParameterCallback parameters = new TestParameterCallback( params );
  try {
    TypeValuePair tvp = bwf.evaluate( new DefaultFormulaContext(), parameters );
    assertTrue( tvp.getValue().equals( Boolean.FALSE ) );
    params[1] = new StaticValue( "ltrin", TextType.TYPE );
    tvp = bwf.evaluate( new DefaultFormulaContext(), parameters );
    assertTrue( tvp.getValue().equals( Boolean.FALSE ) );
  } catch ( Exception ex ) {
    ex.printStackTrace();
    fail();
  }
}
 
Example #12
Source File: FunctionsTest.java    From pentaho-metadata with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Test
public void testLikePositive() {
  LikeFunction bwf = new LikeFunction();
  StaticValue[] params = new StaticValue[2];
  params[0] = new StaticValue( "string", TextType.TYPE );
  params[1] = new StaticValue( "*in*", TextType.TYPE );
  ParameterCallback parameters = new TestParameterCallback( params );
  try {
    TypeValuePair tvp = bwf.evaluate( new DefaultFormulaContext(), parameters );
    assertTrue( tvp.getValue().equals( Boolean.TRUE ) );
    params[1] = new StaticValue( "*ing", TextType.TYPE );
    tvp = bwf.evaluate( new DefaultFormulaContext(), parameters );
    assertTrue( tvp.getValue().equals( Boolean.TRUE ) );
  } catch ( Exception ex ) {
    ex.printStackTrace();
    fail();
  }
}
 
Example #13
Source File: DefaultTypeRegistry.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
public Type guessTypeOfObject( final Object o ) {
  if ( o instanceof Number ) {
    return NumberType.GENERIC_NUMBER;
  } else if ( o instanceof Time ) {
    return DateTimeType.TIME_TYPE;
  } else if ( o instanceof java.sql.Date ) {
    return DateTimeType.DATE_TYPE;
  } else if ( o instanceof Date ) {
    return DateTimeType.DATETIME_TYPE;
  } else if ( o instanceof Boolean ) {
    return LogicalType.TYPE;
  } else if ( o instanceof String ) {
    return TextType.TYPE;
  }

  return AnyType.TYPE;
}
 
Example #14
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 #15
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 #16
Source File: PentahoPathNormalizerFunction.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 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, normalizePath( result ) );
}
 
Example #17
Source File: TypeRegisteryTest.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void testStringDateConversion() throws EvaluationException {
  final Date d = TestFormulaContext.createDate1( 2004, GregorianCalendar.JANUARY, 1, 0, 0, 0, 0 );
  final TypeRegistry typeRegistry = context.getTypeRegistry();
  final Number n = typeRegistry.convertToNumber( DateTimeType.DATE_TYPE, d );
  final Date d1 = typeRegistry.convertToDate( TextType.TYPE, "2004-01-01" );

  if ( d1.getTime() != d.getTime() ) {
    final Number n2 = typeRegistry.convertToNumber( DateTimeType.DATE_TYPE, d );
    final Date dx = typeRegistry.convertToDate( TextType.TYPE, "2004-01-01" );
  }

  assertEquals( "dates are different", d1.getTime(), d.getTime() );
}
 
Example #18
Source File: FunctionsTest.java    From pentaho-metadata with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Test
public void testEqualsNegative() {
  EqualsFunction bwf = new EqualsFunction();
  StaticValue[] params = new StaticValue[2];
  params[0] = new StaticValue( "my favorite string", TextType.TYPE );
  params[1] = new StaticValue( "my", TextType.TYPE );
  ParameterCallback parameters = new TestParameterCallback( params );
  try {
    TypeValuePair tvp = bwf.evaluate( new DefaultFormulaContext(), parameters );
    assertTrue( tvp.getValue().equals( Boolean.FALSE ) );
  } catch ( Exception ex ) {
    fail();
  }
}
 
Example #19
Source File: MidFunctionDescription.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 #20
Source File: DrillDownFunction.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 ( parameters.getParameterCount() != 3 ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_ARGUMENTS_VALUE );
  }

  // 0: Configuration-indicator (gives the pattern indirectly) never the pattern itself
  // 1: the report-path
  // 2: the parameter as 2d-array (name value pairs)

  final TypeRegistry typeRegistry = context.getTypeRegistry();
  final String configIndicator = typeRegistry.convertToText( parameters.getType( 0 ), parameters.getValue( 0 ) );
  String path;
  try {
    path = typeRegistry.convertToText( parameters.getType( 1 ), parameters.getValue( 1 ) );
  } catch ( EvaluationException ee ) {
    if ( LibFormulaErrorValue.ERROR_NA_VALUE.equals( ee.getErrorValue() ) ) {
      path = null;
    } else {
      throw ee;
    }
  }
  final ArrayCallback parameter = typeRegistry.convertToArray( parameters.getType( 2 ), parameters.getValue( 2 ) );

  final LinkCustomizer pattern = createLinkCustomizer( configIndicator );
  return new TypeValuePair( TextType.TYPE, pattern.format( context, configIndicator, path,
    computeParameterEntries( parameter, typeRegistry ) ) );
}
 
Example #21
Source File: QuoteTextFunction.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 = "javascript";
  }

  if ( encodingResult.equals( "xml" ) ) {
    return new TypeValuePair( TextType.TYPE, CharacterEntityParser.createXMLEntityParser()
        .encodeEntities( textResult ) );
  } else if ( encodingResult.equals( "html" ) ) {
    return new TypeValuePair( TextType.TYPE, HtmlCharacterEntities.getEntityParser().encodeEntities( textResult ) );
  } else if ( encodingResult.equals( "formula-string" ) ) {
    return new TypeValuePair( TextType.TYPE, FormulaUtil.quoteString( textResult ) );
  } else if ( encodingResult.equals( "formula-reference" ) ) {
    return new TypeValuePair( TextType.TYPE, FormulaUtil.quoteReference( textResult ) );
  } else { // javascript
    return new TypeValuePair( TextType.TYPE, saveConvert( textResult ) );
  }
}
 
Example #22
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 #23
Source File: FixedFunction.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 > 3 ) {
    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.getNumberInstance( 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() );
  }

  if ( parameterCount == 3 ) {
    final Type typeOmitSeparators = parameters.getType( 2 );
    final Object valueOmitSeparators = parameters.getValue( 2 );
    final Boolean resultOmitSeparators =
      context.getTypeRegistry().convertToLogical( typeOmitSeparators, valueOmitSeparators );
    if ( resultOmitSeparators == null ) {
      throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE );
    }
    currencyInstance.setGroupingUsed( !Boolean.TRUE.equals( resultOmitSeparators ) );
  }
  return new TypeValuePair( TextType.TYPE, currencyInstance.format( result ) );
}
 
Example #24
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 #25
Source File: ReptFunction.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 != 2 ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_ARGUMENTS_VALUE );
  }
  final TypeRegistry typeRegistry = context.getTypeRegistry();

  final Type countType = parameters.getType( 1 );
  final Object countValue = parameters.getValue( 1 );

  final int count = typeRegistry.convertToNumber( countType, countValue ).intValue();
  if ( count < 0 ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE );
  }

  final Type textType1 = parameters.getType( 0 );
  final Object textValue1 = parameters.getValue( 0 );
  final String rawText = typeRegistry.convertToText( textType1, textValue1 );
  if ( rawText == null ) {
    return new TypeValuePair( TextType.TYPE, "" );
  }

  final StringBuffer buffer = new StringBuffer( rawText.length() * count );
  for ( int i = 0; i < count; i++ ) {
    buffer.append( rawText );
  }
  return new TypeValuePair( TextType.TYPE, buffer.toString() );
}
 
Example #26
Source File: ResourceLookupFunction.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * RESOURCELOOKUP(bundleId, resourceId)
 *
 * @param context
 * @param parameters
 * @return
 * @throws EvaluationException
 */
@Override public TypeValuePair evaluate( FormulaContext context, ParameterCallback parameters )
  throws EvaluationException {

  final int parameterCount = parameters.getParameterCount();
  if ( parameterCount < 2 ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_ARGUMENTS_VALUE );
  }

  Type textType = parameters.getType( 0 );
  Object textValue = parameters.getValue( 0 );
  final String bundleId = context.getTypeRegistry().convertToText( textType, textValue );

  if ( bundleId == null || bundleId.isEmpty() ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE );
  }

  textType = parameters.getType( 1 );
  textValue = parameters.getValue( 1 );
  String key = context.getTypeRegistry().convertToText( textType, textValue );

  if ( key == null || key.isEmpty() ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE );
  }

  if ( context instanceof ReportFormulaContext ) {
    ReportFormulaContext fc = ReportFormulaContext.class.cast( context );
    try {
      ResourceBundleFactory factory = fc.getProcessingContext().getResourceBundleFactory();
      ResourceBundle bundle = factory.getResourceBundle( bundleId );
      if ( bundle != null && bundle.containsKey( key ) ) {
        key = bundle.getString( key );
      }
    } catch ( MissingResourceException | ClassCastException e ) {
      // no op
    }
  }

  return new TypeValuePair( TextType.TYPE, key );
}
 
Example #27
Source File: TypeRegisteryTest.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void testStringNumberConversion() throws EvaluationException {
  final Number d = new Double( 2000.5 );
  final TypeRegistry typeRegistry = context.getTypeRegistry();
  final Number n = typeRegistry.convertToNumber( NumberType.GENERIC_NUMBER, d );
  final Number d1 = typeRegistry.convertToNumber( TextType.TYPE, "2000.5" );

  if ( d1.doubleValue() != d.doubleValue() ) {
    final Number n2 = typeRegistry.convertToNumber( DateTimeType.DATE_TYPE, d );
    final Date dx = typeRegistry.convertToDate( TextType.TYPE, "2004-01-01" );
  }

  assertEquals( "dates are different", d1.doubleValue(), d.doubleValue(), 0.0 );
}
 
Example #28
Source File: MParameterTextFunctionDescription.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Returns the parameter type of the function parameters.
 *
 * @param position
 *          the parameter index.
 * @return always TextType.TYPE.
 */
public Type getParameterType( final int position ) {
  if ( position == 2 ) {
    return LogicalType.TYPE;
  }
  if ( position == 1 || position == 3 ) {
    return TextType.TYPE;
  }
  return AnyType.TYPE;
}
 
Example #29
Source File: FunctionsTest.java    From pentaho-metadata with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Test
public void testEndsWithNegative() {
  EndsWithFunction bwf = new EndsWithFunction();
  StaticValue[] params = new StaticValue[2];
  params[0] = new StaticValue( "my favorite string", TextType.TYPE );
  params[1] = new StaticValue( "my", TextType.TYPE );
  ParameterCallback parameters = new TestParameterCallback( params );
  try {
    TypeValuePair tvp = bwf.evaluate( new DefaultFormulaContext(), parameters );
    assertTrue( tvp.getValue().equals( Boolean.FALSE ) );
  } catch ( Exception ex ) {
    fail();
  }
}
 
Example #30
Source File: ConcatOperator.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 TypeValuePair value1,
                               final TypeValuePair value2 )
  throws EvaluationException {
  final TypeRegistry typeRegistry = context.getTypeRegistry();

  // Error or empty string, that's the question ..
  final Object raw1 = value1.getValue();
  final Object raw2 = value2.getValue();
  if ( raw1 == null || raw2 == null ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_NA_VALUE );
  }

  final String text1 = typeRegistry.convertToText( value1.getType(), raw1 );
  final String text2 = typeRegistry.convertToText( value2.getType(), raw2 );
  if ( text1 == null && text2 == null ) {
    throw EvaluationException.getInstance
      ( LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE );
  }
  if ( text1 == null ) {
    return new TypeValuePair( TextType.TYPE, text2 );
  }
  if ( text2 == null ) {
    return new TypeValuePair( TextType.TYPE, text1 );
  }

  return new TypeValuePair( TextType.TYPE, text1 + text2 );
}