Xcos: ZCOS should not store the time
[scilab.git] / scilab / modules / xcos / src / java / org / scilab / modules / xcos / io / spec / DictionaryEntry.java
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2012 - Scilab Enterprises - Clement DAVID
4  *
5  * This file must be used under the terms of the CeCILL.
6  * This source file is licensed as described in the file COPYING, which
7  * you should have received as part of this distribution.  The terms
8  * are also available at
9  * http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
10  *
11  */
12
13 package org.scilab.modules.xcos.io.spec;
14
15 import java.io.BufferedOutputStream;
16 import java.io.IOException;
17 import java.io.InputStream;
18 import java.io.ObjectInputStream;
19 import java.io.ObjectOutputStream;
20 import java.util.logging.Logger;
21 import java.util.zip.ZipEntry;
22 import java.util.zip.ZipOutputStream;
23
24 import org.scilab.modules.types.ScilabList;
25 import org.w3c.dom.Element;
26
27 public class DictionaryEntry implements Entry {
28     private static final Logger LOG = Logger.getLogger(DictionaryEntry.class.getName());
29
30     private XcosPackage pack;
31
32     public DictionaryEntry() {
33     }
34
35     @Override
36     public String getMediaType() {
37         return "bin/ser";
38     }
39
40     @Override
41     public String getFullPath() {
42         return "dictionary/dictionary.ser";
43     }
44
45     @Override
46     public void setup(XcosPackage p) {
47         this.pack = p;
48     }
49
50     @Override
51     public void load(ZipEntry entry, InputStream stream) throws IOException {
52         ObjectInputStream ois = null;
53         try {
54             ois = new ObjectInputStream(stream);
55
56             pack.getDictionary().clear();
57             pack.getDictionary().addAll((ScilabList) ois.readObject());
58         } catch (IOException e) {
59             Logger.getLogger(DictionaryEntry.class.getName()).severe(e.getMessage());
60         } catch (ClassNotFoundException e) {
61             Logger.getLogger(DictionaryEntry.class.getName()).severe(e.getMessage());
62         } finally {
63             ois.close();
64         }
65     }
66
67     @Override
68     public void store(ZipOutputStream stream) throws IOException {
69         /*
70          * Append a ZipEntry
71          */
72         final ZipEntry entry = new ZipEntry(getFullPath());
73         entry.setTime(0l);
74         stream.putNextEntry(entry);
75
76         /*
77          * Store content
78          */
79         LOG.entering("ObjectOutputStream", "writeObject");
80         ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(stream));
81         oos.writeObject(pack.getDictionary());
82         oos.flush();
83         LOG.exiting("ObjectOutputStream", "writeObject");
84
85         /*
86          * Add an entry to the manifest file
87          */
88         final Element e = pack.getManifest().createElement("manifest:file-entry");
89         e.setAttribute("manifest:media-type", getMediaType());
90         e.setAttribute("manifest:full-path", getFullPath());
91         pack.getManifest().getFirstChild().appendChild(e);
92     }
93 }