|
Lines 1-600
Link Here
|
| 1 |
/******************************************************************************* |
|
|
| 2 |
* Copyright (c) 2009, 2016 Fabian Steeg and others. |
| 3 |
* |
| 4 |
* All rights reserved. This program and the accompanying materials |
| 5 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 6 |
* which accompanies this distribution, and is available at |
| 7 |
* http://www.eclipse.org/legal/epl-v10.html |
| 8 |
* |
| 9 |
* Contributors: |
| 10 |
* Fabian Steeg - initial API and implementation (bug #277380) |
| 11 |
* Alexander Nyßen (itemis AG) - several refactorings and additions (bugs #487081, #489793) |
| 12 |
* Tamas Miklossy (itemis AG) - support for arrowType edge decorations (bug #477980) |
| 13 |
* - support for polygon-based node shapes (bug #441352) |
| 14 |
* |
| 15 |
*******************************************************************************/ |
| 16 |
|
| 17 |
package org.eclipse.gef4.dot.internal; |
| 18 |
|
| 19 |
import java.util.ArrayList; |
| 20 |
import java.util.HashMap; |
| 21 |
import java.util.Iterator; |
| 22 |
import java.util.List; |
| 23 |
import java.util.Map; |
| 24 |
|
| 25 |
import org.eclipse.emf.common.util.TreeIterator; |
| 26 |
import org.eclipse.emf.ecore.EObject; |
| 27 |
import org.eclipse.emf.ecore.util.EcoreUtil; |
| 28 |
import org.eclipse.gef4.dot.internal.parser.conversion.DotTerminalConverters; |
| 29 |
import org.eclipse.gef4.dot.internal.parser.dot.AttrList; |
| 30 |
import org.eclipse.gef4.dot.internal.parser.dot.AttrStmt; |
| 31 |
import org.eclipse.gef4.dot.internal.parser.dot.Attribute; |
| 32 |
import org.eclipse.gef4.dot.internal.parser.dot.AttributeType; |
| 33 |
import org.eclipse.gef4.dot.internal.parser.dot.DotAst; |
| 34 |
import org.eclipse.gef4.dot.internal.parser.dot.DotGraph; |
| 35 |
import org.eclipse.gef4.dot.internal.parser.dot.EdgeRhsNode; |
| 36 |
import org.eclipse.gef4.dot.internal.parser.dot.EdgeStmtNode; |
| 37 |
import org.eclipse.gef4.dot.internal.parser.dot.GraphType; |
| 38 |
import org.eclipse.gef4.dot.internal.parser.dot.NodeId; |
| 39 |
import org.eclipse.gef4.dot.internal.parser.dot.NodeStmt; |
| 40 |
import org.eclipse.gef4.dot.internal.parser.dot.Stmt; |
| 41 |
import org.eclipse.gef4.dot.internal.parser.dot.util.DotSwitch; |
| 42 |
import org.eclipse.gef4.dot.internal.parser.splines.Splines; |
| 43 |
import org.eclipse.gef4.graph.Edge; |
| 44 |
import org.eclipse.gef4.graph.Graph; |
| 45 |
import org.eclipse.gef4.graph.Graph.Builder; |
| 46 |
import org.eclipse.gef4.graph.Node; |
| 47 |
|
| 48 |
/** |
| 49 |
* Create a {@link Graph} instance from a DOT string by interpreting the AST of |
| 50 |
* the parsed DOT. |
| 51 |
* |
| 52 |
* @author Fabian Steeg (fsteeg) |
| 53 |
* @author Alexander Nyßen (anyssen) |
| 54 |
*/ |
| 55 |
// TODO: merge this into DotImport; turning DotImport into an Xtend class (so we |
| 56 |
// can use create functions) |
| 57 |
public final class DotInterpreter extends DotSwitch<Object> { |
| 58 |
|
| 59 |
private Builder graphBuilder; |
| 60 |
private Map<String, Node> nodesByName = new HashMap<>(); |
| 61 |
|
| 62 |
private Map<String, String> globalGraphAttributes = new HashMap<>(); |
| 63 |
private Map<String, String> globalNodeAttributes = new HashMap<>(); |
| 64 |
private Map<String, String> globalEdgeAttributes = new HashMap<>(); |
| 65 |
|
| 66 |
private boolean createEdge; |
| 67 |
private String currentArrowHead; |
| 68 |
private String currentArrowTail; |
| 69 |
private String currentArrowSize; |
| 70 |
private String currentEdgeDirection; |
| 71 |
private String currentEdgeStyle; |
| 72 |
private String currentEdgeLabel; |
| 73 |
private String currentEdgeSourceNodeName; |
| 74 |
private String currentEdgePos; |
| 75 |
private String currentEdgeXLabel; |
| 76 |
private String currentEdgeXlp; |
| 77 |
private String currentEdgeLp; |
| 78 |
private String currentEdgeTailLabel; |
| 79 |
private String currentEdgeHeadLabel; |
| 80 |
private String currentEdgeHeadLp; |
| 81 |
private String currentEdgeTailLp; |
| 82 |
private String currentEdgeId; |
| 83 |
private String currentEdgeOp; |
| 84 |
|
| 85 |
/** |
| 86 |
* @param dotAst |
| 87 |
* The DOT abstract syntax tree (AST) to interpret |
| 88 |
* @return A graph instance for the given DOT AST |
| 89 |
*/ |
| 90 |
public List<Graph> interpret(DotAst dotAst) { |
| 91 |
List<Graph> graphs = new ArrayList<>(); |
| 92 |
for (DotGraph dotGraph : dotAst.getGraphs()) { |
| 93 |
// clear global attributes, which only hold for each respective |
| 94 |
// graph |
| 95 |
globalGraphAttributes.clear(); |
| 96 |
globalNodeAttributes.clear(); |
| 97 |
globalEdgeAttributes.clear(); |
| 98 |
|
| 99 |
// create a new graph builder and clear the nodes map |
| 100 |
graphBuilder = new Graph.Builder(); |
| 101 |
nodesByName.clear(); |
| 102 |
|
| 103 |
// process all contents (nodes, edges, attributes) |
| 104 |
TreeIterator<Object> contents = EcoreUtil |
| 105 |
.getAllProperContents(dotGraph, false); |
| 106 |
while (contents.hasNext()) { |
| 107 |
doSwitch((EObject) contents.next()); |
| 108 |
} |
| 109 |
|
| 110 |
// process the graph last, so we can initialize attributes of the |
| 111 |
// created graph object rather than using the builder; we can thus |
| 112 |
// ensure attribute values get properly validated. |
| 113 |
Graph g = (Graph) doSwitch(dotGraph); |
| 114 |
if (g != null) { |
| 115 |
graphs.add(g); |
| 116 |
} |
| 117 |
} |
| 118 |
return graphs; |
| 119 |
} |
| 120 |
|
| 121 |
@Override |
| 122 |
public Object caseDotGraph(DotGraph dotGraph) { |
| 123 |
// name (meta-attribute) |
| 124 |
String name = escaped(dotGraph.getName()); |
| 125 |
if (name != null) { |
| 126 |
graphBuilder.attr(DotAttributes._NAME__GNE, name); |
| 127 |
} |
| 128 |
|
| 129 |
// type (meta-attribute) |
| 130 |
GraphType graphType = dotGraph.getType(); |
| 131 |
graphBuilder.attr(DotAttributes._TYPE__G, |
| 132 |
GraphType.GRAPH.equals(graphType) |
| 133 |
? DotAttributes._TYPE__G__GRAPH |
| 134 |
: DotAttributes._TYPE__G__DIGRAPH); |
| 135 |
Graph graph = graphBuilder.build(); |
| 136 |
|
| 137 |
// layout |
| 138 |
String layout = getAttributeValue(dotGraph, DotAttributes.LAYOUT__G); |
| 139 |
if (layout != null) { |
| 140 |
DotAttributes.setLayout(graph, layout); |
| 141 |
} else if (globalGraphAttributes.containsKey(DotAttributes.LAYOUT__G)) { |
| 142 |
DotAttributes.setLayout(graph, |
| 143 |
globalGraphAttributes.get(DotAttributes.LAYOUT__G)); |
| 144 |
} |
| 145 |
// rankdir |
| 146 |
String rankdir = getAttributeValue(dotGraph, DotAttributes.RANKDIR__G); |
| 147 |
if (rankdir != null) { |
| 148 |
DotAttributes.setRankdir(graph, rankdir); |
| 149 |
} else if (globalGraphAttributes |
| 150 |
.containsKey(DotAttributes.RANKDIR__G)) { |
| 151 |
DotAttributes.setRankdir(graph, |
| 152 |
globalGraphAttributes.get(DotAttributes.RANKDIR__G)); |
| 153 |
} |
| 154 |
// splines |
| 155 |
String splines = getAttributeValue(dotGraph, DotAttributes.SPLINES__G); |
| 156 |
if (splines == null && globalGraphAttributes |
| 157 |
.containsKey(DotAttributes.SPLINES__G)) { |
| 158 |
splines = globalGraphAttributes.get(DotAttributes.SPLINES__G); |
| 159 |
} |
| 160 |
if (splines != null) { |
| 161 |
// XXX: splines can either be a defined enum value or a bool value |
| 162 |
// (which is mapped to respective enum values); we use the enum |
| 163 |
// values alone and thus map the bool value here |
| 164 |
Boolean booleanValue = DotLanguageSupport.parseAttributeValue( |
| 165 |
DotLanguageSupport.BOOL_PARSER, splines); |
| 166 |
if (booleanValue != null) { |
| 167 |
DotAttributes.setSplinesParsed(graph, |
| 168 |
Boolean.TRUE.equals(booleanValue) ? Splines.TRUE |
| 169 |
: Splines.FALSE); |
| 170 |
} else { |
| 171 |
DotAttributes.setSplines(graph, splines); |
| 172 |
} |
| 173 |
} |
| 174 |
return graph; |
| 175 |
} |
| 176 |
|
| 177 |
@Override |
| 178 |
public Object caseAttrStmt(AttrStmt attrStmt) { |
| 179 |
AttributeType type = attrStmt.getType(); |
| 180 |
switch (type) { |
| 181 |
case EDGE: { |
| 182 |
// global edge attributes |
| 183 |
for (AttrList al : attrStmt.getAttrLists()) { |
| 184 |
for (Attribute a : al.getAttributes()) { |
| 185 |
globalEdgeAttributes.put(a.getName(), |
| 186 |
escaped(a.getValue())); |
| 187 |
} |
| 188 |
} |
| 189 |
break; |
| 190 |
} |
| 191 |
case NODE: { |
| 192 |
// global node attributes |
| 193 |
for (AttrList al : attrStmt.getAttrLists()) { |
| 194 |
for (Attribute a : al.getAttributes()) { |
| 195 |
globalNodeAttributes.put(a.getName(), |
| 196 |
escaped(a.getValue())); |
| 197 |
} |
| 198 |
} |
| 199 |
break; |
| 200 |
} |
| 201 |
case GRAPH: { |
| 202 |
// global graph attributes |
| 203 |
for (AttrList al : attrStmt.getAttrLists()) { |
| 204 |
for (Attribute a : al.getAttributes()) { |
| 205 |
globalGraphAttributes.put(a.getName(), |
| 206 |
escaped(a.getValue())); |
| 207 |
} |
| 208 |
} |
| 209 |
break; |
| 210 |
} |
| 211 |
} |
| 212 |
return super.caseAttrStmt(attrStmt); |
| 213 |
} |
| 214 |
|
| 215 |
@Override |
| 216 |
public Object caseNodeStmt(NodeStmt nodeStmt) { |
| 217 |
// name (from grammar definition, not attribute) |
| 218 |
Node node = node(escaped(nodeStmt.getNode().getName())); |
| 219 |
|
| 220 |
// distortion |
| 221 |
String distortion = getAttributeValue(nodeStmt, |
| 222 |
DotAttributes.DISTORTION__N); |
| 223 |
if (distortion != null) { |
| 224 |
DotAttributes.setDistortion(node, distortion); |
| 225 |
} |
| 226 |
|
| 227 |
// id |
| 228 |
String id = getAttributeValue(nodeStmt, DotAttributes.ID__GNE); |
| 229 |
if (id != null) { |
| 230 |
DotAttributes.setId(node, id); |
| 231 |
} |
| 232 |
|
| 233 |
// label |
| 234 |
String label = getAttributeValue(nodeStmt, DotAttributes.LABEL__GNE); |
| 235 |
if (label != null) { |
| 236 |
DotAttributes.setLabel(node, label); |
| 237 |
} |
| 238 |
|
| 239 |
// xlabel |
| 240 |
String xLabel = getAttributeValue(nodeStmt, DotAttributes.XLABEL__NE); |
| 241 |
if (xLabel != null) { |
| 242 |
DotAttributes.setXLabel(node, xLabel); |
| 243 |
} |
| 244 |
|
| 245 |
// pos |
| 246 |
String pos = getAttributeValue(nodeStmt, DotAttributes.POS__NE); |
| 247 |
if (pos != null) { |
| 248 |
DotAttributes.setPos(node, pos); |
| 249 |
} |
| 250 |
|
| 251 |
// xlp |
| 252 |
String xlp = getAttributeValue(nodeStmt, DotAttributes.XLP__NE); |
| 253 |
if (xlp != null) { |
| 254 |
DotAttributes.setXlp(node, xlp); |
| 255 |
} |
| 256 |
|
| 257 |
// width |
| 258 |
String width = getAttributeValue(nodeStmt, DotAttributes.WIDTH__N); |
| 259 |
if (width != null) { |
| 260 |
DotAttributes.setWidth(node, width); |
| 261 |
} |
| 262 |
|
| 263 |
// height |
| 264 |
String height = getAttributeValue(nodeStmt, DotAttributes.HEIGHT__N); |
| 265 |
if (height != null) { |
| 266 |
DotAttributes.setHeight(node, height); |
| 267 |
} |
| 268 |
|
| 269 |
// fixedsize |
| 270 |
String fixedSize = getAttributeValue(nodeStmt, |
| 271 |
DotAttributes.FIXEDSIZE__N); |
| 272 |
if (fixedSize != null) { |
| 273 |
DotAttributes.setFixedSize(node, fixedSize); |
| 274 |
} |
| 275 |
|
| 276 |
// shape |
| 277 |
String shape = getAttributeValue(nodeStmt, DotAttributes.SHAPE__N); |
| 278 |
if (shape != null) { |
| 279 |
DotAttributes.setShape(node, shape); |
| 280 |
} |
| 281 |
|
| 282 |
// sides |
| 283 |
String sides = getAttributeValue(nodeStmt, DotAttributes.SIDES__N); |
| 284 |
if (sides != null) { |
| 285 |
DotAttributes.setSides(node, sides); |
| 286 |
} |
| 287 |
|
| 288 |
// skew |
| 289 |
String skew = getAttributeValue(nodeStmt, DotAttributes.SKEW__N); |
| 290 |
if (skew != null) { |
| 291 |
DotAttributes.setSkew(node, skew); |
| 292 |
} |
| 293 |
|
| 294 |
// style |
| 295 |
String style = getAttributeValue(nodeStmt, DotAttributes.STYLE__GNE); |
| 296 |
if (style != null) { |
| 297 |
DotAttributes.setStyle(node, style); |
| 298 |
} |
| 299 |
return super.caseNodeStmt(nodeStmt); |
| 300 |
} |
| 301 |
|
| 302 |
@Override |
| 303 |
public Object caseEdgeStmtNode(EdgeStmtNode object) { |
| 304 |
currentEdgeId = getAttributeValue(object, DotAttributes.ID__GNE); |
| 305 |
currentEdgeLabel = getAttributeValue(object, DotAttributes.LABEL__GNE); |
| 306 |
currentEdgeLp = getAttributeValue(object, DotAttributes.LP__GE); |
| 307 |
currentEdgeXLabel = getAttributeValue(object, DotAttributes.XLABEL__NE); |
| 308 |
currentEdgeXlp = getAttributeValue(object, DotAttributes.XLP__NE); |
| 309 |
currentEdgeStyle = getAttributeValue(object, DotAttributes.STYLE__GNE); |
| 310 |
currentEdgePos = getAttributeValue(object, DotAttributes.POS__NE); |
| 311 |
currentEdgeHeadLabel = getAttributeValue(object, |
| 312 |
DotAttributes.HEADLABEL__E); |
| 313 |
currentEdgeHeadLp = getAttributeValue(object, DotAttributes.HEAD_LP__E); |
| 314 |
currentEdgeTailLabel = getAttributeValue(object, |
| 315 |
DotAttributes.TAILLABEL__E); |
| 316 |
currentEdgeTailLp = getAttributeValue(object, DotAttributes.TAIL_LP__E); |
| 317 |
currentArrowHead = getAttributeValue(object, |
| 318 |
DotAttributes.ARROWHEAD__E); |
| 319 |
currentArrowTail = getAttributeValue(object, |
| 320 |
DotAttributes.ARROWTAIL__E); |
| 321 |
currentArrowSize = getAttributeValue(object, |
| 322 |
DotAttributes.ARROWSIZE__E); |
| 323 |
currentEdgeDirection = getAttributeValue(object, DotAttributes.DIR__E); |
| 324 |
return super.caseEdgeStmtNode(object); |
| 325 |
} |
| 326 |
|
| 327 |
@Override |
| 328 |
public Object caseNodeId(NodeId object) { |
| 329 |
if (!createEdge) { |
| 330 |
currentEdgeSourceNodeName = escaped(object.getName()); |
| 331 |
} else { |
| 332 |
String targetNodeName = escaped(object.getName()); |
| 333 |
if (currentEdgeSourceNodeName != null && targetNodeName != null) { |
| 334 |
edge(currentEdgeSourceNodeName, currentEdgeOp, targetNodeName); |
| 335 |
// current target node may be source for next EdgeRHS |
| 336 |
currentEdgeSourceNodeName = targetNodeName; |
| 337 |
} |
| 338 |
createEdge = false; |
| 339 |
} |
| 340 |
return super.caseNodeId(object); |
| 341 |
} |
| 342 |
|
| 343 |
private void edge(String sourceNodeName, String edgeOp, |
| 344 |
String targetNodeName) { |
| 345 |
Edge edge = new Edge.Builder(node(sourceNodeName), node(targetNodeName)) |
| 346 |
.attr(DotAttributes._NAME__GNE, |
| 347 |
sourceNodeName + edgeOp + targetNodeName) |
| 348 |
.buildEdge(); |
| 349 |
|
| 350 |
// id |
| 351 |
if (currentEdgeId != null) { |
| 352 |
DotAttributes.setId(edge, currentEdgeId); |
| 353 |
} |
| 354 |
|
| 355 |
// label |
| 356 |
if (currentEdgeLabel != null) { |
| 357 |
DotAttributes.setLabel(edge, currentEdgeLabel); |
| 358 |
} else if (globalEdgeAttributes.containsKey(DotAttributes.LABEL__GNE)) { |
| 359 |
DotAttributes.setLabel(edge, |
| 360 |
globalEdgeAttributes.get(DotAttributes.LABEL__GNE)); |
| 361 |
} |
| 362 |
|
| 363 |
// external label (xlabel) |
| 364 |
if (currentEdgeXLabel != null) { |
| 365 |
DotAttributes.setXLabel(edge, currentEdgeXLabel); |
| 366 |
} else if (globalEdgeAttributes.containsKey(DotAttributes.XLABEL__NE)) { |
| 367 |
DotAttributes.setXLabel(edge, |
| 368 |
globalEdgeAttributes.get(DotAttributes.XLABEL__NE)); |
| 369 |
} |
| 370 |
|
| 371 |
// head label (headllabel) |
| 372 |
if (currentEdgeHeadLabel != null) { |
| 373 |
DotAttributes.setHeadLabel(edge, currentEdgeHeadLabel); |
| 374 |
} else if (globalEdgeAttributes |
| 375 |
.containsKey(DotAttributes.HEADLABEL__E)) { |
| 376 |
DotAttributes.setHeadLabel(edge, |
| 377 |
globalEdgeAttributes.get(DotAttributes.HEADLABEL__E)); |
| 378 |
} |
| 379 |
|
| 380 |
// tail label (taillabel) |
| 381 |
if (currentEdgeTailLabel != null) { |
| 382 |
DotAttributes.setTailLabel(edge, currentEdgeTailLabel); |
| 383 |
} else if (globalEdgeAttributes |
| 384 |
.containsKey(DotAttributes.TAILLABEL__E)) { |
| 385 |
DotAttributes.setTailLabel(edge, |
| 386 |
globalEdgeAttributes.get(DotAttributes.TAILLABEL__E)); |
| 387 |
} |
| 388 |
|
| 389 |
// style |
| 390 |
if (currentEdgeStyle != null) { |
| 391 |
DotAttributes.setStyle(edge, currentEdgeStyle); |
| 392 |
} else if (globalEdgeAttributes.containsKey(DotAttributes.STYLE__GNE)) { |
| 393 |
DotAttributes.setStyle(edge, |
| 394 |
globalEdgeAttributes.get(DotAttributes.STYLE__GNE)); |
| 395 |
} |
| 396 |
|
| 397 |
// arrow head |
| 398 |
if (currentArrowHead != null) { |
| 399 |
DotAttributes.setArrowHead(edge, currentArrowHead); |
| 400 |
} else if (globalEdgeAttributes |
| 401 |
.containsKey(DotAttributes.ARROWHEAD__E)) { |
| 402 |
DotAttributes.setArrowHead(edge, |
| 403 |
globalEdgeAttributes.get(DotAttributes.ARROWHEAD__E)); |
| 404 |
} |
| 405 |
|
| 406 |
// arrow tail |
| 407 |
if (currentArrowTail != null) { |
| 408 |
DotAttributes.setArrowTail(edge, currentArrowTail); |
| 409 |
} else if (globalEdgeAttributes |
| 410 |
.containsKey(DotAttributes.ARROWTAIL__E)) { |
| 411 |
DotAttributes.setArrowTail(edge, |
| 412 |
globalEdgeAttributes.get(DotAttributes.ARROWTAIL__E)); |
| 413 |
} |
| 414 |
|
| 415 |
// arrow size |
| 416 |
if (currentArrowSize != null) { |
| 417 |
DotAttributes.setArrowSize(edge, currentArrowSize); |
| 418 |
} else if (globalEdgeAttributes |
| 419 |
.containsKey(DotAttributes.ARROWSIZE__E)) { |
| 420 |
DotAttributes.setArrowSize(edge, |
| 421 |
globalEdgeAttributes.get(DotAttributes.ARROWSIZE__E)); |
| 422 |
} |
| 423 |
|
| 424 |
// direction |
| 425 |
if (currentEdgeDirection != null) { |
| 426 |
DotAttributes.setDir(edge, currentEdgeDirection); |
| 427 |
} else if (globalEdgeAttributes.containsKey(DotAttributes.DIR__E)) { |
| 428 |
DotAttributes.setDir(edge, |
| 429 |
globalEdgeAttributes.get(DotAttributes.DIR__E)); |
| 430 |
} |
| 431 |
|
| 432 |
// position (pos) |
| 433 |
if (currentEdgePos != null) { |
| 434 |
DotAttributes.setPos(edge, currentEdgePos); |
| 435 |
} |
| 436 |
// label position (lp) |
| 437 |
if (currentEdgeLp != null) { |
| 438 |
DotAttributes.setLp(edge, currentEdgeLp); |
| 439 |
} |
| 440 |
|
| 441 |
// external label position (xlp) |
| 442 |
if (currentEdgeXlp != null) { |
| 443 |
DotAttributes.setXlp(edge, currentEdgeXlp); |
| 444 |
} |
| 445 |
|
| 446 |
// head label position (head_lp) |
| 447 |
if (currentEdgeHeadLp != null) { |
| 448 |
DotAttributes.setHeadLp(edge, currentEdgeHeadLp); |
| 449 |
} |
| 450 |
|
| 451 |
// tail label position (tail_lp) |
| 452 |
if (currentEdgeTailLp != null) { |
| 453 |
DotAttributes.setTailLp(edge, currentEdgeTailLp); |
| 454 |
} |
| 455 |
|
| 456 |
graphBuilder.edges(edge); |
| 457 |
} |
| 458 |
|
| 459 |
@Override |
| 460 |
public Object caseEdgeRhsNode(EdgeRhsNode object) { |
| 461 |
// Set the flag for the node_id case handled above |
| 462 |
createEdge = true; |
| 463 |
currentEdgeOp = object.getOp().getLiteral(); |
| 464 |
return super.caseEdgeRhsNode(object); |
| 465 |
} |
| 466 |
|
| 467 |
private Node node(String nodeName) { |
| 468 |
if (!nodesByName.containsKey(nodeName)) { |
| 469 |
Node node = new Node.Builder() |
| 470 |
.attr(DotAttributes._NAME__GNE, nodeName).buildNode(); |
| 471 |
graphBuilder.nodes(node); |
| 472 |
nodesByName.put(nodeName, node); |
| 473 |
|
| 474 |
// evaluate global attributes |
| 475 |
if (globalNodeAttributes.containsKey(DotAttributes.LABEL__GNE)) { |
| 476 |
DotAttributes.setLabel(node, |
| 477 |
globalNodeAttributes.get(DotAttributes.LABEL__GNE)); |
| 478 |
} |
| 479 |
if (globalNodeAttributes.containsKey(DotAttributes.XLABEL__NE)) { |
| 480 |
DotAttributes.setXLabel(node, |
| 481 |
globalNodeAttributes.get(DotAttributes.XLABEL__NE)); |
| 482 |
} |
| 483 |
if (globalNodeAttributes.containsKey(DotAttributes.WIDTH__N)) { |
| 484 |
DotAttributes.setWidth(node, |
| 485 |
globalNodeAttributes.get(DotAttributes.WIDTH__N)); |
| 486 |
} |
| 487 |
if (globalNodeAttributes.containsKey(DotAttributes.HEIGHT__N)) { |
| 488 |
DotAttributes.setHeight(node, |
| 489 |
globalNodeAttributes.get(DotAttributes.HEIGHT__N)); |
| 490 |
} |
| 491 |
if (globalNodeAttributes.containsKey(DotAttributes.FIXEDSIZE__N)) { |
| 492 |
DotAttributes.setFixedSize(node, |
| 493 |
globalNodeAttributes.get(DotAttributes.FIXEDSIZE__N)); |
| 494 |
} |
| 495 |
if (globalNodeAttributes.containsKey(DotAttributes.DISTORTION__N)) { |
| 496 |
DotAttributes.setDistortion(node, |
| 497 |
globalNodeAttributes.get(DotAttributes.DISTORTION__N)); |
| 498 |
} |
| 499 |
if (globalNodeAttributes.containsKey(DotAttributes.SHAPE__N)) { |
| 500 |
DotAttributes.setShape(node, |
| 501 |
globalNodeAttributes.get(DotAttributes.SHAPE__N)); |
| 502 |
} |
| 503 |
if (globalNodeAttributes.containsKey(DotAttributes.SIDES__N)) { |
| 504 |
DotAttributes.setSides(node, |
| 505 |
globalNodeAttributes.get(DotAttributes.SIDES__N)); |
| 506 |
} |
| 507 |
if (globalNodeAttributes.containsKey(DotAttributes.SKEW__N)) { |
| 508 |
DotAttributes.setSkew(node, |
| 509 |
globalNodeAttributes.get(DotAttributes.SKEW__N)); |
| 510 |
} |
| 511 |
if (globalNodeAttributes.containsKey(DotAttributes.STYLE__GNE)) { |
| 512 |
DotAttributes.setStyle(node, |
| 513 |
globalNodeAttributes.get(DotAttributes.STYLE__GNE)); |
| 514 |
} |
| 515 |
} |
| 516 |
return nodesByName.get(nodeName); |
| 517 |
} |
| 518 |
|
| 519 |
private String getAttributeValue(final DotGraph graph, final String name) { |
| 520 |
for (Stmt stmt : graph.getStmts()) { |
| 521 |
String value = null; |
| 522 |
if (stmt instanceof AttrStmt) { |
| 523 |
value = getAttributeValue((AttrStmt) stmt, name); |
| 524 |
} else if (stmt instanceof Attribute) { |
| 525 |
value = getAttributeValue((Attribute) stmt, name); |
| 526 |
} |
| 527 |
if (value != null) { |
| 528 |
return value; |
| 529 |
} |
| 530 |
} |
| 531 |
return null; |
| 532 |
} |
| 533 |
|
| 534 |
/** |
| 535 |
* @param stmt |
| 536 |
* The {@link NodeStmt} object, e.g. the object corresponding to |
| 537 |
* "node[label="hi"]" |
| 538 |
* @param name |
| 539 |
* The name of the attribute to get the value for, e.g. "label" |
| 540 |
* @return The value of the given attribute, e.g. "hi" |
| 541 |
*/ |
| 542 |
private String getAttributeValue(final NodeStmt stmt, final String name) { |
| 543 |
return getAttributeValue(stmt.getAttrLists(), name); |
| 544 |
} |
| 545 |
|
| 546 |
/** |
| 547 |
* Returns the value of the first attribute with the give name or |
| 548 |
* <code>null</code> if no attribute could be found. |
| 549 |
* |
| 550 |
* @param attrLists |
| 551 |
* The {@link AttrList}s to search. |
| 552 |
* @param name |
| 553 |
* The name of the attribute whose value is to be retrieved. |
| 554 |
* @return The attribute value or <code>null</code> in case the attribute |
| 555 |
* could not be found. |
| 556 |
*/ |
| 557 |
private String getAttributeValue(List<AttrList> attrLists, |
| 558 |
final String name) { |
| 559 |
for (AttrList attrList : attrLists) { |
| 560 |
String value = getAttributeValue(attrList, name); |
| 561 |
if (value != null) { |
| 562 |
return value; |
| 563 |
} |
| 564 |
} |
| 565 |
return null; |
| 566 |
} |
| 567 |
|
| 568 |
private String getAttributeValue(AttrStmt attrStmt, String name) { |
| 569 |
return getAttributeValue(attrStmt.getAttrLists(), name); |
| 570 |
} |
| 571 |
|
| 572 |
private String getAttributeValue(EdgeStmtNode edgeStmtNode, String name) { |
| 573 |
return getAttributeValue(edgeStmtNode.getAttrLists(), name); |
| 574 |
} |
| 575 |
|
| 576 |
private String getAttributeValue(AttrList attrList, final String name) { |
| 577 |
Iterator<EObject> attributeContents = attrList.eContents().iterator(); |
| 578 |
while (attributeContents.hasNext()) { |
| 579 |
EObject next = attributeContents.next(); |
| 580 |
if (next instanceof Attribute) { |
| 581 |
String value = getAttributeValue((Attribute) next, name); |
| 582 |
if (value != null) { |
| 583 |
return value; |
| 584 |
} |
| 585 |
} |
| 586 |
} |
| 587 |
return null; |
| 588 |
} |
| 589 |
|
| 590 |
private String getAttributeValue(Attribute attribute, final String name) { |
| 591 |
if (attribute.getName().equals(name)) { |
| 592 |
return escaped(attribute.getValue()); |
| 593 |
} |
| 594 |
return null; |
| 595 |
} |
| 596 |
|
| 597 |
private String escaped(String id) { |
| 598 |
return DotTerminalConverters.unquote(id); |
| 599 |
} |
| 600 |
} |