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