Xcos: save and load the DEBUG_LEVEL
[scilab.git] / scilab / modules / xcos / src / java / org / scilab / modules / xcos / io / sax / CustomHandler.java
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2015-2015 - 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.sax;
14
15 import java.util.ArrayList;
16 import java.util.HashMap;
17 import java.util.Map.Entry;
18 import java.util.Optional;
19 import java.util.logging.Level;
20 import java.util.stream.Stream;
21
22 import javax.naming.LimitExceededException;
23
24 import org.scilab.modules.xcos.Kind;
25 import org.scilab.modules.xcos.ObjectProperties;
26 import org.scilab.modules.xcos.VectorOfDouble;
27 import org.scilab.modules.xcos.VectorOfInt;
28 import org.scilab.modules.xcos.graph.ScicosParameters;
29 import org.scilab.modules.xcos.graph.model.ScicosObjectOwner;
30 import org.scilab.modules.xcos.graph.model.XcosCell;
31 import org.scilab.modules.xcos.graph.model.XcosCellFactory;
32 import org.scilab.modules.xcos.io.HandledElement;
33 import org.scilab.modules.xcos.io.sax.XcosSAXHandler.UnresolvedReference;
34 import org.scilab.modules.xcos.port.BasicPort;
35 import org.scilab.modules.xcos.port.Orientation;
36 import org.xml.sax.Attributes;
37
38 class CustomHandler implements ScilabHandler {
39
40     private final XcosSAXHandler saxHandler;
41
42     /**
43      * Default constructor
44      *
45      * @param saxHandler
46      *            the shared sax handler
47      */
48     CustomHandler(XcosSAXHandler saxHandler) {
49         this.saxHandler = saxHandler;
50     }
51
52     @Override
53     @SuppressWarnings({ "fallthrough" })
54     public Object startElement(HandledElement found, final Attributes atts) {
55         long uid = 0l;
56         String v;
57
58         switch (found) {
59             case Orientation: {
60                 // defensive programming
61                 if (!(saxHandler.parents.peek() instanceof BasicPort)) {
62                     return null;
63                 }
64                 BasicPort parent = (BasicPort) saxHandler.parents.peek();
65
66                 v = atts.getValue("value");
67                 if (v != null) {
68                     final String orientationString = v;
69                     Optional<Orientation> orientation = Stream.of(Orientation.values()).filter(o -> o.name().equals(orientationString)).findFirst();
70                     if (orientation.isPresent()) {
71                         parent.setOrientation(orientation.get());
72                     }
73                 }
74
75                 return null;
76             }
77             case XcosDiagram:
78                 if (XcosSAXHandler.LOG.isLoggable(Level.FINER)) {
79                     XcosSAXHandler.LOG.entering(CustomHandler.class.getName(), "startElement(\"XcosDiagram\", ...)");
80                 }
81
82                 // do not allocate this is already allocated as #root
83                 uid = saxHandler.root.getUID();
84
85                 /*
86                  * Decode some graph properties
87                  */
88                 v = atts.getValue("savedFile");
89                 if (v != null) {
90                     saxHandler.controller.setObjectProperty(uid, Kind.DIAGRAM, ObjectProperties.PATH, v);
91                 }
92                 v = atts.getValue("debugLevel");
93                 if (v != null) {
94                     saxHandler.controller.setObjectProperty(uid, Kind.DIAGRAM, ObjectProperties.DEBUG_LEVEL, Integer.valueOf(v));
95                 }
96
97                 /*
98                  * Decode simulation properties
99                  */
100                 VectorOfDouble properties = new VectorOfDouble();
101                 saxHandler.controller.getObjectProperty(uid, Kind.DIAGRAM, ObjectProperties.PROPERTIES, properties);
102
103                 v = atts.getValue("finalIntegrationTime");
104                 if (v != null) {
105                     properties.set(ScicosParameters.FINAL_INTEGRATION_TIME, Double.valueOf(v));
106                 }
107                 v = atts.getValue("integratorAbsoluteTolerance");
108                 if (v != null) {
109                     properties.set(ScicosParameters.INTEGRATOR_ABSOLUTE_TOLERANCE, Double.valueOf(v));
110                 }
111                 v = atts.getValue("integratorRelativeTolerance");
112                 if (v != null) {
113                     properties.set(ScicosParameters.INTEGRATOR_RELATIVE_TOLERANCE, Double.valueOf(v));
114                 }
115                 v = atts.getValue("toleranceOnTime");
116                 if (v != null) {
117                     properties.set(ScicosParameters.TOLERANCE_ON_TIME, Double.valueOf(v));
118                 }
119                 v = atts.getValue("maxIntegrationTimeinterval");
120                 if (v != null) {
121                     properties.set(ScicosParameters.MAX_INTEGRATION_TIME_INTERVAL, Double.valueOf(v));
122                 }
123                 v = atts.getValue("maximumStepSize");
124                 if (v != null) {
125                     properties.set(ScicosParameters.MAXIMUM_STEP_SIZE, Double.valueOf(v));
126                 }
127                 v = atts.getValue("realTimeScaling");
128                 if (v != null) {
129                     properties.set(ScicosParameters.REAL_TIME_SCALING, Double.valueOf(v));
130                 }
131                 v = atts.getValue("solver");
132                 if (v != null) {
133                     properties.set(ScicosParameters.SOLVER, Double.valueOf(v));
134                 }
135
136                 saxHandler.controller.setObjectProperty(uid, Kind.DIAGRAM, ObjectProperties.PROPERTIES, properties);
137
138             // no break on purpose, we decode non-root specific properties later
139             case SuperBlockDiagram:
140                 final Kind kind;
141                 if (uid == 0l) {
142                     XcosCell parent = saxHandler.lookupForParentXcosCellElement();
143                     uid = parent.getUID();
144                     kind = parent.getKind();
145                 } else {
146                     kind = Kind.DIAGRAM;
147                 }
148
149                 /*
150                  * Decode the properties shared between a DIAGRAM and a BLOCK
151                  */
152                 VectorOfInt colors = new VectorOfInt();
153                 saxHandler.controller.getObjectProperty(uid, kind, ObjectProperties.COLOR, colors);
154                 v = atts.getValue("background");
155                 if (v != null) {
156                     colors.set(0, Integer.valueOf(v));
157                 }
158                 v = atts.getValue("gridEnabled");
159                 if (v != null) {
160                     colors.set(1, Integer.valueOf(v));
161                 }
162                 saxHandler.controller.setObjectProperty(uid, kind, ObjectProperties.COLOR, colors);
163
164                 v = atts.getValue("title");
165                 if (v != null) {
166                     saxHandler.controller.setObjectProperty(uid, kind, ObjectProperties.TITLE, v);
167                 }
168
169                 /*
170                  * Update some states
171                  */
172                 saxHandler.allChildren.push(new HashMap<>());
173                 return new ScicosObjectOwner(uid, kind);
174             default:
175                 throw new IllegalArgumentException();
176         }
177     }
178
179     @Override
180     public void endElement(HandledElement found) {
181         switch (found) {
182             case Orientation:
183                 break;
184             case XcosDiagram:
185                 resolve();
186                 saxHandler.allChildren.pop();
187                 XcosCellFactory.insertChildren(saxHandler.controller, saxHandler.root);
188
189                 if (XcosSAXHandler.LOG.isLoggable(Level.FINER)) {
190                     XcosSAXHandler.LOG.exiting(CustomHandler.class.getName(), "endElement(\"XcosDiagram\")");
191                 }
192                 break;
193             case SuperBlockDiagram:
194                 resolve();
195                 saxHandler.allChildren.pop();
196                 break;
197             default:
198                 throw new IllegalArgumentException();
199         }
200     }
201
202     private void resolve() {
203         HashMap<String, Long> allLocalChildren = saxHandler.allChildren.peek();
204
205         for (Entry<String, ArrayList<UnresolvedReference>> entry : saxHandler.unresolvedReferences.entrySet()) {
206             Long uidObject = allLocalChildren.get(entry.getKey());
207             if (uidObject != null) {
208                 long uid = uidObject.longValue();
209
210                 for (UnresolvedReference unresolvedReference : entry.getValue()) {
211                     unresolvedReference.resolve(saxHandler.controller, uid, saxHandler.controller.getKind(uid));
212                 }
213             }
214         }
215     }
216 }