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 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;
24 import com.mxgraph.model.mxGeometry;
25 import com.mxgraph.util.mxPoint;
26 import java.util.ArrayList;
28 class JGraphXHandler implements ScilabHandler {
30 private final XcosSAXHandler saxHandler;
32 JGraphXHandler(XcosSAXHandler saxHandler) {
33 this.saxHandler = saxHandler;
37 public Object startElement(HandledElement found, Attributes atts) {
42 v = atts.getValue("parent");
44 long parentUID = saxHandler.allChildren.peek().getOrDefault(v, 0l);
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);
57 mxGeometry g = new mxGeometry();
59 v = atts.getValue("height");
61 g.setHeight(Double.valueOf(v));
63 v = atts.getValue("width");
65 g.setWidth(Double.valueOf(v));
67 v = atts.getValue("x");
69 g.setX(Double.valueOf(v));
71 v = atts.getValue("y");
73 g.setY(Double.valueOf(v));
79 mxPoint p = new mxPoint();
81 v = atts.getValue("x");
83 p.setX(Double.valueOf(v));
85 v = atts.getValue("y");
87 p.setY(Double.valueOf(v));
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);
98 } else if (saxHandler.parents.peek() instanceof RawDataHandler.RawDataDescriptor) {
99 RawDataHandler.RawDataDescriptor parent = (RawDataHandler.RawDataDescriptor) saxHandler.parents.peek();
100 ((ArrayList) parent.value).add(p);
105 throw new IllegalArgumentException();
110 public void endElement(HandledElement found) {
115 // defensive programming
116 if (!(saxHandler.parents.peek() instanceof mxGeometry)) {
119 mxGeometry g = (mxGeometry) saxHandler.parents.peek();
120 if (!(saxHandler.parents.peek(1) instanceof XcosCell)) {
123 XcosCell cell = (XcosCell) saxHandler.parents.peek(1);
131 throw new IllegalArgumentException();