Java Code Examples for org.supercsv.util.CsvContext#getLineNumber()

The following examples show how to use org.supercsv.util.CsvContext#getLineNumber() . 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: SuperCsvCellProcessorExceptionTest.java    From super-csv with Apache License 2.0 6 votes vote down vote up
/**
 * Tests the integrity of the CsvContext in a <code>SuperCsvCellProcessorException</code>
 */
@Test
public void testCsvContext() {
	// This is my reference context. It is the control object to use as the reference for assertions
	CsvContext rc = new CsvContext(1, 2, 3);   // line, row, col
	
	// This is the test context object to use for the test. It must have the same
	// values as the reference context. 
	CsvContext tc = new CsvContext(rc.getLineNumber(), rc.getRowNumber(), rc.getColumnNumber()); 
	
	SuperCsvCellProcessorException e = new SuperCsvCellProcessorException
		(String.class, 123, tc, PROCESSOR);

	// Pre-condition check
	assertEquals(rc, e.getCsvContext());
	
	// Test steps
	tc.setColumnNumber(2*rc.getColumnNumber() + 50);   // Set a column # that is different than the reference
	
	// Check that the exception still returns the context that it was created with
	assertEquals(rc, e.getCsvContext());
}
 
Example 2
Source File: SuperCsvRowException.java    From super-csv-annotation with Apache License 2.0 6 votes vote down vote up
private CsvContext cloneCsvContext(final CsvContext context) {
    
    CsvContext cloned = new CsvContext(
            context.getLineNumber(),
            context.getRowNumber(),
            context.getColumnNumber());
    
    // shallow copy
    List<Object> destRowSource = new ArrayList<Object>(context.getRowSource().size());
    for(Object obj : context.getRowSource()) {
        destRowSource.add(obj);
    }
    cloned.setRowSource(destRowSource);
    
    return cloned;
}
 
Example 3
Source File: AbstractCsvWriter.java    From super-csv with Apache License 2.0 5 votes vote down vote up
/**
 * Writes one or more String columns as a line to the CsvWriter.
 * 
 * @param columns
 *            the columns to write
 * @throws IllegalArgumentException
 *             if columns.length == 0
 * @throws IOException
 *             If an I/O error occurs
 * @throws NullPointerException
 *             if columns is null
 */
protected void writeRow(final String... columns) throws IOException {
	
	if( columns == null ) {
		throw new NullPointerException(String.format("columns to write should not be null on line %d", lineNumber));
	} else if( columns.length == 0 ) {
		throw new IllegalArgumentException(String.format("columns to write should not be empty on line %d",
			lineNumber));
	}
	
	StringBuilder builder = new StringBuilder();
	for( int i = 0; i < columns.length; i++ ) {
		
		columnNumber = i + 1; // column no used by CsvEncoder
		
		if( i > 0 ) {
			builder.append((char) preference.getDelimiterChar()); // delimiter
		}
		
		final String csvElement = columns[i];
		if( csvElement != null ) {
			final CsvContext context = new CsvContext(lineNumber, rowNumber, columnNumber);
			final String escapedCsv = encoder.encode(csvElement, context, preference);
			builder.append(escapedCsv);
			lineNumber = context.getLineNumber(); // line number can increment when encoding multi-line columns
		}
		
	}
	
	builder.append(preference.getEndOfLineSymbols()); // EOL
	writer.write(builder.toString());
}
 
Example 4
Source File: UniqueHashCode.java    From super-csv-annotation with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public Object execute(final Object value, final CsvContext context) {
    
    if(value == null) {
        return next.execute(value, context);
    }
    
    final T result = (T)value;
    final int hashCode = value.hashCode();
    
    if(encounteredElements.containsKey(hashCode)) {
        
        final ValueObject duplicatedObject = encounteredElements.get(result);
        throw createValidationException(context)
            .messageFormat("duplicate hashCode '%s' encountered.", hashCode)
            .rejectedValue(result)
            .messageVariables("hashCode", hashCode)
            .messageVariables("duplicatedRowNumber", duplicatedObject.rowNumber)
            .messageVariables("duplicatedLineNumber", duplicatedObject.lineNumber)
            .messageVariables("printer", getPrinter())
            .build();
        
    } else {
        final ValueObject object = new ValueObject(hashCode, context.getRowNumber(), context.getLineNumber());
        encounteredElements.put(object.hashCode, object);
    }
    
    return next.execute(value, context);
}
 
Example 5
Source File: Unique.java    From super-csv-annotation with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public Object execute(final Object value, final CsvContext context) {
    
    if(value == null) {
        return next.execute(value, context);
    }
    
    final T result = (T)value;
    
    if(encounteredElements.containsKey(result)) {
        
        final String formattedValue = printer.print(result);
        final ValueObject duplicatedObject = encounteredElements.get(result);
        throw createValidationException(context)
            .messageFormat("duplicate value '%s' encountered.", formattedValue)
            .rejectedValue(result)
            .messageVariables("duplicatedLineNumber", duplicatedObject.lineNumber)
            .messageVariables("duplicatedRowNumber", duplicatedObject.rowNumber)
            .messageVariables("printer", getPrinter())
            .build();
        
    } else {
        final ValueObject object = new ValueObject(result, context.getLineNumber(), context.getRowNumber());
        encounteredElements.put(object.value, object);
    }
    
    return next.execute(value, context);
}
 
Example 6
Source File: DefaultCsvEncoderTest.java    From super-csv with Apache License 2.0 4 votes vote down vote up
/**
 * Tests the encode() method with various preferences.
 */
@Test
public void testEncode() {
	
	final CsvContext context = new CsvContext(1, 1, 1);
	
	final String empty = "";
	assertEquals("", csvEncoder.encode(empty, context, PREFS));
	assertEquals("", csvEncoder.encode(empty, context, SURROUNDING_SPACES_REQUIRE_QUOTES_PREFS));
	assertEquals("\"\"", csvEncoder.encode(empty, context, ALWAYS_QUOTE_PREFS));
	
	final String space = " ";
	assertEquals(" ", csvEncoder.encode(space, context, PREFS));
	assertEquals("\" \"", csvEncoder.encode(space, context, SURROUNDING_SPACES_REQUIRE_QUOTES_PREFS));
	assertEquals("\" \"", csvEncoder.encode(space, context, ALWAYS_QUOTE_PREFS));
	
	final String leadingSpace = " leading space";
	assertEquals(leadingSpace, csvEncoder.encode(leadingSpace, context, PREFS));
	assertEquals("\" leading space\"",
		csvEncoder.encode(leadingSpace, context, SURROUNDING_SPACES_REQUIRE_QUOTES_PREFS));
	assertEquals("\" leading space\"", csvEncoder.encode(leadingSpace, context, ALWAYS_QUOTE_PREFS));
	
	final String trailingSpace = "trailing space ";
	assertEquals(trailingSpace, csvEncoder.encode(trailingSpace, context, PREFS));
	assertEquals("\"trailing space \"",
		csvEncoder.encode(trailingSpace, context, SURROUNDING_SPACES_REQUIRE_QUOTES_PREFS));
	assertEquals("\"trailing space \"", csvEncoder.encode(trailingSpace, context, ALWAYS_QUOTE_PREFS));
	
	final String normal = "just a normal phrase";
	assertEquals(normal, csvEncoder.encode(normal, context, PREFS));
	assertEquals(normal, csvEncoder.encode(normal, context, SURROUNDING_SPACES_REQUIRE_QUOTES_PREFS));
	assertEquals("\"just a normal phrase\"", csvEncoder.encode(normal, context, ALWAYS_QUOTE_PREFS));
	
	final String comma = "oh look, a comma";
	assertEquals("\"oh look, a comma\"", csvEncoder.encode(comma, context, PREFS));
	assertEquals("\"oh look, a comma\"", csvEncoder.encode(comma, context, SURROUNDING_SPACES_REQUIRE_QUOTES_PREFS));
	assertEquals("\"oh look, a comma\"", csvEncoder.encode(comma, context, ALWAYS_QUOTE_PREFS));
	
	final String quoted = "\"Watch out for quotes\", he said";
	assertEquals("\"\"\"Watch out for quotes\"\", he said\"", csvEncoder.encode(quoted, context, PREFS));
	assertEquals("\"\"\"Watch out for quotes\"\", he said\"",
		csvEncoder.encode(quoted, context, SURROUNDING_SPACES_REQUIRE_QUOTES_PREFS));
	assertEquals("\"\"\"Watch out for quotes\"\", he said\"",
		csvEncoder.encode(quoted, context, ALWAYS_QUOTE_PREFS));
	
	final String twoLines = "text that spans\ntwo lines";
	int lineNumber = context.getLineNumber();
	assertEquals("\"text that spans\r\ntwo lines\"", csvEncoder.encode(twoLines, context, PREFS));
	assertEquals(++lineNumber, context.getLineNumber());
	assertEquals("\"text that spans\r\ntwo lines\"",
		csvEncoder.encode(twoLines, context, SURROUNDING_SPACES_REQUIRE_QUOTES_PREFS));
	assertEquals(++lineNumber, context.getLineNumber());
	assertEquals("\"text that spans\r\ntwo lines\"", csvEncoder.encode(twoLines, context, ALWAYS_QUOTE_PREFS));
	assertEquals(++lineNumber, context.getLineNumber());
	
	final String quotesTwoLines = "text \"with quotes\" that spans\ntwo lines";
	assertEquals("\"text \"\"with quotes\"\" that spans\r\ntwo lines\"",
		csvEncoder.encode(quotesTwoLines, context, PREFS));
	assertEquals(++lineNumber, context.getLineNumber());
	assertEquals("\"text \"\"with quotes\"\" that spans\r\ntwo lines\"",
		csvEncoder.encode(quotesTwoLines, context, SURROUNDING_SPACES_REQUIRE_QUOTES_PREFS));
	assertEquals(++lineNumber, context.getLineNumber());
	assertEquals("\"text \"\"with quotes\"\" that spans\r\ntwo lines\"",
		csvEncoder.encode(quotesTwoLines, context, ALWAYS_QUOTE_PREFS));
	assertEquals(++lineNumber, context.getLineNumber());
}