* Bug #15456 fixed: Xcos 5.5.x files did not load properly
[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  * Copyright (C) 2012 - 2016 - Scilab Enterprises
6  *
7  * This file is hereby licensed under the terms of the GNU GPL v2.0,
8  * pursuant to article 5.3.4 of the CeCILL v.2.1.
9  * This file was originally licensed under the terms of the CeCILL v2.1,
10  * and continues to be available under such terms.
11  * For more information, see the COPYING file which you should have received
12  * along with this program.
13  *
14  */
15
16 package org.scilab.modules.xcos.io.spec;
17
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.util.logging.Logger;
21 import java.util.zip.ZipEntry;
22 import java.util.zip.ZipOutputStream;
23
24 import javax.xml.stream.XMLOutputFactory;
25 import javax.xml.stream.XMLStreamException;
26 import javax.xml.stream.XMLStreamWriter;
27 import org.scilab.modules.commons.xml.ScilabXMLOutputFactory;
28 import org.scilab.modules.xcos.graph.XcosDiagram;
29 import org.scilab.modules.xcos.io.sax.XcosSAXHandler;
30 import org.scilab.modules.xcos.io.writer.XcosWriter;
31 import org.w3c.dom.Element;
32 import org.xml.sax.InputSource;
33 import org.xml.sax.SAXException;
34 import org.xml.sax.XMLReader;
35 import org.xml.sax.helpers.XMLReaderFactory;
36
37 public class ContentEntry implements Entry {
38     private static final Logger LOG = Logger.getLogger(ContentEntry.class.getName());
39
40     private XcosDiagram content;
41     private XcosPackage pack;
42
43     @Override
44     public String getMediaType() {
45         return "text/xml";
46     }
47
48     @Override
49     public String getFullPath() {
50         return "content.xml";
51     }
52
53     @Override
54     public void setup(XcosPackage p) {
55         pack = p;
56         content = p.getContent();
57     }
58
59     @Override
60     public void load(ZipEntry entry, InputStream stream) throws IOException {
61         try {
62             XcosSAXHandler handler = new XcosSAXHandler(content, pack.getDictionary());
63             XMLReader reader = XMLReaderFactory.createXMLReader();
64             reader.setContentHandler(handler);
65             reader.setErrorHandler(handler);
66
67             LOG.entering("XMLReader", "parse");
68             reader.parse(new InputSource(stream));
69             LOG.exiting("XMLReader", "parse");
70         } catch (SAXException e) {
71             e.printStackTrace();
72             throw new RuntimeException(e);
73         } catch (Exception e) {
74             e.printStackTrace();
75             throw e;
76         }
77     }
78
79     @Override
80     public void store(ZipOutputStream stream) throws IOException {
81         /*
82          * Store content
83          */
84         try {
85             final XMLOutputFactory factory = ScilabXMLOutputFactory.newInstance();
86             final XMLStreamWriter writer = factory.createXMLStreamWriter(stream, "UTF-8");
87
88             LOG.entering("XMLStreamWriter", "write");
89             new XcosWriter(pack.getDictionary(), writer).write(content.getUID(), content.getKind());
90             writer.close();
91             LOG.exiting("XMLStreamWriter", "write");
92
93             /*
94              * Add an entry to the manifest file
95              */
96             final Element e = pack.getManifest().createElement("manifest:file-entry");
97             e.setAttribute("manifest:media-type", getMediaType());
98             e.setAttribute("manifest:full-path", getFullPath());
99             pack.getManifest().getFirstChild().appendChild(e);
100         } catch (XMLStreamException e) {
101             Logger.getLogger(ContentEntry.class.getName()).severe(e.getMessage());
102         } finally {
103             stream.closeEntry();
104         }
105     }
106 }