forked from jgraph/mxgraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmxGraphViewReader.java
More file actions
227 lines (193 loc) · 5.16 KB
/
mxGraphViewReader.java
File metadata and controls
227 lines (193 loc) · 5.16 KB
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
/**
* Copyright (c) 2007, Gaudenz Alder
*/
package com.mxgraph.reader;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import com.mxgraph.canvas.mxICanvas;
import com.mxgraph.util.mxPoint;
import com.mxgraph.util.mxRectangle;
import com.mxgraph.util.mxUtils;
import com.mxgraph.view.mxCellState;
/**
* An abstract converter that renders display XML data onto a canvas.
*/
public abstract class mxGraphViewReader extends DefaultHandler
{
/**
* Holds the canvas to be used for rendering the graph.
*/
protected mxICanvas canvas;
/**
* Holds the global scale of the graph. This is set just before
* createCanvas is called.
*/
protected double scale = 1;
/**
* Specifies if labels should be rendered as HTML markup.
*/
protected boolean htmlLabels = false;
/**
* Sets the htmlLabels switch.
*/
public void setHtmlLabels(boolean value)
{
htmlLabels = value;
}
/**
* Returns the htmlLabels switch.
*/
public boolean isHtmlLabels()
{
return htmlLabels;
}
/**
* Returns the canvas to be used for rendering.
*
* @param attrs Specifies the attributes of the new canvas.
* @return Returns a new canvas.
*/
public abstract mxICanvas createCanvas(Map<String, Object> attrs);
/**
* Returns the canvas that is used for rendering the graph.
*
* @return Returns the canvas.
*/
public mxICanvas getCanvas()
{
return canvas;
}
/* (non-Javadoc)
* @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
*/
public void startElement(String uri, String localName, String qName,
Attributes atts) throws SAXException
{
String tagName = qName.toUpperCase();
Map<String, Object> attrs = new Hashtable<String, Object>();
for (int i = 0; i < atts.getLength(); i++)
{
String name = atts.getQName(i);
// Workaround for possible null name
if (name == null || name.length() == 0)
{
name = atts.getLocalName(i);
}
attrs.put(name, atts.getValue(i));
}
parseElement(tagName, attrs);
}
/**
* Parses the given element and paints it onto the canvas.
*
* @param tagName Name of the node to be parsed.
* @param attrs Attributes of the node to be parsed.
*/
public void parseElement(String tagName, Map<String, Object> attrs)
{
if (canvas == null && tagName.equalsIgnoreCase("graph"))
{
scale = mxUtils.getDouble(attrs, "scale", 1);
canvas = createCanvas(attrs);
if (canvas != null)
{
canvas.setScale(scale);
}
}
else if (canvas != null)
{
boolean edge = tagName.equalsIgnoreCase("edge");
boolean group = tagName.equalsIgnoreCase("group");
boolean vertex = tagName.equalsIgnoreCase("vertex");
if ((edge && attrs.containsKey("points"))
|| ((vertex || group) && attrs.containsKey("x")
&& attrs.containsKey("y")
&& attrs.containsKey("width") && attrs
.containsKey("height")))
{
mxCellState state = new mxCellState(null, null, attrs);
String label = parseState(state, edge);
canvas.drawCell(state);
canvas.drawLabel(label, state, isHtmlLabels());
}
}
}
/**
* Parses the bounds, absolute points and label information from the style
* of the state into its respective fields and returns the label of the
* cell.
*/
public String parseState(mxCellState state, boolean edge)
{
Map<String, Object> style = state.getStyle();
// Parses the bounds
state.setX(mxUtils.getDouble(style, "x"));
state.setY(mxUtils.getDouble(style, "y"));
state.setWidth(mxUtils.getDouble(style, "width"));
state.setHeight(mxUtils.getDouble(style, "height"));
// Parses the absolute points list
List<mxPoint> pts = parsePoints(mxUtils.getString(style, "points"));
if (pts.size() > 0)
{
state.setAbsolutePoints(pts);
}
// Parses the label and label bounds
String label = mxUtils.getString(style, "label");
if (label != null && label.length() > 0)
{
mxPoint offset = new mxPoint(mxUtils.getDouble(style, "dx"),
mxUtils.getDouble(style, "dy"));
mxRectangle vertexBounds = (!edge) ? state : null;
state.setLabelBounds(mxUtils.getLabelPaintBounds(label, state
.getStyle(), mxUtils.isTrue(style, "html", false), offset,
vertexBounds, scale));
}
return label;
}
/**
* Parses the list of points into an object-oriented representation.
*
* @param pts String containing a list of points.
* @return Returns the points as a list of mxPoints.
*/
public static List<mxPoint> parsePoints(String pts)
{
List<mxPoint> result = new ArrayList<mxPoint>();
if (pts != null)
{
int len = pts.length();
String tmp = "";
String x = null;
for (int i = 0; i < len; i++)
{
char c = pts.charAt(i);
if (c == ',' || c == ' ')
{
if (x == null)
{
x = tmp;
}
else
{
result.add(new mxPoint(Double.parseDouble(x), Double
.parseDouble(tmp)));
x = null;
}
tmp = "";
}
else
{
tmp += c;
}
}
result.add(new mxPoint(Double.parseDouble(x), Double
.parseDouble(tmp)));
}
return result;
}
}