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.assertNotNull; |
25 | import static java.util.stream.Collectors.toList; |
26 | |
27 | import java.util.List; |
28 | |
29 | import com.github.javaparser.ast.Node; |
30 | import com.github.javaparser.ast.NodeList; |
31 | import com.github.javaparser.metamodel.NodeMetaModel; |
32 | import com.github.javaparser.metamodel.PropertyMetaModel; |
33 | |
34 | /** |
35 | * Outputs a YAML file containing the AST meant for inspecting it. |
36 | */ |
37 | public class YamlPrinter { |
38 | |
39 | private static final int NUM_SPACES_FOR_INDENT = 4; |
40 | private final boolean outputNodeType; |
41 | |
42 | public YamlPrinter(boolean outputNodeType) { |
43 | this.outputNodeType = outputNodeType; |
44 | } |
45 | |
46 | public String output(Node node) { |
47 | StringBuilder output = new StringBuilder(); |
48 | output.append("---"); |
49 | output(node, "root", 0, output); |
50 | output.append(System.lineSeparator() + "..."); |
51 | return output.toString(); |
52 | } |
53 | |
54 | public void output(Node node, String name, int level, StringBuilder builder) { |
55 | assertNotNull(node); |
56 | NodeMetaModel metaModel = node.getMetaModel(); |
57 | List<PropertyMetaModel> allPropertyMetaModels = metaModel.getAllPropertyMetaModels(); |
58 | List<PropertyMetaModel> attributes = allPropertyMetaModels.stream().filter(PropertyMetaModel::isAttribute) |
59 | .filter(PropertyMetaModel::isSingular).collect(toList()); |
60 | List<PropertyMetaModel> subNodes = allPropertyMetaModels.stream().filter(PropertyMetaModel::isNode) |
61 | .filter(PropertyMetaModel::isSingular).collect(toList()); |
62 | List<PropertyMetaModel> subLists = allPropertyMetaModels.stream().filter(PropertyMetaModel::isNodeList) |
63 | .collect(toList()); |
64 | |
65 | if (outputNodeType) |
66 | builder.append(System.lineSeparator() + indent(level) + name + "(Type=" + metaModel.getTypeName() + "): "); |
67 | else |
68 | builder.append(System.lineSeparator() + indent(level) + name + ": "); |
69 | |
70 | level++; |
71 | for (PropertyMetaModel a : attributes) { |
72 | builder.append(System.lineSeparator() + indent(level) + a.getName() + ": " + escapeValue(a.getValue(node).toString())); |
73 | } |
74 | |
75 | for (PropertyMetaModel sn : subNodes) { |
76 | Node nd = (Node) sn.getValue(node); |
77 | if (nd != null) |
78 | output(nd, sn.getName(), level, builder); |
79 | } |
80 | |
81 | for (PropertyMetaModel sl : subLists) { |
82 | NodeList<? extends Node> nl = (NodeList<? extends Node>) sl.getValue(node); |
83 | if (nl != null && nl.isNonEmpty()) { |
84 | builder.append(System.lineSeparator() + indent(level) + sl.getName() + ": "); |
85 | String slName = sl.getName(); |
86 | slName = slName.endsWith("s") ? slName.substring(0, sl.getName().length() - 1) : slName; |
87 | for (Node nd : nl) |
88 | output(nd, "- " + slName, level + 1, builder); |
89 | } |
90 | } |
91 | } |
92 | |
93 | private String indent(int level) { |
94 | StringBuilder sb = new StringBuilder(); |
95 | for (int i = 0; i < level; i++) |
96 | for (int j = 0; j < NUM_SPACES_FOR_INDENT; j++) |
97 | sb.append(" "); |
98 | return sb.toString(); |
99 | } |
100 | |
101 | private String escapeValue(String value) { |
102 | return "\"" + value |
103 | .replace("\\", "\\\\") |
104 | .replaceAll("\"", "\\\\\"") |
105 | .replace("\n", "\\n") |
106 | .replace("\r", "\\r") |
107 | .replace("\f", "\\f") |
108 | .replace("\b", "\\b") |
109 | .replace("\t", "\\t") + "\""; |
110 | } |
111 | |
112 | public static void print(Node node) { |
113 | System.out.println(new YamlPrinter(true).output(node)); |
114 | } |
115 | } |
116 |
Members