田源
2025-01-15 78fa1f005a9ec2581611e53d7eba8efeacb4df6e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/**
 * $Id: mxGmlCodec.java,v 1.2 2010-10-21 13:15:40 david Exp $
 * Copyright (c) 2010 David Benson, Gaudenz Alder
 */
package com.vci.rmip.workflow.client.editor.code;
 
import com.mxgraph.io.gml.mxGmlConstants;
import com.mxgraph.io.gml.mxGmlData;
import com.mxgraph.io.gml.mxGmlEdge;
import com.mxgraph.io.gml.mxGmlGraph;
import com.mxgraph.io.gml.mxGmlKey;
import com.mxgraph.io.gml.mxGmlKeyManager;
import com.mxgraph.io.gml.mxGmlNode;
import com.mxgraph.io.gml.mxGmlShapeEdge;
import com.mxgraph.io.gml.mxGmlShapeNode;
import com.mxgraph.io.gml.mxGmlUtils;
import com.mxgraph.model.mxCell;
import com.mxgraph.util.mxConstants;
import com.mxgraph.util.mxPoint;
import com.mxgraph.util.mxUtils;
import com.mxgraph.view.mxCellState;
import com.mxgraph.view.mxConnectionConstraint;
 
import com.mxgraph.view.mxGraph;
import com.mxgraph.view.mxGraphView;
import java.util.HashMap;
import java.util.List;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
 
/**
 * Parses a GraphML .graphml file and imports it in the given graph.<br/>
 * This class depends from the classes contained in
 * com.mxgraph.io.gmlImplements.
 */
public class GraphGmlCodec
{
    /**
     * Receives a GraphMl document and parses it generating a new graph that is inserted in graph.
     * @param document XML to be parsed
     * @param graph Graph where the parsed graph is included.
     */
    public static void decode(Document document, mxGraph graph)
    {
        Object parent = graph.getDefaultParent();
 
        graph.getModel().beginUpdate();
 
        // Initialise the key properties.
        mxGmlKeyManager.getInstance().initialise(document);
 
        NodeList graphs = document.getElementsByTagName(mxGmlConstants.GRAPH);
        if (graphs.getLength() > 0)
        {
 
            Element graphElement = (Element) graphs.item(0);
 
            //Create the graph model.
            mxGmlGraph gmlGraph = new mxGmlGraph(graphElement);
 
            gmlGraph.addGraph(graph, parent);
        }
 
        graph.getModel().endUpdate();
        cleanMaps();
    }
 
    /**
     * Remove all the elements in the Defined Maps.
     */
    private static void cleanMaps()
    {
        mxGmlKeyManager.getInstance().getKeyMap().clear();
    }
 
    /**
     * Generates a Xml document with the gmlGraph.
     * @param gmlGraph Graph model.
     * @return The Xml document generated.
     */
    public static Document encodeXML(mxGmlGraph gmlGraph)
    {
        Document doc = mxUtils.createDocument();
 
        Element graphml = doc.createElement(mxGmlConstants.GRAPHML);
 
        graphml.setAttribute("xmlns", "http://graphml.graphdrawing.org/xmlns");
        graphml.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:xsi",
                "http://www.w3.org/2001/XMLSchema-instance");
        graphml.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:jGraph",
                mxGmlConstants.JGRAPH_URL);
        graphml.setAttributeNS(
                "http://www.w3.org/2001/XMLSchema-instance",
                "xsi:schemaLocation",
                "http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd");
 
        HashMap<String, mxGmlKey> keyMap = mxGmlKeyManager.getInstance()
                .getKeyMap();
 
        for (mxGmlKey key : keyMap.values())
        {
            Element keyElement = key.generateElement(doc);
            graphml.appendChild(keyElement);
        }
 
        Element graphE = gmlGraph.generateElement(doc);
        graphml.appendChild(graphE);
 
        doc.appendChild(graphml);
        cleanMaps();
        return doc;
 
    }
 
    /**
     * Generates a Xml document with the cells in the graph.
     * @param graph Graph with the cells.
     * @return The Xml document generated.
     */
    public static Document encode(mxGraph graph)
    {
        mxGmlGraph gmlGraph = new mxGmlGraph();
        Object parent = graph.getDefaultParent();
 
        createKeyElements();
 
        gmlGraph = decodeGraph(graph, parent, gmlGraph);
        gmlGraph.setEdgedefault(mxGmlConstants.EDGE_DIRECTED);
 
        Document document = encodeXML(gmlGraph);
 
        return document;
    }
 
    /**
     * Creates the key elements for the encode.
     */
    private static void createKeyElements()
    {
        HashMap<String, mxGmlKey> keyMap = mxGmlKeyManager.getInstance()
                .getKeyMap();
        mxGmlKey keyNode = new mxGmlKey(mxGmlConstants.KEY_NODE_ID,
                mxGmlKey.keyForValues.NODE, mxGmlConstants.KEY_NODE_NAME,
                mxGmlKey.keyTypeValues.STRING);
        keyMap.put(mxGmlConstants.KEY_NODE_ID, keyNode);
        mxGmlKey keyEdge = new mxGmlKey(mxGmlConstants.KEY_EDGE_ID,
                mxGmlKey.keyForValues.EDGE, mxGmlConstants.KEY_EDGE_NAME,
                mxGmlKey.keyTypeValues.STRING);
        keyMap.put(mxGmlConstants.KEY_EDGE_ID, keyEdge);
        mxGmlKeyManager.getInstance().setKeyMap(keyMap);
    }
 
    /**
     * Returns a Gml graph with the data of the vertexes and edges in the graph.
     * @param gmlGraph Gml document where the elements are put.
     * @param parent Parent cell of the vertexes and edges to be added.
     * @param graph Graph that contains the vertexes and edges.
     * @return Returns the document with the elements added.
     */
    public static mxGmlGraph decodeGraph(mxGraph graph, Object parent,
            mxGmlGraph gmlGraph)
    {
        Object[] vertexes = graph.getChildVertices(parent);
        List<mxGmlEdge> gmlEdges = gmlGraph.getEdges();
        gmlEdges = encodeEdges(gmlEdges, parent, graph);
        gmlGraph.setEdges(gmlEdges);
 
        for (Object vertex : vertexes)
        {
            List<mxGmlNode> Gmlnodes = gmlGraph.getNodes();
 
            mxCell v = (mxCell) vertex;
            String id = v.getId();
 
            mxGmlNode gmlNode = new mxGmlNode(id, null);
            addNodeData(gmlNode, v);
            Gmlnodes.add(gmlNode);
            gmlGraph.setNodes(Gmlnodes);
            mxGmlGraph gmlGraphx = new mxGmlGraph();
 
            gmlGraphx = decodeGraph(graph, vertex, gmlGraphx);
 
            if (!gmlGraphx.isEmpty())
            {
                List<mxGmlGraph> nodeGraphs = gmlNode.getNodeGraph();
                nodeGraphs.add(gmlGraphx);
                gmlNode.setNodeGraph(nodeGraphs);
            }
        }
 
        return gmlGraph;
    }
 
    /**
     * Add the node data in the gmlNode.
     * @param gmlNode Gml node where the data add.
     * @param v mxCell where data are obtained.
     */
    public static void addNodeData(mxGmlNode gmlNode, mxCell v)
    {
        mxGmlData data = new mxGmlData();
        mxGmlShapeNode dataShapeNode = new mxGmlShapeNode();
 
        data.setDataKey(mxGmlConstants.KEY_NODE_ID);
        dataShapeNode
                .setDataHeight(String.valueOf(v.getGeometry().getHeight()));
        dataShapeNode.setDataWidth(String.valueOf(v.getGeometry().getWidth()));
        dataShapeNode.setDataX(String.valueOf(v.getGeometry().getX()));
        dataShapeNode.setDataY(String.valueOf(v.getGeometry().getY()));
        dataShapeNode.setDataLabel(v.getValue() != null ? v.getValue()
                .toString() : "");
        dataShapeNode.setDataStyle(v.getStyle() != null ? v.getStyle() : "");
 
        data.setDataShapeNode(dataShapeNode);
        gmlNode.setNodeData(data);
    }
 
    /**
     * Add the edge data in the gmlEdge.
     * @param gmlEdge Gml edge where the data add.
     * @param v mxCell where data are obtained.
     */
    public static void addEdgeData(mxGmlEdge gmlEdge, mxCell v)
    {
        mxGmlData data = new mxGmlData();
        mxGmlShapeEdge dataShapeEdge = new mxGmlShapeEdge();
 
        data.setDataKey(mxGmlConstants.KEY_EDGE_ID);
        dataShapeEdge.setText(v.getValue() != null ? v.getValue().toString()
                : "");
        dataShapeEdge.setStyle(v.getStyle() != null ? v.getStyle() : "");
 
        data.setDataShapeEdge(dataShapeEdge);
        gmlEdge.setEdgeData(data);
    }
 
    /**
     * Converts a connection point in the string representation of a port.
     * The specials names North, NorthWest, NorthEast, East, West, South, SouthEast and SouthWest
     * may be returned. Else, the values returned follows the pattern "double,double"
     * where double must be in the range 0..1
     * @param point mxPoint
     * @return Name of the port
     */
    private static String pointToPortString(mxPoint point)
    {
        String port = "";
        if (point != null)
        {
            double x = point.getX();
            double y = point.getY();
 
            if (x == 0 && y == 0)
            {
                port = "NorthWest";
            }
            else if (x == 0.5 && y == 0)
            {
                port = "North";
            }
            else if (x == 1 && y == 0)
            {
                port = "NorthEast";
            }
            else if (x == 1 && y == 0.5)
            {
                port = "East";
            }
            else if (x == 1 && y == 1)
            {
                port = "SouthEast";
            }
            else if (x == 0.5 && y == 1)
            {
                port = "South";
            }
            else if (x == 0 && y == 1)
            {
                port = "SouthWest";
            }
            else if (x == 0 && y == 0.5)
            {
                port = "West";
            }
            else
            {
                port = "" + x + "," + y;
            }
        }
        return port;
    }
 
    /**
     * Returns a list of mxGmlEdge with the data of the edges in the graph.
     * @param Gmledges List where the elements are put.
     * @param parent Parent cell of the edges to be added.
     * @param graph Graph that contains the edges.
     * @return Returns the list Gmledges with the elements added.
     */
    private static List<mxGmlEdge> encodeEdges(List<mxGmlEdge> Gmledges,
            Object parent, mxGraph graph)
    {
        Object[] edges = graph.getChildEdges(parent);
        for (Object edge : edges)
        {
            mxCell e = (mxCell) edge;
            mxCell source = (mxCell) e.getSource();
            mxCell target = (mxCell) e.getTarget();
 
            String sourceName = "";
            String targetName = "";
            String sourcePort = "";
            String targetPort = "";
            sourceName = source != null ? source.getId() : "";
            targetName = target != null ? target.getId() : "";
 
            //Get the graph view that contains the states
            mxGraphView view = graph.getView();
            mxPoint sourceConstraint = null;
            mxPoint targetConstraint = null;
            if (view != null)
            {
                mxCellState edgeState = view.getState(edge);
                mxCellState sourceState = view.getState(source);
                mxConnectionConstraint scc = graph.getConnectionConstraint(
                        edgeState, sourceState, true);
                if (scc != null)
                {
                    sourceConstraint = scc.getPoint();
                }
 
                mxCellState targetState = view.getState(target);
                mxConnectionConstraint tcc = graph.getConnectionConstraint(
                        edgeState, targetState, false);
                if (tcc != null)
                {
                    targetConstraint = tcc.getPoint();
                }
            }
 
            //gets the port names
            targetPort = pointToPortString(targetConstraint);
            sourcePort = pointToPortString(sourceConstraint);
 
            mxGmlEdge Gmledge = new mxGmlEdge(sourceName, targetName,
                    sourcePort, targetPort);
 
            String style = e.getStyle();
 
            if (style == null)
            {
                style = "horizontal";
 
            }
 
            HashMap<String, Object> styleMap = mxGmlUtils.getStyleMap(style,
                    "=");
            String endArrow = (String) styleMap.get(mxConstants.STYLE_ENDARROW);
            if ((endArrow != null && !endArrow.equals(mxConstants.NONE))
                    || endArrow == null)
            {
                Gmledge.setEdgeDirected("true");
            }
            else
            {
                Gmledge.setEdgeDirected("false");
            }
            addEdgeData(Gmledge, e);
            Gmledges.add(Gmledge);
        }
 
        return Gmledges;
    }
}