1
2
3
4
5
6 package yawn.io.xml;
7
8 import java.io.File;
9 import java.io.FileInputStream;
10 import java.io.FileNotFoundException;
11 import java.io.FileOutputStream;
12 import java.io.IOException;
13 import java.io.InputStreamReader;
14 import java.io.OutputStreamWriter;
15
16 import yawn.io.DuplicateKeyException;
17 import yawn.io.IStorage;
18 import yawn.io.KeyNotPresentException;
19 import yawn.io.StorageException;
20 import yawn.io.serialization.ISerializer;
21 import yawn.io.serialization.SerializationException;
22 import yawn.io.serialization.xml.CastorXmlSerializer;
23 import yawn.nn.NeuralNetwork;
24
25 /***
26 *
27 * <p>$Id: XmlStorage.java,v 1.5 2005/05/09 11:04:58 supermarti Exp $</p>
28 *
29 * @author <a href="mailto:alexei.guevara@uhn.on.ca">Alexei Guevara </a>
30 * @version $Revision: 1.5 $
31 */
32
33 public class XmlStorage implements IStorage {
34 public static final String DEFAULT_FILE_NAME_SUFFIX=".xml";
35
36 public static final Class DEFAULT_ROOT_SERIALIZATION_CLASS =NeuralNetwork.class;
37
38 public static final Class DEFAULT_SERIALIZER_CLASS = CastorXmlSerializer.class;
39 /***
40 * Defaults to <code>false</code>
41 *
42 * @uml.property name="autoCreateReposotory"
43 */
44 private boolean autoCreateReposotory = false;
45
46 /***
47 * Defaults to ".xml".
48 *
49 * @uml.property name="fileNameSuffix"
50 */
51 private String fileNameSuffix = DEFAULT_FILE_NAME_SUFFIX;
52
53 /***
54 * If file overwriteing is allowed. Defaults to <code>false</code>.
55 *
56 * @uml.property name="overwriteExisting"
57 */
58 private boolean overwriteExisting = false;
59
60 /***
61 * Where XML files are to be read and written.
62 *
63 * @uml.property name="repositoryDir"
64 */
65 private File repositoryDir;
66
67 /***
68 *
69 * @uml.property name="rootSerializationClass"
70 */
71 private Class rootSerializationClass = DEFAULT_ROOT_SERIALIZATION_CLASS;
72
73 /***
74 * The <code>ISerializer</code> instance to use. Defaults to a
75 * <code>CastorXmlSerializer</code> instance.
76 *
77 * @uml.property name="serializer"
78 * @uml.associationEnd multiplicity="(1 1)"
79 */
80 private ISerializer serializer = null;
81
82 /***
83 * @return Returns the fileNameSuffix.
84 *
85 * @uml.property name="fileNameSuffix"
86 */
87 public String getFileNameSuffix() {
88 return fileNameSuffix;
89 }
90
91 /***
92 * @return Returns the repositoryDir.
93 *
94 * @uml.property name="repositoryDir"
95 */
96 public File getRepositoryDir() {
97 return repositoryDir;
98 }
99
100 /***
101 * @return Returns the serializer.
102 *
103 * @uml.property name="serializer"
104 */
105 public ISerializer getSerializer() {
106 return serializer;
107 }
108
109 /***
110 * @return Returns the autoCreateReposotory.
111 *
112 * @uml.property name="autoCreateReposotory"
113 */
114 public boolean isAutoCreateReposotory() {
115 return autoCreateReposotory;
116 }
117
118 /***
119 * @return Returns the overwriteExisting.
120 *
121 * @uml.property name="overwriteExisting"
122 */
123 public boolean isOverwriteExisting() {
124 return overwriteExisting;
125 }
126
127 /***
128 * @param autoCreateReposotory
129 * The autoCreateReposotory to set.
130 *
131 * @uml.property name="autoCreateReposotory"
132 */
133 public void setAutoCreateReposotory(boolean autoCreateReposotory) {
134 this.autoCreateReposotory = autoCreateReposotory;
135 }
136
137 /***
138 * @param fileNameSuffix
139 * The fileNameSuffix to set.
140 *
141 * @uml.property name="fileNameSuffix"
142 */
143 public void setFileNameSuffix(String fileNameSuffix) {
144 this.fileNameSuffix = fileNameSuffix;
145 }
146
147 /***
148 * @param overwriteExisting
149 * The overwriteExisting to set.
150 *
151 * @uml.property name="overwriteExisting"
152 */
153 public void setOverwriteExisting(boolean overwriteExisting) {
154 this.overwriteExisting = overwriteExisting;
155 }
156
157 /***
158 * @param repositoryDir
159 * The repositoryDir to set.
160 *
161 * @uml.property name="repositoryDir"
162 */
163 public void setRepositoryDir(File repositoryDir) {
164 this.repositoryDir = repositoryDir;
165 }
166
167 /***
168 * @param serializer
169 * The serializer to set.
170 *
171 * @uml.property name="serializer"
172 */
173 public void setSerializer(ISerializer serializer) {
174 this.serializer = serializer;
175 }
176
177 /***
178 * @return Returns the rootSerializationClass.
179 *
180 * @uml.property name="rootSerializationClass"
181 */
182 public Class getRootSerializationClass() {
183 return rootSerializationClass;
184 }
185
186 /***
187 * @param rootSerializationClass
188 * The rootSerializationClass to set.
189 *
190 * @uml.property name="rootSerializationClass"
191 */
192 public void setRootSerializationClass(Class rootSerializationClass) {
193 this.rootSerializationClass = rootSerializationClass;
194 }
195
196
197 public XmlStorage() {
198 super();
199 try {
200 serializer = (ISerializer) DEFAULT_SERIALIZER_CLASS.newInstance();
201 } catch (InstantiationException e) {
202
203 e.printStackTrace();
204 } catch (IllegalAccessException e) {
205
206 e.printStackTrace();
207 }
208 }
209
210 /***
211 * @see yawn.io.IStorage#insert(java.lang.Object, java.lang.String)
212 */
213 public void insert(Object object, String key) throws StorageException, DuplicateKeyException {
214 File dest = prepareFile(key);
215
216 if (dest.exists() && !isOverwriteExisting()) {
217 throw new DuplicateKeyException("File `" + dest + "'already exists.");
218 }
219
220 serializeObject(object, dest);
221 }
222
223 protected void serializeObject(Object object, File file) throws StorageException {
224 try {
225 OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(file));
226 getSerializer().serialize(object, writer);
227 writer.close();
228 } catch (SerializationException e) {
229 throw new StorageException(e);
230 } catch (FileNotFoundException e) {
231 throw new StorageException(e);
232 } catch (IOException e) {
233 throw new StorageException(e);
234 }
235 }
236
237 /***
238 *
239 * @see yawn.io.IStorage#generateKey(java.lang.Object)
240 */
241 public String generateKey(Object object) {
242 String prefix = object.getClass().getName();
243 prefix = prefix.substring(prefix.lastIndexOf("."));
244 try {
245 File temp = File.createTempFile(prefix,getFileNameSuffix(), getRepositoryDir());
246 String res = temp.getName();
247 temp.delete();
248 return res;
249 } catch (IOException e) {
250
251 e.printStackTrace();
252 }
253 return null;
254 }
255
256 protected File prepareFile(String key) {
257 return new File(getRepositoryDir().toString() + File.separator + key);
258 }
259
260 /***
261 * @see yawn.io.IStorage#update(java.lang.Object, java.lang.String)
262 */
263 public void update(Object object, String key) throws KeyNotPresentException, StorageException {
264 File dest = prepareFile(key);
265
266 if (!dest.exists()) {
267 throw new KeyNotPresentException("The key `" + key
268 + "' is not present in the repository.");
269 }
270
271 serializeObject(object, dest);
272 }
273
274 /***
275 * Returns the object stored in the file named as the <code>key</code>.
276 * The base class of this object is specified in
277 * <code>XmlContext.getRootSerializationClass()</code>.
278 *
279 * @see XmlContext#getRootSerializationClass()
280 * @see yawn.io.IStorage#get(java.lang.String)
281 */
282 public Object get(String key) throws StorageException, KeyNotPresentException {
283 File source = prepareFile(key);
284 if (!source.exists()) {
285 throw new KeyNotPresentException("The key `" + key
286 + "' is not present in the repository.");
287 }
288 return deSerializeObject(source);
289 }
290
291 protected Object deSerializeObject(File file) throws StorageException {
292 try {
293 InputStreamReader reader = new InputStreamReader(new FileInputStream(file));
294 return getSerializer().deSerialize(getRootSerializationClass(), reader);
295 } catch (SerializationException e) {
296 throw new StorageException(e);
297 } catch (FileNotFoundException e) {
298 throw new StorageException(e);
299 }
300 }
301
302 /***
303 * Returns all objects in the repository.
304 *
305 * @see yawn.io.IStorage#getAll()
306 */
307 public Object[] getAll() throws StorageException {
308 File[] files = getRepositoryDir().listFiles(
309 new XmlFilenameFilter(getFileNameSuffix()));
310 if (files == null) {
311 return null;
312 }
313 Object[] res = new Object[files.length];
314 for (int i = 0; i < files.length; i++) {
315 res[i] = deSerializeObject(files[i]);
316 }
317 return res;
318 }
319
320 /***
321 * @see yawn.io.IStorage#flush()
322 */
323 public void flush() {
324 }
325
326 private class XmlFilenameFilter implements java.io.FilenameFilter {
327 private String extension;
328
329 public XmlFilenameFilter(String extension) {
330 this.extension = extension;
331 }
332
333 public boolean accept(File dir, String name) {
334 return name.endsWith(extension);
335 }
336 }
337 }