Xcos: "Region to superblock" fixed
[scilab.git] / scilab / modules / scicos / includes / XMIResource.hxx
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2016-2016 - Scilab Enterprises - Clement DAVID
4  * Copyright (C) 2017 - ESI Group - Clement DAVID
5  *
6  * This file is hereby licensed under the terms of the GNU GPL v2.0,
7  * pursuant to article 5.3.4 of the CeCILL v.2.1.
8  * This file was originally licensed under the terms of the CeCILL v2.1,
9  * and continues to be available under such terms.
10  * For more information, see the COPYING file which you should have received
11  * along with this program.
12  */
13
14 #include "utilities.hxx"
15 #include "Controller.hxx"
16 #include "model/BaseObject.hxx"
17
18 #include "dynlib_scicos.h"
19
20 #include <map>
21 #include <array>
22 #include <string>
23 #include <vector>
24
25 extern "C" {
26 #include <libxml/xmlwriter.h>
27 #include <libxml/xmlreader.h>
28 }
29
30 namespace org_scilab_modules_scicos
31 {
32
33 class SCICOS_IMPEXP XMIResource
34 {
35
36 public:
37     XMIResource(ScicosID id);
38     ~XMIResource();
39
40     int save(const char* uri);
41     int load(const char* uri);
42
43 private:
44     /*
45      * Save helpers
46      */
47     int writeDatatype(xmlTextWriterPtr writer, const std::vector<int>& datatype);
48     int writePoint(xmlTextWriterPtr writer, double x, double y);
49     int writeGeometry(xmlTextWriterPtr writer, ScicosID id, kind_t kind);
50     int writeAbstractLayer(xmlTextWriterPtr writer, ScicosID id, kind_t kind);
51     int writeAbstractBaseObject(xmlTextWriterPtr writer, ScicosID id, kind_t kind);
52     int writeDiagram(xmlTextWriterPtr writer);
53     int writeSimulationConfig(xmlTextWriterPtr writer, ScicosID id);
54     int writeBlock(xmlTextWriterPtr writer, ScicosID id);
55     int writePort(xmlTextWriterPtr writer, enum object_properties_t container, ScicosID id);
56     int writeLink(xmlTextWriterPtr writer, ScicosID id);
57     int writeAnnotation(xmlTextWriterPtr writer, ScicosID id, bool isLabel);
58
59     /*
60      * Load helpers
61      */
62     int loadDoubleArray(xmlTextReaderPtr reader, enum object_properties_t property, const model::BaseObject& o);
63     int loadIntArray(xmlTextReaderPtr reader, enum object_properties_t property, const model::BaseObject& o);
64     int loadStringArray(xmlTextReaderPtr reader, enum object_properties_t property, const model::BaseObject& o);
65     int loadEncodedStringArray(xmlTextReaderPtr reader, enum object_properties_t property, const model::BaseObject& o);
66     int loadBase64(xmlTextReaderPtr reader, enum object_properties_t property, const model::BaseObject& o);
67     int loadDatatype(xmlTextReaderPtr reader, const model::BaseObject& o);
68     int loadPoint(xmlTextReaderPtr reader, const model::BaseObject& o);
69     int loadGeometry(xmlTextReaderPtr reader, const model::BaseObject& o);
70     int loadAbstractBaseObject(xmlTextReaderPtr reader, const model::BaseObject& o);
71     int loadDiagram(xmlTextReaderPtr reader, const model::BaseObject& o);
72     int loadSimulationConfig(xmlTextReaderPtr reader, const model::BaseObject& o);
73     int loadBlock(xmlTextReaderPtr reader, const model::BaseObject& o);
74     int loadPort(xmlTextReaderPtr reader, const model::BaseObject& o);
75     int loadLink(xmlTextReaderPtr reader, const model::BaseObject& o);
76     int loadAnnotation(xmlTextReaderPtr reader, const model::BaseObject& o);
77
78     int processNode(xmlTextReaderPtr reader);
79     int processElement(xmlTextReaderPtr reader);
80     int processText(xmlTextReaderPtr reader);
81     int processEndElement(xmlTextReaderPtr reader);
82
83 private:
84     /* shared controller */
85     Controller controller;
86     /* id of the diagram */
87     ScicosID root;
88
89     /*
90      * load shared variables
91      */
92
93     /* interned string indexes */
94     enum xcosNames
95     {
96         e_Annotation,
97         e_BaseObject,
98         e_Block,
99         e_CompiledRepresentation,
100         e_Diagram,
101         e_Geometry,
102         e_Layer,
103         e_Link,
104         e_Point,
105         e_Port,
106         e_PortKind,
107         e_SimulationConfig,
108         e_absoluteTolerance,
109         e_base64,
110         e_blocktype,
111         e_child,
112         e_color,
113         e_columns,
114         e_connectedSignal,
115         e_context,
116         e_controlPoint,
117         e_datatype,
118         e_debugLevel,
119         e_deltaH,
120         e_deltaT,
121         e_dependsOnT,
122         e_dependsOnU,
123         e_description,
124         e_destinationPort,
125         e_dstate,
126         e_ein,
127         e_eout,
128         e_equations,
129         e_expression,
130         e_exprs,
131         e_finalTime,
132         e_firing,
133         e_font,
134         e_fontSize,
135         e_functionAPI,
136         e_functionName,
137         e_geometry,
138         e_height,
139         e_implicit,
140         e_in,
141         e_interfaceFunction,
142         e_ipar,
143         e_kind,
144         e_label,
145         e_lineHeight,
146         e_lineWidth,
147         e_nmode,
148         e_nzcross,
149         e_odstate,
150         e_opar,
151         e_out,
152         e_parent,
153         e_parentDiagram,
154         e_path,
155         e_properties,
156         e_realtimeScale,
157         e_relativeTolerance,
158         e_rows,
159         e_rpar,
160         e_solver,
161         e_sourceBlock,
162         e_sourcePort,
163         e_state,
164         e_style,
165         e_timeTolerance,
166         e_title,
167         e_type,
168         e_uid,
169         e_version,
170         e_width,
171         e_x,
172         e_xcos,
173         e_y,
174         NB_XCOS_NAMES
175     };
176     /* temporary interned string content (owned by a reader) */
177     std::array<const xmlChar*, NB_XCOS_NAMES> constXcosNames;
178     /* temporary interned xcos namespace (owned by a reader) */
179     const xmlChar* xcosNamespaceUri;
180     /* temporary interned xsi namespace (owned by a reader) */
181     const xmlChar* xsiNamespaceUri;
182
183     /* stack uid / kind used */
184     std::vector<model::BaseObject> processed;
185
186     /* store the parent of a text node */
187     enum xcosNames parent;
188
189     /* uid string - ScicosID  map */
190     std::map<std::string, ScicosID> references;
191     /* reference to resolve after loading the whole file */
192     struct unresolvedReference
193     {
194         unresolvedReference(ScicosID id, kind_t kind, enum object_properties_t prop, std::string uid)
195             : m_id(id), m_kind(kind), m_prop(prop), m_uid(uid) {}
196         ScicosID m_id;
197         kind_t m_kind;
198         enum object_properties_t m_prop;
199         std::string m_uid;
200     };
201     /* temporary unresolved references */
202     std::vector<unresolvedReference> unresolved;
203 };
204
205 }
206