org.onebusaway.gtfs.serialization.GtfsWriter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of onebusaway-gtfs Show documentation
Show all versions of onebusaway-gtfs Show documentation
A Java library for reading and writing General Transit Feed Spec feeds
package org.onebusaway.gtfs.serialization;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.onebusaway.gtfs.csv.CsvEntityWriter;
import org.onebusaway.gtfs.csv.schema.DefaultEntitySchemaFactory;
import org.onebusaway.gtfs.services.GtfsDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class GtfsWriter extends CsvEntityWriter {
private final Logger _log = LoggerFactory.getLogger(GtfsWriter.class);
public static final String KEY_CONTEXT = GtfsWriter.class.getName()
+ ".context";
private List> _entityClasses = new ArrayList>();
private Map, Comparator>> _entityComparators = new HashMap, Comparator>>();
public GtfsWriter() {
/**
* Prep the Entity Schema Factories
*/
_entityClasses.addAll(GtfsEntitySchemaFactory.getEntityClasses());
_entityComparators.putAll(GtfsEntitySchemaFactory.getEntityComparators());
DefaultEntitySchemaFactory schemaFactory = createEntitySchemaFactory();
setEntitySchemaFactory(schemaFactory);
}
public List> getEntityClasses() {
return _entityClasses;
}
public Map,Comparator>> getEntityComparators() {
return _entityComparators;
}
public void run(GtfsDao dao) throws IOException {
List> classes = getEntityClasses();
for (Class> entityClass : classes) {
_log.info("writing entities: " + entityClass.getName());
Collection> entities = dao.getAllEntitiesForType(entityClass);
entities = sortEntities(entityClass,entities);
for (Object entity : entities)
handleEntity(entity);
flush();
}
close();
}
@SuppressWarnings("unchecked")
private Collection> sortEntities(Class> entityClass, Collection> entities) {
Comparator