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
/**
* Copyright (C) 2011 Brian Ferris
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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.csv_entities.CsvEntityWriter;
import org.onebusaway.csv_entities.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