/** Saves the content of a given system store to file. */ public static SystemStore save(File file, SystemStore store, boolean clearDir) throws IOException { if (!GRAMMAR.hasExtension(file)) { throw new IOException(String.format("File '%s' does not refer to a production system", file)); } // if the file already exists, rename it // in order to be able to restore if saving fails File newFile = null; if (file.exists()) { newFile = file; do { newFile = new File(newFile.getParent(), "Copy of " + newFile.getName()); } while (newFile.exists()); if (clearDir) { if (!file.renameTo(newFile)) { throw new IOException(String.format("Can't save grammar to existing file '%s'", file)); } } else { Util.copyDirectory(file, newFile, true); } } try { DefaultFileSystemStore result = new DefaultFileSystemStore(file, true); result.reload(); // save properties for (ResourceKind kind : ResourceKind.values()) { if (kind == PROPERTIES) { result.putProperties(store.getProperties()); } else if (kind.isTextBased()) { result.putTexts(kind, store.getTexts(kind)); } else { result.putGraphs(kind, store.getGraphs(kind).values(), false); } } if (newFile != null) { boolean deleted = deleteRecursive(newFile); assert deleted : String.format("Failed to delete '%s'", newFile); } return result; } catch (IOException exc) { file.delete(); // attempt to re-rename previously existing file if (newFile != null) { newFile.renameTo(file); } throw exc; } }
@Override public SystemStore save(File file, boolean clearDir) throws IOException { return SystemStore.save(file, this, clearDir); }