[Patchanim-commit] SF.net SVN: patchanim: [236] trunk/patchanim/src/com/mebigfatguy/patchanim/ io/P
Brought to you by:
dbrosius
|
From: <dbr...@us...> - 2008-02-24 16:01:01
|
Revision: 236
http://patchanim.svn.sourceforge.net/patchanim/?rev=236&view=rev
Author: dbrosius
Date: 2008-02-24 08:01:04 -0800 (Sun, 24 Feb 2008)
Log Message:
-----------
use save safe file idiom
Modified Paths:
--------------
trunk/patchanim/src/com/mebigfatguy/patchanim/io/PatchAnimIO.java
Modified: trunk/patchanim/src/com/mebigfatguy/patchanim/io/PatchAnimIO.java
===================================================================
--- trunk/patchanim/src/com/mebigfatguy/patchanim/io/PatchAnimIO.java 2008-02-24 15:38:47 UTC (rev 235)
+++ trunk/patchanim/src/com/mebigfatguy/patchanim/io/PatchAnimIO.java 2008-02-24 16:01:04 UTC (rev 236)
@@ -60,18 +60,18 @@
public static void saveFile(File f, PatchAnimDocument document) throws IOException {
InputStream xslIs = null;
OutputStream xmlOs = null;
+ File temporary = File.createTempFile("patchanim", "paf", f.getParentFile());
try {
- document.setDirty(false);
-
TransformerFactory tf = TransformerFactory.newInstance();
xslIs = new BufferedInputStream(PatchAnimIO.class.getResourceAsStream("/com/mebigfatguy/patchanim/io/PatchAnimDoc.xsl"));
Transformer t = tf.newTransformer(new StreamSource(xslIs));
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document d = db.newDocument();
- xmlOs = new BufferedOutputStream(new FileOutputStream(f));
+ xmlOs = new BufferedOutputStream(new FileOutputStream(temporary));
t.setParameter("doc", document);
t.transform(new DOMSource(d), new StreamResult(xmlOs));
+ document.setDirty(false);
} catch (IOException ioe) {
throw ioe;
} catch (Exception e) {
@@ -81,6 +81,9 @@
} finally {
Closer.close(xslIs);
Closer.close(xmlOs);
+ f.delete();
+ if (!temporary.renameTo(f))
+ throw new IOException("Failed saving file " + f.getPath());
}
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|