Xcos: use LABEL for C identifier and DESCRIPTION for textual or HTML content
[scilab.git] / scilab / modules / xcos / src / java / org / scilab / modules / xcos / io / sax / JGraphXHandler.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  * Copyright (C) 2012 - 2016 - Scilab Enterprises
6  *
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.
13  *
14  */
15
16 package org.scilab.modules.xcos.io.sax;
17
18 import org.scilab.modules.xcos.Kind;
19 import org.scilab.modules.xcos.ObjectProperties;
20 import org.scilab.modules.xcos.graph.model.XcosCell;
21 import org.scilab.modules.xcos.io.HandledElement;
22 import org.xml.sax.Attributes;
23
24 import com.mxgraph.model.mxGeometry;
25 import com.mxgraph.util.mxPoint;
26 import java.util.ArrayList;
27
28 class JGraphXHandler implements ScilabHandler {
29
30     private final XcosSAXHandler saxHandler;
31
32     JGraphXHandler(XcosSAXHandler saxHandler) {
33         this.saxHandler = saxHandler;
34     }
35
36     @Override
37     public Object startElement(HandledElement found, Attributes atts) {
38         String v;
39
40         switch (found) {
41             case mxCell: {
42                 v = atts.getValue("parent");
43                 if (v != null) {
44                     long parentUID = saxHandler.allChildren.peek().getOrDefault(v, 0l);
45                     if (parentUID != 0) {
46                         v = atts.getValue("value");
47                         if (v != null && saxHandler.validCIdentifier.matcher(v).matches()) {
48                             Kind kind = saxHandler.controller.getKind(parentUID);
49                             saxHandler.controller.setObjectProperty(parentUID, kind, ObjectProperties.LABEL, v);
50                         }
51                     }
52                 }
53
54                 return null;
55             }
56             case mxGeometry: {
57                 mxGeometry g = new mxGeometry();
58
59                 v = atts.getValue("height");
60                 if (v != null) {
61                     g.setHeight(Double.valueOf(v));
62                 }
63                 v = atts.getValue("width");
64                 if (v != null) {
65                     g.setWidth(Double.valueOf(v));
66                 }
67                 v = atts.getValue("x");
68                 if (v != null) {
69                     g.setX(Double.valueOf(v));
70                 }
71                 v = atts.getValue("y");
72                 if (v != null) {
73                     g.setY(Double.valueOf(v));
74                 }
75
76                 return g;
77             }
78             case mxPoint: {
79                 mxPoint p = new mxPoint();
80
81                 v = atts.getValue("x");
82                 if (v != null) {
83                     p.setX(Double.valueOf(v));
84                 }
85                 v = atts.getValue("y");
86                 if (v != null) {
87                     p.setY(Double.valueOf(v));
88                 }
89
90                 if (saxHandler.parents.peek() instanceof mxGeometry) {
91                     mxGeometry parent = (mxGeometry) saxHandler.parents.peek();
92                     v = atts.getValue("as");
93                     if ("sourcePoint".equals(v)) {
94                         parent.setSourcePoint(p);
95                     } else if ("targetPoint".equals(v)) {
96                         parent.setTargetPoint(p);
97                     }
98                 } else if (saxHandler.parents.peek() instanceof RawDataHandler.RawDataDescriptor) {
99                     RawDataHandler.RawDataDescriptor parent = (RawDataHandler.RawDataDescriptor) saxHandler.parents.peek();
100                     ((ArrayList) parent.value).add(p);
101                 }
102                 return p;
103             }
104             default:
105                 throw new IllegalArgumentException();
106         }
107     }
108
109     @Override
110     public void endElement(HandledElement found) {
111         switch (found) {
112             case mxCell:
113                 break;
114             case mxGeometry: {
115                 // defensive programming
116                 if (!(saxHandler.parents.peek() instanceof mxGeometry)) {
117                     return;
118                 }
119                 mxGeometry g = (mxGeometry) saxHandler.parents.peek();
120                 if (!(saxHandler.parents.peek(1) instanceof XcosCell)) {
121                     return;
122                 }
123                 XcosCell cell = (XcosCell) saxHandler.parents.peek(1);
124
125                 cell.setGeometry(g);
126             }
127             break;
128             case mxPoint:
129                 break;
130             default:
131                 throw new IllegalArgumentException();
132         }
133     }
134 }