Java Code Examples for org.pentaho.di.core.Const#trimToType()

The following examples show how to use org.pentaho.di.core.Const#trimToType() . 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: ValueMetaInternetAddress.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
protected synchronized InetAddress convertStringToInternetAddress( String string ) throws KettleValueException {
  // See if trimming needs to be performed before conversion
  //
  string = Const.trimToType( string, getTrimType() );

  if ( Utils.isEmpty( string ) ) {
    return null;
  }

  try {
    return InetAddress.getByName( string );
  } catch ( Exception e ) {
    throw new KettleValueException( toString()
      + " : couldn't convert string [" + string + "] to an internet address", e );
  }
}
 
Example 2
Source File: ValueMetaBase.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
protected synchronized Date convertStringToDate( String string ) throws KettleValueException {
  string = Const.trimToType( string, getTrimType() ); // see if trimming needs
  // to be performed before
  // conversion

  if ( Utils.isEmpty( string ) ) {
    return null;
  }

  try {
    ParsePosition pp = new ParsePosition( 0 );
    Date result = getDateFormat( TYPE_DATE ).parse( string, pp );
    if ( pp.getErrorIndex() >= 0 ) {
      // error happen
      throw new ParseException( string, pp.getErrorIndex() );
    }
    // some chars can be after pp.getIndex(). That means, not full value was parsed. For example, for value
    // "25-03-1918 11:54" and format "dd-MM-yyyy", value will be "25-03-1918 00:00" without any exception.
    // If there are only spaces after pp.getIndex() - that means full values was parsed
    return result;
  } catch ( ParseException e ) {
    String dateFormat = ( getDateFormat() != null ) ? getDateFormat().toPattern() : "null";
    throw new KettleValueException( toString() + " : couldn't convert string [" + string
        + "] to a date using format [" + dateFormat + "] on offset location " + e.getErrorOffset(), e );
  }
}
 
Example 3
Source File: ValueMetaBase.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
protected synchronized Double convertStringToNumber( String string ) throws KettleValueException {
  string = Const.trimToType( string, getTrimType() ); // see if trimming needs
  // to be performed before
  // conversion

  if ( Utils.isEmpty( string ) ) {
    return null;
  }

  try {
    DecimalFormat format = getDecimalFormat( false );
    Number number;
    if ( lenientStringToNumber ) {
      number = format.parse( string );
    } else {
      ParsePosition parsePosition = new ParsePosition( 0 );
      number = format.parse( string, parsePosition );

      if ( parsePosition.getIndex() < string.length() ) {
        throw new KettleValueException( toString()
            + " : couldn't convert String to number : non-numeric character found at position "
            + ( parsePosition.getIndex() + 1 ) + " for value [" + string + "]" );
      }

    }

    return new Double( number.doubleValue() );
  } catch ( Exception e ) {
    throw new KettleValueException( toString() + " : couldn't convert String to number ", e );
  }
}
 
Example 4
Source File: ValueMetaBase.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
protected synchronized Long convertStringToInteger( String string ) throws KettleValueException {
  string = Const.trimToType( string, getTrimType() ); // see if trimming needs
  // to be performed before
  // conversion

  if ( Utils.isEmpty( string ) ) {
    return null;
  }

  try {
    Number number;
    if ( lenientStringToNumber ) {
      number = new Long( getDecimalFormat( false ).parse( string ).longValue() );
    } else {
      ParsePosition parsePosition = new ParsePosition( 0 );
      number = getDecimalFormat( false ).parse( string, parsePosition );

      if ( parsePosition.getIndex() < string.length() ) {
        throw new KettleValueException( toString()
            + " : couldn't convert String to number : non-numeric character found at position "
            + ( parsePosition.getIndex() + 1 ) + " for value [" + string + "]" );
      }

    }
    return new Long( number.longValue() );
  } catch ( Exception e ) {
    throw new KettleValueException( toString() + " : couldn't convert String to Integer", e );
  }
}
 
Example 5
Source File: ValueMetaBase.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
protected synchronized BigDecimal convertStringToBigNumber( String string ) throws KettleValueException {
  string = Const.trimToType( string, getTrimType() ); // see if trimming needs
  // to be performed before
  // conversion

  if ( Utils.isEmpty( string ) ) {
    return null;
  }

  try {
    DecimalFormat format = getDecimalFormat( bigNumberFormatting );
    Number number;
    if ( lenientStringToNumber ) {
      number = format.parse( string );
    } else {
      ParsePosition parsePosition = new ParsePosition( 0 );
      number = format.parse( string, parsePosition );

      if ( parsePosition.getIndex() < string.length() ) {
        throw new KettleValueException( toString()
          + " : couldn't convert String to number : non-numeric character found at position "
          + ( parsePosition.getIndex() + 1 ) + " for value [" + string + "]" );
      }
    }

    // PDI-17366: Cannot simply cast a number to a BigDecimal,
    //            If the Number is not a BigDecimal.
    //
    if ( number instanceof Double ) {
      return BigDecimal.valueOf( number.doubleValue() );
    } else if ( number instanceof Long ) {
      return BigDecimal.valueOf( number.longValue() );
    }
    return (BigDecimal) number;

  } catch ( Exception e ) {
    // We added this workaround for PDI-1824
    //
    try {
      return new BigDecimal( string );
    } catch ( NumberFormatException ex ) {
      throw new KettleValueException( toString() + " : couldn't convert string value '" + string
          + "' to a big number.", ex );
    }
  }
}