2017-06-28 1 views
0

以下のコードは、ヘッダーが常に事前にわかっていて、FILE_HEADER_MAPPINGの配列値を宣言できる場合、CSVレコードを解析します。動的ヘッダーCSVParser

CSVFormat csvFileFormat = CSVFormat.DEFAULT.withHeader(FILE_HEADER_MAPPING); 
FileReader fileReader = new FileReader("file"); 
CSVParser csvFileParser = new CSVParser(fileReader, csvFileFormat); 
Iterable<CSVRecord> records = csvFileParser.getRecords(); 

が、ヘッダは、各csvファイル異なりいるCSVファイルのためのCSVParserを作成する方法について説明します。

私は、各可能なCSVヘッダーのCSVパーサーを持ちたいフォーマット

CSVFormat csvFileFormat = CSVFormat.DEFAULT.withHeader(FILE_HEADER_MAPPING); 

を作成するには、CSVファイルのヘッダーを知ることができません。 このシナリオを解決するのを手伝ってください。

+0

これはapache commons CSVパーサーですか? – JoSSte

+0

はいそれはapache csvパーサーです。 – Sharan

+0

ヘッダーの自動検出についてお読みください? おそらく問題を解決する可能性があります: https://commons.apache.org/proper/commons-csv/user-guide.html#Header_auto_detection – JoSSte

答えて

0
package dfi.fin.dcm.syn.loantrading.engine.source.impl; 

import static dfi.fin.dcm.syn.loantrading.engine.task.impl.BackOfficeCSVHelper.AMOUNT; 
import static dfi.fin.dcm.syn.loantrading.engine.task.impl.BackOfficeCSVHelper.FCN; 
import static dfi.fin.dcm.syn.loantrading.engine.task.impl.BackOfficeCSVHelper.FEE_TYPE; 
import static dfi.fin.dcm.syn.loantrading.engine.task.impl.BackOfficeCSVHelper.LINE_TYPE; 
import static dfi.fin.dcm.syn.loantrading.engine.task.impl.BackOfficeCSVHelper.LINE_TYPE_VALUE_CARRY_EVT; 
import static dfi.fin.dcm.syn.loantrading.engine.task.impl.BackOfficeCSVHelper.MARKIT_ID; 
import static dfi.fin.dcm.syn.loantrading.engine.task.impl.BackOfficeCSVHelper.VALUE_DATE; 

import java.io.IOException; 
import java.io.InputStream; 
import java.math.BigDecimal; 
import java.text.ParseException; 
import java.text.SimpleDateFormat; 
import java.util.Arrays; 
import java.util.Calendar; 
import java.util.List; 

import com.csvreader.CsvReader.CatastrophicException; 
import com.csvreader.CsvReader.FinalizedException; 

import dfi.fin.dcm.syn.loantrading.engine.source.SourceException; 
import dfi.fin.dcm.syn.loantrading.model.portfolio.Portfolio; 

@Deprecated 
public class CarryEventStreamSource extends AbstractInputStreamSource<CarryEventData> { 

    private static String [] headers = {LINE_TYPE,VALUE_DATE,MARKIT_ID,FEE_TYPE,AMOUNT}; 


    private SimpleDateFormat dateFormat = null; 

    public CarryEventStreamSource(InputStream stream) { 
     super(stream); 
     dateFormat = new SimpleDateFormat("dd/MM/yy"); 
    } 

    public CarryEventData readNextElementInternal() throws SourceException, IOException, CatastrophicException, FinalizedException { 

     //skipping all events which are not Carry 
     boolean loop = true; 
     while (loop) { 
      // skipping all events which are not Carry 
      if(getReader().readRecord() && !getReader().get(LINE_TYPE).trim().equals(LINE_TYPE_VALUE_CARRY_EVT)) { 
       loop = true; 
      } else { 
       loop = false; 
      } 
     } 
     //EOF? 
     if (getReader().get(LINE_TYPE).trim().equals(LINE_TYPE_VALUE_CARRY_EVT)) { 
      CarryEventData toReturn = new CarryEventData(); 
      toReturn.setComputationDate(Calendar.getInstance().getTime()); 
      try { 
       toReturn.setValueDate(getDateFormat().parse(getReader().get(VALUE_DATE).trim())); 
      } catch (ParseException e) { 
       throw new SourceException(e); 
      } 
      if (!getPortfolio().getMtmSourceType().equals(Portfolio.MTM_SOURCE_TYPE_NONE)) { 
       if (getReader().get(MARKIT_ID).trim() == null) { 
        throw new SourceException("Back Office file invalid data format: the markit id is missing on line "+getReader().getCurrentRecord()); 
       } 
       toReturn.setTrancheMarkitId(getReader().get(MARKIT_ID).trim());    
      } else { 
       if (getReader().get(FCN)==null || "".equals(getReader().get(FCN).trim())) { 
        throw new SourceException("Back Office file invalid data format: missing loan tranche id on line "+getReader().getCurrentRecord());    
       } 
       toReturn.setTrancheMarkitId(getReader().get(FCN).trim());    
      } 
      if (getReader().get(FEE_TYPE).equals("")) { 
       toReturn.setFeeType(null); 
      } else { 
       toReturn.setFeeType(getReader().get(FEE_TYPE).trim());    
      } 
      if (getReader().get(AMOUNT)==null) { 
       throw new SourceException("Back Office file invalid data format: missing amount on line "+getReader().getCurrentRecord());    
      } 
      try { 
       toReturn.setAmount(new BigDecimal(getReader().get(AMOUNT))); 
      } catch (NumberFormatException ex) { 
       throw new SourceException(ex,"Back Office file invalid data format: invalid amount on line "+getReader().getCurrentRecord());        
      } 
      return toReturn; 
     } 

     // no carry found, null is returned 
     return null; 
    } 


    public SimpleDateFormat getDateFormat() { 
     return dateFormat; 
    } 

    public void setDateFormat(SimpleDateFormat dateFormat) { 
     this.dateFormat = dateFormat; 
    } 

    @Override 
    public char getDelimiter() { 
     return ','; 
    } 

    @Override 
    public List<String> getHeaderSet() { 
     return Arrays.asList(headers); 
    } 

    @Override 
    public String getName() { 
     return "File import"; 



     package dfi.fin.dcm.syn.loantrading.engine.source.impl; 

import java.io.IOException; 
import java.io.InputStream; 
import java.math.BigDecimal; 
import java.text.ParseException; 
import java.text.SimpleDateFormat; 
import java.util.Arrays; 
import java.util.Calendar; 
import java.util.List; 

import com.csvreader.CsvReader.CatastrophicException; 
import com.csvreader.CsvReader.FinalizedException; 

import dfi.fin.dcm.syn.loantrading.engine.source.SourceException; 
import dfi.fin.dcm.syn.loantrading.model.common.LTCurrency; 
import dfi.fin.dcm.syn.loantrading.model.engine.event.CurrencyEvent; 

public class SpotForexRateStreamSource extends AbstractInputStreamSource<CurrencyEvent> { 

    private SimpleDateFormat dateFormat; 

    private static String [] headers = {"CURRENCY","DATE","MID"}; 

    public SpotForexRateStreamSource(InputStream stream) { 
     super(stream); 
     dateFormat = new SimpleDateFormat("dd/MM/yy"); 
    } 

    @Override 
    public CurrencyEvent readNextElementInternal() throws SourceException, IOException, FinalizedException, CatastrophicException { 
     //skipping all events which are not Trade 
     if (getReader().readRecord()) { 
      CurrencyEvent event = new CurrencyEvent(); 
      //retrieving the currency 
      LTCurrency currency = getCurrencyDAO().getLTCurrencyByISOCode(getReader().get("CURRENCY")); 
      event.setCurrency(currency); 
      try { 
       event.setDate(getDateFormat().parse(getReader().get("DATE"))); 
      } catch (ParseException e) { 
       throw new SourceException(e, "Parse error while reading currency event date"); 
      } 
      event.setExchangeRate(new BigDecimal(getReader().get("MID"))); 
      event.setComputationDate(Calendar.getInstance().getTime()); 
      return event; 
     } 
     return null; 
    } 
    @Override 
    public char getDelimiter() { 
     return ';'; 
    } 

    public SimpleDateFormat getDateFormat() { 
     return dateFormat; 
    } 

    public void setDateFormat(SimpleDateFormat dateFormat) { 
     this.dateFormat = dateFormat; 
    } 

    @Override 
    public List<String> getHeaderSet() { 
     return Arrays.asList(headers); 
    } 

    @Override 
    public String getName() { 
     return "CSV File"; 
    } 

} 

    } 


} 
関連する問題