20a7c86b82ef7442d4d443b549788cd7f71e4d67
[scilab.git] / scilab / modules / xcos / src / java / org / scilab / modules / xcos / io / writer / CustomWriter.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 package org.scilab.modules.xcos.io.writer;
16
17 import java.rmi.server.UID;
18
19 import javax.xml.stream.XMLStreamException;
20
21 import org.scilab.modules.xcos.Kind;
22 import org.scilab.modules.xcos.ObjectProperties;
23 import org.scilab.modules.xcos.VectorOfDouble;
24 import org.scilab.modules.xcos.VectorOfInt;
25 import org.scilab.modules.xcos.VectorOfScicosID;
26 import org.scilab.modules.xcos.Xcos;
27 import org.scilab.modules.xcos.graph.ScicosParameters;
28 import org.scilab.modules.xcos.io.HandledElement;
29
30 public class CustomWriter extends ScilabWriter {
31     private static final String SEP = " - ";
32
33     public CustomWriter(XcosWriter writer) {
34         super(writer);
35     }
36
37     @Override
38     public void write(long uid, Kind kind) throws XMLStreamException {
39         String[] str = new String[1];
40         int[] integer = new int[1];
41         VectorOfDouble vDouble = new VectorOfDouble();
42         VectorOfScicosID children = new VectorOfScicosID();
43
44         switch (kind) {
45             case DIAGRAM:
46                 shared.stream.writeStartDocument();
47
48                 /*
49                  * Add a version comment at startup
50                  */
51
52                 final Package p = Package.getPackage("org.scilab.modules.xcos");
53                 String comment = new StringBuilder().append(Xcos.TRADENAME).append(SEP).append(Xcos.VERSION).append(SEP)
54                 .append(p.getSpecificationVersion()).append(SEP).append(p.getImplementationVersion()).toString();
55                 shared.stream.writeComment(comment);
56                 shared.stream.writeCharacters("\n");
57
58                 shared.stream.writeStartElement(HandledElement.XcosDiagram.name());
59
60                 /*
61                  * Write diagram content
62                  */
63                 shared.controller.getObjectProperty(uid, kind, ObjectProperties.DEBUG_LEVEL, integer);
64                 shared.stream.writeAttribute("debugLevel", Integer.toString(integer[0]));
65
66                 // write simulation properties
67                 shared.controller.getObjectProperty(uid, kind, ObjectProperties.PROPERTIES, vDouble);
68                 String prop;
69
70                 prop = Double.toString(vDouble.get(ScicosParameters.FINAL_INTEGRATION_TIME));
71                 shared.stream.writeAttribute("finalIntegrationTime", prop);
72                 prop = Double.toString(vDouble.get(ScicosParameters.INTEGRATOR_ABSOLUTE_TOLERANCE));
73                 shared.stream.writeAttribute("integratorAbsoluteTolerance", prop);
74                 prop = Double.toString(vDouble.get(ScicosParameters.INTEGRATOR_RELATIVE_TOLERANCE));
75                 shared.stream.writeAttribute("integratorRelativeTolerance", prop);
76                 prop = Double.toString(vDouble.get(ScicosParameters.TOLERANCE_ON_TIME));
77                 shared.stream.writeAttribute("toleranceOnTime", prop);
78                 prop = Double.toString(vDouble.get(ScicosParameters.MAX_INTEGRATION_TIME_INTERVAL));
79                 shared.stream.writeAttribute("maxIntegrationTimeInterval", prop);
80                 prop = Double.toString(vDouble.get(ScicosParameters.MAXIMUM_STEP_SIZE));
81                 shared.stream.writeAttribute("maximumStepSize", prop);
82                 prop = Double.toString(vDouble.get(ScicosParameters.REAL_TIME_SCALING));
83                 shared.stream.writeAttribute("realTimeScaling", prop);
84                 prop = Double.toString(vDouble.get(ScicosParameters.SOLVER));
85                 shared.stream.writeAttribute("solver", prop);
86
87                 // write the graphical part and children
88                 shared.controller.getObjectProperty(uid, kind, ObjectProperties.CHILDREN, children);
89                 String zeroUID = new UID((short) 0).toString();
90                 shared.uniqueUIDs.add(zeroUID);
91                 writeDiagramAndSuperDiagramContent(uid, kind, children, zeroUID);
92
93                 shared.stream.writeEndElement();
94                 shared.stream.writeEndDocument();
95
96                 // post processing for binary data handling
97                 shared.rawDataWriter.fillSharedDictionary();
98                 break;
99             case BLOCK:
100                 shared.controller.getObjectProperty(uid, kind, ObjectProperties.CHILDREN, children);
101                 if (children.size() > 0) {
102                     shared.stream.writeStartElement(HandledElement.SuperBlockDiagram.name());
103                     shared.stream.writeAttribute("as", "child");
104                     writeDiagramAndSuperDiagramContent(uid, kind, children, shared.layers.peek());
105                     shared.stream.writeEndElement(); // SuperBlockDiagram
106                 }
107                 break;
108             case PORT:
109                 // the orientation is not encoded on the model but propagated inside the style
110                 break;
111             default:
112                 break;
113         }
114     }
115
116     private void writeDiagramAndSuperDiagramContent(long uid, Kind kind, VectorOfScicosID children, String parentUID) throws XMLStreamException {
117         VectorOfInt colors = new VectorOfInt();
118         shared.controller.getObjectProperty(uid, kind, ObjectProperties.COLOR, colors);
119         shared.stream.writeAttribute("background", Integer.toString(colors.get(0)));
120         shared.stream.writeAttribute("gridEnabled", Integer.toString(colors.get(1)));
121
122         String[] str = new String[1];
123         shared.controller.getObjectProperty(uid, kind, ObjectProperties.TITLE, str);
124         shared.stream.writeAttribute("title", str[0]);
125
126         /*
127          * encode some content then the children
128          */
129         shared.rawDataWriter.write(uid, kind);
130
131         /*
132          * Generate uniques but predictables UIDs
133          */
134         String[] parent = parentUID.split(":");
135         long uidCounter = 1;
136         String rootUID = parent[0] + ":" + Long.toString(Long.parseLong(parent[1], 16) + uidCounter, 16) + ":" + parent[2];
137         for (; shared.uniqueUIDs.contains(rootUID); uidCounter++) {
138             rootUID = parent[0] + ":" + Long.toString(Long.parseLong(parent[1], 16) + uidCounter, 16) + ":" + parent[2];
139         }
140         shared.uniqueUIDs.add(rootUID);
141         String layerUID = parent[0] + ":" + Long.toString(Long.parseLong(parent[1], 16) + uidCounter, 16) + ":" + parent[2];
142         for (; shared.uniqueUIDs.contains(layerUID); uidCounter++) {
143             layerUID = parent[0] + ":" + Long.toString(Long.parseLong(parent[1], 16) + uidCounter, 16) + ":" + parent[2];
144         }
145         shared.uniqueUIDs.add(layerUID);
146         shared.layers.push(layerUID);
147
148         // children header
149         shared.stream.writeStartElement("mxGraphModel");
150         shared.stream.writeAttribute("as", "model");
151         shared.stream.writeStartElement("root");
152         shared.stream.writeEmptyElement("mxCell");
153         shared.stream.writeAttribute("id", rootUID);
154         shared.stream.writeEmptyElement("mxCell");
155         shared.stream.writeAttribute("id", layerUID);
156         shared.stream.writeAttribute("parent", rootUID);
157
158         // loop on all children, encode the blocks and ports then the links
159         VectorOfInt kinds = new VectorOfInt();
160         shared.controller.sortAndFillKind(children, kinds);
161
162         final int len = children.size();
163         for (int i = 0; i < len; i++) {
164             long child = children.get(i);
165             Kind childKind = Kind.values()[kinds.get(i)];
166             shared.write(child, childKind);
167         }
168
169         shared.layers.pop();
170         shared.stream.writeEndElement(); // root
171         shared.stream.writeEndElement(); // mxGraphModel
172         shared.stream.writeEmptyElement("mxCell");
173         shared.stream.writeAttribute("as", "defaultParent");
174         shared.stream.writeAttribute("id", layerUID);
175         shared.stream.writeAttribute("parent", rootUID);
176     }
177 }