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

org.onebusaway.gtfs.csv.IndividualCsvEntityWriter Maven / Gradle / Ivy

There is a newer version: 1.3.4
Show newest version
/**
 * 
 */
package org.onebusaway.gtfs.csv;

import org.onebusaway.gtfs.csv.schema.BeanWrapper;
import org.onebusaway.gtfs.csv.schema.BeanWrapperFactory;
import org.onebusaway.gtfs.csv.schema.EntitySchema;
import org.onebusaway.gtfs.csv.schema.FieldMapping;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

class IndividualCsvEntityWriter implements EntityHandler {

  private PrintWriter _writer;

  private List _fieldNames = new ArrayList();

  private EntitySchema _schema;

  private CsvEntityContext _context;

  public IndividualCsvEntityWriter(CsvEntityContext context,
      EntitySchema schema, PrintWriter writer) {
    _writer = writer;
    _schema = schema;
    _context = context;

    _fieldNames.clear();
    for (FieldMapping field : _schema.getFields())
      field.getCSVFieldNames(_fieldNames);
    _writer.println(CSVLibrary.getIterableAsCSV(_fieldNames));
  }

  public void handleEntity(Object object) {
    BeanWrapper wrapper = BeanWrapperFactory.wrap(object);
    Map csvValues = new HashMap();
    for (FieldMapping field : _schema.getFields())
      field.translateFromObjectToCSV(_context, wrapper, csvValues);
    List values = new ArrayList(csvValues.size());
    for (String fieldName : _fieldNames) {
      Object value = csvValues.get(fieldName);
      if (value == null)
        value = "";
      values.add(value);
    }
    _writer.println(CSVLibrary.getIterableAsCSV(values));
  }

  public void flush() {
    _writer.flush();
  }

  public void close() {
    _writer.close();
  }

}