2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2015-2015 - Scilab Enterprises - Clement DAVID
5 * Copyright (C) 2012 - 2016 - Scilab Enterprises
7 * This file is hereby licensed under the terms of the GNU GPL v2.0,
8 * pursuant to article 5.3.4 of the CeCILL v.2.1.
9 * This file was originally licensed under the terms of the CeCILL v2.1,
10 * and continues to be available under such terms.
11 * For more information, see the COPYING file which you should have received
12 * along with this program.
16 package org.scilab.modules.xcos.io.sax;
18 import java.util.ArrayList;
20 import org.scilab.modules.xcos.Kind;
21 import org.scilab.modules.xcos.ObjectProperties;
22 import org.scilab.modules.xcos.VectorOfInt;
23 import org.scilab.modules.xcos.VectorOfScicosID;
24 import org.scilab.modules.xcos.graph.model.ScicosObjectOwner;
25 import org.scilab.modules.xcos.graph.model.XcosCell;
26 import org.scilab.modules.xcos.io.HandledElement;
27 import org.scilab.modules.xcos.io.sax.XcosSAXHandler.UnresolvedReference;
28 import org.scilab.modules.xcos.port.BasicPort;
29 import org.scilab.modules.xcos.port.command.CommandPort;
30 import org.scilab.modules.xcos.port.control.ControlPort;
31 import org.scilab.modules.xcos.port.input.ExplicitInputPort;
32 import org.scilab.modules.xcos.port.input.ImplicitInputPort;
33 import org.scilab.modules.xcos.port.output.ExplicitOutputPort;
34 import org.scilab.modules.xcos.port.output.ImplicitOutputPort;
35 import org.xml.sax.Attributes;
37 class PortHandler implements ScilabHandler {
39 private final XcosSAXHandler shared;
43 * @param saxHandler the shared sax handler
45 PortHandler(XcosSAXHandler saxHandler) {
46 this.shared = saxHandler;
50 public BasicPort startElement(HandledElement found, Attributes atts) {
53 ObjectProperties relatedProperty;
57 * First, check if the port has already been defined. Otherwise, create the object in the model
59 String strUID = atts.getValue("id");
62 if (shared.allChildren.peek().containsKey(strUID)) {
63 uid = shared.allChildren.peek().get(strUID);
65 uid = shared.controller.createObject(Kind.PORT);
66 shared.allChildren.peek().put(strUID, uid);
70 String style = atts.getValue("style");
72 shared.controller.setObjectProperty(uid, Kind.PORT, ObjectProperties.STYLE, style);
75 String value = atts.getValue("value");
76 if (value != null && shared.validCIdentifier.matcher(value).matches()) {
77 shared.controller.setObjectProperty(uid, Kind.PORT, ObjectProperties.LABEL, value);
81 * Allocate the port with the right class to set default properties
86 port = new CommandPort(shared.controller, uid, Kind.PORT, value, style, strUID);
87 relatedProperty = ObjectProperties.EVENT_OUTPUTS;
91 port = new ControlPort(shared.controller, uid, Kind.PORT, value, style, strUID);
92 relatedProperty = ObjectProperties.EVENT_INPUTS;
95 case ExplicitInputPort:
96 port = new ExplicitInputPort(shared.controller, uid, Kind.PORT, value, style, strUID);
97 relatedProperty = ObjectProperties.INPUTS;
100 case ExplicitOutputPort:
101 port = new ExplicitOutputPort(shared.controller, uid, Kind.PORT, value, style, strUID);
102 relatedProperty = ObjectProperties.OUTPUTS;
105 case ImplicitInputPort:
106 port = new ImplicitInputPort(shared.controller, uid, Kind.PORT, value, style, strUID);
107 relatedProperty = ObjectProperties.INPUTS;
110 case ImplicitOutputPort:
111 port = new ImplicitOutputPort(shared.controller, uid, Kind.PORT, value, style, strUID);
112 relatedProperty = ObjectProperties.OUTPUTS;
116 throw new IllegalArgumentException();
120 * Setup the properties
122 VectorOfInt datatype = new VectorOfInt();
123 shared.controller.getObjectProperty(uid, Kind.PORT, ObjectProperties.DATATYPE, datatype);
125 v = atts.getValue("dataType");
126 int intValue = BasicPort.DataType.REAL_MATRIX.ordinal(); // Magic default value, used in Scilab 5.x
128 intValue = BasicPort.DataType.valueOf(v).ordinal();
130 datatype.set(2, intValue);
131 intValue = -2; // Magic default value, used in Scilab 5.x
132 v = atts.getValue("dataColumns");
134 intValue = Integer.parseInt(v);
136 datatype.set(1, intValue);
137 intValue = -1; // Magic default value, used in Scilab 5.x
138 v = atts.getValue("dataLines");
140 intValue = Integer.parseInt(v);
142 datatype.set(0, intValue);
144 shared.controller.setObjectProperty(uid, Kind.PORT, ObjectProperties.DATATYPE, datatype);
146 v = atts.getValue("initialState");
148 shared.controller.setObjectProperty(uid, Kind.PORT, ObjectProperties.FIRING, Double.valueOf(v));
152 * Associate to the parent block : now or later
158 v = atts.getValue("ordering");
160 ordering = Integer.parseInt(v) - 1;
163 v = atts.getValue("parent");
165 parent = shared.allChildren.peek().get(v);
167 // if we can resolve the parent, then connect it directly
168 if (parent != null) {
169 VectorOfScicosID associatedPorts = new VectorOfScicosID();
170 shared.controller.getObjectProperty(parent, Kind.BLOCK, relatedProperty, associatedPorts);
172 associatedPorts.resize(Math.max(associatedPorts.size(), ordering + 1));
173 associatedPorts.set(ordering, uid);
174 shared.controller.referenceObject(uid);
176 shared.controller.setObjectProperty(uid, Kind.PORT, ObjectProperties.SOURCE_BLOCK, parent);
177 shared.controller.setObjectProperty(parent, Kind.BLOCK, relatedProperty, associatedPorts);
179 // resolve the parent later
180 ArrayList<UnresolvedReference> refList = shared.unresolvedReferences.get(v);
181 if (refList == null) {
182 refList = new ArrayList<>();
183 shared.unresolvedReferences.put(v, refList);
185 refList.add(new UnresolvedReference(new ScicosObjectOwner(uid, Kind.PORT), ObjectProperties.SOURCE_BLOCK, relatedProperty, ordering));
190 * Associate to the link if possible (reverse linking)
192 v = atts.getValue("as");
194 ObjectProperties opposite = null;
195 if ("source".equals(v)) {
196 opposite = ObjectProperties.SOURCE_PORT;
197 } else if ("target".equals(v)) {
198 opposite = ObjectProperties.DESTINATION_PORT;
201 XcosCell cell = shared.lookupForParentXcosCellElement();
202 if (cell.getKind() == Kind.LINK) {
203 shared.controller.setObjectProperty(cell.getUID(), cell.getKind(), opposite, port.getUID());
204 shared.controller.setObjectProperty(port.getUID(), port.getKind(), ObjectProperties.CONNECTED_SIGNALS, cell.getUID());
212 public void endElement(HandledElement found) {