Xcos: ZCOS should not store the time
[scilab.git] / scilab / modules / xcos / src / java / org / scilab / modules / xcos / io / spec / ContentEntry.java
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2012-2014 - 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.IOException;
16 import java.io.InputStream;
17 import java.util.logging.Logger;
18 import java.util.zip.ZipEntry;
19 import java.util.zip.ZipOutputStream;
20
21 import javax.xml.stream.XMLOutputFactory;
22 import javax.xml.stream.XMLStreamException;
23 import javax.xml.stream.XMLStreamWriter;
24 import javax.xml.transform.stream.StreamResult;
25 import org.scilab.modules.commons.xml.ScilabXMLOutputFactory;
26 import org.scilab.modules.xcos.graph.XcosDiagram;
27 import org.scilab.modules.xcos.io.sax.XcosSAXHandler;
28 import org.scilab.modules.xcos.io.writer.XcosWriter;
29 import org.w3c.dom.Element;
30 import org.xml.sax.InputSource;
31 import org.xml.sax.SAXException;
32 import org.xml.sax.XMLReader;
33 import org.xml.sax.helpers.XMLReaderFactory;
34
35 public class ContentEntry implements Entry {
36     private static final Logger LOG = Logger.getLogger(ContentEntry.class.getName());
37
38     private XcosDiagram content;
39     private XcosPackage pack;
40
41     @Override
42     public String getMediaType() {
43         return "text/xml";
44     }
45
46     @Override
47     public String getFullPath() {
48         return "content.xml";
49     }
50
51     @Override
52     public void setup(XcosPackage p) {
53         pack = p;
54         content = p.getContent();
55     }
56
57     @Override
58     public void load(ZipEntry entry, InputStream stream) throws IOException {
59         try {
60             XcosSAXHandler handler = new XcosSAXHandler(content, pack.getDictionary());
61             XMLReader reader = XMLReaderFactory.createXMLReader();
62             reader.setContentHandler(handler);
63             reader.setErrorHandler(handler);
64
65             LOG.entering("XMLReader", "parse");
66             reader.parse(new InputSource(stream));
67             LOG.exiting("XMLReader", "parse");
68         } catch (SAXException e) {
69             e.printStackTrace();
70             throw new RuntimeException(e);
71         } catch (Exception e) {
72             e.printStackTrace();
73             throw e;
74         }
75     }
76
77     @Override
78     public void store(ZipOutputStream stream) throws IOException {
79         /*
80          * Append a ZipEntry
81          */
82         final ZipEntry entry = new ZipEntry(getFullPath());
83         entry.setTime(0l);
84         stream.putNextEntry(entry);
85
86         /*
87          * Store content
88          */
89         try {
90             final XMLOutputFactory factory = ScilabXMLOutputFactory.newInstance();
91             final StreamResult result = new StreamResult(stream);
92             final XMLStreamWriter writer = factory.createXMLStreamWriter(result);
93
94             LOG.entering("XMLStreamWriter", "write");
95             new XcosWriter(pack.getDictionary(), writer).write(content.getUID(), content.getKind());
96             writer.close();
97             LOG.exiting("XMLStreamWriter", "write");
98
99             /*
100              * Add an entry to the manifest file
101              */
102             final Element e = pack.getManifest().createElement("manifest:file-entry");
103             e.setAttribute("manifest:media-type", getMediaType());
104             e.setAttribute("manifest:full-path", getFullPath());
105             pack.getManifest().getFirstChild().appendChild(e);
106         } catch (XMLStreamException e) {
107             Logger.getLogger(ContentEntry.class.getName()).severe(e.getMessage());
108         } finally {
109             stream.closeEntry();
110         }
111     }
112 }