OSDN Git Service

mavenによるビルドに変更
[charactermanaj/CharacterManaJ.git] / src / main / java / charactermanaj / model / io / AbstractCharacterDataFileWriter.java
1 package charactermanaj.model.io;\r
2 \r
3 import java.awt.image.BufferedImage;\r
4 import java.io.File;\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
10 \r
11 import charactermanaj.model.CharacterData;\r
12 import charactermanaj.model.PartsIdentifier;\r
13 import charactermanaj.model.PartsSpec;\r
14 \r
15 public abstract class AbstractCharacterDataFileWriter implements CharacterDataWriter {\r
16 \r
17         /**\r
18          * ロガー\r
19          */\r
20         private static final Logger logger = Logger.getLogger(AbstractCharacterDataFileWriter.class.getName());\r
21 \r
22         protected File outFile;\r
23         \r
24         protected File tmpFile;\r
25         \r
26         protected Exception occureException;\r
27         \r
28         protected AbstractCharacterDataFileWriter(File outFile) throws IOException {\r
29                 if (outFile == null) {\r
30                         throw new IllegalArgumentException();\r
31                 }\r
32                 \r
33                 if (outFile.exists()) {\r
34                         if (!outFile.canWrite()) {\r
35                                 throw new IOException("not writable: " + outFile);\r
36                         }\r
37                 }\r
38                 File tmpFile = new File(outFile.getPath() + ".tmp");\r
39                 \r
40                 this.tmpFile = tmpFile;\r
41                 this.outFile = outFile;\r
42         }\r
43         \r
44         public void writeExportProp(Properties prop) throws IOException {\r
45                 if (prop == null) {\r
46                         throw new IllegalArgumentException();\r
47                 }\r
48                 \r
49                 try {\r
50                         internalWriteExportProp(prop);\r
51                         \r
52                 } catch (IOException ex) {\r
53                         occureException = ex;\r
54                         throw ex;\r
55 \r
56                 } catch (Exception ex) {\r
57                         occureException = ex;\r
58                         IOException ex2 = new IOException("write characterdata failed.");\r
59                         ex2.initCause(ex);\r
60                         throw ex2;\r
61                 }\r
62         }\r
63         \r
64         protected abstract void internalWriteExportProp(Properties prop) throws IOException;\r
65         \r
66 \r
67         public void writeCharacterData(CharacterData characterData) throws IOException {\r
68                 if (characterData == null) {\r
69                         throw new IllegalArgumentException();\r
70                 }\r
71                 \r
72                 try {\r
73                         internalWriteCharacterData(characterData);\r
74                         \r
75                 } catch (IOException ex) {\r
76                         occureException = ex;\r
77                         throw ex;\r
78 \r
79                 } catch (Exception ex) {\r
80                         occureException = ex;\r
81                         IOException ex2 = new IOException("write characterdata failed.");\r
82                         ex2.initCause(ex);\r
83                         throw ex2;\r
84                 }\r
85         }\r
86         \r
87         protected abstract void internalWriteCharacterData(CharacterData characterData) throws IOException;\r
88         \r
89         public void writeTextUTF16LE(String name, String contents) throws IOException {\r
90                 if (name == null) {\r
91                         throw new IllegalArgumentException();\r
92                 }\r
93                 \r
94                 try {\r
95                         internalWriteTextUTF16LE(name, contents);\r
96                         \r
97                 } catch (IOException ex) {\r
98                         occureException = ex;\r
99                         throw ex;\r
100 \r
101                 } catch (Exception ex) {\r
102                         occureException = ex;\r
103                         IOException ex2 = new IOException("internalWriteTextUTF16 failed.");\r
104                         ex2.initCause(ex);\r
105                         throw ex2;\r
106                 }\r
107         }\r
108         \r
109         protected abstract void internalWriteTextUTF16LE(String name, String contents) throws IOException;\r
110         \r
111         public void writeSamplePicture(BufferedImage samplePicture) throws IOException {\r
112                 if (samplePicture == null) {\r
113                         throw new IllegalArgumentException();\r
114                 }\r
115 \r
116                 try {\r
117                         internalWriteSamplePicture(samplePicture);\r
118 \r
119                 } catch (IOException ex) {\r
120                         occureException = ex;\r
121                         throw ex;\r
122 \r
123                 } catch (Exception ex) {\r
124                         occureException = ex;\r
125                         IOException ex2 = new IOException("write sample picture failed.");\r
126                         ex2.initCause(ex);\r
127                         throw ex2;\r
128                 }\r
129         }\r
130 \r
131         protected abstract void internalWriteSamplePicture(BufferedImage samplePicture) throws IOException;\r
132 \r
133         public void writePartsImages(Map<PartsIdentifier, PartsSpec> partsImages) throws IOException {\r
134                 if (partsImages == null) {\r
135                         throw new IllegalArgumentException();\r
136                 }\r
137 \r
138                 try {\r
139                         internalWritePartsImages(partsImages);\r
140 \r
141                 } catch (IOException ex) {\r
142                         occureException = ex;\r
143                         throw ex;\r
144 \r
145                 } catch (Exception ex) {\r
146                         occureException = ex;\r
147                         IOException ex2 = new IOException("write parts images failed.");\r
148                         ex2.initCause(ex);\r
149                         throw ex2;\r
150                 }\r
151         }\r
152         \r
153         protected abstract void internalWritePartsImages(Map<PartsIdentifier, PartsSpec> partsImages) throws IOException;\r
154 \r
155         public void writePartsManageData(Map<PartsIdentifier, PartsSpec> partsImages) throws IOException {\r
156                 if (partsImages == null) {\r
157                         throw new IllegalArgumentException();\r
158                 }\r
159 \r
160                 try {\r
161                         internalWritePartsManageData(partsImages);\r
162 \r
163                 } catch (IOException ex) {\r
164                         occureException = ex;\r
165                         throw ex;\r
166 \r
167                 } catch (Exception ex) {\r
168                         occureException = ex;\r
169                         IOException ex2 = new IOException("write parts images failed.");\r
170                         ex2.initCause(ex);\r
171                         throw ex2;\r
172                 }\r
173         }\r
174         \r
175         protected abstract void internalWritePartsManageData(Map<PartsIdentifier, PartsSpec> partsImages) throws IOException;\r
176 \r
177         \r
178         public void close() throws IOException {\r
179                 try {\r
180                         internalClose();\r
181                         \r
182                         if (outFile.exists()) {\r
183                                 if (!outFile.delete()) {\r
184                                         throw new IOException("old file can't delete. " + outFile);\r
185                                 }\r
186                         }\r
187 \r
188                 } catch (Exception ex) {\r
189                         if (occureException == null) {\r
190                                 occureException = ex;\r
191                         }\r
192                 }\r
193                 if (occureException != null) {\r
194                         if (!tmpFile.delete()) {\r
195                                 logger.log(Level.WARNING, "temporary file can't delete. " + tmpFile);\r
196                         }\r
197                         return;\r
198                 }\r
199                 \r
200                 if (!tmpFile.renameTo(outFile)) {\r
201                         throw new IOException("rename failed. " + tmpFile);\r
202                 }\r
203         }\r
204         \r
205         protected abstract void internalClose() throws IOException;\r
206 }\r