田源
2025-01-16 404966637eda6881a0f17683c5aacc7c1c34aed8
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
/**
 * $Id: mxGdCodec.java,v 1.1 2012/11/15 13:26:47 gaudenz Exp $
 * Copyright (c) 2010-2012, JGraph Ltd
 */
package com.mxgraph.io;
 
import com.mxgraph.model.mxGraphModel;
import com.mxgraph.view.mxGraph;
 
import java.io.BufferedReader;
import java.io.StringReader;
import java.util.HashMap;
 
/**
 * Parses a GD .txt file and imports it in the given graph.<br/>
 * This class depends from the classes contained in
 * com.mxgraph.io.gd.
 */
public class mxGdCodec
{
    /**
     * Represents the different states in the parse of a file.
     */
    public enum mxGDParseState
    {
        START, NUM_NODES, PARSING_NODES, PARSING_EDGES
    }
 
    /**
     * Map with the vertex cells added in the addNode method.
     */
    protected static HashMap<String, Object> cellsMap = new HashMap<String, Object>();
 
    /**
     * Parses simple GD format and populate the specified graph
     * @param input GD file to be parsed
     * @param graph Graph where the parsed graph is included.
     */
    public static void decode(String input, mxGraph graph)
    {
        BufferedReader br = new BufferedReader(new StringReader(input));
        mxGDParseState state = mxGDParseState.START;
        Object parent = graph.getDefaultParent();
 
        graph.getModel().beginUpdate();
        
        try
        {
            String line = br.readLine().trim();
            while (line != null)
            {
                switch (state)
                {
                    case START:
                    {
                        if (!line.startsWith("#"))
                        {
                            state = mxGDParseState.NUM_NODES;
                        }
                        else
                        {
                            break;
                        }
                    }
                    case NUM_NODES:
                    {
                        if (!line.startsWith("#"))
                        {
                            int numVertices = Integer.valueOf(line);
                            
                            for (int i = 0; i < numVertices; i++)
                            {
                                String label = String.valueOf(i);
                                Object vertex = graph.insertVertex(parent, label, label,
                                        0, 0, 10, 10);
                                
                                cellsMap.put(label, vertex);
                            }
                        }
                        else
                        {
                            state = mxGDParseState.PARSING_EDGES;
                        }
                        
                        break;
                    }
                    case PARSING_NODES:
                    {
                        if (line.startsWith("# Edges"))
                        {
                            state = mxGDParseState.PARSING_EDGES;
                        }
                        else if (!line.equals(""))
                        {
                            String[] items = line.split(",");
                            if (items.length != 5)
                            {
                                throw new Exception("Error in parsing");
                            }
                            else
                            {
                                double x = Double.valueOf(items[1]);
                                double y = Double.valueOf(items[2]);
                                double width = Double.valueOf(items[3]);
                                double height = Double.valueOf(items[4]);
                                
                                
                                //Set the node name as label.
                                String label = items[0];
 
                                //Insert a new vertex in the graph
                                Object vertex = graph.insertVertex(parent, label, label,
                                        x - width / 2.0, y - height / 2.0, width,
                                        height);
                                
                                cellsMap.put(label, vertex);
                            }
                        }
                        break;
                    }
                    case PARSING_EDGES:
                    {
                        if (!line.equals(""))
                        {
                            String[] items = line.split(" ");
                            if (items.length != 2)
                            {
                                throw new Exception("Error in parsing");
                            }
                            else
                            {
                                Object source = cellsMap.get(items[0]);
                                Object target = cellsMap.get(items[1]);
 
                                graph.insertEdge(parent, null, "", source, target);
                            }
                        }
                        break;
                    }
                }
 
                line = br.readLine();
            }
        }
        
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            graph.getModel().endUpdate();
        }
    }
    
    /**
     * Generates a GD text output with the cells in the graph.
     * The implementation only uses the cells located in the default parent.
     * @param graph Graph with the cells.
     * @return The GD document generated.
     */
    public static String encode(mxGraph graph)
    {
        StringBuilder builder = new StringBuilder();
        
        Object parent = graph.getDefaultParent();
        Object[] vertices = mxGraphModel.getChildCells(graph.getModel(), parent, true, false);
        
        builder.append("# Number of Nodes (0-" + String.valueOf(vertices.length - 1) + ")");
        builder.append(String.valueOf(vertices.length));
        
        // TODO
 
        return builder.toString();
    }
}