[Minor] code cleanup in FileIo

This commit is contained in:
Robert von Burg 2023-04-06 10:21:31 +02:00
parent c909f9c46f
commit abf02c3c3d
Signed by: eitch
GPG Key ID: 75DB9C85C74331F7
1 changed files with 21 additions and 25 deletions

View File

@ -79,15 +79,9 @@ public class FileIo {
xmlWriter.flush();
}
if (this.path.exists() && !this.path.delete())
throw new IllegalStateException("Failed to delete existing file " + this.path.getAbsolutePath());
if (!this.tmpPath.renameTo(this.path)) {
throw new IllegalStateException(
"Failed to rename temp file " + this.tmpPath.getName() + " to " + this.path.getAbsolutePath());
}
if (ctx.getLastModified() != -1L)
this.path.setLastModified(ctx.getLastModified());
if (logger.isDebugEnabled())
logger.info(MessageFormat.format("Wrote SAX to {0}", this.tmpPath.getAbsolutePath()));
moveTmpFileToPath(ctx.getLastModified());
} catch (FactoryConfigurationError | XMLStreamException | IOException e) {
if (this.tmpPath.exists()) {
@ -158,8 +152,7 @@ public class FileIo {
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty(OutputKeys.ENCODING, encoding);
transformer
.setOutputProperty("{http://xml.apache.org/xalan}indent-amount", "2"); //$NON-NLS-2$
transformer.setOutputProperty("{http://xml.apache.org/xalan}indent-amount", "2"); //$NON-NLS-2$
// transformer.setOutputProperty("{http://xml.apache.org/xalan}line-separator", "\t");
// Transform to file
@ -169,20 +162,9 @@ public class FileIo {
transformer.transform(xmlSource, result);
}
if (logger.isDebugEnabled()) {
String msg = MessageFormat.format("Wrote DOM to {0}", this.tmpPath.getAbsolutePath());
logger.info(msg);
}
if (this.path.exists() && !this.path.delete())
throw new IllegalStateException("Failed to delete existing file " + this.path.getAbsolutePath());
if (!this.tmpPath.renameTo(this.path)) {
throw new IllegalStateException(
"Failed to rename temp file " + this.tmpPath.getName() + " to " + this.path.getAbsolutePath());
}
if (ctx.getLastModified() != -1L)
this.path.setLastModified(ctx.getLastModified());
if (logger.isDebugEnabled())
logger.info(MessageFormat.format("Wrote DOM to {0}", this.tmpPath.getAbsolutePath()));
moveTmpFileToPath(ctx.getLastModified());
} catch (IOException | TransformerFactoryConfigurationError | TransformerException e) {
if (this.tmpPath.exists()) {
@ -198,6 +180,20 @@ public class FileIo {
}
}
private void moveTmpFileToPath(long lastModified) {
if (this.path.exists() && !this.path.delete())
throw new IllegalStateException("Failed to delete existing file " + this.path.getAbsolutePath());
if (!this.tmpPath.renameTo(this.path)) {
throw new IllegalStateException(
"Failed to rename temp file " + this.tmpPath.getName() + " to " + this.path.getAbsolutePath());
}
if (lastModified != -1L) {
if (!this.path.setLastModified(lastModified))
logger.error("Failed to set last modified of path " + this.path.getAbsolutePath());
}
}
public <T> void readDom(PersistenceContext<T> ctx) {
try {