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.

This commit is contained in:
ghidragon 2023-08-25 11:22:21 -04:00
parent e2be5bb275
commit 4824d03b8b
3 changed files with 46 additions and 23 deletions

View File

@ -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;
@Override
public 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;
}
@Override
public String getFileExtension() {
return "gf";

View File

@ -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: &nbsp;" + 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: &nbsp;" + 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", "&nbsp;");
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", "&nbsp;");
buf.append(split);
buf.append("</TD></TR>");
}
}

View File

@ -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 transform
graphics.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;