class XMLFileModelPersister implements ModelPersister {

  @Override
  public void saveModel(Model model, @Nullable String modelName)
      throws IllegalStateException {
    requireNonNull(model, "model");

    LOG.debug("Saving model '{}', modelName is '{}'.", model.getName(), modelName);

    File modelFile = new File(dataDirectory, MODEL_FILE_NAME);
    // Check if writing the model is possible.
    checkState(dataDirectory.isDirectory() || dataDirectory.mkdirs(),
               "%s is not an existing directory and could not be created, either.",
               dataDirectory.getPath());
    checkState(!modelFile.exists() || modelFile.isFile(),
               "%s exists, but is not a regular file",
               modelFile.getPath());
    try {
      if (modelFile.exists()) {
        createBackup();
      }
      try (OutputStream outStream = new FileOutputStream(modelFile)) {
        XMLModelWriter writer = writerProvider.get();
        writer.writeXMLModel(model, modelName, outStream);
      }
    }
    catch (IOException exc) {
      throw new IllegalStateException("Exception saving model", exc);
    }
  }

  @Inject public  XMLFileModelPersister(@ApplicationHome File directory,
                               Provider<XMLModelReader> readerProvider,
                               Provider<XMLModelWriter> writerProvider);

}