Xcos refactoring: move link classes
[scilab.git] / scilab / modules / xcos / src / java / org / scilab / modules / xcos / io / sax / LinkHandler.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
17 import org.scilab.modules.xcos.Kind;
18 import org.scilab.modules.xcos.ObjectProperties;
19 import org.scilab.modules.xcos.graph.model.ScicosObjectOwner;
20 import org.scilab.modules.xcos.io.HandledElement;
21 import org.scilab.modules.xcos.io.sax.XcosSAXHandler.UnresolvedReference;
22 import org.scilab.modules.xcos.link.BasicLink;
23 import org.scilab.modules.xcos.link.CommandControlLink;
24 import org.scilab.modules.xcos.link.ExplicitLink;
25 import org.scilab.modules.xcos.link.ImplicitLink;
26 import org.xml.sax.Attributes;
27 import org.xml.sax.SAXParseException;
28
29 class LinkHandler implements ScilabHandler {
30
31     private final XcosSAXHandler saxHandler;
32
33     /**
34      * Default constructor
35      *
36      * @param saxHandler
37      *            the shared sax handler
38      */
39     LinkHandler(XcosSAXHandler saxHandler) {
40         this.saxHandler = saxHandler;
41     }
42
43     @Override
44     public BasicLink startElement(HandledElement found, Attributes atts) {
45         String v;
46         BasicLink link;
47         int linkKind;
48         final long uid = saxHandler.controller.createObject(Kind.LINK);
49
50         switch (found) {
51             case CommandControlLink:
52                 link = new CommandControlLink(uid);
53                 linkKind = -1;
54                 break;
55             case ExplicitLink:
56                 link = new ExplicitLink(uid);
57                 linkKind = 1;
58                 break;
59             case ImplicitLink:
60                 link = new ImplicitLink(uid);
61                 linkKind = 2;
62                 break;
63             default:
64                 throw new IllegalArgumentException();
65         }
66
67         /*
68          * Set the attributes
69          */
70         v = atts.getValue("id");
71         if (v != null) {
72             link.setId(v);
73             saxHandler.allChildren.peek().put(v, uid);
74         }
75
76         saxHandler.controller.setObjectProperty(uid, Kind.LINK, ObjectProperties.KIND, linkKind);
77
78         v = atts.getValue("source");
79         if (v != null) {
80             Long src = saxHandler.allChildren.peek().get(v);
81             if (src != null) {
82                 // if the attribute is present then the connected port is already
83                 // decoded and present in the map
84                 saxHandler.controller.setObjectProperty(uid, Kind.LINK, ObjectProperties.SOURCE_PORT, src.longValue());
85                 saxHandler.controller.setObjectProperty(src.longValue(), Kind.PORT, ObjectProperties.CONNECTED_SIGNALS, uid);
86             } else {
87                 // if not present then it will be resolved later
88                 ArrayList<UnresolvedReference> refList = saxHandler.unresolvedReferences.get(v);
89                 if (refList == null) {
90                     refList = new ArrayList<>();
91                     saxHandler.unresolvedReferences.put(v, refList);
92                 }
93                 refList.add(new UnresolvedReference(new ScicosObjectOwner(uid, Kind.LINK), ObjectProperties.SOURCE_PORT, ObjectProperties.CONNECTED_SIGNALS, 0));
94             }
95         }
96
97         v = atts.getValue("target");
98         if (v != null) {
99             Long dst = saxHandler.allChildren.peek().get(v);
100             if (dst != null) {
101                 // if the attribute is present then the connected port is already
102                 // decoded and present in the map
103                 saxHandler.controller.setObjectProperty(uid, Kind.LINK, ObjectProperties.DESTINATION_PORT, dst.longValue());
104                 saxHandler.controller.setObjectProperty(dst.longValue(), Kind.PORT, ObjectProperties.CONNECTED_SIGNALS, uid);
105             } else {
106                 // if not present then it will be resolved later
107                 ArrayList<UnresolvedReference> refList = saxHandler.unresolvedReferences.get(v);
108                 if (refList == null) {
109                     refList = new ArrayList<>();
110                     saxHandler.unresolvedReferences.put(v, refList);
111                 }
112                 refList.add(new UnresolvedReference(new ScicosObjectOwner(uid, Kind.LINK), ObjectProperties.DESTINATION_PORT, ObjectProperties.CONNECTED_SIGNALS, 0));
113             }
114         }
115
116         v = atts.getValue("style");
117         if (v != null) {
118             saxHandler.controller.setObjectProperty(uid, Kind.LINK, ObjectProperties.STYLE, v);
119         }
120
121         v = atts.getValue("value");
122         if (v != null) {
123             saxHandler.controller.setObjectProperty(uid, Kind.LINK, ObjectProperties.LABEL, v);
124         }
125
126         saxHandler.insertChild(link);
127         return link;
128     }
129
130     @Override
131     public void endElement(HandledElement found) {
132     }
133 }