All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.mygreen.supercsv.exception.SuperCsvRowException Maven / Gradle / Ivy

Go to download

CSVのJavaライブラリであるSuperCSVに、アノテーション機能を追加したライブラリです。

There is a newer version: 2.3
Show newest version
package com.github.mygreen.supercsv.exception;

import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import org.supercsv.exception.SuperCsvCellProcessorException;
import org.supercsv.exception.SuperCsvException;
import org.supercsv.util.CsvContext;


/**
 * 行のエラーをまとめた例外。
 *
 * @author T.TSUCHIE
 *
 */
public class SuperCsvRowException extends SuperCsvException {
    
    /** serialVersionUID */
    private static final long serialVersionUID = 1L;
    
    private final List columnErrors = new ArrayList<>();
    
    public SuperCsvRowException(final String msg, final CsvContext context) {
        super(msg, context);
    }
    
    public void addError(final SuperCsvException error) {
        this.columnErrors.add(error);
    }
    
    public void addError(final SuperCsvCellProcessorException error) {
        
        final SuperCsvCellProcessorException cloned;
        if(error instanceof SuperCsvValidationException) {
            cloned = ((SuperCsvValidationException)error).clone();
        } else {
            cloned = new SuperCsvCellProcessorException(error.getMessage(),
                    cloneCsvContext(error.getCsvContext()),
                    error.getProcessor());
        }
        
        this.columnErrors.add(cloned);
    }
    
    private CsvContext cloneCsvContext(final CsvContext context) {
        
        CsvContext cloned = new CsvContext(
                context.getLineNumber(),
                context.getRowNumber(),
                context.getColumnNumber());
        
        // shallow copy
        List destRowSource = new ArrayList(context.getRowSource().size());
        for(Object obj : context.getRowSource()) {
            destRowSource.add(obj);
        }
        cloned.setRowSource(destRowSource);
        
        return cloned;
    }
    
    public void addAllErrors(final List errors) {
        for(SuperCsvException error : errors) {
            addError(error);
        }
    }
    
    public List getColumnErrors() {
        return columnErrors;
    }
    
    public boolean isEmptyColumnErrors() {
        return getColumnErrors().isEmpty();
    }
    
    public boolean isNotEmptyColumnErrors() {
        return !isEmptyColumnErrors();
    }
    
    @Override
    public void printStackTrace(final PrintStream s) {
        
        super.printStackTrace(s);
        
        int count = 1;
        for(SuperCsvException e : columnErrors) {
            s.printf("[ColumnError-%d] : ", count);
            e.printStackTrace(s);
            count++;
        }
        
    }
    
    @Override
    public void printStackTrace(final PrintWriter s) {
        
        super.printStackTrace(s);
        
        int count = 1;
        for(SuperCsvException e : columnErrors) {
            s.printf("[ColumnError-%d] : ", count);
            e.printStackTrace(s);
            count++;
        }
        
    }
    
    
}