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