1 package charactermanaj.model.io;
\r
3 import java.awt.image.BufferedImage;
\r
5 import java.io.IOException;
\r
6 import java.util.Map;
\r
7 import java.util.Properties;
\r
8 import java.util.logging.Level;
\r
9 import java.util.logging.Logger;
\r
11 import charactermanaj.model.CharacterData;
\r
12 import charactermanaj.model.PartsIdentifier;
\r
13 import charactermanaj.model.PartsSpec;
\r
15 public abstract class AbstractCharacterDataFileWriter implements CharacterDataWriter {
\r
20 private static final Logger logger = Logger.getLogger(AbstractCharacterDataFileWriter.class.getName());
\r
22 protected File outFile;
\r
24 protected File tmpFile;
\r
26 protected Exception occureException;
\r
28 protected AbstractCharacterDataFileWriter(File outFile) throws IOException {
\r
29 if (outFile == null) {
\r
30 throw new IllegalArgumentException();
\r
33 if (outFile.exists()) {
\r
34 if (!outFile.canWrite()) {
\r
35 throw new IOException("not writable: " + outFile);
\r
38 File tmpFile = new File(outFile.getPath() + ".tmp");
\r
40 this.tmpFile = tmpFile;
\r
41 this.outFile = outFile;
\r
44 public void writeExportProp(Properties prop) throws IOException {
\r
46 throw new IllegalArgumentException();
\r
50 internalWriteExportProp(prop);
\r
52 } catch (IOException ex) {
\r
53 occureException = ex;
\r
56 } catch (Exception ex) {
\r
57 occureException = ex;
\r
58 IOException ex2 = new IOException("write characterdata failed.");
\r
64 protected abstract void internalWriteExportProp(Properties prop) throws IOException;
\r
67 public void writeCharacterData(CharacterData characterData) throws IOException {
\r
68 if (characterData == null) {
\r
69 throw new IllegalArgumentException();
\r
73 internalWriteCharacterData(characterData);
\r
75 } catch (IOException ex) {
\r
76 occureException = ex;
\r
79 } catch (Exception ex) {
\r
80 occureException = ex;
\r
81 IOException ex2 = new IOException("write characterdata failed.");
\r
87 protected abstract void internalWriteCharacterData(CharacterData characterData) throws IOException;
\r
89 public void writeTextUTF16LE(String name, String contents) throws IOException {
\r
91 throw new IllegalArgumentException();
\r
95 internalWriteTextUTF16LE(name, contents);
\r
97 } catch (IOException ex) {
\r
98 occureException = ex;
\r
101 } catch (Exception ex) {
\r
102 occureException = ex;
\r
103 IOException ex2 = new IOException("internalWriteTextUTF16 failed.");
\r
109 protected abstract void internalWriteTextUTF16LE(String name, String contents) throws IOException;
\r
111 public void writeSamplePicture(BufferedImage samplePicture) throws IOException {
\r
112 if (samplePicture == null) {
\r
113 throw new IllegalArgumentException();
\r
117 internalWriteSamplePicture(samplePicture);
\r
119 } catch (IOException ex) {
\r
120 occureException = ex;
\r
123 } catch (Exception ex) {
\r
124 occureException = ex;
\r
125 IOException ex2 = new IOException("write sample picture failed.");
\r
131 protected abstract void internalWriteSamplePicture(BufferedImage samplePicture) throws IOException;
\r
133 public void writePartsImages(Map<PartsIdentifier, PartsSpec> partsImages) throws IOException {
\r
134 if (partsImages == null) {
\r
135 throw new IllegalArgumentException();
\r
139 internalWritePartsImages(partsImages);
\r
141 } catch (IOException ex) {
\r
142 occureException = ex;
\r
145 } catch (Exception ex) {
\r
146 occureException = ex;
\r
147 IOException ex2 = new IOException("write parts images failed.");
\r
153 protected abstract void internalWritePartsImages(Map<PartsIdentifier, PartsSpec> partsImages) throws IOException;
\r
155 public void writePartsManageData(Map<PartsIdentifier, PartsSpec> partsImages) throws IOException {
\r
156 if (partsImages == null) {
\r
157 throw new IllegalArgumentException();
\r
161 internalWritePartsManageData(partsImages);
\r
163 } catch (IOException ex) {
\r
164 occureException = ex;
\r
167 } catch (Exception ex) {
\r
168 occureException = ex;
\r
169 IOException ex2 = new IOException("write parts images failed.");
\r
175 protected abstract void internalWritePartsManageData(Map<PartsIdentifier, PartsSpec> partsImages) throws IOException;
\r
178 public void close() throws IOException {
\r
182 if (outFile.exists()) {
\r
183 if (!outFile.delete()) {
\r
184 throw new IOException("old file can't delete. " + outFile);
\r
188 } catch (Exception ex) {
\r
189 if (occureException == null) {
\r
190 occureException = ex;
\r
193 if (occureException != null) {
\r
194 if (!tmpFile.delete()) {
\r
195 logger.log(Level.WARNING, "temporary file can't delete. " + tmpFile);
\r
200 if (!tmpFile.renameTo(outFile)) {
\r
201 throw new IOException("rename failed. " + tmpFile);
\r
205 protected abstract void internalClose() throws IOException;
\r