1 | /* |
---|---|
2 | * Copyright (C) 2007-2010 JĂșlio Vilmar Gesser. |
3 | * Copyright (C) 2011, 2013-2020 The JavaParser Team. |
4 | * |
5 | * This file is part of JavaParser. |
6 | * |
7 | * JavaParser can be used either under the terms of |
8 | * a) the GNU Lesser General Public License as published by |
9 | * the Free Software Foundation, either version 3 of the License, or |
10 | * (at your option) any later version. |
11 | * b) the terms of the Apache License |
12 | * |
13 | * You should have received a copy of both licenses in LICENCE.LGPL and |
14 | * LICENCE.APACHE. Please refer to those files for details. |
15 | * |
16 | * JavaParser is distributed in the hope that it will be useful, |
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
19 | * GNU Lesser General Public License for more details. |
20 | */ |
21 | |
22 | package com.github.javaparser.printer; |
23 | |
24 | import static com.github.javaparser.utils.Utils.SYSTEM_EOL; |
25 | import static com.github.javaparser.utils.Utils.assertNotNull; |
26 | import static java.util.stream.Collectors.toList; |
27 | |
28 | import java.util.List; |
29 | |
30 | import com.github.javaparser.ast.Node; |
31 | import com.github.javaparser.ast.NodeList; |
32 | import com.github.javaparser.metamodel.NodeMetaModel; |
33 | import com.github.javaparser.metamodel.PropertyMetaModel; |
34 | |
35 | /** |
36 | * Outputs a Graphviz diagram of the AST. |
37 | */ |
38 | public class DotPrinter { |
39 | |
40 | private int nodeCount; |
41 | private final boolean outputNodeType; |
42 | |
43 | public DotPrinter(boolean outputNodeType) { |
44 | this.outputNodeType = outputNodeType; |
45 | } |
46 | |
47 | public String output(Node node) { |
48 | nodeCount = 0; |
49 | StringBuilder output = new StringBuilder(); |
50 | output.append("digraph {"); |
51 | output(node, null, "root", output); |
52 | output.append(SYSTEM_EOL + "}"); |
53 | return output.toString(); |
54 | } |
55 | |
56 | public void output(Node node, String parentNodeName, String name, StringBuilder builder) { |
57 | assertNotNull(node); |
58 | NodeMetaModel metaModel = node.getMetaModel(); |
59 | List<PropertyMetaModel> allPropertyMetaModels = metaModel.getAllPropertyMetaModels(); |
60 | List<PropertyMetaModel> attributes = allPropertyMetaModels.stream().filter(PropertyMetaModel::isAttribute) |
61 | .filter(PropertyMetaModel::isSingular).collect(toList()); |
62 | List<PropertyMetaModel> subNodes = allPropertyMetaModels.stream().filter(PropertyMetaModel::isNode) |
63 | .filter(PropertyMetaModel::isSingular).collect(toList()); |
64 | List<PropertyMetaModel> subLists = allPropertyMetaModels.stream().filter(PropertyMetaModel::isNodeList) |
65 | .collect(toList()); |
66 | |
67 | String ndName = nextNodeName(); |
68 | if (outputNodeType) |
69 | builder.append(SYSTEM_EOL + ndName + " [label=\"" + escape(name) + " (" + metaModel.getTypeName() |
70 | + ")\"];"); |
71 | else |
72 | builder.append(SYSTEM_EOL + ndName + " [label=\"" + escape(name) + "\"];"); |
73 | |
74 | if (parentNodeName != null) |
75 | builder.append(SYSTEM_EOL + parentNodeName + " -> " + ndName + ";"); |
76 | |
77 | for (PropertyMetaModel a : attributes) { |
78 | String attrName = nextNodeName(); |
79 | builder.append(SYSTEM_EOL + attrName + " [label=\"" + escape(a.getName()) + "='" |
80 | + escape(a.getValue(node).toString()) + "'\"];"); |
81 | builder.append(SYSTEM_EOL + ndName + " -> " + attrName + ";"); |
82 | |
83 | } |
84 | |
85 | for (PropertyMetaModel sn : subNodes) { |
86 | Node nd = (Node) sn.getValue(node); |
87 | if (nd != null) |
88 | output(nd, ndName, sn.getName(), builder); |
89 | } |
90 | |
91 | for (PropertyMetaModel sl : subLists) { |
92 | NodeList<? extends Node> nl = (NodeList<? extends Node>) sl.getValue(node); |
93 | if (nl != null && nl.isNonEmpty()) { |
94 | String ndLstName = nextNodeName(); |
95 | builder.append(SYSTEM_EOL + ndLstName + " [label=\"" + escape(sl.getName()) + "\"];"); |
96 | builder.append(SYSTEM_EOL + ndName + " -> " + ndLstName + ";"); |
97 | String slName = sl.getName().substring(0, sl.getName().length() - 1); |
98 | for (Node nd : nl) |
99 | output(nd, ndLstName, slName, builder); |
100 | } |
101 | } |
102 | } |
103 | |
104 | private String nextNodeName() { |
105 | return "n" + (nodeCount++); |
106 | } |
107 | |
108 | private static String escape(String value) { |
109 | return value.replace("\"", "\\\""); |
110 | } |
111 | } |
112 |
Members