2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2015-2015 - Scilab Enterprises - Clement DAVID
4 * Copyright (C) 2017-2018 - ESI Group - Clement DAVID
6 * Copyright (C) 2012 - 2016 - Scilab Enterprises
8 * This file is hereby licensed under the terms of the GNU GPL v2.0,
9 * pursuant to article 5.3.4 of the CeCILL v.2.1.
10 * This file was originally licensed under the terms of the CeCILL v2.1,
11 * and continues to be available under such terms.
12 * For more information, see the COPYING file which you should have received
13 * along with this program.
17 package org.scilab.modules.xcos.io.sax;
19 import java.util.Arrays;
20 import java.util.regex.Pattern;
22 import org.scilab.modules.xcos.Kind;
23 import org.scilab.modules.xcos.ObjectProperties;
24 import org.scilab.modules.xcos.VectorOfInt;
25 import org.scilab.modules.xcos.block.AfficheBlock;
26 import org.scilab.modules.xcos.block.BasicBlock;
27 import org.scilab.modules.xcos.block.BasicBlock.SimulationFunctionType;
28 import org.scilab.modules.xcos.block.SplitBlock;
29 import org.scilab.modules.xcos.block.SuperBlock;
30 import org.scilab.modules.xcos.block.TextBlock;
31 import org.scilab.modules.xcos.block.io.EventInBlock;
32 import org.scilab.modules.xcos.block.io.EventOutBlock;
33 import org.scilab.modules.xcos.block.io.ExplicitInBlock;
34 import org.scilab.modules.xcos.block.io.ExplicitOutBlock;
35 import org.scilab.modules.xcos.block.io.ImplicitInBlock;
36 import org.scilab.modules.xcos.block.io.ImplicitOutBlock;
37 import org.scilab.modules.xcos.block.custom.BigSom;
38 import org.scilab.modules.xcos.block.custom.GroundBlock;
39 import org.scilab.modules.xcos.block.custom.Product;
40 import org.scilab.modules.xcos.block.custom.RoundBlock;
41 import org.scilab.modules.xcos.block.custom.Summation;
42 import org.scilab.modules.xcos.block.custom.VoltageSensorBlock;
43 import org.scilab.modules.xcos.graph.model.BlockInterFunction;
44 import org.scilab.modules.xcos.io.HandledElement;
45 import org.xml.sax.Attributes;
47 import com.mxgraph.util.mxUtils;
49 class BlockHandler implements ScilabHandler {
50 private static Pattern validCIdentifier = null;
52 private final XcosSAXHandler saxHandler;
56 * @param saxHandler the shared sax handler
58 BlockHandler(XcosSAXHandler saxHandler) {
59 this.saxHandler = saxHandler;
63 public BasicBlock startElement(HandledElement found, Attributes atts) {
67 if (found == HandledElement.TextBlock) {
68 kind = Kind.ANNOTATION;
72 final long uid = saxHandler.controller.createObject(kind);
74 String value = atts.getValue("value");
75 if (validCIdentifier == null) {
76 validCIdentifier = Pattern.compile("[a-zA-Z_][a-zA-Z0-9_]*");
78 if (value != null && validCIdentifier.matcher(value).matches()) {
79 saxHandler.controller.setObjectProperty(uid, kind, ObjectProperties.DESCRIPTION, value);
82 // escape any html style / head
83 value = mxUtils.getBodyMarkup(value, false);
86 String style = atts.getValue("style");
88 saxHandler.controller.setObjectProperty(uid, kind, ObjectProperties.STYLE, style);
91 String strUID = atts.getValue("id");
93 saxHandler.allChildren.peek().put(strUID, uid);
98 block = new AfficheBlock(saxHandler.controller, uid, kind, value, null, style, strUID);
101 block = new BasicBlock(saxHandler.controller, uid, kind, value, null, style, strUID);
104 block = new BigSom(saxHandler.controller, uid, kind, value, null, style, strUID);
107 block = new BasicBlock(saxHandler.controller, uid, kind, value, null, style, strUID);
110 block = new EventInBlock(saxHandler.controller, uid, kind, value, null, style, strUID);
113 block = new EventOutBlock(saxHandler.controller, uid, kind, value, null, style, strUID);
115 case ExplicitInBlock:
116 block = new ExplicitInBlock(saxHandler.controller, uid, kind, value, null, style, strUID);
118 case ExplicitOutBlock:
119 block = new ExplicitOutBlock(saxHandler.controller, uid, kind, value, null, style, strUID);
122 block = new BasicBlock(saxHandler.controller, uid, kind, value, null, style, strUID);
125 block = new GroundBlock(saxHandler.controller, uid, kind, value, null, style, strUID);
127 case ImplicitInBlock:
128 block = new ImplicitInBlock(saxHandler.controller, uid, kind, value, null, style, strUID);
130 case ImplicitOutBlock:
131 block = new ImplicitOutBlock(saxHandler.controller, uid, kind, value, null, style, strUID);
134 block = new BasicBlock(saxHandler.controller, uid, kind, value, null, style, strUID);
137 block = new Product(saxHandler.controller, uid, kind, value, null, style, strUID);
140 block = new RoundBlock(saxHandler.controller, uid, kind, value, null, style, strUID);
143 block = new SplitBlock(saxHandler.controller, uid, kind, value, null, style, strUID);
146 block = new Summation(saxHandler.controller, uid, kind, value, null, style, strUID);
149 block = new SuperBlock(saxHandler.controller, uid, kind, value, null, style, strUID);
152 block = new TextBlock(saxHandler.controller, uid, kind, value, null, style, strUID);
154 case VoltageSensorBlock:
155 block = new VoltageSensorBlock(saxHandler.controller, uid, kind, value, null, style, strUID);
158 throw new IllegalArgumentException();
164 v = atts.getValue("interfaceFunctionName");
166 saxHandler.controller.setObjectProperty(uid, kind, ObjectProperties.INTERFACE_FUNCTION, v);
167 } else if (kind == Kind.BLOCK) {
168 // Setup the interface function accordingly to the mapping table
169 // by default it is empty and might not be serialized (depending on
171 String defaultInterfaceFunction = Arrays.stream(BlockInterFunction.values()).filter(n -> n.getKlass().equals(block.getClass())).map(e -> e.name())
173 saxHandler.controller.setObjectProperty(uid, kind, ObjectProperties.INTERFACE_FUNCTION, defaultInterfaceFunction);
175 v = atts.getValue("simulationFunctionName");
177 saxHandler.controller.setObjectProperty(uid, kind, ObjectProperties.SIM_FUNCTION_NAME, v);
179 v = atts.getValue("blockType");
181 saxHandler.controller.setObjectProperty(uid, kind, ObjectProperties.SIM_BLOCKTYPE, v);
183 VectorOfInt vecOfInt = new VectorOfInt(2);
184 v = atts.getValue("dependsOnU");
188 v = atts.getValue("dependsOnT");
192 saxHandler.controller.setObjectProperty(uid, kind, ObjectProperties.SIM_DEP_UT, vecOfInt);
193 v = atts.getValue("simulationFunctionType");
195 SimulationFunctionType type = SimulationFunctionType.valueOf(v);
196 saxHandler.controller.setObjectProperty(uid, kind, ObjectProperties.SIM_FUNCTION_API, type.getValue());
199 saxHandler.insertChild(block);
204 public void endElement(HandledElement found) {