org.supercsv.exception.SuperCsvCellProcessorException Java Examples

The following examples show how to use org.supercsv.exception.SuperCsvCellProcessorException. 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: ConnectionCsvDeserializer.java    From Showcase with Apache License 2.0 6 votes vote down vote up
@Override
/**
 * The airlines codes contained in the csv can be IATA ( 2-letter) codes or ICAO (3-letter) codes.
 */
public Object execute(Object value, CsvContext context) {
    validateInputNotNull(value, context);
    String airlineCode = (String) value;
    if ((airlineCode.length() == AIRLINE_IATACODE_LENGTH)) {
        return next.execute(value, context);
    }
    // attempt to convert the icao code to an IATA code. If not possible proceed with the code as it is.
    if (airlineCode.length() == AIRLINE_ICAOCODE_LENGTH) {

        List<Airline> airlines = airlineBusinessService.findAirlinesByIcaoCode(airlineCode);
        if (!airlines.isEmpty()) {
            return next.execute(airlines.get(0)
                    .getIataCode(), context);
        } else {
            return next.execute(airlineCode, context);
        }
    }
    throw new SuperCsvCellProcessorException(
            String.format("Could not parse '%s' neither as an IATA nor as an IACAO code for Airlines", value),
            context, this);
}
 
Example #2
Source File: ParseChar.java    From super-csv with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 * 
 * @throws SuperCsvCellProcessorException
 *             if value is null, isn't a Character or String, or is a String of multiple characters
 */
public Object execute(final Object value, final CsvContext context) {
	validateInputNotNull(value, context);
	
	final Character result;
	if( value instanceof Character ) {
		result = (Character) value;
	} else if( value instanceof String ) {
		final String stringValue = (String) value;
		if( stringValue.length() == 1 ) {
			result = Character.valueOf(stringValue.charAt(0));
		} else {
			throw new SuperCsvCellProcessorException(String.format(
				"'%s' cannot be parsed as a char as it is a String longer than 1 character", stringValue), context,
				this);
		}
	} else {
		final String actualClassName = value.getClass().getName();
		throw new SuperCsvCellProcessorException(String.format(
			"the input value should be of type Character or String but is of type %s", actualClassName), context,
			this);
	}
	
	return next.execute(result, context);
}
 
Example #3
Source File: DMinMax.java    From super-csv with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 * 
 * @throws SuperCsvCellProcessorException
 *             if value is null or can't be parsed as a Double
 * @throws SuperCsvConstraintViolationException
 *             if value doesn't lie between min and max (inclusive)
 */
public Object execute(final Object value, final CsvContext context) {
	validateInputNotNull(value, context);
	
	final Double result;
	if( value instanceof Double ) {
		result = (Double) value;
	} else {
		try {
			result = Double.parseDouble(value.toString());
		}
		catch(final NumberFormatException e) {
			throw new SuperCsvCellProcessorException(String.format("'%s' could not be parsed as a Double", value),
				context, this, e);
		}
	}
	
	if( result < min || result > max ) {
		throw new SuperCsvConstraintViolationException(String.format(
			"%f does not lie between the min (%f) and max (%f) values (inclusive)", result, min, max), context,
			this);
	}
	
	return next.execute(result, context);
}
 
Example #4
Source File: LMinMax.java    From super-csv with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 * 
 * @throws SuperCsvCellProcessorException
 *             if value is null or can't be parsed as a Long
 * @throws SuperCsvConstraintViolationException
 *             if value, or doesn't lie between min and max (inclusive)
 */
public Object execute(final Object value, final CsvContext context) {
	validateInputNotNull(value, context);
	
	final Long result;
	if( value instanceof Long ) {
		result = (Long) value;
	} else {
		try {
			result = Long.parseLong(value.toString());
		}
		catch(final NumberFormatException e) {
			throw new SuperCsvCellProcessorException(String.format("'%s' could not be parsed as a Long", value),
				context, this, e);
		}
	}
	
	if( result < min || result > max ) {
		throw new SuperCsvConstraintViolationException(String.format(
			"%d does not lie between the min (%d) and max (%d) values (inclusive)", result, min, max), context,
			this);
	}
	
	return next.execute(result, context);
}
 
Example #5
Source File: FmtNumber.java    From super-csv with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 * 
 * @throws SuperCsvCellProcessorException
 *             if value is null or not a Number, or if an invalid decimalFormat String was supplied
 */
public Object execute(final Object value, final CsvContext context) {
	validateInputNotNull(value, context);
	
	if( !(value instanceof Number) ) {
		throw new SuperCsvCellProcessorException(Number.class, value, context, this);
	}
	
	// create a new DecimalFormat if one is not supplied
	final DecimalFormat decimalFormatter;
	try {
		decimalFormatter = formatter != null ? formatter : new DecimalFormat(decimalFormat);
	}
	catch(IllegalArgumentException e) {
		throw new SuperCsvCellProcessorException(
			String.format("'%s' is not a valid decimal format", decimalFormat), context, this, e);
	}
	
	final String result = decimalFormatter.format(value);
	return next.execute(result, context);
}
 
Example #6
Source File: ParseEnum.java    From super-csv with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 * 
 * @throws SuperCsvCellProcessorException
 *             if value is null or can't be parsed as an Enum
 */
public Object execute(final Object value, final CsvContext context) {
	validateInputNotNull(value, context);
	
	final String inputString = value.toString();
	
	for( final Enum<?> enumConstant : enumClass.getEnumConstants() ) {
		String constantName = enumConstant.name();
		if( ignoreCase ? constantName.equalsIgnoreCase(inputString) : constantName.equals(inputString) ) {
			return enumConstant;
		}
	}
	
	throw new SuperCsvCellProcessorException(String.format("'%s' could not be parsed as a enum of type %s", value,
		enumClass.getName()), context, this);
	
}
 
Example #7
Source File: ParseDouble.java    From super-csv with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 * 
 * @throws SuperCsvCellProcessorException
 *             if value is null, isn't a Double or String, or can't be parsed as a Double
 */
public Object execute(final Object value, final CsvContext context) {
	validateInputNotNull(value, context);
	
	final Double result;
	if( value instanceof Double ) {
		result = (Double) value;
	} else if( value instanceof String ) {
		try {
			result = new Double((String) value);
		}
		catch(final NumberFormatException e) {
			throw new SuperCsvCellProcessorException(String.format("'%s' could not be parsed as a Double", value),
				context, this, e);
		}
	} else {
		final String actualClassName = value.getClass().getName();
		throw new SuperCsvCellProcessorException(String.format(
			"the input value should be of type Double or String but is of type %s", actualClassName), context, this);
	}
	
	return next.execute(result, context);
}
 
Example #8
Source File: ParseDuration.java    From super-csv with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 * 
 * @throws SuperCsvCellProcessorException
 *             if value is null or is not a String
 */
public Object execute(final Object value, final CsvContext context) {
	validateInputNotNull(value, context);
	if (!(value instanceof String)) {
		throw new SuperCsvCellProcessorException(String.class, value,
				context, this);
	}
	final Duration result;
	try {
		result = Duration.parse((String) value);
	} catch (IllegalArgumentException e) {
		throw new SuperCsvCellProcessorException(
				"Failed to parse value as a Duration", context, this, e);
	}
	return next.execute(result, context);
}
 
Example #9
Source File: ParseLocalTimeTest.java    From super-csv with Apache License 2.0 5 votes vote down vote up
@Theory
public void testNullInput(final ParseLocalTime p) {
	exception.expect(SuperCsvCellProcessorException.class);
	exception.expectMessage("this processor does not accept null input - "
		+ "if the column is optional then chain an Optional() processor before this one");
	p.execute(null, ANONYMOUS_CSVCONTEXT);
}
 
Example #10
Source File: FmtDateTimeTest.java    From super-csv with Apache License 2.0 5 votes vote down vote up
@Test
public void testNonDateTimeInput() {
	for (CellProcessor p : processors) {
		try {
			p.execute(123, ANONYMOUS_CSVCONTEXT);
			fail("expecting SuperCsvCellProcessorException");
		} catch (SuperCsvCellProcessorException e) {
			assertEquals(
					"the input value should be of type org.joda.time.DateTime but is java.lang.Integer",
					e.getMessage());
		}
	}
}
 
Example #11
Source File: ParseIconColour.java    From super-csv with Apache License 2.0 5 votes vote down vote up
/**
	 * {@inheritDoc}
	 */
	public Object execute(Object value, CsvContext context) {
		
		validateInputNotNull(value, context);
		
		for( IconColour colour : IconColour.values() ) {
			if( colour.name().equalsIgnoreCase(value.toString()) ) {
				return next.execute(colour, context);
			}
		}
		
		throw new SuperCsvCellProcessorException(String.format("Unknown IconColour: %s", value), context, this);

}
 
Example #12
Source File: ParseZoneIdTest.java    From super-csv with Apache License 2.0 5 votes vote down vote up
@Theory
public void testNullInput(final ParseZoneId p) {
	exception.expect(SuperCsvCellProcessorException.class);
	exception.expectMessage("this processor does not accept null input - "
		+ "if the column is optional then chain an Optional() processor before this one");
	p.execute(null, ANONYMOUS_CSVCONTEXT);
}
 
Example #13
Source File: ParseLocalDateTest.java    From super-csv with Apache License 2.0 5 votes vote down vote up
@Test
public void testNonStringInput() {
	for (CellProcessor p : processors) {
		try {
			p.execute(123, ANONYMOUS_CSVCONTEXT);
			fail("expecting SuperCsvCellProcessorException");
		} catch (SuperCsvCellProcessorException e) {
			assertEquals(
					"the input value should be of type java.lang.String but is java.lang.Integer",
					e.getMessage());
		}
	}
}
 
Example #14
Source File: ParseDuration.java    From super-csv with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * @throws SuperCsvCellProcessorException if value is null or is not a String
 */
public Object execute(final Object value, final CsvContext context) {
	validateInputNotNull(value, context);
	if( !(value instanceof String) ) {
		throw new SuperCsvCellProcessorException(String.class, value, context, this);
	}
	final Duration result;
	try {
		result = Duration.parse((String) value);
	}
	catch(DateTimeParseException e) {
		throw new SuperCsvCellProcessorException("Failed to parse value as a Duration", context, this, e);
	}
	return next.execute(result, context);
}
 
Example #15
Source File: FmtLocalDateTest.java    From super-csv with Apache License 2.0 5 votes vote down vote up
@Test
public void testNonLocalDateInput() {
	for (CellProcessor p : processors) {
		try {
			p.execute(123, ANONYMOUS_CSVCONTEXT);
			fail("expecting SuperCsvCellProcessorException");
		} catch (SuperCsvCellProcessorException e) {
			assertEquals(
					"the input value should be of type org.joda.time.LocalDate but is java.lang.Integer",
					e.getMessage());
		}
	}
}
 
Example #16
Source File: ParseDurationTest.java    From super-csv with Apache License 2.0 5 votes vote down vote up
@Test
public void testUnparsableString() {
	for (CellProcessor p : processors) {
		try {
			p.execute("not valid", ANONYMOUS_CSVCONTEXT);
			fail("expecting SuperCsvCellProcessorException");
		} catch (SuperCsvCellProcessorException e) {
			assertEquals("Failed to parse value as a Duration",
					e.getMessage());
		}
	}
}
 
Example #17
Source File: FmtLocalDateTest.java    From super-csv with Apache License 2.0 5 votes vote down vote up
@Test
public void testInvalidDateFormat() {
	final CellProcessor p = new FmtLocalDate("not valid");
	try {
		p.execute(LOCAL_DATE, ANONYMOUS_CSVCONTEXT);
		fail("expecting SuperCsvCellProcessorException");
	} catch (SuperCsvCellProcessorException e) {
		assertEquals("Failed to format value as a LocalDate", e.getMessage());
	}
}
 
Example #18
Source File: ParseLocalTimeTest.java    From super-csv with Apache License 2.0 5 votes vote down vote up
@Test
public void testNullInput() {
	for (CellProcessor p : processors) {
		try {
			p.execute(null, ANONYMOUS_CSVCONTEXT);
			fail("expecting SuperCsvCellProcessorException");
		} catch (SuperCsvCellProcessorException e) {
			assertEquals(
					"this processor does not accept null input - "
							+ "if the column is optional then chain an Optional() processor before this one",
					e.getMessage());
		}
	}
}
 
Example #19
Source File: ParseIntervalTest.java    From super-csv with Apache License 2.0 5 votes vote down vote up
@Test
public void testNullInput() {
	for (CellProcessor p : processors) {
		try {
			p.execute(null, ANONYMOUS_CSVCONTEXT);
			fail("expecting SuperCsvCellProcessorException");
		} catch (SuperCsvCellProcessorException e) {
			assertEquals(
					"this processor does not accept null input - "
							+ "if the column is optional then chain an Optional() processor before this one",
					e.getMessage());
		}
	}
}
 
Example #20
Source File: FmtDurationTest.java    From super-csv with Apache License 2.0 5 votes vote down vote up
@Test
public void testNonDurationInput() {
	for (CellProcessor p : processors) {
		try {
			p.execute(123, ANONYMOUS_CSVCONTEXT);
			fail("expecting SuperCsvCellProcessorException");
		} catch (SuperCsvCellProcessorException e) {
			assertEquals(
					"the input value should be of type org.joda.time.Duration but is java.lang.Integer",
					e.getMessage());
		}
	}
}
 
Example #21
Source File: ParseDurationTest.java    From super-csv with Apache License 2.0 5 votes vote down vote up
@Test
public void testNonStringInput() {
	for (CellProcessor p : processors) {
		try {
			p.execute(123, ANONYMOUS_CSVCONTEXT);
			fail("expecting SuperCsvCellProcessorException");
		} catch (SuperCsvCellProcessorException e) {
			assertEquals(
					"the input value should be of type java.lang.String but is java.lang.Integer",
					e.getMessage());
		}
	}
}
 
Example #22
Source File: FormatStopTypeAndName.java    From super-csv with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public Object execute(Object value, CsvContext context) {
	
	validateInputNotNull(value, context);
	
	String stopTypeAndName = (String) MAP.get(value);
	if( stopTypeAndName == null ) {
		throw new SuperCsvCellProcessorException(String.format("Unknown StopTypeAndName: %s", value), context, this);
	}
	return next.execute(stopTypeAndName, context);
}
 
Example #23
Source File: FmtNumberTest.java    From super-csv with Apache License 2.0 5 votes vote down vote up
/**
 * Tests execution with a invalid number format (should throw an Exception).
 */
@Test(expected = SuperCsvCellProcessorException.class)
public void testWithInvalidNumberFormat() {
	final double number = 12.34;
	CellProcessor invalidNumberFormatProcessor = new FmtNumber("%%%");
	invalidNumberFormatProcessor.execute(number, ANONYMOUS_CSVCONTEXT);
}
 
Example #24
Source File: FmtDuration.java    From super-csv with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 * 
 * @throws SuperCsvCellProcessorException
 *             if value is null or not a Duration
 */
public Object execute(final Object value, final CsvContext context) {
	validateInputNotNull(value, context);
	if (!(value instanceof Duration)) {
		throw new SuperCsvCellProcessorException(Duration.class, value,
				context, this);
	}
	final Duration duration = (Duration) value;
	final String result = duration.toString();
	return next.execute(result, context);
}
 
Example #25
Source File: ParseZonedDateTimeTest.java    From super-csv with Apache License 2.0 5 votes vote down vote up
@Theory
public void testNullInput(final ParseZonedDateTime p) {
	exception.expect(SuperCsvCellProcessorException.class);
	exception.expectMessage("this processor does not accept null input - "
		+ "if the column is optional then chain an Optional() processor before this one");
	p.execute(null, ANONYMOUS_CSVCONTEXT);
}
 
Example #26
Source File: ParseLocalTimeTest.java    From super-csv with Apache License 2.0 5 votes vote down vote up
@Test
public void testNonStringInput() {
	for (CellProcessor p : processors) {
		try {
			p.execute(123, ANONYMOUS_CSVCONTEXT);
			fail("expecting SuperCsvCellProcessorException");
		} catch (SuperCsvCellProcessorException e) {
			assertEquals(
					"the input value should be of type java.lang.String but is java.lang.Integer",
					e.getMessage());
		}
	}
}
 
Example #27
Source File: FmtLocalDateTimeTest.java    From super-csv with Apache License 2.0 5 votes vote down vote up
@Test
public void testInvalidDateFormat() {
	final CellProcessor p = new FmtLocalDateTime("not valid");
	try {
		p.execute(LOCAL_DATE_TIME, ANONYMOUS_CSVCONTEXT);
		fail("expecting SuperCsvCellProcessorException");
	} catch (SuperCsvCellProcessorException e) {
		assertEquals("Failed to format value as a LocalDateTime",
				e.getMessage());
	}
}
 
Example #28
Source File: FmtDateTimeZoneTest.java    From super-csv with Apache License 2.0 5 votes vote down vote up
@Test
public void testNullInput() {
	for (CellProcessor p : processors) {
		try {
			p.execute(null, ANONYMOUS_CSVCONTEXT);
			fail("expecting SuperCsvCellProcessorException");
		} catch (SuperCsvCellProcessorException e) {
			assertEquals(
					"this processor does not accept null input - "
							+ "if the column is optional then chain an Optional() processor before this one",
					e.getMessage());
		}
	}
}
 
Example #29
Source File: FmtInterval.java    From super-csv with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 * 
 * @throws SuperCsvCellProcessorException
 *             if value is null or not a Interval
 */
public Object execute(final Object value, final CsvContext context) {
	validateInputNotNull(value, context);
	if (!(value instanceof Interval)) {
		throw new SuperCsvCellProcessorException(Interval.class, value,
				context, this);
	}
	final Interval interval = (Interval) value;
	final String result = interval.toString();
	return next.execute(result, context);
}
 
Example #30
Source File: FmtPeriodTest.java    From super-csv with Apache License 2.0 5 votes vote down vote up
@Test
public void testNonPeriodInput() {
	for (CellProcessor p : processors) {
		try {
			p.execute(123, ANONYMOUS_CSVCONTEXT);
			fail("expecting SuperCsvCellProcessorException");
		} catch (SuperCsvCellProcessorException e) {
			assertEquals(
					"the input value should be of type org.joda.time.Period but is java.lang.Integer",
					e.getMessage());
		}
	}
}