Ghidra is a software reverse engineering (SRE) framework
GP-3779 Changed DOT graph exporter to convert 'Name' vertex attribute to 'label' which is the display attribute DOT graphs use. Also, fixed label display to not include id. The id can now be seen in the tooltip.
ghidragon4824d03b8b6a
diff --git a/Ghidra/Features/GraphServices/src/main/java/ghidra/graph/exporter/DotGraphExporter.java b/Ghidra/Features/GraphServices/src/main/java/ghidra/graph/exporter/DotGraphExporter.javaindex 53e8a0583b..3492faa4f2 100644--- a/Ghidra/Features/GraphServices/src/main/java/ghidra/graph/exporter/DotGraphExporter.java+++ b/Ghidra/Features/GraphServices/src/main/java/ghidra/graph/exporter/DotGraphExporter.java@@ -17,7 +17,9 @@ package ghidra.graph.exporter;import java.io.File;import java.io.IOException;+import java.util.Map;+import org.jgrapht.nio.Attribute;import org.jgrapht.nio.dimacs.DIMACSExporter;import org.jgrapht.nio.dimacs.DIMACSFormat;import org.jgrapht.nio.dot.DOTExporter;@@ -28,7 +30,6 @@ public class DotGraphExporter extends AbstractAttributedGraphExporter {protected DIMACSFormat dimacsFormat = DIMACSExporter.DEFAULT_DIMACS_FORMAT;-@Overridepublic void exportGraph(AttributedGraph graph, File file) throws IOException {DOTExporter<AttributedVertex, AttributedEdge> exporter =@@ -46,6 +47,20 @@ public class DotGraphExporter extends AbstractAttributedGraphExporter {}}+ /**+ * DOT graphs use a special attribute call "label" which we call "Name", so when+ * exporting to DOT format, change the "Name" attribute to "label".+ */+ @Override+ protected Map<String, Attribute> getAttributes(Attributed attributed) {+ Map<String, Attribute> attributes = super.getAttributes(attributed);+ Attribute attribute = attributes.remove("Name");+ if (attribute != null) {+ attributes.put("label", attribute);+ }+ return attributes;+ }+@Overridepublic String getFileExtension() {return "gf";diff --git a/Ghidra/Features/GraphServices/src/main/java/ghidra/graph/visualization/AttributedToolTipInfo.java b/Ghidra/Features/GraphServices/src/main/java/ghidra/graph/visualization/AttributedToolTipInfo.javaindex 836083393d..11bcd02a2f 100644--- a/Ghidra/Features/GraphServices/src/main/java/ghidra/graph/visualization/AttributedToolTipInfo.java+++ b/Ghidra/Features/GraphServices/src/main/java/ghidra/graph/visualization/AttributedToolTipInfo.java@@ -93,30 +93,31 @@ public class AttributedToolTipInfo extends ToolTipInfo<Attributed> {private void addToolTipTextForVertex(StringBuilder buf, AttributedVertex vertex) {String vertexType = vertex.getVertexType();- buf.append("<H4>");+ buf.append("<H3>");String escapedText = HTMLUtilities.toLiteralHTML(vertex.getName(), 80);buf.append(escapedText);+ buf.append("</H3><TABLE>");if (vertexType != null) {- buf.append("<br>");- buf.append("Type: " + vertexType);+ appendAttribute(buf, "Type", vertexType);}- buf.append("</H4>");-addAttributes(buf, AttributedVertex.NAME_KEY, AttributedVertex.VERTEX_TYPE_KEY);+ appendAttribute(buf, "Id", vertex.getId());+ buf.append("</TABLE>");}private void addToolTipTextForEdge(StringBuilder buf, AttributedEdge edge) {String edgeType = edge.getEdgeType();+ buf.append("<TABLE>");if (edgeType != null) {- buf.append("<H4>");- buf.append("Type: " + edgeType);- buf.append("</H4>");+ appendAttribute(buf, "Type", edgeType);}addAttributes(buf, AttributedEdge.EDGE_TYPE_KEY);+ appendAttribute(buf, "Id", edge.getId());+ buf.append("</TABLE>");}- private void addAttributes(StringBuilder buf, String...excludedKeys) {-+ private void addAttributes(StringBuilder buf, String... excludedKeys) {+Set<Entry<String, String>> entries = graphObject.entrySet();for (Map.Entry<String, String> entry : entries) {@@ -124,14 +125,18 @@ public class AttributedToolTipInfo extends ToolTipInfo<Attributed> {if (ArrayUtils.contains(excludedKeys, key)) {continue; // skip keys handled in header}- buf.append(key);- buf.append(": ");- String escapedText = HTMLUtilities.toLiteralHTML(entry.getValue(), 80);- String split = String.join("<br>", Splitter.on('\n').split(escapedText));- split = split.replaceAll("\\s", " ");- buf.append(split);- buf.append("<br>");+ appendAttribute(buf, key, entry.getValue());}}+ private void appendAttribute(StringBuilder buf, String key, String value) {+ buf.append("<TR><TD>");+ buf.append(key);+ buf.append(":</TD><TD>");+ String escapedText = HTMLUtilities.toLiteralHTML(value, 80);+ String split = String.join("<BR>", Splitter.on('\n').split(escapedText));+ split = split.replaceAll("\\s", " ");+ buf.append(split);+ buf.append("</TD></TR>");+ }}diff --git a/Ghidra/Features/GraphServices/src/main/java/ghidra/graph/visualization/DefaultGraphRenderer.java b/Ghidra/Features/GraphServices/src/main/java/ghidra/graph/visualization/DefaultGraphRenderer.javaindex 5cee638e1d..7fa7b462b8 100644--- a/Ghidra/Features/GraphServices/src/main/java/ghidra/graph/visualization/DefaultGraphRenderer.java+++ b/Ghidra/Features/GraphServices/src/main/java/ghidra/graph/visualization/DefaultGraphRenderer.java@@ -28,6 +28,7 @@ import java.util.function.Function;import javax.swing.*;import javax.swing.border.Border;+import org.apache.commons.lang3.StringUtils;import org.jungrapht.visualization.RenderContext;import org.jungrapht.visualization.VisualizationViewer;import org.jungrapht.visualization.decorators.*;@@ -104,7 +105,11 @@ public class DefaultGraphRenderer implements GraphRenderer {}private String getVertexRenderedLabel(AttributedVertex v) {- return HTMLUtilities.toLiteralHTML(v.toString(), 80);+ String displayName = v.getName();+ if (StringUtils.isBlank(displayName)) {+ displayName = v.getId();+ }+ return HTMLUtilities.toLiteralHTML(displayName, 80);}@Override@@ -310,10 +315,8 @@ public class DefaultGraphRenderer implements GraphRenderer {graphics.setTransform(graphicsTransform); // restore the original transformgraphics.dispose();- Image scaledImage =- ImageUtils.createScaledImage(bufferedImage, iconWidth * ICON_ZOOM,- iconHeight * ICON_ZOOM,- Image.SCALE_FAST);+ Image scaledImage = ImageUtils.createScaledImage(bufferedImage, iconWidth * ICON_ZOOM,+ iconHeight * ICON_ZOOM, Image.SCALE_FAST);ImageIcon imageIcon = new ImageIcon(scaledImage);return imageIcon;