Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Freight: possibility to create output-directory when writing carrier files #2776

Closed
wants to merge 8 commits into from
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.matsim.vehicles.VehicleType;

import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.util.*;

Expand Down Expand Up @@ -77,11 +78,11 @@ public void putAttributeConverters( final Map<Class<?>, AttributeConverter<?>> c
}

/**
* Writes carriers and their plans into a xml-file.
* This now writes out the carriers file.
*
* @param filename should be the target xml-file
* @param filename should be the target xml-file including the path to it.
*/
public void write(String filename) {
private void writeCarrierPlan(String filename) {
logger.info("write carrier plans");
try {
openFile(filename);
Expand All @@ -105,6 +106,37 @@ public void write(String filename) {
}
}

/**
* Writes carriers and their plans into a xml-file.
* When using this, the directory, where the file goes to must be present.
*
* @deprecated Please use the method write(String filename, boolean createDirectory, String directoryPath) instead. KMT oct'23
* @param filename should be the target xml-file
*/
@Deprecated(since = "oct'23")
public void write(String filename) {
write(filename, false);
}

public void write(String filename, boolean createDirectory) {
File file = new File(filename);
String directoryPath = file.getParent();

if (createDirectory) {
if (!directoryPath.isEmpty()) {
File dir = new File(directoryPath);
if (!dir.mkdir()) {
if (dir.exists()) {
logger.info("directory " + directoryPath + " exist already.");
} else {
logger.warn("Failed to create directory: " + directoryPath);
}
}
}
}
writeCarrierPlan(filename);
}

private void writeRootElement() throws UncheckedIOException, IOException {
List<Tuple<String, String>> atts = new ArrayList<>();
atts.add(createTuple(XMLNS, MatsimXmlWriter.MATSIM_NAMESPACE));
Expand Down