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