Java源码示例:org.pentaho.di.core.exception.KettleValueException
示例1
/**
* Convert the binary data to the actual data type.<br>
* - byte[] --> Long (Integer) - byte[] --> Double (Number) - byte[] --> BigDecimal (BigNumber) - byte[] --> Date
* (Date) - byte[] --> Boolean (Boolean) - byte[] --> byte[] (Binary)
*
* @param binary
* @return
* @throws KettleValueException
*/
@Override
public Object convertBinaryStringToNativeType( byte[] binary ) throws KettleValueException {
if ( binary == null ) {
return null;
}
numberOfBinaryStringConversions++;
// OK, so we have an internal representation of the original object, read
// from file.
// First we decode it in the correct encoding
//
String string = convertBinaryStringToString( binary );
// In this method we always must convert the data.
// We use the storageMetadata object to convert the binary string object.
//
// --> Convert from the String format to the current data type...
//
return convertData( storageMetadata, string );
}
示例2
protected InetAddress convertIntegerToInternetAddress( Long l ) throws KettleValueException {
if ( l == null ) {
return null;
}
byte[] addr;
if ( l >= Math.pow( 256, 4 ) ) {
addr = new byte[16];
} else {
addr = new byte[4];
}
for ( int i = 0; i < addr.length; i++ ) {
long mask = 0xFF << ( i * 8 );
addr[addr.length - 1 - i] = (byte) ( ( l & mask ) >> ( 8 * i ) );
}
try {
return InetAddress.getByAddress( addr );
} catch ( Exception e ) {
throw new KettleValueException( "Unable to convert an Integer to an internet address", e );
}
}
示例3
public String getGraphNodeDataId( GraphNode node, List<NodeAndPropertyData> nodeProperties ) throws KettleValueException {
StringBuffer id = new StringBuffer();
for ( NodeAndPropertyData napd : nodeProperties ) {
if ( napd.node.equals( node ) ) {
if ( napd.property.isPrimary() ) {
String propertyString = napd.sourceValueMeta.getString( napd.sourceValueData );
if ( id.length() > 0 ) {
id.append( "-" );
}
id.append( propertyString );
}
}
}
return id.toString();
}
示例4
@Test
public void rowWrittenEvent_boolean() throws KettleStepException, ReportDataFactoryException, KettleValueException {
Boolean[] row = new Boolean[] { true };
RowMetaInterface rowMetaInterface = mock( RowMetaInterface.class );
when( rowMetaInterface.size() ).thenReturn( 1 );
ValueMetaInterface valueMeta1 = mock( ValueMetaInterface.class );
when( valueMeta1.getName() ).thenReturn( "COLUMN_1" );
when( valueMeta1.getType() ).thenReturn( ValueMetaInterface.TYPE_BOOLEAN );
when( rowMetaInterface.getValueMeta( eq( 0 ) ) ).thenReturn( valueMeta1 );
when( rowMetaInterface.getBoolean( eq( row ), eq( 0 ) ) ).thenReturn( row[0] );
TableProducer tableProducer = new TableProducer( rowMetaInterface, 0, true );
tableProducer.rowWrittenEvent( rowMetaInterface, row );
TypedTableModel expectedModel = new TypedTableModel( new String[] { "COLUMN_1" }, new Class[] { Boolean.class } );
expectedModel.addRow( true );
assertEquals( expectedModel, tableProducer.getTableModel() );
}
示例5
/**
* Rounding with no decimal places (using default rounding method ROUND_HALF_CEILING)
*
* @param metaA
* Metadata of value to round
* @param dataA
* Value to round
* @return The rounded value
* @throws KettleValueException
*/
public static Object round( ValueMetaInterface metaA, Object dataA ) throws KettleValueException {
if ( dataA == null ) {
return null;
}
switch ( metaA.getType() ) {
case ValueMetaInterface.TYPE_NUMBER:
return new Double( Math.round( metaA.getNumber( dataA ).doubleValue() ) );
case ValueMetaInterface.TYPE_INTEGER:
return metaA.getInteger( dataA );
case ValueMetaInterface.TYPE_BIGNUMBER:
return new BigDecimal( Math.round( metaA.getNumber( dataA ).doubleValue() ) );
default:
throw new KettleValueException( "The 'round' function only works on numeric data" );
}
}
示例6
@Test
public void rowWrittenEvent_none() throws KettleStepException, ReportDataFactoryException, KettleValueException {
String[] row = new String[] { "NONE" };
RowMetaInterface rowMetaInterface = mock( RowMetaInterface.class );
when( rowMetaInterface.size() ).thenReturn( 1 );
ValueMetaInterface valueMeta1 = mock( ValueMetaInterface.class );
when( valueMeta1.getName() ).thenReturn( "COLUMN_1" );
when( valueMeta1.getType() ).thenReturn( ValueMetaInterface.TYPE_NONE );
when( rowMetaInterface.getValueMeta( eq( 0 ) ) ).thenReturn( valueMeta1 );
when( rowMetaInterface.getString( eq( row ), eq( 0 ) ) ).thenReturn( row[0] );
TableProducer tableProducer = new TableProducer( rowMetaInterface, 0, true );
tableProducer.rowWrittenEvent( rowMetaInterface, row );
TypedTableModel expectedModel = new TypedTableModel( new String[] { "COLUMN_1" }, new Class[] { String.class } );
expectedModel.addRow( "NONE" );
assertEquals( expectedModel, tableProducer.getTableModel() );
}
示例7
/**
* Convert the specified data to the data type specified in this object.
*
* @param meta2
* the metadata of the object to be converted
* @param data2
* the data of the object to be converted
* @return the object in the data type of this value metadata object
* @throws KettleValueException
* in case there is a data conversion error
*/
@Override
public Object convertData( ValueMetaInterface meta2, Object data2 ) throws KettleValueException {
switch ( getType() ) {
case TYPE_NONE:
case TYPE_STRING:
return meta2.getString( data2 );
case TYPE_NUMBER:
return meta2.getNumber( data2 );
case TYPE_INTEGER:
return meta2.getInteger( data2 );
case TYPE_DATE:
return meta2.getDate( data2 );
case TYPE_BIGNUMBER:
return meta2.getBigNumber( data2 );
case TYPE_BOOLEAN:
return meta2.getBoolean( data2 );
case TYPE_BINARY:
return meta2.getBinary( data2 );
default:
throw new KettleValueException( toString() + " : I can't convert the specified value to data type : "
+ getType() );
}
}
示例8
public static Date str2dat( String arg0, String arg1, String val ) throws KettleValueException {
SimpleDateFormat df = new SimpleDateFormat();
DateFormatSymbols dfs = new DateFormatSymbols();
if ( arg1 != null ) {
dfs.setLocalPatternChars( arg1 );
}
if ( arg0 != null ) {
df.applyPattern( arg0 );
}
try {
return df.parse( val );
} catch ( Exception e ) {
throw new KettleValueException( "TO_DATE Couldn't convert String to Date " + e.toString() );
}
}
示例9
/**
* Rounding with no decimal places with a given rounding method
*
* @param metaA
* Metadata of value to round
* @param dataA
* Value to round
* @param roundingMode
* The mode for rounding, e.g. java.math.BigDecimal.ROUND_HALF_EVEN
* @return The rounded value
* @throws KettleValueException
*/
public static Object round( ValueMetaInterface metaA, Object dataA, int roundingMode ) throws KettleValueException {
if ( dataA == null ) {
return null;
}
switch ( metaA.getType() ) {
// Use overloaded Const.round(value, precision, mode)
case ValueMetaInterface.TYPE_NUMBER:
return new Double( Const.round( metaA.getNumber( dataA ), 0, roundingMode ) );
case ValueMetaInterface.TYPE_INTEGER:
return new Long( Const.round( metaA.getInteger( dataA ), 0, roundingMode ) );
case ValueMetaInterface.TYPE_BIGNUMBER:
return Const.round( metaA.getBigNumber( dataA ), 0, roundingMode );
default:
throw new KettleValueException( "The 'round' function only works on numeric data" );
}
}
示例10
public static Object hourOfDay( ValueMetaInterface metaA, Object dataA ) throws KettleValueException {
if ( dataA == null ) {
return null;
}
Calendar calendar = Calendar.getInstance();
calendar.setTime( metaA.getDate( dataA ) );
Boolean oldDateCalculation = Boolean.parseBoolean(
Const.getEnvironmentVariable( Const.KETTLE_COMPATIBILITY_CALCULATION_TIMEZONE_DECOMPOSITION, "false" ) );
if ( !oldDateCalculation ) {
calendar.setTimeZone( metaA.getDateFormatTimeZone() );
}
return new Long( calendar.get( Calendar.HOUR_OF_DAY ) );
}
示例11
/**
* Converts the specified data object to the binary string storage type.
*
* @param object
* the data object to convert
* @return the data in a binary string storage type
* @throws KettleValueException
* In case there is a data conversion error.
*/
@Override
public Object convertToBinaryStringStorageType( Object object ) throws KettleValueException {
if ( object == null ) {
return null;
}
switch ( storageType ) {
case STORAGE_TYPE_NORMAL:
return convertNormalStorageTypeToBinaryString( object );
case STORAGE_TYPE_BINARY_STRING:
return object;
case STORAGE_TYPE_INDEXED:
return convertNormalStorageTypeToBinaryString( index[(Integer) object] );
default:
throw new KettleValueException( toStringMeta() + " : Unknown storage type [" + storageType
+ "] while converting to normal storage type" );
}
}
示例12
protected static Object multiplyString( ValueMetaInterface metaA, Object dataA, ValueMetaInterface metaB,
Object dataB ) throws KettleValueException {
StringBuffer s;
String append = "";
int n;
if ( metaB.isString() ) {
s = new StringBuffer( metaB.getString( dataB ) );
append = metaB.getString( dataB );
n = metaA.getInteger( dataA ).intValue();
} else {
s = new StringBuffer( metaA.getString( dataA ) );
append = metaA.getString( dataA );
n = metaB.getInteger( dataB ).intValue();
}
if ( n == 0 ) {
s.setLength( 0 );
} else {
for ( int i = 1; i < n; i++ ) {
s.append( append );
}
}
return s.toString();
}
示例13
protected synchronized String convertIntegerToString( Long integer ) throws KettleValueException {
if ( integer == null ) {
if ( !outputPaddingEnabled || length < 1 ) {
return null;
} else {
// Return strings padded to the specified length...
// This is done for backward compatibility with 2.5.x
// We just optimized this a bit...
//
String[] emptyPaddedStrings = Const.getEmptyPaddedStrings();
if ( length < emptyPaddedStrings.length ) {
return emptyPaddedStrings[length];
} else {
return Const.rightPad( "", length );
}
}
}
try {
return getDecimalFormat( false ).format( integer );
} catch ( Exception e ) {
throw new KettleValueException( toString() + " : couldn't convert Long to String ", e );
}
}
示例14
/**
* Convert the specified data to the data type specified in this object. For String conversion, be compatible with
* version 2.5.2.
*
* @param meta2
* the metadata of the object to be converted
* @param data2
* the data of the object to be converted
* @return the object in the data type of this value metadata object
* @throws KettleValueException
* in case there is a data conversion error
*/
@Override
public Object convertDataCompatible( ValueMetaInterface meta2, Object data2 ) throws KettleValueException {
switch ( getType() ) {
case TYPE_STRING:
return meta2.getCompatibleString( data2 );
case TYPE_NUMBER:
return meta2.getNumber( data2 );
case TYPE_INTEGER:
return meta2.getInteger( data2 );
case TYPE_DATE:
return meta2.getDate( data2 );
case TYPE_BIGNUMBER:
return meta2.getBigNumber( data2 );
case TYPE_BOOLEAN:
return meta2.getBoolean( data2 );
case TYPE_BINARY:
return meta2.getBinary( data2 );
default:
throw new KettleValueException( toString() + " : I can't convert the specified value to data type : "
+ getType() );
}
}
示例15
/**
* We return the content of a Value with the given name. We cache the position of the field indexes.
*
* @see org.jfree.formula.FormulaContext#resolveReference(java.lang.Object)
*/
public Object resolveReference( Object name ) throws EvaluationException {
if ( name instanceof String ) {
ValueMetaInterface valueMeta;
Integer idx = valueIndexMap.get( name );
if ( idx != null ) {
valueMeta = rowMeta.getValueMeta( idx.intValue() );
} else {
int index = rowMeta.indexOfValue( (String) name );
if ( index < 0 ) {
ErrorValue errorValue = new LibFormulaErrorValue( LibFormulaErrorValue.ERROR_INVALID_ARGUMENT );
throw new EvaluationException( errorValue );
}
valueMeta = rowMeta.getValueMeta( index );
idx = new Integer( index );
valueIndexMap.put( (String) name, idx );
}
Object valueData = rowData[idx];
try {
return getPrimitive( valueMeta, valueData );
} catch ( KettleValueException e ) {
throw new EvaluationException( LibFormulaErrorValue.ERROR_ARITHMETIC_VALUE );
}
}
return null;
}
示例16
@Test
public void testCompare_Representations() throws UnknownHostException, KettleValueException {
ValueMetaInternetAddress vm = new ValueMetaInternetAddress();
InetAddress extended = InetAddress.getByName( "1080:0:0:0:8:800:200C:417A" );
InetAddress condensed = InetAddress.getByName( "1080::8:800:200C:417A" );
assertEquals( 0, vm.compare( extended, condensed ) );
assertEquals( 0, vm.compare( condensed, extended ) );
extended = InetAddress.getByName( "0:0:0:0:0:0:0:1" );
condensed = InetAddress.getByName( "::1" );
assertEquals( 0, vm.compare( extended, condensed ) );
assertEquals( 0, vm.compare( condensed, extended ) );
extended = InetAddress.getByName( "0:0:0:0:0:0:0:0" );
condensed = InetAddress.getByName( "::0" );
assertEquals( 0, vm.compare( extended, condensed ) );
assertEquals( 0, vm.compare( condensed, extended ) );
}
示例17
public static Object weekOfYear( ValueMetaInterface metaA, Object dataA ) throws KettleValueException {
if ( dataA == null ) {
return null;
}
Calendar calendar = Calendar.getInstance();
calendar.setTime( metaA.getDate( dataA ) );
return new Long( calendar.get( Calendar.WEEK_OF_YEAR ) );
}
示例18
protected synchronized String convertInternetAddressToString( InetAddress inetAddress ) throws KettleValueException {
if ( inetAddress == null ) {
return null;
}
return inetAddress.getHostAddress();
}
示例19
@Override
public int compare( Object[] o1, Object[] o2 ) {
try {
return rowMeta.compare( o1, o2, fieldNrs );
} catch ( KettleValueException e ) {
logError( "Error comparing rows: " + e.toString() );
return 0;
}
}
示例20
@Deprecated
boolean isNull( Object data, boolean emptyStringDiffersFromNull ) throws KettleValueException {
try {
Object value = data;
if ( isStorageBinaryString() ) {
if ( value == null || !emptyStringDiffersFromNull && ( (byte[]) value ).length == 0 ) {
return true; // shortcut
}
value = convertBinaryStringToNativeType( (byte[]) data );
}
// Re-check for null, even for lazy conversion.
// A value (5 spaces for example) can be null after trim and conversion
//
if ( value == null ) {
return true;
}
if ( emptyStringDiffersFromNull ) {
return false;
}
// If it's a string and the string is empty, it's a null value as well
//
if ( isString() ) {
if ( value.toString().length() == 0 ) {
return true;
}
}
// We tried everything else so we assume this value is not null.
//
return false;
} catch ( ClassCastException e ) {
throw new RuntimeException( "Unable to verify if [" + toString() + "] is null or not because of an error:"
+ e.toString(), e );
}
}
示例21
@Test
public void testWritiLine_String() throws KettleValueException {
String sample = "sample";
Mockito.when( value.getType() ).thenReturn( ValueMetaInterface.TYPE_STRING );
Mockito.when( mi.getString( Mockito.any( Object[].class ), Mockito.anyInt() ) ).thenReturn( sample );
testWritiLine( new String[] { String.valueOf( sample ) }, value, String.valueOf( sample ) + Const.CR );
}
示例22
@Test
public void setEntryValue_string() throws KettleValueException {
StepInjectionMetaEntry entry = mock( StepInjectionMetaEntry.class );
doReturn( ValueMetaInterface.TYPE_STRING ).when( entry ).getValueType();
RowMetaAndData row = createRowMetaAndData( new ValueMetaString( TEST_FIELD ), TEST_VALUE );
SourceStepField sourceField = new SourceStepField( TEST_SOURCE_STEP_NAME, TEST_FIELD );
MetaInject.setEntryValue( entry, row, sourceField );
verify( entry ).setValue( TEST_VALUE );
}
示例23
@Override
public String toString() {
try {
return rowMeta.getString( data );
} catch ( KettleValueException e ) {
return rowMeta.toString() + ", error presenting data: " + e.toString();
}
}
示例24
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 );
}
}
示例25
/**
* Extracts the primitive data from an old style Value object
*
* @param value
* the old style Value object
* @return the value's data, NOT the meta data.
* @throws KettleValueException
* case there is a data conversion problem
*/
@Override
public Object getValueData( Value value ) throws KettleValueException {
if ( value == null || value.isNull() ) {
return null;
}
// So far the old types and the new types map to the same thing.
// For compatibility we just ask the old-style value to convert to the new
// one.
// In the old transformation this would happen sooner or later anyway.
// It doesn't throw exceptions or complain either (unfortunately).
//
switch ( getType() ) {
case ValueMetaInterface.TYPE_STRING:
return value.getString();
case ValueMetaInterface.TYPE_NUMBER:
return value.getNumber();
case ValueMetaInterface.TYPE_INTEGER:
return value.getInteger();
case ValueMetaInterface.TYPE_DATE:
return value.getDate();
case ValueMetaInterface.TYPE_BOOLEAN:
return value.getBoolean();
case ValueMetaInterface.TYPE_BIGNUMBER:
return value.getBigNumber();
case ValueMetaInterface.TYPE_BINARY:
return value.getBytes();
default:
throw new KettleValueException( toString() + " : We can't convert original data type " + value.getTypeDesc()
+ " to a primitive data type" );
}
}
示例26
public static Row createOriginalRow( RowMetaInterface rowMeta, Object[] rowData ) throws KettleValueException {
Row row = new Row();
for ( int i = 0; i < rowMeta.size(); i++ ) {
ValueMetaInterface valueMeta = rowMeta.getValueMeta( i );
Object valueData = rowData[ i ];
Value value = valueMeta.createOriginalValue( valueData );
row.addValue( value );
}
return row;
}
示例27
@Override
public int string2intPrimitive( String v ) throws KettleValueException {
for ( int i = 0; i < fieldFormatTypeCodes.length; i++ ) {
if ( fieldFormatTypeCodes[i].equalsIgnoreCase( v ) ) {
return i;
}
}
return FIELD_FORMAT_TYPE_OK;
}
示例28
@Test
public void testWritiLine_Date() throws KettleValueException, ParseException {
String sampleDate = "2000-10-09";
Date sample = DateDetector.getDateFromString( sampleDate );
Mockito.when( value.getType() ).thenReturn( ValueMetaInterface.TYPE_DATE );
Mockito.when( mi.getDate( Mockito.any( Object[].class ), Mockito.anyInt() ) ).thenReturn( sample );
testWritiLine( new String[] { String.valueOf( sample ) }, value, sampleDate + Const.CR );
}
示例29
/**
* Check the 2 lists comparing the rows in order. If they are not the same fail the test.
*
* @param rows1
* first row set to compare
* @param rows2
* second row set to compare
*/
public void checkRows( List<RowMetaAndData> rows1, List<RowMetaAndData> rows2 ) {
int idx = 1;
if ( rows1.size() != rows2.size() ) {
fail( "Number of rows is not the same: " + rows1.size() + " and " + rows2.size() );
}
Iterator<RowMetaAndData> it1 = rows1.iterator();
Iterator<RowMetaAndData> it2 = rows2.iterator();
while ( it1.hasNext() && it2.hasNext() ) {
RowMetaAndData rm1 = it1.next();
RowMetaAndData rm2 = it2.next();
Object[] r1 = rm1.getData();
Object[] r2 = rm2.getData();
if ( rm1.size() != rm2.size() ) {
fail( "row nr " + idx + " is not equal" );
}
int[] fields = new int[rm1.size()];
for ( int ydx = 0; ydx < rm1.size(); ydx++ ) {
fields[ydx] = ydx;
}
try {
if ( rm1.getRowMeta().compare( r1, r2, fields ) != 0 ) {
fail( "row nr " + idx + " is not equal" );
}
} catch ( KettleValueException e ) {
fail( "row nr " + idx + " is not equal" );
}
idx++;
}
}
示例30
public Value atan2( double arg0 ) throws KettleValueException {
if ( isNull() ) {
return this;
}
if ( isNumeric() ) {
setValue( Math.atan2( getNumber(), arg0 ) );
} else {
throw new KettleValueException( "Function ATAN2 only works with numbers" );
}
return this;
}