View Javadoc

1   /*
2    * @(#)$Id: ZipStorageEngine.java 724 2009-08-04 22:13:35Z kurmannu $
3    *
4    * Author		:	Ueli Kurmann, igesture@uelikurmann.ch
5    *                  
6    *
7    * Purpose		: 
8    *
9    * -----------------------------------------------------------------------
10   *
11   * Revision Information:
12   *
13   * Date				Who			Reason
14   *
15   * 28.04.2008			ukurmann	Initial Release
16   *
17   * -----------------------------------------------------------------------
18   *
19   * Copyright 1999-2009 ETH Zurich. All Rights Reserved.
20   *
21   * This software is the proprietary information of ETH Zurich.
22   * Use is subject to license terms.
23   * 
24   */
25  
26  package org.ximtec.igesture.storage;
27  
28  import java.io.File;
29  import java.io.IOException;
30  import java.util.ArrayList;
31  import java.util.HashMap;
32  import java.util.List;
33  import java.util.logging.Level;
34  import java.util.logging.Logger;
35  import java.util.zip.ZipEntry;
36  
37  import org.ximtec.igesture.configuration.Configuration;
38  import org.ximtec.igesture.core.DataObject;
39  import org.ximtec.igesture.core.GestureSet;
40  import org.ximtec.igesture.core.TestSet;
41  import org.ximtec.igesture.util.XMLTool;
42  import org.ximtec.igesture.util.ZipFS;
43  
44  /**
45   * Comment
46   * 
47   * @version 1.0 28.04.2008
48   * @author Ueli Kurmann
49   */
50  public class ZipStorageEngine extends DefaultFileStorageEngine {
51  
52    private static final Logger LOGGER = Logger.getLogger(XMLStorageEngine.class.getName());
53  
54    private final static String GESTURE_SET = "GestureSet";
55    private final static String GESTURE_SET_PATH = "sets";
56    private final static String CONFIGURATION_PATH = "configs";
57    private final static String TEST_SET_PATH = "testsets";
58    private final static String XML_EXTENSION = ".xml";
59  
60    private List<DataObject> dataObjectsToRemove;
61  
62    // private ZipFS zipFS;
63  
64    public ZipStorageEngine(String filename) {
65      super(new File(filename));
66      dataObjectsToRemove = new ArrayList<DataObject>();
67    }
68  
69    private List<DataObject> initGestureSet(ZipFS zipFS) {
70      List<DataObject> dataObjects = new ArrayList<DataObject>();
71  
72      for (ZipEntry entry : zipFS.listFiles(GESTURE_SET_PATH)) {
73        if (entry.isDirectory()) {
74          for (ZipEntry entrySet : zipFS.listFiles(entry.getName())) {
75            if (entrySet.getName().toLowerCase().endsWith(XML_EXTENSION)) {
76              try {
77                GestureSet gestureSet = XMLTool.importGestureSet(zipFS.getInputStream(entrySet));
78                dataObjects.add(gestureSet);
79  
80                // TODO load images
81  
82              } catch (IOException e) {
83                LOGGER.log(Level.SEVERE, "Could not import GestureSet.", e);
84              }
85            }
86          }
87        }
88      }
89  
90      return dataObjects;
91    }
92  
93    private List<DataObject> initTestSet(ZipFS zipFS) {
94  
95      List<DataObject> dataObjects = new ArrayList<DataObject>();
96  
97      for (ZipEntry entry : zipFS.listFiles(TEST_SET_PATH)) {
98        if (entry.getName().toLowerCase().endsWith(XML_EXTENSION)) {
99          try {
100           TestSet testSet = XMLTool.importTestSet(zipFS.getInputStream(entry));
101 
102           if (testSet != null) {
103             dataObjects.add(testSet);
104           }
105 
106         } catch (IOException e) {
107           LOGGER.log(Level.SEVERE, "Could not import Test sets.", e);
108         }
109       }
110     }
111 
112     return dataObjects;
113   }
114 
115   private List<DataObject> initConfig(ZipFS zipFS) {
116 
117     List<DataObject> dataObjects = new ArrayList<DataObject>();
118     for (ZipEntry entry : zipFS.listFiles(CONFIGURATION_PATH)) {
119       if (entry.getName().toLowerCase().endsWith(XML_EXTENSION)) {
120         try {
121           Configuration config = XMLTool.importConfiguration(zipFS.getInputStream(entry));
122           dataObjects.add(config);
123         } catch (IOException e) {
124           LOGGER.log(Level.SEVERE, "Could not import Configuration.", e);
125         }
126       }
127     }
128 
129     return dataObjects;
130   }
131 
132   private void persistGestureSets(List<DataObject> gestureSets, ZipFS zipFS) {
133     for (DataObject dataObject : gestureSets) {
134       try {
135         GestureSet set = (GestureSet) dataObject;
136         String name = getGestureSetPath(set.getId());
137         zipFS.store(name, XMLTool.exportGestureSetAsStream(set));
138       } catch (IOException e) {
139         e.printStackTrace();
140       }
141     }
142   }
143 
144   private String getGestureSetPath(String id) {
145     return GESTURE_SET_PATH + ZipFS.SEPERATOR + id + ZipFS.SEPERATOR + GESTURE_SET + XML_EXTENSION;
146   }
147 
148   private void persistConfiguration(List<DataObject> configurations, ZipFS zipFS) {
149     for (DataObject dataObject : configurations) {
150       try {
151         Configuration config = (Configuration) dataObject;
152         String name = getConfigurationPath(config.getId());
153         zipFS.store(name, XMLTool.exportConfigurationAsStream(config));
154       } catch (IOException e) {
155         e.printStackTrace();
156       }
157     }
158   }
159 
160   private String getConfigurationPath(String id) {
161     return CONFIGURATION_PATH + ZipFS.SEPERATOR + id + XML_EXTENSION;
162   }
163 
164   private void persistTestSets(List<DataObject> testSets, ZipFS zipFS) {
165     for (DataObject dataObject : testSets) {
166       try {
167         TestSet testSet = (TestSet) dataObject;
168         String name = getTestSetPath(testSet.getId());
169         zipFS.store(name, XMLTool.exportTestSetAsStream(testSet));
170       } catch (IOException e) {
171         e.printStackTrace();
172       }
173     }
174   }
175 
176   private String getTestSetPath(String id) {
177     return TEST_SET_PATH + ZipFS.SEPERATOR + id + XML_EXTENSION;
178   }
179 
180   @Override
181   public void dispose() {
182     // FIXME clear dataobjects list?
183     LOGGER.info("Dispose the ZIP Storage Engine ZIP file");
184 
185   }
186 
187   /*
188    * Attention: Only Configuration, TestSet, GestureSet are processed! TODO: OOP
189    * implementation (non-Javadoc)
190    * 
191    * @see
192    * org.ximtec.igesture.storage.StorageEngine#remove(org.ximtec.igesture.core
193    * .DataObject)
194    */
195   @Override
196   public void remove(DataObject dataObject) {
197     removeDataObject(dataObject);
198     dataObjectsToRemove.add(dataObject);
199     setDoChanged(true);
200 
201   } // remove
202 
203   /*
204    * (non-Javadoc)
205    * 
206    * @see org.ximtec.igesture.storage.StorageEngine#copyTo(java.io.File)
207    */
208   @Override
209   public synchronized void copyTo(File file) {
210     setStorageFile(file);
211     commit();
212 
213   }
214 
215   @Override
216   protected HashMap<Class<? extends DataObject>, List<DataObject>> deserialize(File storageFile) {
217     HashMap<Class<? extends DataObject>, List<DataObject>> dataObjects = new HashMap<Class<? extends DataObject>, List<DataObject>>();
218 
219     try {
220       ZipFS zipFS = new ZipFS(storageFile);
221       dataObjects.put(GestureSet.class, initGestureSet(zipFS));
222 
223       dataObjects.put(Configuration.class, initConfig(zipFS));
224 
225       dataObjects.put(TestSet.class, initTestSet(zipFS));
226       
227       zipFS.close();
228 
229     } catch (IOException e) {
230       LOGGER.log(Level.SEVERE, "Could not initialise ZIP Storage Engine.", e);
231       return new HashMap<Class<? extends DataObject>, List<DataObject>>();
232     }
233 
234     return dataObjects;
235   }
236 
237   @Override
238   protected void serialize(HashMap<Class<? extends DataObject>, List<DataObject>> dataObjects, File file) {
239 
240     if (isDoChanged() || true) {
241       try {
242 
243         if (file.exists()) {
244           file.delete();
245         }
246 
247         ZipFS zipFS = new ZipFS(file);
248 
249         // remove deleted data objects from zip fs
250         removeDataObjectsFromZipFS(zipFS);
251 
252         // persist gesture sets
253         persistGestureSets(dataObjects.get(GestureSet.class), zipFS);
254 
255         // persist configurations
256         persistConfiguration(dataObjects.get(Configuration.class), zipFS);
257 
258         // persist test sets
259         persistTestSets(dataObjects.get(TestSet.class), zipFS);
260 
261         // FIXME why close zipFS?
262         try {
263           zipFS.close();
264         } catch (IOException e) {
265           e.printStackTrace();
266         }
267 
268         //dataObjects = deserialize(getStorageFile());
269 
270         setDoChanged(false);
271 
272       } catch (IOException e) {
273         LOGGER.log(Level.SEVERE, "Could not initialise ZIP Storage Engine.", e);
274       }
275     }
276 
277   }
278 
279   private void removeDataObjectsFromZipFS(ZipFS zipFS) {
280     for (DataObject dataObjecttoRemove : dataObjectsToRemove) {
281       String path = null;
282 
283       if (dataObjecttoRemove instanceof Configuration) {
284         path = getConfigurationPath(dataObjecttoRemove.getId());
285       } else if (dataObjecttoRemove instanceof GestureSet) {
286         path = getGestureSetPath(dataObjecttoRemove.getId());
287       } else if (dataObjecttoRemove instanceof TestSet) {
288         path = getTestSetPath(dataObjecttoRemove.getId());
289       }
290 
291       if (path != null) {
292         zipFS.delete(path);
293       }
294     }
295   }
296 }